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

export default class Commission {
    @observable commissions = [];
    @observable isLoading = false;
    @observable commissionId = '';
    @observable selectedData = {};
    @observable commissionData = {
        id : '',
        airlines_id : '',
        value : '',
        from : '',
        to: '',
        created_at: '',
        modified_at:'',
        deleted_at : '',
    };

    constructor(context) {
        this.http = context.http;
        this.context = context;
    }

    @action
    getAll() {
      this.isLoading = true;
      return this.http.get("commision")
        .then(res => {
            res.map(item => {
                if(item.airlines_id != null && item.to === null){
                    this.commissions.push(item)
                }
            })
            this.isLoading = false;
        })
        .catch(err => {
          this.isLoading = false;
          throw err;
        })
    }

    @action
    delete(id){
        this.http.put(`commision/${id}`, {}).then(res => {
            return res;
        })
    }

    @action
    add(data){
        return this.http.post(`commision/role`, data).then(res => {})
    }

    @action
	getById(id){
		this.taskId = id;
		this.isLoading = true;
		return this.http.get(`commision/${id}`).then(detail => {
			this.commissionData = detail;
			this.isLoading = false;
		})
    }

    @computed 
	get isEmpty(){
		if(this.commissions.length > 0){
			return false;
		}
		return true;
    }
    
    @action
    saveCommission(){
		this.isLoading = true;
        return this.http.post(`commision`, this.commissions).then(res => {
            this.isLoading = false;
        })
    }

}