import * as api from "../api/api.js";
export default {
    namespaced: true,
    state: {
        /* common */
        snackbar: {
            color: 'success',
            msg: '',
            state: false
        },
        loading_process: false,

        /* main layout */
        filterasset: {
            startdate: moment(new Date()).format('YYYY-MM-DD'),
            enddate: moment(new Date()).format('YYYY-MM-DD'),
            status: 'All',
            search: ''
        },
        list_status: ['All', 'Confirmed', 'Unconfirmed'],
        list_roasset: {
            records: [],
            total: 0
        },
        currpage: 1,
        selected_roasset: {},

        /* form receive */
        dialog_receive_aset: false,
        form_receive: {
            date: moment(new Date()).format('YYYY-MM-DD'),
            supplier: "",
            reference: "",
            ponumber: "",
            contractlength: 0,
            datepayment: "",
            cicilan: 0,
            downpayment: 0,
            totalbill: 0,
            catatan: ""
        },
        item_receive: [],
        dialog_pick_contract: false,

        /* upload attachment */
        dialog_attachment: false,
        preview_attachment: [],

        /* delete */
        dialog_delete: false
    },
    mutations: {
        /* common */
        update_snackbar(state, val) {
            state.snackbar = val
        },
        update_loading_process(state, val) {
            state.loading_process = val
        },

        /* main layout */
        update_filterasset(state, val) {
            state.filterasset = val
        },
        update_list_status(state, val) {
            state.list_status = val
        },
        update_list_roasset(state, val) {
            state.list_roasset = val
        },
        update_currpage(state, val) {
            state.currpage = val
        },
        update_selected_roasset(state, val) {
            state.selected_roasset = val
        },

        /* form receive */
        update_dialog_receive_aset(state, val) {
            state.dialog_receive_aset = val
        },
        update_form_receive(state, val) {
            state.form_receive = val
        },
        update_item_receive(state, val) {
            state.item_receive = val
        },
        update_dialog_pick_contract(state, val) {
            state.dialog_pick_contract = val
        },

        /* attachment */
        update_dialog_attachment(state, val) {
            state.dialog_attachment = val
        },
        update_preview_attachment(state, val) {
            state.preview_attachment = val
        },

        /* delete */
        update_dialog_delete(state, val) {
            state.dialog_delete = val
        }
    },
    actions: {
        async lookupOrderAssetReceived(context) {
            try {
                // const param = {
                //     token: one_token(),
                // }

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

                // context.commit("update_list_roasset", resp.data);

                const dummy = {
                    records: [
                        {
                            date: moment(new Date()).format('YYYY-MM-DD'),
                            receiveordernumber: "GR-003/SDM-14/SM-1/VI/2026",
                            purchaseordernumber: "PO-002/OPR-RL/SM-2/VI/2026",
                            suppliername: "PT MEJA ABADI",
                            uangmuka: 4000000,
                            totaltagihan: 10000000,
                            besarcicilan: 500000,
                            numbercicilan: 12,
                            cicilanterbayar: 0,
                            status: "Unconfirmed"
                        }
                    ],
                    total: 1
                }

                context.commit("update_list_roasset", dummy);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: false };
                context.commit("update_snackbar", snac);
            }
        }
    }
}
