// 1 => LOADING
// 2 => DONE
// 3 => ERROR
import * as api from "../api/expedition.js";
export default {
  namespaced: true,
  state: {
    expedition: [],
    lookup_expedition: 2,
    search_status: false,
    current_page: 1,
    last_id: -1,
    x_search: "",
    total: 0,
    total_filter: 0,
    selected_item: {},
    dialog_form: false,
    staff: [],
    save_status: 2,
    error_message: "",
    msg_success: "",
    alert_success: false,
    sorting: {
      sortBy: "ExpeditionID",
      type: "asc",
    },
  },
  mutations: {
    update_lookup_expedition(state, val) {
      state.lookup_expedition = val;
    },
    update_search_status(state, val) {
      state.search_status = val;
    },
    update_expedition(state, val) {
      state.expedition = val.records;
      state.total = val.total;
      state.total_filter = val.total_filter;
    },
    update_selected_item(state, val) {
      state.selected_item = val;
    },
    update_current_page(state, val) {
      state.current_page = val;
    },
    update_x_search(state, val) {
      state.x_search = val;
      state.current_page = 1;
    },
    update_sorting(state, val) {
      state.sorting = val;
    },
    update_dialog_form(state, val) {
      state.dialog_form = val;
    },
    update_staff(state, val) {
      state.staff = val.records;
    },
    update_save_status(state, val) {
      state.save_status = val;
    },
    update_error_message(state, val) {
      state.error_message = val;
    },
    update_last_id(state, val) {
      state.last_id = val;
    },
    update_msg_success(state, val) {
      state.msg_success = val;
    },
    update_alert_success(state, val) {
      state.alert_success = val;
    },
  },
  actions: {
    async lookup(context) {
      context.commit("update_lookup_expedition", 1);
      context.commit("update_search_status", true);
      try {
        var tkn = JSON.stringify({
          token: one_token(),
          current_page: context.state.current_page,
          search: context.state.x_search,
          order_by: context.state.sorting.sortBy,
          order_type: context.state.sorting.type,
        });
        let resp = await api.lookup(tkn);
        if (resp.status != "OK") {
          context.commit("update_search_status", false);
          context.commit("update_lookup_expedition", 3);
          console.log(resp.message);
        } else {
          context.commit("update_search_status", false);
          context.commit("update_lookup_expedition", 2);
          let data = resp.data;
          if (context.state.last_id == -1 && resp.data.records.length >= 1) {
            context.commit("update_selected_item", resp.data.records[0]);
          } else {
            let idx = _.findIndex(resp.data.records, function (o) {
              return o.id == context.state.last_id;
            });
            if (resp.data.records[idx] != undefined) {
              context.commit("update_selected_item", resp.data.records[idx]);
            } else {
              context.commit("update_selected_item", resp.data.records[0]);
            }
          }
          if (resp.data.records.length < 1) {
            context.commit("update_selected_item", {
              id: "",
              name: "",
              internal: "",
              staff: [],
            });
          }

          context.commit("update_expedition", data);
        }
      } catch (e) {
        context.commit("update_search_status", false);
        context.commit("update_lookup_expedition", 3);
        // context.commit("update_lookup_error_message", e.message);
        console.log(e);
      }
    },
    async getStaff(context) {
      context.commit("update_lookup_expedition", 1);
      context.commit("update_search_status", true);
      try {
        var tkn = JSON.stringify({
          token: one_token(),
        });
        let resp = await api.get_staff(tkn);
        if (resp.status != "OK") {
          context.commit("update_search_status", false);
          context.commit("update_lookup_expedition", 3);
          console.log(resp.message);
        } else {
          context.commit("update_search_status", false);
          context.commit("update_lookup_expedition", 2);
          let data = resp.data;
          context.commit("update_staff", data);
        }
      } catch (e) {
        context.commit("update_search_status", false);
        context.commit("update_lookup_expedition", 3);
        // context.commit("update_lookup_error_message", e.message);
        console.log(e);
      }
    },
    async addData(context, prm) {
      context.commit("update_save_status", 1);
      try {
        prm.token = one_token();
        let resp = await api.additem(prm);
        if (resp.status != "OK") {
          context.commit("update_save_status", 3);
          context.commit("update_error_message", resp.message);
          console.log(resp.message);
        } else {
          context.commit("update_error_message", "");
          context.commit("update_save_status", 2);
          let data = resp.data;
          context.dispatch("lookup");
          var msg = "Ekspedisi " + prm.name + " sudah tersimpan";
          context.commit("update_msg_success", msg);
          context.commit("update_alert_success", true);
          context.commit("update_dialog_form", false);
        }
      } catch (e) {
        context.commit("update_error_message", e);
        context.commit("update_lookup_expedition", 3);
        console.log(e);
      }
    },
    async editData(context, prm) {
      context.commit("update_save_status", 1);
      try {
        prm.token = one_token();
        let resp = await api.edititem(prm);
        if (resp.status != "OK") {
          context.commit("update_save_status", 3);
          context.commit("update_error_message", resp.message);
          console.log(resp.message);
        } else {
          context.commit("update_error_message", "");
          context.commit("update_save_status", 2);
          let data = resp.data;
          context.dispatch("lookup");
          var msg = "Ekspedisi " + prm.name + " berhasil diubah";
          context.commit("update_msg_success", msg);
          context.commit("update_alert_success", true);
          context.commit("update_dialog_form", false);
        }
      } catch (e) {
        context.commit("update_error_message", e);
        context.commit("update_lookup_expedition", 3);
        console.log(e);
      }
    },
    async deleteData(context, prm) {
      context.commit("update_save_status", 1);
      try {
        prm.token = one_token();
        let resp = await api.deleteitem(prm);
        if (resp.status != "OK") {
          context.commit("update_save_status", 3);
          context.commit("update_error_message", resp.message);
          console.log(resp.message);
        } else {
          context.commit("update_error_message", "");
          context.commit("update_save_status", 2);
          let data = resp.data;
          context.dispatch("lookup");
          var msg = "Ekspedisi " + prm.name + " berhasil dihapus";
          context.commit("update_msg_success", msg);
          context.commit("update_alert_success", true);
          context.commit("update_dialog_form", false);
        }
      } catch (e) {
        context.commit("update_error_message", e);
        context.commit("update_lookup_expedition", 3);
        console.log(e);
      }
    },
  },
};
