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

export default {
    namespaced: true,
    state: {
        dialog_receive_aset: false,
        readonly_mode: false,
        form_receive: {
            date: moment(new Date()).format('YYYY-MM-DD'),
            supplier: "",
            reference: "",
            ponumber: "",
            branchID: "",
            gudangID: "",
            shipping: 0,
            shippingIsPaid: "N",
            catatan: ""
        },
        list_cabang: [],
        list_gudang: [],
        list_supplier: [],
        order_received: [],

        dialog_pick_contract: false,
        list_item_order: [],

        dialog_inspeksi: false,
        list_staff: [],
        selected_item_received: {},
        form_inspeksi: {
            qty_po: 0,
            qty_ro: 0,
            date_po: moment(new Date()).format('YYYY-MM-DD'),
            date_ro: moment(new Date()).format('YYYY-MM-DD'),
            condt_kemasan: '',
            catatan_kemasan: '',
            condt_pengiriman: '',
            catatan_kirim: '',
            summary: '',
            catatan: '',
            pemeriksa: '',
            pengirim: ''
        }
    },
    mutations: {
        update_dialog_receive_aset(state, val) {
            state.dialog_receive_aset = val
        },
        update_readonly_mode(state, val) {
            state.readonly_mode = val
        },
        update_form_receive(state, val) {
            state.form_receive = val
        },
        update_list_cabang(state, val) {
            state.list_cabang = val
        },
        update_list_gudang(state, val) {
            state.list_gudang = val
        },
        update_list_supplier(state, val) {
            state.list_supplier = val
        },
        update_order_received(state, val) {
            state.order_received = val
        },
        update_dialog_pick_contract(state, val) {
            state.dialog_pick_contract = val
        },
        update_list_item_order(state, val) {
            state.list_item_order = val
        },
        update_dialog_inspeksi(state, val) {
            state.dialog_inspeksi = val
        },
        update_list_staff(state, val) {
            state.list_staff = val
        },
        update_selected_item_received(state, val) {
            state.selected_item_received = val
        },
        update_form_inspeksi(state, val) {
            state.form_inspeksi = val
        }
    },
    actions: {
        async listingCabang(context) {
            try {
                let param = { token: one_token() }

                const resp = await api.getListCabang(param);
                if (resp.status != "OK") {
                    throw new Error(resp.message);
                }

                context.commit('update_list_cabang', resp.data);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("roasset/update_snackbar", snac, { root: true });
            }
        },
        async listingGudang(context, branchID) {
            try {
                let param = {
                    token: one_token(),
                    M_BranchID: branchID
                }

                const resp = await api.getListGudang(param);
                if (resp.status != "OK") {
                    throw new Error(resp.message);
                }

                context.commit('update_list_gudang', resp.data);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("roasset/update_snackbar", snac, { root: true });
            }
        },
        async listingSupplier(context) {
            try {
                const params = { token: one_token() };
                const resp = await api.getListSupplier(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                context.commit('update_list_supplier', resp.data);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("roasset/update_snackbar", snac, { root: true });
            }
        },
        async listingStaff(context) {
            try {
                const params = { token: one_token() };
                const resp = await api.getListStaff(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                context.commit('update_list_staff', resp.data);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("roasset/update_snackbar", snac, { root: true });
            }
        },
        async searchOrderAsset(context, keyword) {
            try {
                const form = context.state.form_receive;
                const params = {
                    token: one_token(),
                    search: keyword,
                    supplierID: form.supplier,
                    gudangID: form.gudangID
                };
                const resp = await api.searchOrderAsset(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                context.commit("update_list_item_order", resp.data)
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("roasset/update_snackbar", snac, { root: true });
            }
        },
        async createReceiveOrder(context) {
            try {
                context.commit("roasset/update_loading_process", true, { root: true });
                const detail = context.state.order_received;
                const params = {
                    ...context.state.form_receive,
                    detail: detail,
                    token: one_token()
                };

                const resp = await api.createReceiveOrder(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                const snac = { color: 'success', msg: resp.data, state: true };
                context.commit("roasset/update_snackbar", snac, { root: true });

                context.dispatch("roasset/lookupOrderAssetReceived", {}, {root: true});
                context.commit("roasset/update_loading_process", false, { root: true });
                context.state.dialog_receive_aset = false;
            } catch (e) {
                context.commit("roasset/update_loading_process", false, { root: true });
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("roasset/update_snackbar", snac, { root: true });
            }
        },
        async editReceiveOrder(context) {
            try {
                context.commit("roasset/update_loading_process", true, { root: true });
                const detail = context.state.order_received;
                const params = {
                    ...context.state.form_receive,
                    detail: detail,
                    token: one_token()
                };

                const resp = await api.editReceiveOrder(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                const snac = { color: 'success', msg: resp.data, state: true };
                context.commit("roasset/update_snackbar", snac, { root: true });

                context.dispatch("roasset/lookupOrderAssetReceived", {}, {root: true});
                context.commit("roasset/update_loading_process", false, { root: true });
                context.state.dialog_receive_aset = false;
            } catch (e) {
                context.commit("roasset/update_loading_process", false, { root: true });
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("roasset/update_snackbar", snac, { root: true });
            }
        }
    }
}
