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

export default class PackageSchedules {
	@observable packageSchedulesList = [];
	@observable packageSchedulesData = {
		id : '',
		package_id : '',
		depature_date : '',
		return_date : '',
		pax : '',
		price : '',
		currency : {
			name : ''
		}
	};
	@observable avaliablePax = [];
	@observable packageSchedulesId = '';
	@observable isLoading = false;

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

	@action
	getPackageSchedulesByPackageId(id){
		this.isLoading = true;
		this.http.get(`package_schedules?package_id=${id}`).then(res => {
			this.packageSchedulesList = res;
			console.log(res);
			this.isLoading = false;
			// console.log(this.contactId);
		});
	}

	@action
	getAvaliable(id){
		this.isLoading = true;
		this.http.get(`pax/${id}`).then(res => {
			this.avaliablePax = res;
			this.isLoading = false;
			// console.log(this.contactId);
		});
	}

	@action
	getPackageSchedulesById(id){
		this.isLoading = true;
		this.packageSchedulesId = id;
		return this.http.get(`package_schedules/${id}`).then(res => {
			this.packageSchedulesData = res[0];
			this.isLoading = false;
			// console.log(this.contactId);
		});
	}

	@computed
	get isPackageSchedulesEmpty() {
		if(this.packageSchedulesList.length > 0) {
			return false;
		}
		return true;
	}

	@action
	postPackageSchedules(package_schedules_id){
		this.isLoading = true;
		this.http.post(`package_schedules`, this.packageSchedulesData).then(res => {
			this.isLoading = false;
			this.getPackageSchedulesByPackageId(package_schedules_id);
		});
	}

	@action
	putPackageSchedules(id, package_schedules_id){
		this.isLoading = true;
		this.http.put(`package_schedules/${id}`, this.packageSchedulesData).then(res => {
			this.isLoading = false;
			this.getPackageSchedulesByPackageId(package_schedules_id);
		});
	}

	@action
	delPackageSchedules(id,package_schedules_id){
		this.packageSchedulesId = id;
		this.isLoading = true;
		this.http.delete(`package_schedules/${id}`).then(res => {
			this.isLoading = false;
			// console.log(detail[0]);
			this.getPackageSchedulesByPackageId(package_schedules_id);
			return res;
		})
	}
}	
