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

export default {
    namespaced: true,
    state: {
        loading: false,
        reports: [],
        report: {},
        error: '',
        details: [],
        detail: [],
        loading_detail: false,
        error_detail: '',
        params: [],
        total_params: 0,
        total_filter_params: 0,
        search_status: 0,
        current_page: 1,
        get_data_status: 0,
        get_data_error_message: '',
        reportautocompletes: [],
        // defaultdetailcompanymodel:{id:0},
        // defaultdetailregionalmodel:{id:0},
        // defaultdetailbranchmodel:{id:0},
        // defaultdetailcompanyitems:[{id:0}],
        // defaultdetailregionalitems:[{id:0}],
        // defaultdetailbranchitems:[{id:0}],
        autocomplete_status: 0
    },
    mutations: {
        update_defaultdetailcompanymodel(state, val) {
            state.defaultdetailcompanymodel = val
        },
        update_defaultdetailcompanyitems(state, val) {
            state.defaultdetailcompanyitems = val
        },
        update_defaultdetailregionalmodel(state, val) {
            state.defaultdetailcompanymodel = val
        },
        update_defaultdetailregionalitems(state, val) {
            state.defaultdetailcompanyitems = val
        },
        update_defaultdetailbranchmodel(state, val) {
            state.defaultdetailcompanymodel = val
        },
        update_defaultdetailbranchitems(state, val) {
            state.defaultdetailcompanyitems = val
        },
        update_loading(state, status) {
            state.loading = status
        },
        update_reports(state, val) {
            state.reports = val
        },
        update_report(state, report) {
            state.report = report
        },
        update_error(state, val) {
            state.error = val
        },
        update_details(state, val) {
            state.details = val
        },
        update_detail(state, data) {
            state.detail = data.detail
        },
        update_loading_detail(state, val) {
            state.loading_detail = val
        },
        update_error_detail(state, val) {
            state.error_detail = val
        },
        update_params(state, val) {
            state.params = val
        },
        update_total_params(state, val) {
            state.total_params = val
        },
        update_total_filter_params(state, val) {
            state.total_filter_params = val
        },
        update_current_page(state, val) {
            state.current_page = val
        },
        update_get_data_status(state, val) {
            state.get_data_status = val
        },
        update_get_data_error_message(state, val) {
            state.get_data_error_message = val
        },
        update_reportdropdowns(state, val) {
            state.reportdropdowns = val
        },
        update_reportdropdown(state, val) {
            state.reportdropdown = val
        },
        update_reportautocompletes(state, val) {
            state.reportautocompletes = val
        },
        update_autocomplete_status(state, val) {
            state.autocomplete_status = val
        }
    },
    actions: {
        async list(context, prm) {
            context.commit("update_loading", true)
            try {
                let resp = await api.list(prm)
                if (resp.statusText != "OK") {
                    context.commit("update_loading", false)
                    context.commit("update_error", resp.message)
                } else {
                    context.commit("update_loading", false)
                    context.commit("update_error", "")
                    context.commit("update_reports", resp.data.data.records)
                }
            } catch (e) {
                context.commit("update_loading", false)
                context.commit("update_error", e.message)
            }
        },
        async lookupparam(context, prm) {
            context.commit("update_loading", true)
            try {
                prm.token = one_token()
                let resp = await api.lookupparam(prm)
                if (resp.statusText != "OK") {
                    context.commit("update_loading", false)
                    context.commit("update_error", resp.message)
                } else {
                    context.commit("update_loading", false)
                    context.commit("update_error", "")
                    let data = {
                        records: resp.data.records,
                        total: resp.data.total,
                        total_filter: resp.data.total_filter
                    }

                    context.commit("update_params", resp.data.data.records)
                    context.commit("update_total_params", resp.data.data.total)
                    context.commit("update_total_filter_params", resp.data.data.total_filter)
                }
            } catch (e) {
                context.commit("update_loading", false)
                context.commit("update_error", e.message)
            }
        },
        async detail(context, id) {
            context.commit("update_loading_detail", true)
            try {
                let resp = await api.detail(id)
                let user = one_user();
                //console.log(resp)
                if (resp.statusText != "OK") {
                    context.commit("update_loading_detail", false)
                    context.commit("update_error_detail", resp.message)
                } else {
                    context.commit("update_loading_detail", false)
                    context.commit("update_error_detail", "")
                    var arrOrders = resp.data.data.records
                    arrOrders.forEach((value, index) => {
                        arrOrders[index]["items"] = []
                        if (value.R_InputTypeName === 'dropdown') {
                            var sp = value.R_ReportDetailSourceSp
                            context.dispatch("listdropdown", { sp: sp, index: index })
                        }
                        if (value.R_InputTypeName === 'company') {
                            arrOrders[index]['model'] = { id: user.M_BranchCompanyID, name: user.M_BranchCompanyName }
                            arrOrders[index]['items'] = [{ id: user.M_BranchCompanyID, name: user.M_BranchCompanyName }]
                            // context.commit("update_defaultdetailcompanymodel",{id: user.M_BranchCompanyID,name: user.M_BranchCompanyName})
                            // context.commit("update_defaultdetailcompanymodel",[{id: user.M_BranchCompanyID,name: user.M_BranchCompanyName}])
                        }
                        if (value.R_InputTypeName === 'regional') {
                            arrOrders[index]['model'] = { id: user.S_RegionalID, name: user.S_RegionalName }
                            arrOrders[index]['items'] = [{ id: user.S_RegionalID, name: user.S_RegionalName }]
                            // context.commit("update_defaultdetailregionalmodel",{id: user.S_RegionalID,name: user.S_RegionalName})
                            // context.commit("update_defaultdetailregionalmodel",[{id: user.S_RegionalID,name: user.S_RegionalName}])
                        }
                        if (value.R_InputTypeName === 'branch') {
                            var sp = value.R_ReportDetailSourceSp
                            context.dispatch("listdropdown", { sp: sp, index: index })
                            // console.log("user", user)
                            // if(user.M_BranchID !== 0 && user.M_BranchName !== ""){
                            //     console.log("masuk")
                            //     // arrOrders[index]['model'] = {id: user.M_BranchBranchID,name: user.M_BranchBranchName}
                            // arrOrders[index]['items'] = [{id: user.M_BranchBranchID,name: user.M_BranchBranchName}] 
                            // }

                            // context.commit("update_defaultdetailbranchmodel",{id: user.M_BranchBranchID,name: user.M_BranchBranchName})
                            // context.commit("update_defaultdetailbranchmodel",[{id: user.M_BranchBranchID,name: user.M_BranchBranchName}])
                        }
                    });
                    context.commit("update_details", arrOrders)
                }
            } catch (e) {
                context.commit("update_loading_detail", false)
                context.commit("update_error_detail", e.message)
            }
        },
        async listdropdown(context, param) {
            context.commit("update_get_data_status", 1)
            try {
                let resp = await api.listdropdown(param.sp)
                //disimpan dalam report detail
                let user = one_user();
                let arrOrders = context.state.details
                arrOrders[param.index].items = resp
                context.commit("update_details", arrOrders)

                if (arrOrders[param.index].R_ReportDetailR_InputTypeID == 7) {
                    let new_list = arrOrders[param.index]['items']
                    let new_items = []
                    //Masuk sebagai cabang
                    if (user.M_BranchID !== 0 && user.M_BranchName !== "") {
                        new_list.forEach((value) => {
                            if (value.id == user.M_BranchID) {
                                new_items.push({ code: value.code, name: value.name });
                                arrOrders[param.index]['model'] = { code: value.code, name: value.name };
                                arrOrders[param.index]['items'] = [{ code: value.code, name: value.name }];
                            }
                        });
                    }
                    // Masuk sebagai regional
                    if (user.M_BranchID == 0 && user.M_BranchName == "") {
                        new_list.forEach((value) => {
                            if (value.regionalID == user.S_RegionalID) {
                                new_items.push({ code: value.code, name: value.name });
                            }
                        });
                        arrOrders[param.index]['items'] = new_items; // Update items dengan hasil filtering
                    }
                }
            } catch (e) {
                context.commit("update_get_data_status", 3)
                context.commit("update_get_data_error_message", e.message)
                console.log(e)
            }
        },
        async listautocomplete(context, prm) {
            context.commit("update_autocomplete_status", 1)
            try {
                let resp = await api.listautocomplete(prm)
                context.commit("update_autocomplete_status", 2)
                let arrOrders = context.state.details
                arrOrders[prm.index].items = resp.data
                context.commit("update_details", arrOrders)
            } catch (e) {
                context.commit("update_autocomplete_status", 3)
                console.log(e)
            }
        }
    }
}
