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

export default {
    namespaced: true,
    state: {
        snack: {
            isOpen: false,
            color: "success",
            message: ""
        },
        list_staff: {
            records: [],
            total: 0
        },
        list_po: {
            records: [],
            total: 0
        },
        list_supplier: {
            records: [],
            total: 0
        },
        list_ro: {
            records: [],
            total: 0
        },
        list_item: {
            records: [],
            total: 0
        },
        selected_item: [],
        page_item: 1,
        search_item: "",

        page_list: 1,
        list_faktur: {
            records: [],
            total: 0
        },
        filter: {
            date: moment(new Date()).format("YYYY-MM-DD"),
            enddate: moment(new Date()).format("YYYY-MM-DD"),
            supplier: "0",
            status: "All",
            nomor: ""
        },
        viewfaktur: false,

        dialog_attachment: false,
        preview_attachment: []
    },
    mutations: {
        update_snack(state, data) {
            state.snack = data
        },
        update_list_staff(state, data) {
            state.list_staff = data
        },
        update_list_po(state, data) {
            state.list_po = data
        },
        update_list_supplier(state, data) {
            state.list_supplier = data
        },
        update_list_ro(state, data) {
            state.list_ro = data
        },
        update_list_item(state, data) {
            state.list_item = data
        },
        update_selected_item(state, data) {
            state.selected_item = data
        },
        update_page_item(state, data) {
            state.page_item = data
        },
        update_search_item(state, data) {
            state.search_item = data
        },
        update_page_list(state, data) {
            state.page_list = data
        },
        update_list_faktur(state, data) {
            state.list_faktur = data
        },
        update_filter(state, data) {
            state.filter = data
        },
        update_viewfaktur(state, data) {
            state.viewfaktur = data
        },
        update_dialog_attachment(state, data) {
            state.dialog_attachment = data
        },
        update_preview_attachment(state, data) {
            state.preview_attachment = data
        }
    },
    actions: {
        async getListPO(context) {
            try {
                let supid = context.rootState.form.form_faktur.form_supplier.SupplierID
                let payload = {
                    token: one_token(),
                    supID: supid
                }

                let response = await api.LookupPO(payload)
                if (response.status != "OK") {

                } else {
                    let po = response.data
                    context.commit("update_list_po", po)
                }
            } catch (error) {

            }
        },
        async getListSupplier(context) {
            try {
                let payload = {
                    token: one_token(),
                    name: ""
                }

                let response = await api.LookupSupplier(payload)
                if (response.status != "OK") {
                    let snacc = {isOpen: true, message: response.message, color: "error"}
                    context.commit("update_snack", snacc)
                } else {
                    let sup = response.data
                    context.commit("update_list_supplier", sup)
                }
            } catch (error) {
                let snacc = {isOpen: true, message: error.message, color: "error"}
                context.commit("update_snack", snacc)
            }
        },
        async getListStaff(context) {
            try {
                let payload = {
                    token: one_token(),
                    name: ""
                }

                let response = await api.LookupStaff(payload)
                if (response.status != "OK") {
                    let snacc = {isOpen: true, message: response.message, color: "error"}
                    context.commit("update_snack", snacc)
                } else {
                    let staff = response.data
                    context.commit("update_list_staff", staff)
                }
            } catch (error) {
                let snacc = {isOpen: true, message: error.message, color: "error"}
                context.commit("update_snack", snacc)
            }
        },
        async getDetailPO(context) {
            try {
                let supid = context.rootState.form.form_faktur.form_supplier.SupplierID
                let numpo = context.rootState.form.form_faktur.form_po.ReceiveOrderPoNumber
                let payload = {
                    token: one_token(),
                    supID: supid,
                    numPO: numpo,
                }

                let response = await api.LookupDetailPO(payload)
                if (response.status != "OK") {
                    let snacc = {isOpen: true, message: response.message, color: "error"}
                    context.commit("update_snack", snacc)
                } else {
                    let po = response.data.records
                    let form = context.rootState.form.form_faktur

                    let duedate = moment(po.PurchaseOrderDate).add(Number(po.PurchaseOrderPaymentTerm), 'days').format("YYYY-MM-DD")

                    form.form_duedate = duedate
                    form.form_refnumber = po.PurchaseOrderRefNumber
                    form.form_tax_percent_pph = po.PurchaseOrderTaxPercentPph
                    form.form_tax_percent_ppn = po.PurchaseOrderTaxPercentPpn
                    form.form_disc_po = po.DiscountType == 'R' ? po.SupplierInvoiceDiscountAmount : po.SupplierInvoiceDiscountPercent
                    form.form_disc_type = po.DiscountType
                    form.form_shipping_cost = po.PurchaseOrderShippingCost


                    context.commit("form/update_form_faktur", form, {root: true})
                }
            } catch (error) {
                let snacc = {isOpen: true, message: error.message, color: "error"}
                context.commit("update_snack", snacc)
            }
        },
        async getListRO(context) {
            try {
                let poID = context.rootState.form.form_faktur.form_po.PurchaseOrderID
                let supID = context.rootState.form.form_faktur.form_supplier.SupplierID

                let payload = {
                    token: one_token(),
                    poID: poID,
                    supplierID: supID
                }

                var resp = await api.LookupRO(payload)
                if (resp.status != "OK") {
                    let snacc = {isOpen: true, message: resp.message, color: "error"}
                    context.commit("update_snack", snacc)
                } else {
                    context.commit("update_list_ro", resp.data)
                }
            } catch (error) {
                let snacc = {isOpen: true, message: error.message, color: "error"}
                context.commit("update_snack", snacc)
            }
        },
        async getListItemRO(context) {
            try {
                let payload = {
                    token: one_token(),
                    poID: context.rootState.form.form_faktur.form_po.PurchaseOrderID,
                    roID: context.rootState.form.form_ro.ReceiveOrderPoID,
                    name: context.state.search_item,
                    currpage: context.state.page_item,
                }

                let resp = await api.LookupItemRO(payload)
                if (resp.status != "OK") {
                    let snacc = {isOpen: true, message: resp.message, color: "error"}
                    context.commit("update_snack", snacc)
                } else {
                    context.commit("update_list_item", resp.data)
                }
            } catch (error) {
                let snacc = {isOpen: true, message: error.message, color: "error"}
                context.commit("update_snack", snacc)
            }
        },
        async getListFaktur(context) {
            try {
                let payload = {
                    token: one_token(),
                    page: context.state.page_list,
                    nomor: context.state.filter.nomor,
                    status: context.state.filter.status,
                    enddate: context.state.filter.enddate,
                    date: context.state.filter.date,
                    supplier: context.state.filter.supplier
                }

                let resp = await api.LookupFaktur(payload)
                if (resp.status != "OK") {
                    let snacc = {isOpen: true, message: resp.message, color: "error"}
                    context.commit("update_snack", snacc)
                } else {
                    context.commit("update_list_faktur", resp.data)
                }
            } catch (error) {
                let snacc = {isOpen: true, message: error.message, color: "error"}
                context.commit("update_snack", snacc)
            }
        },
        async getDetailFaktur(context, params) {
            try {
                params.token = one_token()
                let resp = await api.LookupFakturDetail(params)
                if (resp.status != 'OK') {
                    let snacc = {isOpen: true, message: resp.message, color: "error"}
                    context.commit("data/update_snack", snacc)
                } else {
                    let temp = resp.data
                    let faktur = context.rootState.form.form_faktur
                    faktur.form_date = temp.SupplierInvoiceDate
                    faktur.form_duedate = temp.SupplierInvoiceDueDate
                    faktur.form_draftdate = temp.SupplierInvoiceDraftPaymentDate
                    faktur.form_supplier = {
                        SupplierCode: temp.SupplierCode,
                        SupplierName: temp.SupplierName,
                        SupplierID: temp.SupplierInvoiceSupplierID
                    }
                    faktur.form_po = {
                        PurchaseOrderID: temp.SupplierInvoicePurchaseOrderID,
                        PurchaseOrderDate: temp.PurchaseOrderDate,
                        ReceiveOrderPoID: temp.SupplierInvoiceReceiveOrderPoID,
                        ReceiveOrderPoNumber: temp.ReceiveOrderPoNumber
                    }
                    let po = faktur.form_po
                    console.log(po)
                    context.commit("update_list_po", po)
                    faktur.form_refnumber = temp.SupplierInvoiceRefNumber
                    faktur.form_supplier_invoicenum = temp.SupplierInvoiceSupplierInvoiceNumber
                    faktur.form_supplier_invoicedate = temp.SupplierInvoiceSupplierInvoiceDate
                    faktur.form_tax_percent_pph = temp.SupplierInvoiceTaxPercentPph
                    faktur.form_tax_percent_ppn = temp.SupplierInvoiceTaxPercentPpn
                    faktur.form_disc_po = temp.DiscountType == 'R' ? temp.SupplierInvoiceDiscountAmount : temp.SupplierInvoiceDiscountPercent
                    faktur.form_disc_type = temp.DiscountType
                    faktur.form_note = temp.SupplierInvoiceNote
                    faktur.form_adjust_amount = temp.SupplierInvoiceAdjustmentAmount
                    faktur.form_adjust_note = temp.SupplierInvoiceAdjustmentNote
                    faktur.form_shipping_cost = temp.SupplierInvoiceShippingCost
                    faktur.fakturID = temp.SupplierInvoiceID

                    context.commit("form/update_form_faktur", faktur, {root: true})

                    let detail = resp.data.detail
                    let mapitem = []
                    detail.forEach(item => {
                        let a = {
                            "PurchaseOrderNumber": item.PurchaseOrderNumber,
                            "ReceiveOrderPoDetailPurchaseOrderID": item.SupplierInvoiceDetailPurchaseOrderID,
                            "ReceiveOrderPoDetailPurchaseOrderSummaryID": item.SupplierInvoiceDetailPurchaseOrderSummaryID,
                            "ReceiveOrderPoDetailReceiveOrderPoID": item.SupplierInvoiceDetailReceiveOrderPoID,
                            "ReceiveOrderPoDetailID": item.SupplierInvoiceDetailReceiveOrderPoDetailID,
                            "M_ItemID": item.SupplierInvoiceDetailItemID,
                            "M_ItemCode": item.M_ItemCode,
                            "M_ItemDesc": item.M_ItemDesc,
                            "ReceiveOrderPoItemUnitID": item.SupplierInvoiceDetailItemUnitID,
                            "ReceiveOrderPoDetailQty": item.SupplierInvoiceDetailQty,
                            "ReceiveOrderPoDetailPrice": item.SupplierInvoiceDetailPrice,
                            "Discount": item.SupplierInvoiceDetailDiscountDiscountType == 'P' ? item.SupplierInvoiceDetailDiscountPercent: item.SupplierInvoiceDetailDiscountDiscountRupiah,
                            "PurchaseOrderSummaryDiscountType": item.SupplierInvoiceDetailDiscountDiscountType,
                            "ReceiveOrderPoDetailDiskonPoProrata": item.SupplierInvoiceDetailDiscountPoProrata,
                            "disc_amount": item.SupplierInvoiceDetailDiscountAmount,
                            "subtotal": item.SupplierInvoiceDetailTotal
                        }

                        mapitem.push(a)
                    });

                    context.commit("form/update_form_item", mapitem, {root: true})
                    //context.dispatch("getListPO");
                }
            } catch (error) {
                let snacc = {isOpen: true, message: error.message, color: "error"}
                context.commit("update_snack", snacc)
            }
        },
        async getListAttachment(context, param) {
            try {
                param.token = one_token()

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

                context.commit('update_preview_attachment', resp.data)
            } catch (error) {
                let snacc = {isOpen: true, message: error.message, color: "error"}
                context.commit("update_snack", snacc)
            }
        }
    }
}
