import { observable, action, computed } from 'mobx';

export default class Reward {
    
        @observable rewardList = [];
        @observable selectedReward = '';
        @observable rewardListLoading = false;
        @observable postRewardLoading = false;
        @observable detailRewardLoading = false;
        @observable rewardId = '';
        @observable delRewardLoading = false;
        @observable putRewardLoading = false;
        @observable rewardData = {
            reward_per : '',
            reward_type : '',
            reward_value : '',
            active_till : '',
            id: '',
            name: ''
        };
    
        constructor(context) {
            this.http = context.http;
            this.context = context;
        }
    
    
        @action
        getRewardList(){
            this.rewardListLoading = true;
            return this.http.get(`package_rewards`).then(res => {
                this.rewardList = res;
                this.rewardListLoading = false;
            });
        }
    
        @computed
        get isRewardEmpty() {
            if(this.rewardList.length > 0) {
                return false;
            }
            return true;
        }  
        
        @action
        getRewardDetail(id){
            this.rewardId = id;
            this.detailRewardLoading = true;
            return this.http.get(`package_rewards/${id}`).then(detail => {
                this.rewardData = detail[0];
                this.detailRewardLoading = false;
            })
        }

    
        @action
        postReward(){
            this.postRewardLoading = true;
            this.http.post('package_rewards', this.rewardData).then(res => {
                this.postRewardLoading = false;
                this.getRewardList();
                return res;
            })
            console.log(this.rewardData)
        }
    
        @action
        putReward(id){
            this.putRewardLoading = true;
            this.http.put(`package_rewards/${id}`, this.rewardData).then(res => {
                this.getRewardList();
                return res;
            });
            console.log(this.rewardData)
        }
    
        @action
        deleteReward(id){
            this.rewardId = id;
            this.delRewardLoading = true;
            this.http.delete(`package_rewards/${id}`).then(res => {
                this.delRewardLoading = false;
                // console.log(detail[0]);
                this.getRewardList();
                return res;
            })
        }
    }