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

export default {
    namespaced: true,
    state: {
        user: one_user(),
        api_status: 1,
        detaildialog: false,
        err_msg: "",
        err_val: 0,

        regional: [],
        selected_regional: {
            S_RegionalID: one_user()['S_RegionalID'],
            S_RegionalName: one_user()['S_RegionalName']
        },
        branch: [],
        selected_branch: {},
        selected_status: "SEMUA",

        startdate: new Date().toISOString().substr(0, 10),
        enddate: new Date().toISOString().substr(0, 10),
        list_pr: {},
        pagination: 1
    },
    mutations: {
        update_err_msg(state, data) {
            state.err_msg = data
        },
        update_err_val(state, data) {
            state.err_val = data
        },
        update_startdate(state, data) {
            state.startdate = data
        },
        update_enddate(state, data) {
            state.enddate = data
        },
        update_regional(state, data) {
            state.regional = data
        },
        update_selected_regional(state, data) {
            state.selected_regional = data
        },
        update_branch(state, data) {
            state.branch = data
        },
        update_selected_branch(state, data) {
            state.selected_branch = data
        },
        update_selected_status(state, data) {
            state.selected_status = data
        },
        update_list_pr(state, data) {
            state.list_pr = data
        },
        update_pagination(state, data) {
            state.pagination = data
        },
        update_api_status(state, data) {
            state.api_status = data
        },
        update_detaildialog(state, data) {
            state.detaildialog = data
        }
    },
    actions: {
        async getregional(context, params) {
            context.commit("update_err_val", 0)
            try {
                params.token = one_token()
                let response = await api.listing_regional(params)
                if (response.status != "OK") {
                    context.commit("update_err_val", 1)
                } else {
                    let data = response.data.records
                    context.commit("update_regional", data)
                }
            } catch (error) {
                context.commit("update_err_val", 1)
            }
        },
        async getbranch(context, params) {
            context.commit("update_err_val", 0)
            try {
                params.token = one_token()
                let response = await api.listing_branch(params)
                if (response.status != "OK") {
                    context.commit("update_err_val", 1)
                    context.commit("update_err_msg", "error get listing branch: ", response.message)
                } else {
                    let data = [
                        {
                            M_BranchCode: "ALL",
                            M_BranchName: "SEMUA"
                        },
                        ...response.data.result
                    ]
                    
                    context.commit("update_branch", data)
                }
            } catch (error) {
                context.commit("update_err_val", 1)
                context.commit("update_err_msg", "error get listing branch: ", error.message)
            }
        },
        async getlistingpurchase(context, params) {
            context.commit("update_err_val", 0)
            try {
                params.token = one_token()
                let resp = await api.listing_purchase_req(params)
                if (resp.status != "OK") {
                    context.commit("update_err_val", 1)
                    context.commit("update_err_msg", "error get listing branch: ", response.message)
                } else {
                    let data = {
                        records: resp.data.records,
                        total: parseInt(resp.data.total)
                    }
                    context.commit("update_list_pr", data)
                }
            } catch (error) {
                context.commit("update_err_val", 1) 
                context.commit("update_err_msg", "error get listing request: ", error.message)
            }
        },
        async addflagstatus(context, params) {
            context.commit("update_err_val", 0)
            context.commit("update_api_status", 1)
            try {
                params.token = one_token()
                let resp = await api.add_flag_status(params)
                if (resp.status != "OK") {
                    context.commit("update_api_status", 3)
                    context.commit("update_err_val", 1)
                    context.commit("update_err_msg", "error add flag request: ", resp.message)
                } else {
                    context.dispatch("refresh_list")
                    context.commit("update_api_status", 2)
                }
            } catch (error) {
                context.commit("update_api_status", 3)
                context.commit("update_err_val", 1)
                context.commit("update_err_msg", "error add flag request: ", error.message)
            }
        },
        async editflagstatus(context, params) {
            context.commit("update_api_status", 1)
            context.commit("update_err_val", 0)
            try {
                params.token = one_token()
                let resp = await api.edit_flag_status(params)
                if (resp.status != "OK") {
                    context.commit("update_api_status", 3)
                    context.commit("update_err_val", 1)
                    context.commit("update_err_msg", "error edit flag request: ", resp.message)
                } else {
                    context.dispatch("refresh_list")
                    context.commit("update_api_status", 2)
                }
            } catch (error) {
                context.commit("update_api_status", 3)
                context.commit("update_err_val", 1)
                context.commit("update_err_msg", "error edit flag request: ", error.message)
            }
        },
        async refresh_list(context) {
            context.commit("update_err_val", 0) 
            try {
                let params = {
                    token: one_token(),
                    currpage: context.state.pagination,
                    startdate: context.state.startdate,
                    enddate: context.state.enddate,
                    regional: context.state.selected_regional.S_RegionalID,
                    branch: !context.state.selected_branch ? "" : context.state.selected_branch.M_BranchCode,
                    status: context.state.selected_status
                }
                let resp = await api.listing_purchase_req(params)

                if (resp.status != "OK") {
                    context.commit("update_err_val", 1) 
                    context.commit("update_err_msg", "error refresh request: ", resp.message)                    
                } else {
                    let data = {
                        records: resp.data.records,
                        total: parseInt(resp.data.total)
                    }
                    context.commit("update_list_pr", data)
                }
            } catch (error) {
                context.commit("update_err_val", 1) 
                context.commit("update_err_msg", "error refresh request: ", error.message)
            }
        }
    }
}