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

export default {
    namespaced: true,
    state: {
        form_kontrak: {
            nama_kontrak: '',
            tanggal_kontrak: moment(new Date()).format('YYYY-MM-DD'),
            tanggalAwal_kontrak: moment(new Date()).format('YYYY-MM-DD'),
            tanggalAkhir_kontrak: moment(new Date()).format('YYYY-MM-DD'),
            durasi_cicilan: 1,
            jumlah_cicilan: 1,
            tanggalBayar_cicilan: 1,
            besarNilai_cicilan: 0,
            type_downpayment: 'percent',
            besarNilai_downpayment: 0
        }
    },
    mutations: {
        update_form_kontrak(state, val) {
            state.form_kontrak = val
        }
    },
    actions: {
        async saveOrderAsset(context, params) {
            try {
                context.commit("common/update_loading_process", true, { root: true });
                params.token = one_token();
                const resp = await api.createPoAsset(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }
                const snac = { color: 'success', msg: resp.data, state: true };
                context.commit("common/update_snackbar", snac, { root: true });

                context.commit("common/update_loading_process", false, { root: true });
                context.commit("orderAsset/update_dialog_create_order", false, { root: true });
                context.dispatch('common/getListingPOAsset', {}, { root: true });
            } catch (error) {
                context.commit("common/update_loading_process", false, { root: true });
                const snac = { color: 'error', msg: error.message, state: true };
                context.commit("common/update_snackbar", snac, { root: true });
            }
        },
        async editOrderAsset(context, params) {
            try {
                context.commit("common/update_loading_process", true, { root: true });
                params.token = one_token();
                const resp = await api.editPoAsset(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }
                const snac = { color: 'success', msg: resp.data, state: true };
                context.commit("common/update_snackbar", snac, { root: true });

                context.commit("common/update_loading_process", false, { root: true });
                context.commit("orderAsset/update_dialog_create_order", false, { root: true });
                context.dispatch('common/getListingPOAsset', {}, { root: true });
            } catch (error) {
                context.commit("common/update_loading_process", false, { root: true });
                const snac = { color: 'error', msg: error.message, state: true };
                context.commit("common/update_snackbar", snac, { root: true });
            }
        },
        async sendRequestPO(context, poID) {
            try {
                context.commit("common/update_loading_process", true, { root: true });
                let params = {
                    token: one_token(),
                    approvelevel: "0",
                    poID: poID,
                }
                const resp = await api.updateApprovalPO(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }
                const snac = { color: 'success', msg: resp.data, state: true };
                context.commit("common/update_snackbar", snac, { root: true });

                context.commit("common/update_loading_process", false, { root: true });
                context.commit("orderAsset/update_dialog_create_order", false, { root: true });
                context.dispatch('common/getListingPOAsset', {}, { root: true });
            } catch (error) {
                context.commit("common/update_loading_process", false, { root: true });
                const snac = { color: 'error', msg: error.message, state: true };
                context.commit("common/update_snackbar", snac, { root: true });
            }
        },
        async getDetailDataPO(context, poID) {
            try {
                let params = {
                    token: one_token(),
                    poID: poID,
                }
                const resp = await api.getDataKontrakPoAset(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                let formOrder = {
                    podate: resp.data.podate,
                    supplierID: resp.data.supplierID,
                    reference: resp.data.reference,
                    branchID: resp.data.branchID,
                    gudangID: resp.data.gudangID,
                    typepajak: resp.data.typepajak,
                    valuepajak: parseFloat(resp.data.valuepajak),
                    typediskon: resp.data.typediskon,
                    valuediskon: parseFloat(resp.data.valuediskon),
                    catatan: resp.data.catatan
                };
                let formKontrak = {
                    nama_kontrak: resp.data.contractName,
                    tanggal_kontrak: resp.data.contractDate,
                    tanggalAwal_kontrak: resp.data.contractStart,
                    tanggalAkhir_kontrak: resp.data.contractEnd,
                    durasi_cicilan: resp.data.contractDuration,
                    jumlah_cicilan: resp.data.installmentNumber,
                    tanggalBayar_cicilan: resp.data.installmentDate,
                    besarNilai_cicilan: parseFloat(resp.data.installmentPayAmount),
                    type_downpayment: resp.data.installmentDownPaymentType,
                    besarNilai_downpayment: parseFloat(resp.data.installmentDownPayment),
                };
                let summary = resp.data.summary;
                let selected_request = resp.data.detail;
                selected_request.forEach(item => {
                    item.SupplierPrice = parseFloat(item.SupplierPrice)
                    item.DiskonAmount = parseFloat(item.DiskonAmount)
                });

                context.dispatch('orderAsset/listingGudang', resp.data.branchID, { root: true });

                context.commit('update_form_kontrak', formKontrak);
                context.commit('orderAsset/update_form_order_asset', formOrder, { root: true });
                context.commit('orderAsset/update_selected_request_asset', selected_request, { root: true });
                context.commit('orderAsset/update_summary_order', summary, { root: true });
                context.commit('common/update_selected_orderaset', resp.data, { root: true });

                context.dispatch('orderAsset/recalculateOrderSummary', {}, { root: true });
                context.commit('orderAsset/update_dialog_create_order', true, { root: true });
            } catch (error) {
                const snac = { color: 'error', msg: error.message, state: true };
                context.commit("common/update_snackbar", snac, { root: true });
            }
        }
    }
}
