// 1 => LOADING
// 2 => DONE
// 3 => ERROR

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

export default {
    namespaced: true,
    state: {
        user: one_user(),
        statusOptions: [
            { value: 'All', title: "All" },
            { value: 'Draft', title: "Draft" },
            { value: 'Pending', title: "Pending" },
            { value: 'Approved', title: "Approved" },
            { value: 'Rejected', title: "Rejected" },
            { value: 'Terkirim', title: "Terkirim" },
            { value: 'Partial', title: "Partial" },
            { value: 'Selesai', title: "Selesai" },
        ],
        selectedStatus: { value: 'All', title: "All" },
        selectedDate: moment(new Date()).format('YYYY-MM-DD'),
        selectedEndDate: moment(new Date()).format('YYYY-MM-DD'),
        search: '',
        totalPage: 0,
        selectedPage: 1,
        loading: false,
        prList: [],
        dialogApprove: false,
        detailData: [],
        selectedPr: {},
        loadingDetail: false,
        snackbar: {
            state: false,
            type: 'success',
            message: '',
        },
        levelname: [],
        user_approvelevel: {},
        status: '',
        xstartdate: '',
        act_dialog: '',
        dialogQty: false,
        selectedItems: [],
        flag: '',
    },
    mutations: {
        updateLevelName(state, val) {
            state.levelname = val;
          },
        update_snackbar(state, val) {
            state.snackbar = val;
        },
        update_loadingDetail(state, val) {
            state.loadingDetail = val;
        },
        update_selectedPr(state, val) {
            state.selectedPr = val;
        },
        update_detailData(state, val) {
            state.detailData = val;
        },
        update_dialogApprove(state, val) {
            state.dialogApprove = val;
        },
        update_selectedStatus(state, val) {
            state.selectedStatus = val;
        },
        update_selectedDate(state, val) {
            state.selectedDate = val;
        },
        update_selectedEndDate(state, val) {
            state.selectedEndDate = val;
        },
        update_search(state, val) {
            state.search = val;
        },
        update_loading(state, val) {
            state.loading = val;
        },
        update_totalPage(state, val) {
            state.totalPage = val;
        },
        update_selectedPage(state, val) {
            state.selectedPage = val;
        },
        update_prList(state, val) {
            state.prList = val;
        },
        updatte_user_approvelevel(state, val) {
            state.user_approvelevel = val
        },
        update_status(state, val) {
            state.status = val
        },
        update_xstartdate(state, val) {
            state.xstartdate = val
        },
        update_act_dialog(state, val) {
            state.act_dialog = val
        },
        update_dialogQty(state, val) {
            state.dialogQty = val
        },
        update_selectedItems(state, val) {
            state.selectedItems = val;
        },
        update_flag(state, val) {
            state.flag = val
        },
    },
    actions: {
        async search(context) {
            context.commit("update_loading", true);

            try {

                var url_string = window.location.href
                var url = new URL(url_string);
                var status = url.searchParams.get("status");
                var xstartdate = url.searchParams.get("startdate");
                var xsearch = url.searchParams.get("search");
                var xflag = url.searchParams.get("flag");

                // untuk notifikasi
                if (xflag == 'N') {

                    if (xsearch != null && xsearch != '') {
                        context.commit("update_search", xsearch);
                    }
                    if (status != null && status != '') {
                        context.commit("update_selectedStatus", { value: status, title: status });
                    }
                    if (xstartdate != null && xstartdate != '') {
                        context.commit("update_selectedDate", xstartdate);
                        context.commit("update_selectedEndDate", xstartdate);
                    }

                } else if (xflag == 'G') {
                    // untuk get data param approve all

                    if (status != null && status != '') {
                        context.commit("update_selectedStatus", { value: status, title: status });
                    }
                    if (xstartdate != null && xstartdate != '') {
                        context.commit("update_selectedDate", xstartdate);
                        context.commit("update_selectedEndDate", moment(new Date()).format('YYYY-MM-DD'));
                    }
                    context.commit("update_flag", xflag);

                }

                var payload = {
                    token: one_token(),
                    search: context.state.search,
                    startDate: context.state.selectedDate,
                    endDate: context.state.selectedEndDate,
                    status: context.state.selectedStatus.value,
                    page: context.state.selectedPage,
                    flag: context.state.flag,
                };

                let response = await api.search(payload);
                if (response.status != "OK") {
                    let snackbar = {
                        state: true,
                        type: "error",
                        message: response.message,
                    }
                    context.commit("update_snackbar", snackbar);
                    context.commit("update_loading", false);
                } else {
                    let data = {
                        records: response.data.records,
                        total: response.data.total,
                    };

                    context.commit("update_totalPage", data.total);
                    context.commit("update_prList", data.records);
                    context.commit("update_loading", false);
                }
            } catch (e) {
                let snackbar = {
                    state: true,
                    type: "error",
                    message: e.message,
                }
                context.commit("update_snackbar", snackbar);
                context.commit("update_loading", false);
                // context.commit("updateMainTableLoadingStatus", 3);
                // context.commit("updateMainTableErrorMessage", e.message);
            }
        },
        async approve(context, prm) {
            context.commit("update_loading", true);
            try {
                prm.token = one_token()
                let response = await api.approve(prm);
                if (response.status != "OK") {
                    let snackbar = {
                        state: true,
                        type: "error",
                        message: response.message,
                    }
                    context.commit("update_snackbar", snackbar);
                    context.commit("update_loading", false);
                } else {
                    let data = {
                        records: response.data.records,
                        total: response.data.total,
                    };

                    // context.commit("update_totalPage", data.total);
                    // context.commit("update_prList", data.records);
                    await context.dispatch("search");
                    context.commit("update_dialogApprove", false);
                    let msg = '';
                    if (prm.type === 'A') {
                        msg = 'Berhasil approve purchase request ' + prm.pr.prNumber
                    } else if (prm.type === 'R') {
                        msg = 'Berhasil reject purchase request ' + prm.pr.prNumber
                    } else if (prm.type === 'U') {
                        msg = 'Berhasil unapprove purchase request ' + prm.pr.prNumber
                    }
                    let snackbar = {
                        state: true,
                        type: "success",
                        message: msg,
                    }
                    context.commit("update_snackbar", snackbar);
                    context.commit("update_loading", false);
                }
            } catch (e) {
                let snackbar = {
                    state: true,
                    type: "error",
                    message: e.message,
                }
                context.commit("update_snackbar", snackbar);
                context.commit("update_loading", false);
                // context.commit("updateMainTableLoadingStatus", 3);
                // context.commit("updateMainTableErrorMessage", e.message);
            }
        },
        async getlevel(context) {
            context.commit("update_loadingDetail", true);

            try {
                var payload = {
                    token: one_token()};

                let response = await api.getlevel(payload);
                if (response.status != "OK") {
                    context.commit("update_loadingDetail", false);
                } else {
                    let data = {
                        records: response.data.records,
                        total: response.data.total,
                    };
                    context.commit("updateLevelName", data.records[0].M_ApproveLevelName);
                    context.commit("updatte_user_approvelevel", data.records[0])
                    context.commit("update_loadingDetail", false);
                }
            } catch (e) {
                context.commit("update_loadingDetail", false);
                // context.commit("updateMainTableLoadingStatus", 3);
                // context.commit("updateMainTableErrorMessage", e.message);
            }
        },
        async getDetail(context) {
            context.commit("update_loadingDetail", true);

            try {
                var payload = {
                    token: one_token(),
                    id: context.state.selectedPr.prID,

                };

                let response = await api.getDetail(payload);
                if (response.status != "OK") {
                    let snackbar = {
                        state: true,
                        type: "error",
                        message: response.message,
                    }
                    context.commit("update_snackbar", snackbar);
                    context.commit("update_loadingDetail", false);
                } else {
                    let data = {
                        records: response.data.records,
                        total: response.data.total,
                    };


                    context.commit("update_detailData", response.data);
                    context.commit("update_loadingDetail", false);
                }
            } catch (e) {
                let snackbar = {
                    state: true,
                    type: "error",
                    message: e.message,
                }
                context.commit("update_snackbar", snackbar);
                context.commit("update_loadingDetail", false);
                // context.commit("updateMainTableLoadingStatus", 3);
                // context.commit("updateMainTableErrorMessage", e.message);
            }
        },
        async updateqty(context, prm) {
            context.commit("update_loading", true);
            try {
                prm.token = one_token()
                let response = await api.updateqty(prm);
                if (response.status != "OK") {
                    let snackbar = {
                        state: true,
                        type: "error",
                        message: response.message,
                    }
                    context.commit("update_snackbar", snackbar);
                    context.commit("update_loading", false);
                } else {
                    let data = {
                        records: response.data.records,
                        total: response.data.total,
                    };

                    // context.commit("update_totalPage", data.total);
                    // context.commit("update_prList", data.records);
                    await context.dispatch("search");
                    context.commit("update_dialogQty", false);
                    context.commit("update_detailData", []);
                    let msg = 'Berhasil edit qty';
                    let snackbar = {
                        state: true,
                        type: "success",
                        message: msg,
                    }
                    context.commit("update_snackbar", snackbar);
                    context.commit("update_loading", false);
                }
            } catch (e) {
                let snackbar = {
                    state: true,
                    type: "error",
                    message: e.message,
                }
                context.commit("update_snackbar", snackbar);
                context.commit("update_loading", false);
                // context.commit("updateMainTableLoadingStatus", 3);
                // context.commit("updateMainTableErrorMessage", e.message);
            }
        },
    },
};
