import { UniversalDataService } from '/js/ApiDataServer/Universal.js?tf=202411131831';
import { StoreDataService } from '/js/ApiDataServer/Store.js';
import { StoreProductPriceDataService } from '/js/ApiDataServer/StoreProductPrice.js';
import { AuthDataService } from '/js/ApiDataServer/Auth.js?tf=202411121501';
import { RESPONSE_STATUS } from '/js/constants.js';
import { Decorator } from '/js/util/dacorator.js';
import { enMessage } from '/js/constants.js';
import { UJ } from '/js/util/uj.js';

var vMain = new Vue({
    el: "#dvMain",
    data: function () {
        return {
            pageTitle: "門市價格",
            authDesc: "門市_門市管理",
            isSystemManager: false,
            isLoading: false,
            isBindFancybox: true,
            store: {
                id: -1
            },
            ltProductType: [],
            ltCategory: [],
            query: {
                storeId: -1,
                productType: -1,
                parentId: -1,
                categoryId: -1,
                keyword: "",
                currentPage: 1,
                pageSize: 20
            },
            list: [],
            pager: {}
        }
    },
    created: async function () {
        var v = this;
        v.isSystemManager = await AuthDataService.IsSystemManager();
        v.store.id = UJ.getParameterByName("sid");

        var res = await StoreDataService.Get(v.store.id);
        if (res.status == RESPONSE_STATUS.OK) {
            v.store = res.data;
            v.store.createDate = Decorator.ChangeISODateTimeToDate(v.store.createDatetime);

            v.ltProductType = await UniversalDataService.GetProductType();
            v.ltCategory = await UniversalDataService.GetProductCategory();
            v.query.storeId = v.store.id;

            v.clearCriteria();

            v.isFinishRequest = true;

        } else {
            var data = res.data;
            showErrMsg(data.message, "", "", function () {
                if (data.enMessage == enMessage.NotFound) {
                    UJ.BackToList();
                }
            });
        }
    },
    updated: function () {
        var v = this;
        if (!v.isBindFancybox) {
            bindFancybox();
            v.isBindFancybox = true;
        }

        initAdminutil();
        setTimeout(function () {
            v.isLoading = false;
        }, 100);
    },
    methods: {
        clearCriteria() {
            var v = this;
            v.query.productType = -1;
            v.query.parentId = -1;
            v.query.categoryId = -1;
            v.query.keyword = "";

            v.getList(1);
        },
        async getList(pageNum) {
            var v = this;
            v.isLoading = true;
            if (pageNum > 0) {
                v.query.currentPage = pageNum;
            }
            let res = await StoreProductPriceDataService.GetList(v.query);
            v.list = res.list;
            v.list.forEach(function (item) {
                item.parentCateId = -1;
                item.cateName = "";
                v.ltCategory.forEach(function (cate) {
                    if (cate.id == item.categoryId) {
                        item.cateName = cate.name;
                        item.parentCateId = cate.parentId;
                    }
                });
                item.paretName = "";
                v.ltCategory.forEach(function (cate) {
                    if (cate.id == item.parentCateId) {
                        item.paretName = cate.name;
                    }
                });
            });
            v.pager = res.pager;
            v.isLoading = false;
            v.isBindFancybox = false;//重新綁定fancybox
        },
        goPage(pageNum) {
            this.getList(pageNum);
        },
        async saveProduct(item) {
            var v = this;
            swal({
                title: "確定要儲存價格？",
                type: "question",
                showCancelButton: true,
                confirmButtonText: "確定",
                cancelButtonText: "取消"
            }).then(async function (result) {
                if (result.value) {
                    v.isFinishRequest = false;
                    v.isLoading = true;

                    let res = await StoreProductPriceDataService.Update({
                        id: item.id,
                        storeId: item.storeId,
                        productId: item.productId,
                        salePrice: item.salePrice,
                        monthlyCharge: item.monthlyCharge,
                        weeklyCharge: item.weeklyCharge,
                        salePriceCost: item.salePriceCost,
                        monthlyCost: item.monthlyCost,
                        weeklyCost: item.weeklyCost
                    });
                    if (res.status == RESPONSE_STATUS.OK) {
                        showMsg('儲存成功!', null, null, null, null, function () {
                            v.getList(v.query.currentPage);
                        });
                    } else {
                        var data = res.data;
                        if (data.title != undefined) {
                            var errMsg = Object.keys(data.errors) + " is required.";
                            showErrMsg("", errMsg, "", null);
                        } else {
                            showErrMsg(data.message, "", "", null);
                        }
                        v.isFinishRequest = true;
                        v.isLoading = false;
                    }
                }
            });
        },
        async removeProduct(item) {
            var v = this;
            swal({
                title: "確定要移除？",
                type: "question",
                showCancelButton: true,
                confirmButtonText: "確定",
                cancelButtonText: "取消"
            }).then(async function (result) {
                if (result.value) {
                    v.isFinishRequest = false;
                    v.isLoading = true;

                    let res = await StoreProductPriceDataService.Delete(item.id);
                    if (res.status == RESPONSE_STATUS.OK) {
                        showMsg('移除成功!', null, null, null, null, function () {
                            v.getList(v.query.currentPage);
                        });
                    } else {
                        var data = res.data;
                        if (data.title != undefined) {
                            var errMsg = Object.keys(data.errors) + " is required.";
                            showErrMsg("", errMsg, "", null);
                        } else {
                            showErrMsg(data.message, "", "", null);
                        }
                        v.isFinishRequest = true;
                        v.isLoading = false;
                    }
                }
            });
        },
        async saveAll() {
            var v = this;
            swal({
                title: "確定要儲存所有價格？",
                type: "question",
                showCancelButton: true,
                confirmButtonText: "確定",
                cancelButtonText: "取消"
            }).then(async function (result) {
                if (result.value) {
                    v.isFinishRequest = false;
                    v.isLoading = true;

                    let res = await StoreProductPriceDataService.UpdateAll(v.list);
                    if (res.status == RESPONSE_STATUS.OK) {
                        showMsg('儲存成功!', null, null, null, null, function () {
                            v.getList(v.query.currentPage);
                        });
                    } else {
                        var data = res.data;
                        if (data.title != undefined) {
                            var errMsg = Object.keys(data.errors) + " is required.";
                            showErrMsg("", errMsg, "", null);
                        } else {
                            showErrMsg(data.message, "", "", null);
                        }
                        v.isFinishRequest = true;
                        v.isLoading = false;
                    }
                }
            });
        }
    }
});


$(function () {
    setTimeout(function () {
        openLeftMenu('/store/list');
    }, 100);
});