// 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: 'Send Request', title: "Send Request" },
      { value: 'Process', title: "Process" },
      { value: 'Rejected', title: "Rejected" },
      { value: 'Partial', title: "Partial" },
      { 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,
    warehouseOptions: [],
    selectedWarehouse: {},
    user: one_user(),
    dialogItem: false,
    dialogApprove: false,
    act_approve: "approve",
    levelname: [],
    loadingSaveRequest: false,
    approveLevelID: 0,
    approveDivision: '',
    approveDivisionUserID: 0,
    categoryApprove: '',
    emptycheckvalue: [],
    flag: ''
  },
  mutations: {
    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;
    },
    updateWarehouseOptions(state, val) {
      state.warehouseOptions = val
    },
    updateSelectedWarehouse(state, val) {
      state.selectedWarehouse = val
    },
    updateUser(state, val) {
      state.user = val
    },
    updateDialogItem(state, val) {
      state.dialogItem = val
    },
    update_dialogApprove(state, val) {
      state.dialogApprove = val
    },
    update_act_approve(state, val) {
      state.act_approve = val
    },
    updateLevelName(state, val) {
      state.levelname = val;
    },
    updateLoadingSaveRequest(state, val) {
      state.loadingSaveRequest = val;
    },
    updateApproveLevelID(state, val) {
      state.approveLevelID = val;
    },
    updateApproveDivision(state, val) {
      state.approveDivision = val;
    },
    updateApproveDivisionUserID(state, val) {
      state.approveDivisionUserID = val;
    },
    updateCategoryApprove(state, val) {
      state.categoryApprove = val;
    },
    updateEmptycheckvalue(state, val) {
      state.emptycheckvalue = val
    },
    update_flag(state, val) {
      state.flag = val
    }
  },
  actions: {
    async search(context) {
      context.commit("updateMainTableLoadingStatus", 1);

      try {
        var url_string = window.location.href
        var url = new URL(url_string);
        var status = url.searchParams.get("status");
        var xstartdate = url.searchParams.get("startdate");
        var xsearch = url.searchParams.get("search");
        var xflag = url.searchParams.get("flag");

        if (status === 'SendRequest') {
          status = 'Send Request';
        }

        // untuk notifikasi
        if (xflag == 'N') {

            if (xsearch != null && xsearch != '') {
                context.commit("updateFSearch", xsearch);
            }
            if (status != null && status != '') {
                context.commit("updateFStatus", status);
            }
            if (xstartdate != null && xstartdate != '') {
                context.commit("updateFStartDate", xstartdate);
                context.commit("updateFEndDate", xstartdate);
            }

        } else if (xflag == 'G') {
            // untuk get data param approve all

            if (status != null && status != '') {
                context.commit("updateFStatus", status);
            }
            if (xstartdate != null && xstartdate != '') {
                context.commit("updateFStartDate", xstartdate);
                context.commit("updateFEndDate", moment(new Date()).format('YYYY-MM-DD'));
            }
            context.commit("update_flag", xflag);

        }

        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,
          flag: context.state.flag,
          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);
        }
      } 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 deleteRequest(context, payload) {
      try {
        payload.token = one_token();

        let response = await api.deleteRequest(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 = `Request dengan kode ${payload.RIONumber} berhasil 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, payload) {
      context.commit("updateLoadingAutocomplete", true)
      try {
        payload.token = one_token()
        payload.userId = context.state.user.M_UserID
        let response = await api.getDivisionByUser(payload);
        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("updateErrMessage", "cek divison");
          context.commit("updateDivisionOptions", data.records);
          context.commit("updateSelectedDivision", data.selected[0]);
          // context.dispatch("getItemGroupSubGroup",data.selected[0]); 
        }
      } catch (e) {
          context.commit("updateLoadingAutocomplete", false)
          context.commit("updateErrMessage", e.message);
          context.commit("updateAlertError", true);
      }
    },
    async getItemGroupSubGroup(context) {
      context.commit("updateLoadingItemGroup", true)
      context.commit("updateLoadingItemSubGroup", true)
      try {
        let response = await api.getItemGroupSubGroup({ token: one_token(), division: context.state.selectedDivision.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", "item group");
          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]);

      }
    } 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", "cekkk");
          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 saveRequest(context, payload) {
      context.commit("updateLoadingSave", true);
      try {
        payload.token = one_token();
        let response = await api.saveRequest(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: 'Request pengeluaran barang 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 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 updateRequest(context, payload) {
      context.commit("updateLoadingSave", true);
      try {
        payload.token = one_token();
        let response = await api.getAPI('updateRequest',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);
          if (payload.act == 'sendrequest') {
            context.commit("updateSnackbar", {
              state: true,
              type: 'success',
              message: 'Send Request '+payload.RIONumber+' berhasil',
            });  
          } else {
            context.commit("updateSnackbar", {
              state: true,
              type: 'success',
              message: 'Request '+payload.RIONumber+' berhasil diupdate',
            });  
          }

          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 getWarehouse(context) {
      context.commit("updateLoadingAutocomplete", true)
      try {
        let prm = {}
        prm.regionalId = context.state.user.S_RegionalID
        prm.branchCode = context.state.user.M_BranchCode
        prm.token = one_token()
        let response = await api.getWarehouse(prm);
        if (response.status !== "OK") {
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
          context.commit("updateLoadingAutocomplete", false)
        } else {
          context.commit("updateLoadingAutocomplete", false)
          context.commit("updateErrMessage", "cekk warehouse");
          let data = {
            records: response.data.records,
          };

            // context.commit("updateWarehouseOptions", data.records);
            context.commit("updateSelectedWarehouse", data.records);
        }
      } catch (e) {
        context.commit("updateLoadingAutocomplete", true)
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },

    async getlevel(context) {
      context.commit("updateMainTableLoadingStatus", 1);

      try {
        var payload = {
          token: one_token()
        };

        let response = await api.getlevel(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("updateLevelName", data.records[0].M_ApproveLevelName);
          context.commit("updateApproveLevelID", data.records[0].M_ApproveLevelID);
        }
      } catch (e) {
        context.commit("updateMainTableLoadingStatus", 3);
        context.commit("updateMainTableErrorMessage", e.message);
      }
    },

    async getDivisionUser(context) {
      context.commit("updateMainTableLoadingStatus", 1);

      try {
        var payload = {
          token: one_token()
        };

        let response = await api.getDivisionUser(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,
          };

          context.commit("updateApproveDivision", data.records[0].DivisionName);
          context.commit("updateApproveDivisionUserID", data.records[0].M_UserDivisionDivisionID);
        }
      } catch (e) {
        context.commit("updateMainTableLoadingStatus", 3);
        context.commit("updateMainTableErrorMessage", e.message);
      }
    },

    async approveRequest(context, payload) {
      context.commit("updateLoadingSaveRequest", true);
      try {
        payload.token = one_token();
        let response = await api.approveRequest(payload);
        if (response.status != "OK") {
          context.commit("updateLoadingSaveRequest", false);
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateLoadingSaveRequest", false);
          context.commit("update_dialogApprove", false);
          context.commit("updateSnackbar", {
            state: true,
            type: 'success',
            message: 'Request pengeluaran barang berhasil approved',
          });
          context.dispatch("search");
        }

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

    async rejectRequest(context, payload) {
      context.commit("updateLoadingSaveRequest", true);
      try {
        payload.token = one_token();
        let response = await api.rejectRequest(payload);
        if (response.status != "OK") {
          context.commit("updateLoadingSaveRequest", false);
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateLoadingSaveRequest", false);
          context.commit("update_dialogApprove", false);
          context.commit("updateSnackbar", {
            state: true,
            type: 'success',
            message: 'Request pengeluaran barang berhasil reject',
          });
          context.dispatch("search");
        }

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