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

export default class PaymentMethod {
	@observable paymentList = [];
	@observable paymentMethodId = "";
	@observable paymentMethodDetail = {
	  	id : '',
		type : '',
		name : '',
		pinalty_type: '',
		pinalty_type: '',
		discount_type: '',
		downpayment_type: '',
		discount: '',
		downpayment: '',
		downpayment_deadline : '',
		start_date: '',
		end_date: null,
		cancelation_rules: [{
			period : "",
			change_date : '',
			money_return : '',
			change_package :'',
		}],
  };

	// @observable getDriverByIdData = [{
	// 	id: "",
	// 	contact_id: "",
	// 	name: "",
	// 	nik: "",
	// 	phone_number: "",
	// 	date_of_birth: "",
	// 	address: "",
	// 	photo: "",
	// 	created_at: "",
	// 	updated_at: "",
	// 	deleted_at: null
	// }];
	// @observable driverId = '';
	@observable isLoading = false;
	
	constructor(context) {
	    this.http = context.http;
	    this.context = context;
	}

	@action
	reset(){
	  this.paymentMethodDetail = {
			id : '',
			type : '',
			name : '',
			pinalty_type: '',
			pinalty_type: '',
			discount_type: '',
			downpayment_type: '',
			discount: '',
			downpayment: '',
			downpayment_deadline : '',
			start_date: '',
			end_date: null,
			cancelation_rules: [{
				period : "",
				change_date : '',
				money_return : '',
				change_package :'',
			}],
		}
	}

	@action
	getAll(){
		this.isLoading = true;
		return this.http.get(`payment_methods`).then(res => {
			this.paymentList = res;
			// console.log(this.paymentList);
			this.isLoading = false;
		});
	}

	// @action
	// getById(id){
	// 	if(id != 0){
	// 		this.isLoading = true;
	// 		this.http.get(`payment_methods?id=${id}`).then(res => {
	// 			this.paymentMethodDetail = res;
	// 			this.isLoading = false;
	// 			// console.log(this.contactId);
	// 		});
	// 	}
	// }
	@action
	getById(id){
		this.paymentMethodId = id;
		this.isLoading = true;
		return this.http.get(`payment_methods?id=${id}`).then(detail => {
			this.paymentMethodDetail = detail[0];
			this.isLoading = false;
		})
	}

	@action
	reset(){
		this.paymentMethodDetail = {
		  	id : '',
			type : '',
			name : '',
			pinalty_type: '',
			pinalty_type: '',
			discount_type: '',
			discount: '',
			downpayment: '',
			start_date: '',
			end_date: null,
			cancelation_rules: [],
  		};
	}

	@computed
	get isDetailEmpty(){
		if(this.paymentMethodDetail.name == ''){
			return true;
		}
		else{
			return false;
		}
	}

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

	@computed 
	get isRulesEmpty(){
		if(this.paymentMethodDetail.cancelation_rules.length > 0){
			return false;
		}
		return true;
	}

	@action
	post(){
		this.isLoading = true;
		this.http.post(`payment_methods`, this.paymentMethodDetail).then(res => {
			this.isLoading = false;
			this.getAll();
			return res;
		});
		console.log(this.paymentMethodDetail);
	}

	@action
	put(id){
		this.isLoading = true;
		this.http.put(`payment_methods/${id}`, this.paymentMethodDetail).then(res => {
			this.isLoading = false;
			this.getAll();
			return res;
		});
	}

	@action
	del(id){
		this.isLoading = true;
		this.http.delete(`payment_methods/${id}`).then(res => {
			this.isLoading = false;
			this.getAll();
			return res;
		})
	}
}
