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

export default class CustomerStore {

  @observable customers = [];
  @observable selectedCustomer = {};
  @observable isLoading = false;
  @observable CustomerData = {
    name:'',
    date_of_birth:'',
    gender:'',
    address:'',
    occupation:'',
    passport_number:'',
    email:'',
    birth_place:'',
    phone:'',
    handphone:'',
    martial_status:'',
    id_tax_number:'',
    id_type:'',
    id_number:'',
    id_photo:'',
    passport_photo:'',
    error_email : '',
  };
  @observable orders = [];
  @observable searchQuery = '';
  @observable searchResults = [];
  @observable searchQueryPic = '';
  @observable searchResultsPic = [];

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

  @action
  setSearchQuery(searchQuery) {
    this.searchQuery = searchQuery;
    return this.http.get(`customers?search=${this.searchQuery}`).then(res => {
      this.searchResults = res;
    });
  }

  @action
  search(searchQuery) {
    return this.http.get(`customers?search=${searchQuery}`).then(res => {
      this.searchResults = res;
    });
  }

  @action
  setSearchQueryPic(searchQuery) {
    this.searchQueryPic = searchQuery;
    return this.http.get(`customers?search=${this.searchQueryPic}`).then(res => {
      this.searchResultsPic = res;
    });
  }

  @action
  reset(){
    this.CustomerData = {
      name:'',
      date_of_birth:'',
      gender:'',
      address:'',
      occupation:'',
      passport_number:'',
      email:'',
      birth_place:'',
      phone:'',
      handphone:'',
      martial_status:'',
      id_tax_number:'',
      id_type:'',
      id_number:'',
      id_photo:'',
      passport_photo:'',
    };
  }

  // @action
  // getAll() {
  //   this.isLoading = true;
  //   return this.http.get("customers")
  //     .then(res => {
  //       this.isLoading = false;
  //       this.customers = res;
  //     })
  //     .catch(err => {
  //       this.isLoading = false;
  //       throw err;
  //     })
  // }

  @action
	getAll(){
		this.isLoading = true;
		return this.http.get(`customers`).then(res => {
			this.customers = res;
			this.isLoading = false;
		});
	}

  @action
  getDetail(id) {
    this.isLoading = true;
    this.CustomerData.id = id;
    this.http.get(`customers/${id}`)
      .then(res => {
        this.CustomerData = res;
        this.isLoading = false;
      })
      .catch(err => {
        this.isLoading = false;
        throw err;
      })
  }

  @action
  getOrder(id=this.selectedCustomer.id) {
    this.isLoading = true;
    return this.http.get(`customers/${id}/orders`)
      .then(res => {
        this.orders = res;
        this.isLoading = false;
      })
      .catch(err => {
        this.isLoading = false;
        throw err;
      })
  }

  @action
  create() {
    this.isLoading = true;
    delete this.CustomerData.error_email;
    return this.http.post("customers", this.CustomerData)
      .then(res => {
        this.isLoading = false;
        return res;
      })
      .catch(err => {
        this.isLoading = false;
        throw err;
      })
  }

  @action
  update(id) {
    this.isLoading = true;
    return this.http.put(`customers/${id}`, this.CustomerData)
      .then(res => {
        this.isLoading = false;
        return res;
      })
      .catch(err => {
        this.isLoading = false;
        throw err;
      })
  }
}
