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

import * as api from "../api/api-manager.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: 0, title: "All" },
      { value: 2, title: "Pending" },
      { value: 3, title: "Approved" },
      { value: 4, title: "Rejected" },
      { value: 5, title: "Paid" },
      { value: 6, title: "Completed" },
    ],

    menuStartDate: false,
    menuEndDate: 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: 0,
    fSearch: "",

    xNote: "",
    xAmount: 0,
    xPRID: 0,
    xPRDID: 0,
    xStatus: "",
    xDescriptionDetail: "",

    dialogDetail: false,
    errMessage: "",
    succesMessage: "",
    alertError: false,
    alertSuccess: false,
    errors: [],
  },
  mutations: {
    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;
    },

    updateMenuStartDate(state, val) {
      state.menuStartDate = val;
    },
    updateMenuEndDate(state, val) {
      state.menuEndDate = 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;
    },

    updateXNote(state, val) {
      state.xNote = val;
    },
    updateXAmount(state, val) {
      state.xAmount = val;
    },
    updateXPRID(state, val) {
      state.xPRID = val;
    },
    updateXPRDID(state, val) {
      state.xPRDID = val;
    },
    updateXStatus(state, val) {
      state.xStatus = val;
    },
    updateXDescriptionDetail(state, val) {
      state.xDescriptionDetail = val;
    },

    updateDialogDetail(state, val) {
      state.dialogDetail = 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;
    },
  },
  actions: {
    async search(context) {
      context.commit("updateMainTableLoadingStatus", 1);

      try {
        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);
        }
      } 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.PurchaseRequestDirectID,
        };

        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 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 = `Request [${payload.PRDDescription}] telah diperbarui`;
            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 rejectRequest(context, payload) {
      try {
        payload.token = one_token();

        let response = await api.rejectRequest(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);
            var msg = `Purchase Request [${payload.PRNumber}] sudah ditolak`;
            context.commit("updateSuccessMessage", msg);
            context.commit("updateSelectedMainTable", {});
            context.commit("updateDetailTable", []);
            context.commit("updateSelectedDetailTable", {});
            context.dispatch("search");
          } 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);
            var msg = `Request [${payload.PRDDescription}] 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 approveRequest(context, payload) {
      try {
        payload.token = one_token();

        let response = await api.approveRequest(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);
            var msg = `Purchase Request [${payload.PRNumber}] telah diapprove`;
            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);
      }
    },
  },
};
