// 1 => LOADING
// 2 => DONE
// 3 => ERROR

import * as api from "../api/api.js";

export default {
  namespaced: true,
  state: {
    mainTable: [],
    detailTable: [],
    countMainTable: 0,
    countDetailTable: 0,
    mainTableLoadingStatus: 0,
    detailTableLoadingStatus: 0,
    mainTableErrorMessage: "",
    detailTableErrorMessage: "",
    mainTableCurrentPage: 1,
    mainTableLastId: -1,

    selectedMainTable: {},
    selectedDetailTable: {},

    statusOptions: [
      { value: '', title: "All" },
      { value: 'draft', title: "Draft" },
      { value: 'pending', title: "Pending" },
      { value: 'approved', title: "Approved" },
      { value: 'rejected', title: "Rejected" },
      { value: 'paid', title: "Paid" },
      { value: 'completed', title: "Completed" },
    ],
    branchOptions: [],
    regionalOptions: [],

    menuStartDate: false,
    menuEndDate: false,
    menuDateUse: false,

    dStartDate: new Date().toISOString().substring(0, 10),
    dEndDate: new Date().toISOString().substring(0, 10),
    fStartDate: moment(new Date()).format("YYYY-MM-DD"),
    fEndDate: moment(new Date()).format("YYYY-MM-DD"),
    fStatus: '',
    fSearch: "",

    dDateUse: new Date().toISOString().substring(0, 10),
    xDateUse: moment(new Date()).format("YYYY-MM-DD"),
    xRegional: "",
    xBranch: "",
    xDescription: "",
    xPRID: 0,
    xNumber: "",
    xPRDID: 0,
    xDescriptionDetail: "",
    xAmountRequest: 0,
    xEstimationPrice: 0,

    dialogRequest: false,
    dialogDetail: false,
    act: "new",
    errMessage: "",
    succesMessage: "",
    alertError: false,
    alertSuccess: false,
    errors: [],
    total_price: 0,
    item_units: [],
    selected_item_unit: {},
    search_item_unit: "",
    loadingAutocomplete: false,
    divisionOptions: [],
    selectedDivision: {},
    itemCurrentPage: 1,
    itemTotalPage: 0,
    itemLastId: -1,
    items: [],
    selectedItems: [],
    selectedItem: {},
    searchItem: "",
    itemCurrentPage: 1,
    itemTotalPage: 0,
    loadingItem: false,
    itemGroupOptions: [],
    selectedItemGroup: {},
    loadingItemGroup: false,
    itemSubGroupOptions: [],
    selectedItemSubGroup: {},
    loadingItemSubGroup: false,
    dialogUnit: false,
    selectedUnit: {},
    unitOptions: [],
    loadingUnit: false,
    loadingSave: false,
    snackbar: {
        state: false,
        type: 'success',
        message: '',
    },
    loadingDetailRequest: false,
    itemCategoryOptions: [],
    selectedItemCategory: {},

    dialog_riwayat: false,
    riwayat_item: {},
    user: one_user(),
    dialogConfirmHandover: false,
  },
  mutations: {
    update_dialog_riwayat(state, val) {
      state.dialog_riwayat = val
    },
    update_riwayat_item(state,val) {
      state.riwayat_item = val
    },
    updateItemCategoryOptions(state, val) {
      state.itemCategoryOptions = val;
    },
    updateSelectedItemCategory(state, val) {
      state.selectedItemCategory = val;
    },
    updateLoadingDetailRequest(state, val) {
      state.loadingDetailRequest = val;
    },
    updateSnackbar(state, val) {
      state.snackbar = val;
    },
    updateSelectedItem(state, val) {
      state.selectedItem = val;
    },
    updateLoadingSave(state, val) {
      state.loadingSave = val;
    },
    updateLoadingUnit(state, val) {
      state.loadingUnit = val;
    },
    updateDialogUnit(state, val) {
      state.dialogUnit = val;
    },
    updateSelectedUnit(state, val) {
      state.selectedUnit = val;
    },
    updateUnitOptions(state, val) {
      state.unitOptions = val;
    },
    updateItemCurrentPage(state, val) {
      state.itemCurrentPage = val;
    },
    updateItemTotalPage(state, val) {
      state.itemTotalPage = val;
    },
    updateItemLastId(state, val) {
      state.itemLastId = val;
    },
    updateDivisionOptions(state, val) {
      state.divisionOptions = val;
    },
    updateSelectedDivision(state, val) {
      state.selectedDivision = val;
    },
    updateMainTable(state, val) {
      state.mainTable = val;
    },
    updateDetailTable(state, val) {
      state.detailTable = val;
    },
    updateCountMainTable(state, val) {
      state.countMainTable = val;
    },
    updateCountDetailTable(state, val) {
      state.countDetailTable = val;
    },
    updateMainTableLoadingStatus(state, val) {
      state.mainTableLoadingStatus = val;
    },
    updateDetailTableLoadingStatus(state, val) {
      state.detailTableLoadingStatus = val;
    },
    updateMainTableErrorMessage(state, val) {
      state.mainTableErrorMessage = val;
    },
    updateDetailTableErrorMessage(state, val) {
      state.detailTableErrorMessage = val;
    },
    updateMainTableCurrentPage(state, val) {
      state.mainTableCurrentPage = val;
    },
    updateMainTableLastId(state, val) {
      state.mainTableLastId = val;
    },

    updateSelectedMainTable(state, val) {
      state.selectedMainTable = val;
    },
    updateSelectedDetailTable(state, val) {
      state.selectedDetailTable = val;
    },

    updateBranchOptions(state, val) {
      state.branchOptions = val;
    },

    updateRegionalOptions(state, val) {
      state.regionalOptions = val;
    },

    updateMenuStartDate(state, val) {
      state.menuStartDate = val;
    },
    updateMenuEndDate(state, val) {
      state.menuEndDate = val;
    },
    updateMenuDateUse(state, val) {
      state.menuDateUse = val;
    },

    updateDStartDate(state, val) {
      state.dStartDate = val;
    },
    updateDEndDate(state, val) {
      state.dEndDate = val;
    },
    updateFStartDate(state, val) {
      state.fStartDate = val;
    },
    updateFEndDate(state, val) {
      state.fEndDate = val;
    },
    updateFStatus(state, val) {
      state.fStatus = val;
    },
    updateFSearch(state, val) {
      state.fSearch = val;
    },

    updateDDateUse(state, val) {
      state.dDateUse = val;
    },
    updateXDateUse(state, val) {
      state.xDateUse = val;
    },
    updateXRegional(state, val) {
      state.xRegional = val;
    },
    updateXBranch(state, val) {
      state.xBranch = val;
    },
    updateXDescription(state, val) {
      state.xDescription = val;
    },
    updateXPRID(state, val) {
      state.xPRID = val;
    },
    updateXNumber(state, val) {
      state.xNumber = val;
    },
    updateXPRDID(state, val) {
      state.xPRDID = val;
    },
    updateXDescriptionDetail(state, val) {
      state.xDescriptionDetail = val;
    },
    updateXAmountRequest(state, val) {
      state.xAmountRequest = val;
    },
    updateXEstimationPrice(state, val) {
      state.xEstimationPrice = val;
    },

    updateDialogRequest(state, val) {
      state.dialogRequest = val;
    },
    updateDialogDetail(state, val) {
      state.dialogDetail = val;
    },
    updateAct(state, val) {
      state.act = val;
    },
    updateErrMessage(state, val) {
      state.errMessage = val;
    },
    updateSuccessMessage(state, val) {
      state.successMessage = val;
    },
    updateAlertError(state, val) {
      state.alertError = val;
    },
    updateAlertSuccess(state, val) {
      state.alertSuccess = val;
    },
    updateErrors(state, val) {
      state.errors = val;
    },
    updateTotalPrice(state, val) {
      state.total_price = val;
    },
    updateItemUnits(state, val) {
      state.item_units = val;
    },
    updateSelectedItemUnit(state, val) {
      state.selected_item_unit = val;
    },
    updateSearchItemUnit(state, val) {
      state.search_item_unit = val;
    },
    updateLoadingAutocomplete(state, val) {
      state.loadingAutocomplete = val;
    },
    updateItems(state, val) {
      state.items = val;
    },
    updateSelectedItems(state, val) {
      state.selectedItems = val;
    },
    updateSearchItem(state, val) {
      state.searchItem = val;
    },
    updateLoadingItem(state, val) {
      state.loadingItem = val;
    },
    updateItemGroupOptions(state, val) {
      state.itemGroupOptions = val;
    },
    updateSelectedItemGroup(state, val) {
      state.selectedItemGroup = val;
    },
    updateItemSubGroupOptions(state, val) {
      state.itemSubGroupOptions = val;
    },
    updateSelectedItemSubGroup(state, val) {
      state.selectedItemSubGroup = val;
    },
    updateLoadingItemGroup(state, val) {
      state.loadingItemGroup = val;
    },
    updateLoadingItemSubGroup(state, val) {
      state.loadingItemSubGroup = val;
    },
    updateUser(state, val) {
      state.user = val
    },
    updateDialogConfirmHandover(state, val) {
      state.dialogConfirmHandover = val
    },
    
    
  },
  actions: {
    async search(context) {
      context.commit("updateMainTableLoadingStatus", 1);

      try {
        var url_string = window.location.href
        var url = new URL(url_string);
        var xdate = url.searchParams.get("date");
        var xsearch = url.searchParams.get("search");

        if (xsearch) {
          context.commit("updateFSearch", xsearch);
        }
        if (xdate) {
          context.commit("updateFStartDate", xdate);
          context.commit("updateFEndDate", xdate);
        }

        var payload = {
          token: one_token(),
          currentPage: context.state.mainTableCurrentPage,
          startDate: context.state.fStartDate,
          endDate: context.state.fEndDate,
          status: context.state.fStatus,
          search: context.state.fSearch,
          lastId: -1,
        };

        let response = await api.search(payload);
        if (response.status != "OK") {
          context.commit("updateMainTableLoadingStatus", 3);
          context.commit("updateMainTableErrorMessage", response.message);
        } else {
          context.commit("updateMainTableLoadingStatus", 2);
          context.commit("updateMainTableErrorMessage", "");
          let data = {
            records: response.data.records,
            total: response.data.total,
          };

          context.commit("updateMainTable", data.records);
          context.commit("updateCountMainTable", data.total);
          context.dispatch("system/getlistnotif", null, { root: true })
        }
      } catch (e) {
        context.commit("updateMainTableLoadingStatus", 3);
        context.commit("updateMainTableErrorMessage", e.message);
      }
    },
    async searchDetail(context) {
      context.commit("updateDetailTableLoadingStatus", 1);

      try {
        var payload = {
          token: one_token(),
          PRID: context.state.selectedMainTable.PurchaseRequestID,
        };

        let response = await api.searchDetail(payload);
        if (response.status != "OK") {
          context.commit("updateDetailTableLoadingStatus", 3);
          context.commit("updateDetailTableErrorMessage", response.message);
        } else {
          context.commit("updateDetailTableLoadingStatus", 2);
          context.commit("updateDetailTableErrorMessage", "");
          let data = {
            records: response.data.records,
            total: response.data.total,
          };

          context.commit("updateDetailTable", data.records);
          context.commit("updateCountDetailTable", data.total);
        }
      } catch (e) {
        context.commit("updateDetailTableLoadingStatus", 3);
        context.commit("updateDetailTableErrorMessage", e.message);
      }
    },
    async regional(context) {
      let response = await api.getRegional({ token: one_token() });
      if (response.status === "OK") {
        let data = {
          records: response.data.records,
        };

        context.commit("updateRegionalOptions", data.records);
      }
    },
    async branch(context) {
      let response = await api.getBranch({ token: one_token() });
      if (response.status === "OK") {
        let data = {
          records: response.data.records,
        };

        context.commit("updateBranchOptions", data.records);
      }
    },
    async saveRequest(context, payload) {
      try {
        payload.token = one_token();

        let response = await api.saveRequest(payload);
        if (response.status !== "OK") {
          context.commit("updateErrMessage", response.message);
        } else {
          context.commit("updateErrMessage", "");
          let data = {
            records: response.data.records,
            total: response.data.total,
          };

          if (data.total !== -1) {
            context.commit("updateErrors", []);
            context.commit("updateAlertSuccess", true);
            context.commit("updateDialogRequest", false);
            var msg = `Purchase Request baru dengan kode ${data.records[0].PurchaseRequestDirectNumber} sudah tersimpan`;
            context.commit("updateSuccessMessage", msg);
            context.dispatch("search");
            context.commit("updateSelectedMainTable", data.records[0]);
            context.dispatch("searchDetail");
          } else {
            context.commit("updateErrors", response.data.errors);
          }
        }
      } catch (e) {
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async updateRequest(context, payload) {
      try {
        payload.token = one_token();

        let response = await api.updateRequest(payload);
        if (response.status !== "OK") {
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateErrMessage", "");
          let data = {
            records: response.data.records,
            total: response.data.total,
          };

          if (data.total !== -1) {
            context.commit("updateErrors", []);
            context.commit("updateAlertSuccess", true);
            context.commit("updateDialogRequest", false);
            var msg = `Perubahan Purchase Request dengan kode ${data.records[0].PurchaseRequestDirectNumber} sudah tersimpan`;
            context.commit("updateSuccessMessage", msg);
            context.dispatch("search");
            context.commit("updateSelectedMainTable", data.records[0]);
            context.dispatch("searchDetail");
          } else {
            context.commit("updateErrors", response.data.errors);
          }
        }
      } catch (e) {
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async deleteRequest(context, payload) {
      try {
        payload.token = one_token();

        let response = await api.getAPI('deletePurchaseRequest', payload);
        if (response.status !== "OK") {
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateErrMessage", "");
          let data = {
            records: response.data.records,
            total: response.data.total,
          };

          if (data.total !== -1) {
            context.commit("updateErrors", []);
            context.commit("updateAlertSuccess", true);
            context.commit("updateDialogRequest", false);
            var msg = `Purchase Request dengan kode ${payload.PRNumber} sudah dihapus`;
            context.commit("updateSuccessMessage", msg);
            context.dispatch("search");
          } else {
            context.commit("updateErrors", response.data.errors);
          }
        }
      } catch (e) {
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async saveDetail(context, payload) {
      try {
        payload.token = one_token();

        let response = await api.saveDetail(payload);
        if (response.status !== "OK") {
          context.commit("updateErrMessage", response.message);
        } else {
          context.commit("updateErrMessage", "");
          let data = {
            records: response.data.records,
            total: response.data.total,
          };

          if (data.total !== -1) {
            context.commit("updateErrors", []);
            context.commit("updateAlertSuccess", true);
            context.commit("updateDialogDetail", false);
            var msg = `Request baru ${payload.PRDDescription} sudah tersimpan`;
            context.commit("updateSuccessMessage", msg);
            context.dispatch("search");
            context.dispatch("searchDetail");
          } else {
            context.commit("updateErrors", response.data.errors);
          }
        }
      } catch (e) {
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async updateDetail(context, payload) {
      try {
        payload.token = one_token();

        let response = await api.updateDetail(payload);
        if (response.status !== "OK") {
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateErrMessage", "");
          let data = {
            records: response.data.records,
            total: response.data.total,
          };

          if (data.total !== -1) {
            context.commit("updateErrors", []);
            context.commit("updateAlertSuccess", true);
            context.commit("updateDialogDetail", false);
            var msg = "Perubahan Request sudah tersimpan";
            context.commit("updateSuccessMessage", msg);
            context.dispatch("search");
            context.dispatch("searchDetail");
          } else {
            context.commit("updateErrors", response.data.errors);
          }
        }
      } catch (e) {
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async deleteDetail(context, payload) {
      try {
        payload.token = one_token();

        let response = await api.deleteDetail(payload);
        if (response.status !== "OK") {
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateErrMessage", "");
          let data = {
            records: response.data.records,
            total: response.data.total,
          };

          if (data.total !== -1) {
            context.commit("updateErrors", []);
            context.commit("updateAlertSuccess", true);
            context.commit("updateDialogDetail", false);
            var msg = `Request [${payload.PRDDescriptionDetail}] sudah dihapus`;
            context.commit("updateSuccessMessage", msg);
            context.dispatch("search");
            context.dispatch("searchDetail");
          } else {
            context.commit("updateErrors", response.data.errors);
          }
        }
      } catch (e) {
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async orderRequest(context, payload) {
      try {
        payload.token = one_token();

        let response = await api.orderRequest(payload);
        if (response.status !== "OK") {
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateErrMessage", "");
          let data = {
            records: response.data.records,
            total: response.data.total,
          };

          if (data.total !== -1) {
            context.commit("updateErrors", []);
            context.commit("updateAlertSuccess", true);
            context.commit("updateDialogDetail", false);
            context.dispatch("search");
            context.commit("updateSelectedMainTable", data.records[0]);
            context.dispatch("searchDetail");
          } else {
            context.commit("updateErrors", response.data.errors);
          }
        }
      } catch (e) {
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },

    async getUnit(context, prm) {
        context.commit("updateLoadingAutocomplete", true)
        try {
            let resp = await api.getUnit(one_token(), prm)
            if (resp.status != "OK") {
                context.commit("updateLoadingAutocomplete", false)
                context.commit("updateErrMessage", response.message);
                context.commit("updateAlertError", true);
            } else {
                context.commit("updateLoadingAutocomplete", false)
                context.commit("updateErrMessage", "");
                let data = {
                    records: resp.data.records
                }

                context.commit("updateItemUnits", data.records)
            }
        } catch (e) {
            context.commit("updateLoadingAutocomplete", true)
            context.commit("updateErrMessage", e.message);
            context.commit("updateAlertError", true);
        }
    },
    async getDivisionByUser(context) {
      context.commit("updateLoadingAutocomplete", true)
      try {
        let response = await api.getDivisionByUser({ token: one_token() });
        if (response.status != "OK") {
          context.commit("updateLoadingAutocomplete", false)
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateLoadingAutocomplete", false)
          let data = {
          records: response.data.records,
          selected: response.data.selected
          };
          context.commit("updateDivisionOptions", data.records);
          context.commit("updateSelectedDivision", data.selected);
          context.dispatch("getItemGroupSubGroup",data.selected); 
        }
      } catch (e) {
          context.commit("updateLoadingAutocomplete", false)
          context.commit("updateErrMessage", e.message);
          context.commit("updateAlertError", true);
      }
    },
    async getItemGroupSubGroup(context, payload) {
      context.commit("updateLoadingItemGroup", true)
      context.commit("updateLoadingItemSubGroup", true)
      try {
        let response = await api.getItemGroupSubGroup({ token: one_token(), division: payload.M_DivisionID ? payload.M_DivisionID : "" });
        if (response.status != "OK") {
          context.commit("updateLoadingItemGroup", false)
          context.commit("updateLoadingItemSubGroup", false)
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateLoadingItemGroup", false)
          context.commit("updateLoadingItemSubGroup", false)
          context.commit("updateErrMessage", "");
          context.commit("updateItemGroupOptions", response.data.records);
          context.commit("updateSelectedItemGroup", response.data.records[0]);
          context.commit("updateItemSubGroupOptions", response.data.records[0].subGroups);
          context.commit("updateSelectedItemSubGroup", response.data.records[0].subGroups[0]);

          /*let division = payload.M_DivisionID ? payload.M_DivisionID : "";
          let itemGroup = response.data.records[0].Nat_GroupID ? response.data.records[0].Nat_GroupID : "";
          let itemSubGroup = response.data.records[0].subGroups[0].Nat_SubGroupID ? response.data.records[0].subGroups[0].Nat_SubGroupID : "";

          let payload = {
            search: "",
            division: division,
            itemGroup: itemGroup,
            itemSubGroup: itemSubGroup
          };
          context.dispatch("getItems", payload);*/
      }
    } catch (e) {
      context.commit("updateLoadingItemGroup", false)
      context.commit("updateLoadingItemSubGroup", false)
      context.commit("updateErrMessage", e.message);
      context.commit("updateAlertError", true);
    }
    },
    async getItems(context, payload) {
      context.commit("updateLoadingItem", true)
      try {
        payload.token = one_token();
        payload.currentPage = context.state.itemCurrentPage;
        let response = await api.getItems(payload);
        if (response.status != "OK") {
          context.commit("updateLoadingItem", false)
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateLoadingItem", false)
          context.commit("updateErrMessage", "");
          context.commit("updateItems", response.data.records);
          context.commit("updateItemTotalPage", response.data.total);
          if(!isEmpty(context.state.selectedMainTable)) {
            context.dispatch("getSelectedItem", context.state.selectedMainTable);
          }
        }
      } catch (e) {
        context.commit("updateLoadingItem", false)
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async getSelectedItem(context, payload) {
      try {
        payload.token = one_token();
        let response = await api.getSelectedItem(payload);
        if (response.status != "OK") {
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateErrMessage", "");
          context.commit("updateSelectedItem", response.data.records);
        }
      } catch (e) {
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async getUnitByItemId(context, payload) {
      context.commit("updateLoadingUnit", true)
      context.commit("updateDialogUnit", true);
      try {
        payload.token = one_token();
        let response = await api.getUnitByItemId(payload);
        if (response.status != "OK") {
          context.commit("updateLoadingUnit", false)
          context.commit("updateDialogUnit", false);
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateLoadingUnit", false)
          context.commit("updateErrMessage", "");
          context.commit("updateUnitOptions", response.data.records);
          context.commit("updateSelectedUnit", payload.selectedUnit);
          
        }
      } catch (e) {
        context.commit("updateLoadingUnit", false)
        context.commit("updateDialogUnit", false);
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async getGroupByDivision(context, payload) {
      context.commit("updateLoadingItemGroup", true)
      try {
        let response = await api.getGroupByDivision(payload);
        if (response.status != "OK") {
          context.commit("updateLoadingItemGroup", false)
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateLoadingItemGroup", false)
          context.commit("updateErrMessage", "");
          context.commit("updateItemGroupOptions", response.data.records);
          context.commit("updateSelectedItemGroup", response.data.records[0]);
        }
      } catch (e) {
        context.commit("updateLoadingItemGroup", false)
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async savePurchaseRequest(context, payload) {
      context.commit("updateLoadingSave", true);
      try {
        payload.token = one_token();
        let response = await api.savePurchaseRequest(payload);
        if (response.status != "OK") {
          context.commit("updateLoadingSave", false);
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateLoadingSave", false);
          context.commit("updateDialogRequest", false);
          context.commit("updateSnackbar", {
            state: true,
            type: 'success',
            message: 'Purchase Request berhasil disimpan',
          });
          console.log(context.state.snackbar);
          context.commit("updateSelectedItems", []);
          context.commit("updateSelectedItem", {});
          context.commit("updateItems", []);
          context.commit("updateItemTotalPage", 0);
          context.commit("updateItemCurrentPage", 1);
          context.commit("updateItemLastId", -1);
          context.dispatch("search");
        }

      } catch (e) {
        context.commit("updateLoadingSave", false);
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },

    async saveAndDoPurchaseRequest(context, payload) {
      context.commit("updateLoadingSave", true);
      try {
        payload.token = one_token();
        
        let response = await api.saveAndDoPurchaseRequest(payload);
        if (response.status !== "OK") {
          context.commit("updateLoadingSave", false);
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateLoadingSave", false);
          context.commit("updateDialogRequest", false);
          context.commit("updateSnackbar", {
            state: true,
            type: 'success',
            message: 'Purchase Request berhasil disimpan dan diajukan',
          });
          console.log(context.state.snackbar);
          context.commit("updateSelectedItems", []);
          context.commit("updateSelectedItem", {});
          context.commit("updateItems", []);
          context.commit("updateItemTotalPage", 0);
          context.commit("updateItemCurrentPage", 1);
          context.commit("updateItemLastId", -1);
          context.dispatch("search");
          context.dispatch("system/getlistnotif", null, { root: true })
        }
      } catch (e) {
        context.commit("updateLoadingSave", false);
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },

    async getDetailRequest(context, payload) {
      context.commit("updateLoadingDetailRequest", true)
      try {
        payload.token = one_token();
        let response = await api.getAPI('getDetailRequest',payload);
        if (response.status != "OK") {
          context.commit("updateLoadingDetailRequest", false)
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateSelectedItems", response.data.records);
          context.commit("updateLoadingDetailRequest", false)
        }
      } catch (e) {
        context.commit("updateLoadingDetailRequest", false)
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async updatePurchaseRequest(context, payload) {
      context.commit("updateLoadingSave", true);
      try {
        payload.token = one_token();
        let response = await api.getAPI('updatePurchaseRequest',payload);
        if (response.status != "OK") {
          context.commit("updateLoadingSave", false);
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateLoadingSave", false);
          context.commit("updateDialogRequest", false);
          context.commit("updateSnackbar", {
            state: true,
            type: 'success',
            message: 'Purchase Request '+payload.PRNumber+' berhasil diupdate',
          });
          console.log(context.state.snackbar);
          context.commit("updateSelectedItems", []);
          context.commit("updateSelectedItem", {});
          context.commit("updateItems", []);
          context.commit("updateItemLastId", -1);
          context.dispatch("search");
        }

      } catch (e) {
        context.commit("updateLoadingSave", false);
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async getRegionalBranchByUser(context) {
      console.log("getRegionalBranchByUser");
      let response = await api.getAPI('getRegionalBranchByUser',{ token: one_token() });
      if (response.status != "OK") {
        context.commit("updateErrMessage", response.message);
        context.commit("updateAlertError", true);
      } else {
        context.commit("updateBranchOptions", response.data);
      }
    },
    async getItemCategory(context) {
      let response = await api.getAPI('getItemCategory',{ token: one_token() });
      if (response.status != "OK") {
        context.commit("updateErrMessage", response.message);
        context.commit("updateAlertError", true);
      } else {
        context.commit("updateItemCategoryOptions", response.data.records);
      }

    },
    async getRiwayatItem(context, param) {
      try {
        param.token = one_token()
        param.page = 1

        let response = await api.getRiwayatItem(param);
        if (response.status != 'OK') {
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("update_riwayat_item", response.data)
        }
      } catch (error) {
        context.commit("updateErrMessage", error.message);
        context.commit("updateAlertError", true);
      }
    },
    async confirmHandOver(context, payload) {
      context.commit("updateLoadingSave", true);
      try {
        payload.token = one_token();
        let response = await api.confirmHandOver(payload);
        if (response.status != "OK") {
          context.commit("updateLoadingSave", false);
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateLoadingSave", false);
          context.commit("updateDialogConfirmHandover", false);
          context.commit("updateSnackbar", {
            state: true,
            type: 'success',
            message: response.data,
          });
          context.dispatch("search");
        }

      } catch (e) {
        context.commit("updateLoadingSave", false);
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
  },
};