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

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

export default {
  namespaced: true,
  state: {
    dialog_voucher: false,
    status_options: [ 
      { val: 0, text: 'All' }, { val: 1, text: 'Draft' },
      { val: 2, text: 'Paid' }, { val: 3, text: 'Realitation' }
    ],
    selected_status: { val: 1, text: 'Draft' },
    menuStartDate: false,
    menuEndDate: false,

    dStartDate: new Date().toISOString().substring(0, 10),
    dEndDate: new Date().toISOString().substring(0, 10),
    xStartDate: moment(new Date()).format('YYYY-MM-DD'),
    xEndDate: moment(new Date()).format('YYYY-MM-DD'),

    // listing
    currentPage: 1,
    search_status: 0,
    search_error_message: "",
    voucherList: [],
    selected_voucher: {},
    total_page_voucher: 0,
    xsearch: "",
    act: "new",

    // form
    load_status_branch: 0,
    error_message: "",
    alert_error: false,
    branchList: [],
    selected_branch: {},
    search_branch: "",
    accountDanaList: [],
    selected_accountDana: {},
    search_accountDana: "",
    load_status_accDana: 0,
    accountBiayaList: [],
    selected_accountBiaya: {},
    search_accountBiaya: "",
    load_status_accBiaya: 0,
    accountTempList: [],
    selected_accountTemp: {},
    search_accountTemp: "",
    load_status_accTemp: 0,
    load_status: 0,
    purchaseList: [],
    selected_purchase: [],
    resume_total: {"totalRealitation": 0},
    errors: [],
    alert_error: false,
    alert_success: false,
    success_message: "",
    totalApproved: 0,

    // detail
    search_status_detail: 0,
    details: [],

    // add coa per item
    dialog_account_item: false,
    list_voucher_detail: [],
    list_accitem: []
  },
  mutations: {
    update_dialog_voucher(state, val) {
      state.dialog_voucher = val;
    },
    update_status_options(state, val) {
      state.status_options = val;
    },
    update_selected_status(state, val) {
      state.selected_status = val;
    },
    update_menuStartDate(state, val) {
      state.menuStartDate = val;
    },
    update_menuEndDate(state, val) {
      state.menuEndDate = val;
    },
    update_dStartDate(state, val) {
      state.dStartDate = val;
    },
    update_dEndDate(state, val) {
      state.dEndDate = val;
    },
    update_xStartDate(state, val) {
      state.xStartDate = val;
    },
    update_xEndDate(state, val) {
      state.xEndDate = val;
    },
    update_currentPage(state, val) {
      state.currentPage = val;
    },
    update_search_status(state, val) {
      state.search_status = val;
    },
    update_search_error_message(state, val) {
      state.search_error_message = val;
    },
    update_voucherList(state, val) {
      state.voucherList = val;
    },
    update_selected_voucher(state, val) {
      state.selected_voucher = val;
    },
    update_total_page_voucher(state, val) {
      state.total_page_voucher = val;
    },
    update_xsearch(state, val) {
      state.xsearch = val;
      state.currentPage = 1;
    },
    update_act(state, val) {
      state.act = val
    },
    update_load_status_branch(state, val) {
      state.load_status_branch = val;
    },
    update_error_message(state, val) {
      state.error_message = val;
    },
    update_alert_error(state, val) {
      state.alert_error = val;
    },
    update_branchList(state, val) {
      state.branchList = val;
    },
    update_selected_branch(state, val) {
      state.selected_branch = val;
    },
    update_search_branch(state, val) {
      state.search_branch = val;
    },
    update_accountDanaList(state, val) {
      state.accountDanaList = val;
    },
    update_selected_accountDana(state, val) {
      state.selected_accountDana = val;
    },
    update_search_accountDana(state, val) {
      state.search_accountDana = val;
    },
    update_load_status_accDana(state, val) {
      state.load_status_accDana = val;
    },
    update_accountBiayaList(state, val) {
      state.accountBiayaList = val;
    },
    update_selected_accountBiaya(state, val) {
      state.selected_accountBiaya = val;
    },
    update_search_accountBiaya(state, val) {
      state.search_accountBiaya = val;
    },
    update_load_status_accBiaya(state, val) {
      state.load_status_accBiaya = val;
    },
    update_accountTempList(state, val) {
      state.accountTempList = val;
    },
    update_selected_accountTemp(state, val) {
      state.selected_accountTemp = val;
    },
    update_search_accountTemp(state, val) {
      state.search_accountTemp = val;
    },
    update_load_status_accTemp(state, val) {
      state.load_status_accTemp = val;
    },
    update_load_status(state, val) {
      state.load_status = val;
    },
    update_purchaseList(state, val) {
      state.purchaseList = val;
    },
    update_selected_purchase(state, val) {
      state.selected_purchase = val;
    },
    update_resume_total(state, val) {
      state.resume_total = val;
    },
    update_errors(state, val) {
      state.errors = val;
    },
    update_alert_error(state, val) {
      state.alert_error = val;
    },
    update_alert_success(state, val) {
      state.alert_success = val;
    },
    update_success_message(state, val) {
      state.success_message = val;
    },
    update_totalApproved(state, val) {
      state.totalApproved = val
    },
    update_search_status_detail(state, val) { 
      state.search_status_detail = val; 
    },
    update_details(state, val) {
      state.details = val
    },
    update_dialog_account_item(state, val) {
      state.dialog_account_item = val
    },
    update_list_voucher_detail(state, val) {
      state.list_voucher_detail = val
    },
    update_list_accitem(state, val) {
      state.list_accitem = val
    }
  },
  actions: {
    async search(context) {
      context.commit("update_search_status", 1);

      try {
        var prm = {
          token: one_token(),
          currentPage: context.state.currentPage,
          status: context.state.selected_status.text,
          search: context.state.xsearch,
          startdate: context.state.xStartDate,
          enddate: context.state.xEndDate,
          lastId: -1,
        };

        let resp = await api.search(prm);
        if (resp.status != "OK") {
          context.commit("update_search_status", 3);
          context.commit("update_search_error_message", resp.message);
        } else {
          context.commit("update_search_status", 2);
          context.commit("update_search_error_message", "");
          let data = {
            records: resp.data.records,
            total: resp.data.total,
          };

          context.commit("update_voucherList", data.records);
          context.commit("update_total_page_voucher", data.total);
        }
      } catch (e) {
        context.commit("update_search_status", 3);
        context.commit("update_search_error_message", e.message);
      }
    },

    async searchBranch(context, prm) {
        context.commit("update_load_status_branch", 1)
        try {
            let resp = await api.searchBranch(one_token(), prm)
            if (resp.status != "OK") {
                context.commit("update_load_status_branch", 3)
                context.commit("update_error_message", resp.message);
                context.commit("update_alert_error", true);
            } else {
                context.commit("update_load_status_branch", 2)
                context.commit("update_error_message", "");
                let data = {
                    records: resp.data.records
                }

                context.commit("update_branchList", data.records)
            }
        } catch (e) {
            context.commit("update_load_status_branch", 3)
            context.commit("update_error_message", e.message);
            context.commit("update_alert_error", true);
        }
    },

    async searchAccDana(context, prm) {
        context.commit("update_load_status_accDana", 1)
        try {
            let resp = await api.searchAccDana(one_token(), prm)
            if (resp.status != "OK") {
                context.commit("update_load_status_accDana", 3)
                context.commit("update_error_message", resp.message);
                context.commit("update_alert_error", true);
            } else {
                context.commit("update_load_status_accDana", 2)
                context.commit("update_error_message", "");
                let data = {
                    records: resp.data.records
                }

                context.commit("update_accountDanaList", data.records)
            }
        } catch (e) {
            context.commit("update_load_status_accDana", 3)
            context.commit("update_error_message", e.message);
            context.commit("update_alert_error", true);
        }
    },

    async searchAccBiaya(context, prm) {
        context.commit("update_load_status_accBiaya", 1)
        try {
            let resp = await api.searchAccBiaya(one_token(), prm)
            if (resp.status != "OK") {
                context.commit("update_load_status_accBiaya", 3)
                context.commit("update_error_message", resp.message);
                context.commit("update_alert_error", true);
            } else {
                context.commit("update_load_status_accBiaya", 2)
                context.commit("update_error_message", "");
                let data = {
                    records: resp.data.records
                }

                context.commit("update_accountBiayaList", data.records)
            }
        } catch (e) {
            context.commit("update_load_status_accBiaya", 3)
            context.commit("update_error_message", e.message);
            context.commit("update_alert_error", true);
        }
    },

    async searchAccTemp(context, prm) {
        context.commit("update_load_status_accTemp", 1)
        try {
            let resp = await api.searchAccTemp(one_token(), prm)
            if (resp.status != "OK") {
                context.commit("update_load_status_accTemp", 3)
                context.commit("update_error_message", resp.message);
                context.commit("update_alert_error", true);
            } else {
                context.commit("update_load_status_accTemp", 2)
                context.commit("update_error_message", "");
                let data = {
                    records: resp.data.records
                }

                context.commit("update_accountTempList", data.records)
            }
        } catch (e) {
            context.commit("update_load_status_accTemp", 3)
            context.commit("update_error_message", e.message);
            context.commit("update_alert_error", true);
        }
    },

    async getPurchase(context, prm) {
        context.commit("update_load_status", 1)
        try {
            let resp = await api.getPurchase(one_token(), prm)
            if (resp.status != "OK") {
                context.commit("update_load_status", 3)
                context.commit("update_error_message", resp.message);
                context.commit("update_alert_error", true);
            } else {
                context.commit("update_load_status", 2)
                context.commit("update_error_message", "");
                let data = {
                    records: resp.data.records
                }

                context.commit("update_purchaseList", data.records)
            }
        } catch (e) {
            context.commit("update_load_status", 3)
            context.commit("update_error_message", e.message);
            context.commit("update_alert_error", true);
        }
    },

    async searchdetail(context, prm) {
        context.commit("update_search_status_detail", 1)
        try {
            let resp = await api.searchdetail(one_token(), prm)
            if (resp.status != "OK") {
                context.commit("update_search_status_detail", 3)
                context.commit("update_search_error_message", resp.message);
            } else {
                context.commit("update_search_status_detail", 2)
                context.commit("update_search_error_message", "");
                let data = {
                    records: resp.data.records
                }

                context.commit("update_details", data.records)
            }
        } catch (e) {
            context.commit("update_search_status_detail", 3)
            context.commit("update_search_error_message", e.message);
        }
    },

    async save(context, prm) {
        context.commit("update_search_status_detail", 1)
        try {

            let resp = await api.saveDetail(one_token(), prm)
            if (resp.status != "OK") {
                context.commit("update_search_status_detail", 3)
                context.commit("update_error_message", resp.message);
                context.commit("update_alert_error", true);
            } else {
                context.commit("update_search_status_detail", 2)
                context.commit("update_error_message", resp.message);

                let data = {
                    records: resp.data.records
                }

                context.commit("update_alert_success", true);
                context.commit("update_dialog_voucher", false);
                var msg = `Sukses menyimpan data baru dengan kode ${data.records[0].PaymentVoucherNumber}`;
                context.commit("update_success_message", msg);
                context.commit("update_selected_voucher", data.records[0]);
                context.dispatch("search");
                context.dispatch("searchdetail", data.records[0].PaymentVoucherID);
            }
        } catch (e) {
            context.commit("update_search_status_detail", 3)
            context.commit("update_error_message", e.message);
            context.commit("update_alert_error", true);
        }
    },

    async getTotalApproved(context, prm) {
        context.commit("update_search_status", 1)
        try {
            let resp = await api.getTotalApproved(one_token(), prm)
            if (resp.status != "OK") {
                context.commit("update_search_status", 3)
                context.commit("update_error_message", resp.message);
            } else {
                context.commit("update_search_status", 2)
                context.commit("update_error_message", "");
                let data = {
                    total: resp.data.total
                }

                context.commit("update_totalApproved", data.total)
            }
        } catch (e) {
            context.commit("update_search_status", 3)
            context.commit("update_error_message", e.message);
        }
    },

    async delete(context, prm) {
        context.commit("update_search_status", 1)
        try {

            let resp = await api.deleteVoucher(one_token(), prm)
            if (resp.status != "OK") {
                context.commit("update_search_status", 3)
                context.commit("update_error_message", resp.message);
                context.commit("update_alert_error", true);
            } else {
                context.commit("update_search_status", 2)
                context.commit("update_error_message", resp.message);

                let data = {
                    records: resp.data.records
                }

                context.commit("update_alert_success", true);
                context.commit("update_dialog_voucher", false);
                var msg = `Sukses menghapus data dengan kode ` + prm.PVNumber;
                context.commit("update_success_message", msg);
                // context.commit("update_selected_voucher", data.records[0]);
                context.dispatch("search");
                context.dispatch("searchdetail", "");
            }
        } catch (e) {
            context.commit("update_search_status", 3)
            context.commit("update_error_message", e.message);
            context.commit("update_alert_error", true);
        }
    },

    async update(context, prm) {
        context.commit("update_search_status_detail", 1)
        try {

            let resp = await api.editVoucher(one_token(), prm)
            if (resp.status != "OK") {
                context.commit("update_search_status_detail", 3)
                context.commit("update_error_message", resp.message);
                context.commit("update_alert_error", true);
            } else {
                context.commit("update_search_status_detail", 2)
                context.commit("update_error_message", resp.message);

                let data = {
                    records: resp.data.records
                }

                context.commit("update_alert_success", true);
                context.commit("update_dialog_voucher", false);
                var msg = `Sukses edit data dengan kode ${data.records[0].PaymentVoucherNumber}`;
                context.commit("update_success_message", msg);
                context.commit("update_selected_voucher", data.records[0]);
                context.dispatch("search");
                context.dispatch("searchdetail", data.records[0].PaymentVoucherID);
            }
        } catch (e) {
            context.commit("update_search_status_detail", 3)
            context.commit("update_error_message", e.message);
            context.commit("update_alert_error", true);
        }
    },

    async getPurchaseUpdateNew(context, prm) {
        context.commit("update_load_status", 1)
        try {
            let resp = await api.getPurchaseUpdateNew(one_token(), prm)
            if (resp.status != "OK") {
                context.commit("update_load_status", 3)
                context.commit("update_error_message", resp.message);
                context.commit("update_alert_error", true);
            } else {
                context.commit("update_load_status", 2)
                context.commit("update_error_message", "");
                let data = {
                    records: resp.data.records
                }

                context.commit("update_purchaseList", data.records)
            }
        } catch (e) {
            context.commit("update_load_status", 3)
            context.commit("update_error_message", e.message);
            context.commit("update_alert_error", true);
        }
    },

    async getVoucherDetailItem(context, param) {
      try {
        param.token = one_token()
        let resp = await api.getVoucherDetailItem(param)
        if (resp.status != 'OK') {
          context.commit("update_error_message", resp.message)
          context.commit("update_alert_error", true)
        } else {
          context.commit("update_list_voucher_detail", resp.data)
        }
      } catch (error) {
        context.commit("update_error_message", error.message)
        context.commit("update_alert_error", true)
      }
    },

    async searchAccountItem(context, prm) {
        try {
            prm.token = one_token()
            let resp = await api.searchAccBiaya(prm)
            if (resp.status != "OK") {
                context.commit("update_error_message", resp.message);
                context.commit("update_alert_error", true);
            } else {
                context.commit("update_error_message", "");
                context.commit("update_list_accitem", resp.data.records);
            }
        } catch (e) {
            context.commit("update_error_message", e.message);
            context.commit("update_alert_error", true);
        }
    },

    async saveAccountItem(context, headerID) {
        try {
            let prm = {
              token: one_token(),
              PRDID: headerID,
              detail: context.state.list_voucher_detail
            }
            
            let resp = await api.saveAccountItem(prm)
            if (resp.status != "OK") {
                context.commit("update_error_message", resp.message);
                context.commit("update_alert_error", true);
            } else {
                context.commit("update_alert_success", true);  
                context.commit("update_success_message", "Success");

                let ID = context.state.selected_voucher.PaymentVoucherID
                context.dispatch("searchdetail", ID)

                context.commit("update_list_voucher_detail", []);
                context.commit("update_dialog_account_item", false);
            }
        } catch (e) {
            context.commit("update_error_message", e.message);
            context.commit("update_alert_error", true);
        }
    },

    async moveTokasir(context) {
      try {
        let prm = {
          token: one_token(),
          ID: context.state.selected_voucher.PaymentVoucherID
        }
        let resp = await api.moveToKasir(prm)
        if (resp.status != 'OK') {
          context.commit("update_error_message", resp.message);
          context.commit("update_alert_error", true);
        } else {
          
          let ID = context.state.selected_voucher.PaymentVoucherID
          context.dispatch("searchdetail", ID)

          context.commit("update_alert_success", true);
          context.commit("update_success_message", "Success");
        }
      } catch (error) {
        context.commit("update_error_message", error.message);
        context.commit("update_alert_error", true);
      }
    }
  }
};

