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

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

export default {
  namespaced: true,
  state: {
    request: [],
    selectedRequest: [],
    totalPage: 0,
    currentPage: 1,
    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"),
    menuStartDate: false,
    menuEndDate: false,

    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" },
    ],
    fStatus: {value: "", title: "All"},
    fSearch: "",
    lastId: -1,
    loadingStatus: 0,
    errorMessage: "",
    selectedItems: [],
    selectedItem: {},
    opendialoginfo: false,
    msginfo: "",
    loadingSave: false,
    errMessage: "",
    alertError: false,
    snackbar: {
        state: false,
        type: 'success',
        message: '',
    },
    dialogapprove: false,
    msgapprove: '',
    dialogreject: false,
    msgreject: '',
    approveDivisionUserID: 0,
    approveDivision: '',
  },
  mutations: {
    update_request(state, val) {
        state.request = val
    },
    update_selectedRequest(state, val) {
        state.selectedRequest = val
    },
    update_totalPage(state, val) {
        state.totalPage = val
    },
    update_currentPage(state, val) {
        state.currentPage = val
    },
    update_dStartDate(state, val) {
        state.dStartDate = val
    },
    update_dEndDate(state, val) {
        state.dEndDate = val
    },
    update_fStartDate(state, val) {
        state.fStartDate = val
    },
    update_fEndDate(state, val) {
        state.fEndDate = val
    },
    update_menuStartDate(state, val) {
        state.menuStartDate = val
    },
    update_menuEndDate(state, val) {
        state.menuEndDate = val
    },
    update_statusOptions(state, val) {
        state.statusOptions = val
    },
    update_fStatus(state, val) {
        state.fStatus = val
    },
    update_fSearch(state, val) {
        state.fSearch = val
    },
    update_lastId(state, val) {
        state.lastId = val
    },
    update_loadingStatus(state, val) {
        state.loadingStatus = val
    },
    update_errorMessage(state, val) {
        state.errorMessage = val
    },
    update_selectedItems(state, val) {
        state.selectedItems = val
    },
    update_selectedItem(state, val) {
        state.selectedItem = val
    },
    update_opendialoginfo(state, val) {
        state.opendialoginfo = val
    },
    update_msginfo(state, val) {
        state.msginfo = val
    },
    update_loadingSave(state, val) {
        state.loadingSave = val
    },
    update_errMessage(state, val) {
        state.errMessage = val
    },
    update_alertError(state, val) {
        state.alertError = val
    },
    update_snackbar(state, val) {
        state.snackbar = val
    },
    update_dialogapprove(state, val) {
        state.dialogapprove = val
    },
    update_msgapprove(state, val) {
        state.msgapprove = val
    },
    update_dialogreject(state, val) {
        state.dialogreject = val
    },
    update_msgreject(state, val) {
        state.msgreject = val
    },
    updateApproveDivisionUserID(state, val) {
      state.approveDivisionUserID = val;
    },
    updateApproveDivision(state, val) {
      state.approveDivision = val;
    }
  },
  actions: {
    async listrequestitem(context) {
        context.commit("update_loadingStatus", 1);
  
        try {
          var prm = {
            token: one_token(),
            current_page: context.state.currentPage,
            startDate: context.state.fStartDate,
            endDate: context.state.fEndDate,
            status: context.state.fStatus.value,
            search: context.state.fSearch
          };
  
          let response = await api.listrequestitem(prm);
          if (response.status != "OK") {
            context.commit("update_loadingStatus", 3);
            context.commit("update_errorMessage", response.message);
          } else {
            context.commit("update_loadingStatus", 2);
            context.commit("update_errorMessage", "");
            let data = {
              records: response.data.records,
              total: response.data.totalPage,
            };
  
            context.commit("update_request", data.records);
            context.commit("update_totalPage", data.total);
  
          }
        } catch (e) {
          context.commit("update_loadingStatus", 3);
          context.commit("update_errorMessage", e.message);
        }
    },
    async getDivisionUser(context) {

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

        let response = await api.getDivisionUser(payload);
        if (response.status != "OK") {
          context.commit("update_errorMessage", response.message);
        } else {
          context.commit("update_errorMessage", "");
          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("update_errorMessage", e.message);
      }
    },
    async approveRequest(context, prm) {
      context.commit("update_loadingSave", true);
      try {
        prm.token = one_token();
        let response = await api.approveRequest(prm);
        if (response.status != "OK") {
          context.commit("update_loadingSave", false);
          context.commit("update_errMessage", response.message);
          context.commit("update_alertError", true);
        } else {
          context.commit("update_loadingSave", false);

            context.commit("update_snackbar", {
                state: true,
                type: 'success',
                message: 'Request berhasil diapprove',
            })

          context.commit("update_dialogapprove", false);
          context.commit("update_selectedItems", []);
          context.commit("update_selectedItem", {});
          context.dispatch("listrequestitem");
        }

      } catch (e) {
        context.commit("update_loadingSave", false);
        context.commit("update_errMessage", e.message);
        context.commit("update_alertError", true);
      }
    },
    async rejectedRequest(context, prm) {
      context.commit("update_loadingSave", true);
      try {
        prm.token = one_token();
        let response = await api.rejectedRequest(prm);
        if (response.status != "OK") {
          context.commit("update_loadingSave", false);
          context.commit("update_errMessage", response.message);
          context.commit("update_alertError", true);
        } else {
          context.commit("update_loadingSave", false);

            context.commit("update_snackbar", {
                state: true,
                type: 'success',
                message: 'Request berhasil ditolak',
            })

          context.commit("update_dialogreject", false);
          context.commit("update_selectedItems", []);
          context.commit("update_selectedItem", {});
          context.dispatch("listrequestitem");
        }

      } catch (e) {
        context.commit("update_loadingSave", false);
        context.commit("update_errMessage", e.message);
        context.commit("update_alertError", true);
      }
    },
  },
};

