// 1 => LOADING
// 2 => DONE
// 3 => ERROR
import * as api from "../api/supplier.js"

export default {
    namespaced: true,
    state: {
        search_status: 0,
        search_error_message: "",
        suppliers: [],
        selected_supplier: {},
        total_page: 0,
        current_page: 1,
        xsearch: "",
        last_id: -1,
        dialog_form: false,
        act: "new",
        errors: "",
        save_status: 0,
        save_error_message: "",
        alert_error: false,
        alert_success: false,
        msg_success: "",

        // detail
        search_status_detail: 0,
        search_error_detail_message: "",
        supplier_prices: [],
        selected_supplier_price: {},
        total_page_supplier_price: 0,
        current_page_detail: 1,
        xsearch_detail: "",
        selected_supplier_id: 0,
        dialog_form_detail: false,
        act_detail: "new",
        loading_autocomplete: false,
        itemList: [],
        selected_item: {},
        itemUnits: [],
        selected_unit: {},
        alert_error_detail: false,
        alert_success_detail: false,
        selected_type: {id:"1", name:"Tak Terhingga", value:"T"},
        min_value:'',
        max_value:'',
        min_inclusive:false,
        max_inclusive:false,
    },
    mutations: {
        update_search_status(state, val) {
            state.search_status = val
        },
        update_search_error_message(state, val) {
            state.search_error_message = val
        },
        update_suppliers(state, val) {
            state.suppliers = val
        },
        update_selected_supplier(state, val) {
            state.selected_supplier = val
        },
        update_total_page(state, val) {
            state.total_page = val
        },
        update_current_page(state, val) {
            state.current_page = val
        },
        update_xsearch(state, val) {
            state.xsearch = val
            state.current_page = 1
        },
        update_last_id(state, val) {
            state.last_id = val
        },
        update_dialog_form(state, val) {
            state.dialog_form = val
        },
        update_act(state, val) {
            state.act = val
        },
        update_save_status(state, val) {
            state.save_status = val
        },
        update_save_error_message(state, val) {
            state.save_error_message = val
        },
        update_alert_error(state, val) {
            state.alert_error = val
        },
        update_alert_success(state, val) {
            state.alert_success = val
        },
        update_msg_success(state, val) {
            state.msg_success = val
        },

        // detail
        update_search_status_detail(state, val) {
            state.search_status_detail = val
        },
        update_search_error_detail_message(state, val) {
            state.search_error_detail_message = val
        },
        update_supplier_prices(state, val) {
            state.supplier_prices = val
        },
        update_selected_supplier_price(state, val) {
            state.selected_supplier_price = val
        },
        update_total_page_supplier_price(state, val) {
            state.total_page_supplier_price = val
        },
        update_current_page_detail(state, val) {
            state.current_page_detail = val
        },
        update_xsearch_detail(state, val) {
            state.xsearch_detail = val
            state.current_page_detail = 1
        },
        update_selected_supplier_id(state, val) {
            state.selected_supplier_id = val
        },
        update_errors(state, val) {
            state.errors = val
        },
        update_dialog_form_detail(state, val) {
            state.dialog_form_detail = val
        },
        update_act_detail(state, val) {
            state.act_detail = val
        },
        update_loading_autocomplete(state, val) {
            state.loading_autocomplete = val
        },
        update_itemList(state, val) {
            state.itemList = val
        },
        update_selected_item(state, val) {
            state.selected_item = val
        },
        update_itemUnits(state, val) {
            state.itemUnits = val
        },
        update_selected_unit(state, val) {
            state.selected_unit = val
        },
        update_alert_error_detail(state, val) {
            state.alert_error_detail = val
        },
        update_alert_success_detail(state, val) {
            state.alert_success_detail = val
        },
        update_selected_type(state, val) {
            state.selected_type = val
        },
        update_min_inclusive(state, val) {
            state.min_inclusive = val
        },
        update_max_inclusive(state, val) {
            state.max_inclusive = val
        },
        update_min_value(state, val) {
            state.min_value = val
        },
        update_max_value(state, val) {
            state.max_value = val
        }
    },
    actions: {
        async search(context) {
            context.commit("update_search_status", 1)
            try {
                var prm = {
                    token: one_token(),
                    current_page: context.state.current_page,
                    search: context.state.xsearch,
                    last_id: -1
                }
                let resp = await api.search(prm)
                if (resp.status != "OK") {
                    context.commit("update_search_status", 3)
                    context.commit("update_search_error_message", resp.message)
                } else {
                    context.commit("update_search_status", 2)
                    context.commit("update_search_error_message", "")
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total_page
                    }

                    context.commit("update_suppliers", data.records)
                    context.commit("update_total_page", data.total)

                }
            } catch (e) {
                context.commit("update_search_status", 3)
                context.commit("update_search_error_message", e.message)
            }
        },
        async searchdetail(context) {
            context.commit("update_search_status_detail", 1)
            try {
                var prm = {
                    token: one_token(),
                    current_page: context.state.current_page_detail,
                    search: context.state.xsearch_detail,
                    supplier_id: context.state.selected_supplier.SupplierID
                }
                let resp = await api.searchdetail(prm)
                if (resp.status != "OK") {
                    context.commit("update_search_status_detail", 3)
                    context.commit("update_search_error_detail_message", resp.message)
                } else {
                    context.commit("update_search_status_detail", 2)
                    context.commit("update_search_error_detail_message", "")
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total_page
                    }

                    context.commit("update_supplier_prices", data.records)
                    context.commit("update_total_page_supplier_price", data.total)

                }
            } catch (e) {
                context.commit("update_search_status_detail", 3)
                context.commit("update_search_error_detail_message", e.message)
            }
        },
        async save(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.save(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 3)
                    context.commit("update_alert_error", true)
                } else {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 2)
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    };
        
                    if (data.total !== -1) {
                        context.commit("update_errors", [])
                        context.commit("update_alert_success", true)
                        context.commit("update_dialog_form", false)
                        var msg = `Supplier baru dengan kode ${data.records[0].SupplierCode} sudah tersimpan`;
                        context.commit("update_msg_success", msg)
                        context.dispatch("search")
                        context.commit("update_selected_supplier", data.records[0]);
                        context.dispatch("searchdetail")
                    }
                } 
            } catch (e) {
                context.commit("update_save_error_message", e.message)
                context.commit("update_save_status", 3)
                context.commit("update_alert_error", true)
            }
        },
        async update(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.update(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 3)
                    context.commit("update_alert_error", true)
                } else {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 2)
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    };
        
                    if (data.total !== -1) {
                        context.commit("update_errors", [])
                        context.commit("update_alert_success", true)
                        context.commit("update_dialog_form", false)
                        var msg = `Perubahan supplier dengan kode ${data.records[0].SupplierCode} sudah tersimpan`;
                        context.commit("update_msg_success", msg)
                        context.dispatch("search")
                        context.commit("update_selected_supplier", data.records[0]);
                        context.dispatch("searchdetail")
                    }
                } 
            } catch (e) {
                context.commit("update_save_error_message", e.message)
                context.commit("update_save_status", 3)
                context.commit("update_alert_error", true)
            }
        },
        async deletesupplier(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.deletesupplier(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 3)
                    context.commit("update_alert_error", true)
                } else {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 2)
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    };
        
                    if (data.total !== -1) {
                        context.commit("update_errors", [])
                        context.commit("update_alert_success", true)
                        context.commit("update_dialog_form", false)
                        var msg = "Supplier dengan kode " + prm.codeSupplier + " sudah dihapus";
                        context.commit("update_msg_success", msg)
                        context.dispatch("search")
                        context.dispatch("searchdetail")
                    }
                } 
            } catch (e) {
                context.commit("update_save_error_message", e.message)
                context.commit("update_save_status", 3)
                context.commit("update_alert_error", true)
            }
        },

        async searchItem(context, prm) {
            context.commit("update_loading_autocomplete", true)
            try {
                let resp = await api.searchItem(one_token(), prm)
                if (resp.status != "OK") {
                    context.commit("update_loading_autocomplete", false)
                    context.commit("update_search_error_message", resp.message);
                } else {
                    context.commit("update_loading_autocomplete", false)
                    context.commit("update_search_error_message", "");
                    let data = {
                        records: resp.data.records
                    }
    
                    context.commit("update_itemList", data.records)
                }
            } catch (e) {
                context.commit("update_loading_autocomplete", true)
                context.commit("update_search_error_message", e.message);
            }
        },

        async getUnit(context, prm) {
            context.commit("update_loading_autocomplete", true)
            try {
                prm.token = one_token()
                let resp = await api.getUnit(prm)
                if (resp.status != "OK") {
                    context.commit("update_loading_autocomplete", false)
                    context.commit("update_search_error_message", resp.message);
                } else {
                    context.commit("update_loading_autocomplete", false)
                    context.commit("update_search_error_message", "");
                    let data = {
                        records: resp.data.records
                    }
    
                    context.commit("update_itemUnits", data.records)
                }
            } catch (e) {
                context.commit("update_loading_autocomplete", true)
                context.commit("update_search_error_message", e.message);
            }
        },

        async saveprice(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.saveprice(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 3)
                    context.commit("update_alert_error_detail", true)
                } else {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 2)
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    };
        
                    if (data.total !== -1) {
                        context.commit("update_errors", [])
                        context.commit("update_alert_success_detail", true)
                        context.commit("update_dialog_form_detail", false)
                        var msg = "Harga baru berhasil ditambahkan";
                        context.commit("update_msg_success", msg)
                        context.commit("update_selected_supplier_price", data.records[0]);
                        context.dispatch("searchdetail")
                    }
                } 
            } catch (e) {
                context.commit("update_save_error_message", e.message)
                context.commit("update_save_status", 3)
                context.commit("update_alert_error_detail", true)
            }
        },

        async updateprice(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.updateprice(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 3)
                    context.commit("update_alert_error_detail", true)
                } else {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 2)
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    };
        
                    if (data.total !== -1) {
                        context.commit("update_errors", [])
                        context.commit("update_alert_success_detail", true)
                        context.commit("update_dialog_form_detail", false)
                        var msg = "Harga baru berhasil di update";
                        context.commit("update_msg_success", msg)
                        context.commit("update_selected_supplier_price", data.records[0]);
                        context.dispatch("searchdetail")
                    }
                } 
            } catch (e) {
                context.commit("update_save_error_message", e.message)
                context.commit("update_save_status", 3)
                context.commit("update_alert_error_detail", true)
            }
        },

        async deleteprice(context, prm) {
            context.commit("update_save_status", 1)
            try {
                prm.token = one_token()
                let resp = await api.deleteprice(prm);
                if (resp.status != "OK") {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 3)
                    context.commit("update_alert_error_detail", true)
                } else {
                    context.commit("update_save_error_message", resp.message)
                    context.commit("update_save_status", 2)
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total
                    };
        
                    if (data.total !== -1) {
                        context.commit("update_errors", [])
                        context.commit("update_alert_success_detail", true)
                        var msg = "Harga baru berhasil di hapus";
                        context.commit("update_msg_success", msg)
                        context.dispatch("searchdetail")
                    }
                } 
            } catch (e) {
                context.commit("update_save_error_message", e.message)
                context.commit("update_save_status", 3)
                context.commit("update_alert_error_detail", true)
            }
        },
    }
}