import {action, observable} from 'mobx';
import {BaseStore} from "./base_store";

export default class OrderStore {

  @observable isLoading = false;
  @observable isSearching = false;
  @observable dataFiltered = [];
  @observable gojekPrice = {};
  @observable filterBy = 'active'; 
  @observable selectedOrder = {
    "id": "",
    "user_address_store_id": "",
    "user_address_buyer_id": "",
    "weight": "",
    "shipping_price": "",
    "created_at": "",
    "updated_at": "",
    "deleted_at": null,
    "subtotal": "",
    "user_order_id": "",
    "user_store_id": "",
    "order_status_id": "",
    "user_orders_id": "",
    "user_order_items": [],
    "shipping_method" : {
      "id" : "",
      "name" : ""
    },
    "courier_booking_id" : "",
    "user_address_store": {
      "id": "",
      "user_id": "",
      "name": "",
      "address": "",
      "created_at": "",
      "updated_at": "",
      "deleted_at": null,
      "province_id": "",
      "city_id": "",
      "is_shipment": "",
      "is_default": "",
      "receiver_name": "",
      "city": {
        "id": "",
        "province_id": "",
        "name": "",
        "type": "",
        "raja_ongkir_code": "",
        "created_at": "",
        "updated_at": "",
        "deleted_at": null
      },
      "province": {
        "id": "",
        "name": "",
        "raja_ongkir_code": "",
        "created_at": "",
        "updated_at": "",
        "deleted_at": null
      }
    },
    "user_address_buyer": {
      "id": "",
      "user_id": "",
      "name": "",
      "address": "",
      "created_at": "",
      "updated_at": "",
      "deleted_at": null,
      "province_id": "",
      "city_id": "",
      "is_shipment": false,
      "is_default": false,
      "receiver_name": "",
      "city": {
        "id": "",
        "province_id": "",
        "name": "",
        "type": "",
        "raja_ongkir_code": "",
        "created_at": "",
        "updated_at": "",
        "deleted_at": null
      },
      "province": {
        "id": "",
        "name": "",
        "raja_ongkir_code": "",
        "created_at": "",
        "updated_at": "",
        "deleted_at": null
      }
    },
    "user_orders": {
      "id": "",
      "user_id": "",
      "order_status_id": "",
      "created_at": "",
      "updated_at": "",
      "deleted_at": null,
      "firstname": '',
      "lastname": '',
      "gender": '',
      "phone": "",
      "total": "",
      "user": {
        "id": "",
        "user_type_id": "",
        "email": "",
        "username": "",
        "password": "",
        "salt": "",
        "created_at": "",
        "updated_at": "",
        "deleted_at": null,
        "device_unique_id": null,
        "profile":{
          "firstname": '',
          "lastname": '',
          "gender": '',
          "phone": "",
        }

      }
    },
    "tracking" : [],
    "order_status": {
      "id": "",
      "name": "",
      "visible": true,
      "order": 0,
      "created_at": "",
      "updated_at": "",
      "deleted_at": ""
    },
    histories : []
  };
  @observable newOrder = {};
  @observable orderList = [];
  @observable orderDetail = {};

  @observable orderAirlineDetail = {
    'id': '',
    'agent_id': '',
    'customer_id': '',
    'pnrid': '',
    'created_at': '',
    'updated_at': '',
    'deleted_at': '',
    'status': '',
    'retrieve': {
      'bookingcode': '',
      'status': '',
      'airlinecode': '',
      'timelimit': '',
      'total_fare': '',
      'pax': [],
      'paxcontact': {
        'firstname': '',
        'lastname': '',
        'phone1': '',
        'phone2': '',
        'email': ''
      },
      'flights': [],
    },
  }

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

  filterData = (query) => {
    return this.orderList.filter((data) => data.id.indexOf(query) > -1);
  }

  @action
  changeFilterBy = (val)=>{
    this.filterBy = val;
  }

  @action
  search(text) {
    console.log(this.orderList);
    this.dataFiltered = this.filterData(text);
  }

  @action
  getAllOrder() {
    this.isLoading = true;
    return this.http.get("order_stores")
      .then(res => {
        this.orderList = res;
        this.isLoading = false;
        console.log(res, 'ini');
      })
      .catch(err => {
        this.isLoading = false;
        throw err;
      })
  }


  @action
  getDetail(id) {
    this.isLoading = true;
    this.selectedOrder.id = id;

    this.http.get(`order_stores/${id}`)
      .then(res => {
        console.log(res)
        this.selectedOrder = res;
        this.isLoading = true;
      })
      .catch(err => {
        this.isLoading = false;
        throw err;
      });
  }

  @action
  acceptOrder() {
    this.http.post(`order_stores/${this.selectedOrder.id}/accept_order`).then(() => {
      this.context.globalUI.openSnackbar('Order successfully accepted');
      this.getDetail(this.selectedOrder.id);
    }).catch(err => {
      console.log('err', err.message);
      this.getDetail(this.selectedOrder.id);
      this.context.globalUI.showAlert(err.message);
    });
  }

  @action
  declineOrder() {
    this.http.post(`order_stores/${this.selectedOrder.id}/decline_order`).then(() => {
      this.context.globalUI.openSnackbar('Order successfully declined');
      this.getDetail(this.selectedOrder.id);
    }).catch(err => {
      this.getDetail(this.selectedOrder.id);
      this.context.globalUI.showAlert(err.message);
    });
  }

  @action
  inputShippingCode(shippingCode) {
    this.http.post(`order_stores/${this.selectedOrder.id}/input_shipping_code`, {
      shipping_code: shippingCode
    }).then(() => {
      this.context.globalUI.openSnackbar('Shipping code successfully submitted');
      this.getDetail(this.selectedOrder.id);
    }).catch(err => {
      this.getDetail(this.selectedOrder.id);
      this.context.globalUI.showAlert(err.message);
    });
  }

  @action
  createOrder() {

  }

  @action
  bookingGojek() {
    return this.http.post(`order_stores/${this.selectedOrder.id}/booking_gojek`, {}).then(() => {
      this.context.globalUI.openSnackbar('Booking Courier Gojek Success');
      this.getDetail(this.selectedOrder.id);
    }).catch(err => {
      this.getDetail(this.selectedOrder.id);
      console.log(err, 'yyohohohoho')
      this.context.globalUI.showAlert(err.message);
    });
  }

  @action
  calculatePrice() {
    return this.http.get(`order_stores/${this.selectedOrder.id}/calculate_price_gojek`).then((res) => {
      this.gojekPrice = res
    }).catch(err => {
      console.log(err)
      throw err
    });
  }

  @action
  addStoreNotes(data) {
    return this.http.put(`preorder/notes`, data).then((res) => {
      return res
    }).catch(err => {
      console.log(err)
      throw err
    });
  }
}
