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

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

export default {
  namespaced: true,
  state: {
    errMessage: "",
    alertError: false,
    snackbar: {
        state: false,
        type: 'success',
        message: '',
    },
    user: one_user(),
    currentPage: 1,
    loadingStatus: 1, // 1 => LOADING, 2 => DONE, 3 => ERROR
    itemUsedList: [],
    selectedItemUsed: {},
    totalPageUsed: 0,
    selectedUsedQty: 0,
    dialogUsed: false,
    errors: [],
    dialogDelete: false,
    dialogConfirmation: false,
    search: "",
    statusConfirmOptions: [
        { value: "X", text: "Semua" },
        { value: "N", text: "Belum Dikonfirmasi" },
        { value: "Y", text: "Sudah Dikonfirmasi" },
        
    ],
    statusConfirm: "X",
    menuStartDate: false,
    menuEndDate: false,
    fStartDate: moment(new Date()).format("YYYY-MM-DD"),
    fEndDate: moment(new Date()).format("YYYY-MM-DD"),
    dStartDate: new Date().toISOString().substring(0, 10),
    dEndDate: new Date().toISOString().substring(0, 10)
  },
  mutations: {
    updateErrMessage(state, val) {
      state.errMessage = val;
    },
    updateAlertError(state, val) {
      state.alertError = val;
    },
    updateSnackbar(state, val) {
      state.snackbar = val;
    },
    updateLoadingAutocomplete(state, val) {
      state.loadingAutocomplete = val;
    },
    updateDivisionOptions(state, val) {
      state.divisionOptions = val;
    },
    updateSelectedDivision(state, val) {
      state.selectedDivision = val;
    },
    updateUser(state, val) {
      state.user = val;
    },
    updateCurrentPage(state, val) {
      state.currentPage = val;
    },
    updateLoadingStatus(state, val) {
      state.loadingStatus = val;
    },
    updateItemUsedList(state, val) {
      state.itemUsedList = val;
    },
    updateSelectedItemUsed(state, val) {
      state.selectedItemUsed = val;
    },
    updateTotalPageUsed(state, val) {
      state.totalPageUsed = val;
    },
    updateSelectedUsedQty(state, val) {
      state.selectedUsedQty = val;
    },
    updateDialogUsed(state, val) {
      state.dialogUsed = val;
    },
    updateErrors(state, val) {
      state.errors = val;
    },
    updateDialogDelete(state, val) {
      state.dialogDelete = val;
    },
    updateDialogConfirmation(state, val) {
      state.dialogConfirmation = val;
    },
    updateSearch(state, val) {
      state.search = val;
      state.currentPage = 1; // Reset to first page on new search
    },
    updateStatusConfirm(state, val) {
      state.statusConfirm = val;
    },
    updateStatusConfirmOptions(state, val) {
      state.statusConfirmOptions = val;
    },
    updateMenuStartDate(state, val) {
      state.menuStartDate = val;
    },
    updateMenuEndDate(state, val) {
      state.menuEndDate = val;
    },
    updateFStartDate(state, val) {
      state.fStartDate = val;
    },
    updateFEndDate(state, val) {
      state.fEndDate = val;
    },
    updateDStartDate(state, val) {
      state.dStartDate = val;
    },
    updateDEndDate(state, val) {
      state.dEndDate = val;
    }
  },
  actions: {
    async getItemUsed(context, payload) {
      context.commit("updateLoadingStatus", 1);
      try {
        payload.token = one_token();
        // var payload = {
        //     itemUsageID: context.state.usage.selectedItemUsage.ItemUsageID,
        //     currentPage: context.state.currentPage,
        //     search: context.state.search,
        //     statusConfirm: context.state.statusConfirm,
        //     token: one_token()
        // };

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

          context.commit("updateItemUsedList", data.records);
          context.commit("updateTotalPageUsed", data.total_page);
        }
      } catch (e) {
        context.commit("updateLoadingStatus", 3);
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async updateItemUsed(context, payload) {
      context.commit("updateLoadingStatus", 1);
      try {
        payload.token = one_token();

        let response = await api.updateItemUsed(payload);
        if (response.status != "OK") {
          context.commit("updateLoadingStatus", 3);
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateLoadingStatus", 2);
          context.commit("updateErrMessage", "");


          context.commit("updateDialogUsed", false);
          context.commit("updateSelectedItemUsed", {});
          context.commit("updateSelectedUsedQty", 0);
          context.commit("usage/update_last_id", payload.itemUsageID, { root: true });

          context.commit("updateSnackbar", {
            state: true,
            type: 'success',
            message: 'Item used berhasil diupdate.'
          });
          context.dispatch("usage/search",{}, { root: true });
          context.dispatch("getItemUsed", {
            itemUsageID: payload.itemUsageID,
            currentPage: payload.currentPage,
            search: payload.search,
            statusConfirm: payload.statusConfirm,
            startdate: payload.startdate,
            enddate: payload.enddate
          });
        }
      } catch (e) {
        context.commit("updateLoadingStatus", 3);
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async deleteItemUsed(context, payload) {
      context.commit("updateLoadingStatus", 1);
      try {
        payload.token = one_token();

        let response = await api.deleteItemUsed(payload);
        if (response.status != "OK") {
          context.commit("updateLoadingStatus", 3);
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateLoadingStatus", 2);
          context.commit("updateErrMessage", "");


          context.commit("updateDialogDelete", false);
          context.commit("updateSelectedItemUsed", {});
          context.commit("usage/update_last_id", payload.itemUsageID, { root: true });

          context.commit("updateSnackbar", {
            state: true,
            type: 'success',
            message: 'Item used berhasil dihapus.'
          });
          context.dispatch("usage/search",{}, { root: true });
          context.dispatch("getItemUsed", {
            itemUsageID: payload.itemUsageID,
            currentPage: payload.currentPage,
            search: payload.search,
            statusConfirm: payload.statusConfirm,
            startdate: payload.startdate,
            enddate: payload.enddate
          });
        }
      } catch (e) {
        context.commit("updateLoadingStatus", 3);
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    },
    async confirmItemUsed(context, payload) {
      context.commit("updateLoadingStatus", 1);
      try {
        payload.token = one_token();

        let response = await api.confirmItemUsed(payload);
        if (response.status != "OK") {
          context.commit("updateLoadingStatus", 3);
          context.commit("updateErrMessage", response.message);
          context.commit("updateAlertError", true);
        } else {
          context.commit("updateLoadingStatus", 2);
          context.commit("updateErrMessage", "");


            context.commit("updateSelectedItemUsed", {});
            context.commit("updateDialogConfirmation", false);
            context.commit("usage/update_last_id", payload.itemUsageID, { root: true });

          context.commit("updateSnackbar", {
            state: true,
            type: 'success',
            message: 'Item used berhasil dikonfirmasi.'
          });
          context.dispatch("getItemUsed", {
            itemUsageID: payload.itemUsageID,
            currentPage: payload.currentPage,
            search: payload.search,
            statusConfirm: payload.statusConfirm,
            startdate: payload.startdate,
            enddate: payload.enddate
          });
        }
      } catch (e) {
        context.commit("updateLoadingStatus", 3);
        context.commit("updateErrMessage", e.message);
        context.commit("updateAlertError", true);
      }
    }
  }
};