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

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

export default {
  namespaced: true,
  state: {
    dialog_form_mutasi: false,
    mutasi_date: moment(new Date()).format('YYYY-MM-DD'),
    list_category: [],
    selected_category: {},
    list_branch_asal: [],
    selected_branch_asal: {},
    list_branch_tujuan: [],
    selected_branch_tujuan: {},
    note: '',
    dialog_select_item: false,
    list_ro: [],
    selected_ro: {},
    current_page: 1,
    x_search: '',
    total_pages: 0,
    list_item_receive: [],
    select_item_temp: [],
    list_mutasi_item: [],
    dialog_form_mutasi_edit: false
  },
  mutations: {
    update_dialog_form_mutasi(state, val) {
      state.dialog_form_mutasi = val;
    },
    update_mutasi_date(state, val) {
      state.mutasi_date = val;
    },
    update_list_category(state, val) {
      state.list_category = val;
    },
    update_selected_category(state, val) {
      state.selected_category = val;
    },
    update_list_branch_asal(state, val) {
      state.list_branch_asal = val;
    },
    update_selected_branch_asal(state, val) {
      state.selected_branch_asal = val;
    },
    update_list_branch_tujuan(state, val) {
      state.list_branch_tujuan = val;
    },
    update_selected_branch_tujuan(state, val) {
      state.selected_branch_tujuan = val;
    },
    update_note(state, val) {
        state.note = val;
    },
    update_dialog_select_item(state, val) {
        state.dialog_select_item = val;
    },
    update_current_page(state, val) {
        state.current_page = val;
    },
    update_x_search(state, val) {
        state.x_search = val;
        state.current_page = 1;
    },
    update_total_pages(state, val) {
        state.total_pages = val;
    },
    update_list_item_receive(state, val) {
        state.list_item_receive = val;
    },
    update_select_item_temp(state, val) {
        state.select_item_temp = val;
    },
    update_list_ro(state, val) {
        state.list_ro = val;
    },
    update_selected_ro(state, val) {
        state.selected_ro = val;
    },
    update_list_mutasi_item(state, val) {
        state.list_mutasi_item = val;
    },
    update_dialog_form_mutasi_edit(state, val) {
        state.dialog_form_mutasi_edit = val
    }
  },
  actions: {
    resetForm(context) {
        context.state.mutasi_date = moment(new Date()).format('YYYY-MM-DD')
        context.state.selected_category = {}
        context.state.selected_branch_asal = {}
        context.state.selected_branch_tujuan = {}
        context.state.note = ''
        context.state.list_mutasi_item = []
    },

    async getCategory(context) {
        try {
            let param = {
                token: one_token(),
                search: ''
            }
            const resp = await api.getCategory(param);
            if (resp.status != 'OK') {
                context.commit("mutasi/update_snackbar", {
                    color: 'error',
                    msg: resp.message,
                    isOpen: true
                }, { root: true });
            } else {
                context.commit("update_list_category", resp.data.records);
            }
        } catch (error) {
            context.commit("mutasi/update_snackbar", {
                color: 'error',
                msg: error.message,
                isOpen: true
            }, { root: true });
        }
    },
    
    async getBranchAsal(context) {
        try {
            let param = {
                token: one_token()
            }
            const resp = await api.getBranchAsal(param);
            if (resp.status != 'OK') {
                context.commit("mutasi/update_snackbar", {
                    color: 'error',
                    msg: resp.message,
                    isOpen: true
                }, { root: true });
            } else {
                context.commit("update_list_branch_asal", resp.data.records);
                context.commit("update_selected_branch_asal", resp.data.records[0]);
            }
        } catch (error) {
            context.commit("mutasi/update_snackbar", {
                color: 'error',
                msg: error.message,
                isOpen: true
            }, { root: true });
        }
    },

    async getBranchTujuan(context) {
        try {
            let param = {
                token: one_token()
            }
            const resp = await api.getBranchTujuan(param);
            if (resp.status != 'OK') {
                context.commit("mutasi/update_snackbar", {
                    color: 'error',
                    msg: resp.message,
                    isOpen: true
                }, { root: true });
            } else {
                context.commit("update_list_branch_tujuan", resp.data.records);
            }
        } catch (error) {
            context.commit("mutasi/update_snackbar", {
                color: 'error',
                msg: error.message,
                isOpen: true
            }, { root: true });
        }
    },

    async getReceiveOrderPo(context) {
        try {
            let param = {
                token: one_token(),
                branchAsalCode: context.state.selected_branch_asal.M_BranchCode,
                itemCategoryID: context.state.selected_category.itemCategoryID,
                search: '',
            }
            const resp = await api.getReceiveOrderPo(param);
            if (resp.status != 'OK') {
                context.commit("mutasi/update_snackbar", {
                    color: 'error',
                    msg: resp.message,
                    isOpen: true
                }, { root: true });
            } else {
                context.commit("update_list_ro", resp.data.records);
            }
        } catch (error) {
            context.commit("mutasi/update_snackbar", {
                color: 'error',
                msg: error.message,
                isOpen: true
            }, { root: true });
        }
    },

    async getItemReceive(context) {
        try {
            let param = {
                token: one_token(),
                warhouseID: context.state.selected_ro.ReceiveOrderPoWarehouseID,
                branchAsalCode: context.state.selected_ro.ReceiveOrderPoM_BranchCode,
                receiveOrderPoID: context.state.selected_ro.ReceiveOrderPoID,
                search: '',
                current_page: context.state.current_page
            }
            const resp = await api.getItemReceive(param);
            if (resp.status != 'OK') {
                context.commit("mutasi/update_snackbar", {
                    color: 'error',
                    msg: resp.message,
                    isOpen: true
                }, { root: true });
            } else {
                context.commit("update_list_item_receive", resp.data.records);
                context.commit("update_total_pages", resp.data.total_page);
            }
        } catch (error) {
            context.commit("mutasi/update_snackbar", {
                color: 'error',
                msg: error.message,
                isOpen: true
            }, { root: true });
        }
    },

    async saveMutasi(context) {
        try {
            let param = {
                token: one_token(),
                mutasi_date: context.state.mutasi_date,
                category_id: context.state.selected_category.itemCategoryID,
                branch_asal_id: context.state.selected_branch_asal.M_BranchID,
                branch_tujuan_id: context.state.selected_branch_tujuan.M_BranchID,
                note: context.state.note,
                list_mutasi: context.state.list_mutasi_item
            }
            const resp = await api.saveMutasi(param);
            if (resp.status != 'OK') {
                context.commit("mutasi/update_snackbar", {
                    color: 'error',
                    msg: resp.message,
                    isOpen: true
                }, { root: true });
            } else {
                context.dispatch("resetForm");
                context.dispatch("mutasi/search", {}, {root: true});

                context.commit("mutasi/update_snackbar", {
                    color: 'success',
                    msg: resp.data,
                    isOpen: true
                }, {root: true});

                context.commit("update_dialog_form_mutasi", false);
            }
        } catch (error) {
            context.commit("mutasi/update_snackbar", {
                color: 'error',
                msg: error.message,
                isOpen: true
            }, { root: true });
        }
    },

    async saveEditMutasi(context) {
        try {
            let param = {
                token: one_token(),
                mutasi_request_id: context.rootState.mutasi.selected_list_data.MutasiRequestID,
                mutasi_date: context.state.mutasi_date,
                category_id: context.state.selected_category.itemCategoryID,
                branch_asal_id: context.state.selected_branch_asal.M_BranchID,
                branch_tujuan_id: context.state.selected_branch_tujuan.M_BranchID,
                note: context.state.note,
                list_mutasi: context.state.list_mutasi_item
            }
            const resp = await api.saveEditMutasi(param);
            if (resp.status != 'OK') {
                context.commit("mutasi/update_snackbar", {
                    color: 'error',
                    msg: resp.message,
                    isOpen: true
                }, { root: true });
            } else {
                context.dispatch("resetForm");
                context.dispatch("mutasi/search", {}, {root: true});

                context.commit("mutasi/update_snackbar", {
                    color: 'success',
                    msg: resp.data,
                    isOpen: true
                }, {root: true});

                context.commit("update_dialog_form_mutasi_edit", false);
            }
        } catch (error) {
            context.commit("mutasi/update_snackbar", {
                color: 'error',
                msg: error.message,
                isOpen: true
            }, { root: true });
        }
    },

  },
};