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

import mutasi from "../../acc-one-mutasi-request-approved/modules/mutasi.js";
import * as api from "../api/api.js";

export default {
  namespaced: true,
  state: {
    alert_error: false,
    err_message: "",
    menu_start_date: false,
    menu_end_date: false,
    d_start_date: new Date().toISOString().substring(0, 10),
    d_end_date: new Date().toISOString().substring(0, 10),
    f_start_date: moment(new Date()).format("YYYY-MM-DD"),
    f_end_date: moment(new Date()).format("YYYY-MM-DD"),
    f_status: '',
    status_options: [
        { text: 'All', value: '' },
        { text: 'sent', value: 'sent' },
        { text: 'Received', value: 'Received' },
    ],
    f_search : '',
    list_data: [],
    selected_list_data: {},
    isLoading: 0,
    current_page: 1,
    total_pages: 0,
    snackbar: {
        color: 'success',
        msg: '',
        isOpen: false
    },
    dialog_delete: false,
    approve_level: 0,
    dialog_reject: false,
    note_reject: '',
    dialog_form_mutasi_receive: false,
    dialog_form_mutasi_view: false,
    dialog_barcode: false,
    indeterminatex: false,
    bar_chx_all: false,
    barcodes: [],
    urlQrCode: '',
    selected_barcode: []
  },
  mutations: {
    update_alert_error(state, val) {
      state.alert_error = val;
    },
    update_err_message(state, val) {
      state.err_message = val;
    },
    update_menu_start_date(state, val) {
      state.menu_start_date = val;
    },
    update_menu_end_date(state, val) {
      state.menu_end_date = val;
    },
    update_d_start_date(state, val) {
        state.d_start_date = val;
    },
    update_d_end_date(state, val) {
        state.d_end_date = val;
    },
    update_f_start_date(state, val) {
        state.f_start_date = val;
    },
    update_f_end_date(state, val) {
        state.f_end_date = val;
    },
    update_f_status(state, val) {
        state.f_status = val;
    },
    update_status_options(state, val) {
        state.status_options = val;
    },
    update_f_search(state, val) {
        state.f_search = val;
        state.current_page = 1
    },
    update_list_data(state, val) {
        state.list_data = val;
    },
    update_selected_list_data(state, val) {
        state.selected_list_data = val;
    },
    update_isLoading(state, val) {
        state.isLoading = val;
    },
    update_current_page(state, val) {
        state.current_page = val;
    },
    update_total_pages(state, val) {
        state.total_pages = val;
    },
    update_snackbar(state, data) {
        state.snackbar = data
    },
    update_dialog_delete(state, val) {
        state.dialog_delete = val
    },
    update_approve_level(state, data) {
        state.approve_level = data
    },
    update_dialog_reject(state, val) {
        state.dialog_reject = val
    },
    update_note_reject(state, val) {
        state.note_reject = val
    },
    update_dialog_form_mutasi_receive(state, val) {
        state.dialog_form_mutasi_receive = val
    },
    update_dialog_form_mutasi_view(state, val) {
        state.dialog_form_mutasi_view = val
    },
    update_dialog_barcode(state, val) {
        state.dialog_barcode = val
    },
    update_indeterminatex(state, val) {
        state.indeterminatex = val
    },
    update_bar_chx_all(state, val) {
        state.bar_chx_all = val
    },
    update_selected_barcode(state, val) {
        state.selected_barcode = val
    },
    update_barcodes(state, val) {
        state.barcodes = val
    },
    update_urlQrCode(state, val) {
        state.urlQrCode = val
    }
  },
  actions: {
    async search(context) {
        context.commit("update_isLoading", 1)
        try {
            let param = {
                token: one_token(),
                search: context.state.f_search,
                start_date: context.state.f_start_date,
                end_date: context.state.f_end_date,
                current_page: context.state.current_page,
                status: context.state.f_status
            }
            const resp = await api.search(param);
            if (resp.status != 'OK') {
                context.commit("update_isLoading", 3)
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: resp.message,
                    isOpen: true
                }, { root: true });
            } else {
                context.commit("update_isLoading", 2)
                context.commit("update_list_data", resp.data.records);
                context.commit("update_total_pages", resp.data.total_page);
            }
        } catch (error) {
            context.commit("update_isLoading", 3)
            context.commit("update_snackbar", {
                color: 'error',
                msg: error.message,
                isOpen: true
            }, { root: true });
        }
    },

    async getItemDetail(context) {
        try {
            let param = {
                token: one_token(),
                mutasiHandoverID: context.state.selected_list_data.MutasiHandoverID
            }
            const resp = await api.getItemDetail(param);
            if (resp.status != 'OK') {
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: resp.message,
                    isOpen: true
                });
            } else {
                context.commit("form/update_mutasi_date", context.state.selected_list_data.MutasiHandoverDate, { root: true });

                let category = {
                    itemCategoryID: context.state.selected_list_data.itemCategoryID,
                    itemCategoryName: context.state.selected_list_data.itemCategoryName
                }
                context.commit("form/update_selected_category", category, { root: true });

                let branch_asal = {
                    M_BranchID: context.state.selected_list_data.branch_asal_id,
                    M_BranchName: context.state.selected_list_data.branch_asal_name,
                    M_BranchCode: context.state.selected_list_data.branch_asal_code,
                }
                context.commit("form/update_selected_branch_asal", branch_asal, { root: true });

                let branch_tujuan = {
                    M_BranchID: context.state.selected_list_data.branch_tujuan_id,
                    M_BranchName: context.state.selected_list_data.branch_tujuan_name,
                    M_BranchCode: context.state.selected_list_data.branch_tujuan_code,
                }
                context.commit("form/update_selected_branch_tujuan", branch_tujuan, { root: true });

                if (context.state.selected_list_data.WarehouseID != null && context.state.selected_list_data.WarehouseID != '') {
                    let warehouse = {
                        WarehouseID: context.state.selected_list_data.WarehouseID,
                        WarehouseName:  context.state.selected_list_data.WarehouseName
                    }
                    context.commit("form/update_selected_warehouse", warehouse, { root: true });
                }
                
                if (context.state.selected_list_data.M_RuanganID != null && context.state.selected_list_data.M_RuanganID != '') {
                    let ruangan = {
                        M_RuanganID: context.state.selected_list_data.M_RuanganID,
                        M_RuanganName:  context.state.selected_list_data.M_RuanganName
                    }
                    context.commit("form/update_selected_ruangan", ruangan, { root: true });
                }
                
                context.commit("form/update_note", context.state.selected_list_data.MutasiHandoverNote, { root: true });
                context.commit("form/update_list_mutasi_item", resp.data.records, { root: true });

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

    async receive(context) {
        try {
            let param = {
                token: one_token(),
                mutasi_handover_id: context.state.selected_list_data.MutasiHandoverID,
                warehouse_id: context.rootState.form.selected_warehouse.WarehouseID,
                ruangan_id: context.rootState.form.selected_ruangan.M_RuanganID
            }
            const resp = await api.receive(param);
            if (resp.status != 'OK') {
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: resp.message,
                    isOpen: true
                });
            } else {
                context.dispatch("search");

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

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

    async getBarcodes(context) {
        try {
            let param = {
                token: one_token(),
                mutasi_handover_id: context.state.selected_list_data.MutasiHandoverID
            }
            const resp = await api.getBarcodes(param);
            if (resp.status != 'OK') {
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: resp.message,
                    isOpen: true
                });
            } else {
                context.commit("update_barcodes", resp.data);
            }
        } catch (error) {
            context.commit("update_snackbar", {
                color: 'error',
                msg: error.message,
                isOpen: true
            });
        }
    },

  },
};