import { UniversalDataService } from '/js/ApiDataServer/Universal.js?tf=202411131831';
import { ProductMainDataService } from '/js/ApiDataServer/ProductMain.js';
import { ProductRelationDataService } from '/js/ApiDataServer/ProductRelation.js';
import { AuthDataService } from '/js/ApiDataServer/Auth.js?tf=202411121501';
import { RESPONSE_STATUS } from '/js/constants.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,
            ltCategory: [],
            mainObj: {
                id: -1
            },
            relationType: -1,
            list: [],
            checkAll: false,
            ltChecked: []
        }
    },
    created: async function () {
        var v = this;
        v.isSystemManager = await AuthDataService.IsSystemManager();
        v.mainObj.id = UJ.getParameterByName("mid");
        v.relationType = UJ.getParameterByName("t");
        if (v.relationType == 200) {
            v.pageTitle = "輔具列表-可加租耗材&配件"
        } else if (v.relationType == 300) {
            v.pageTitle = "輔具列表-可加購配件"
        }

        var res = await ProductMainDataService.Get(v.mainObj.id);
        if (res.status == RESPONSE_STATUS.OK) {
            v.mainObj = res.data;

            v.ltCategory = await UniversalDataService.GetProductCategory();
            v.getList();
            
        } 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: {
        async getList() {
            var v = this;
            v.isLoading = true;

            let res = await ProductRelationDataService.GetList(v.mainObj.id, v.relationType);
            if (res.status == RESPONSE_STATUS.OK) {
                v.list = res.data;
                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.isBindFancybox = false;//重新綁定fancybox
                v.isLoading = false;
            } else {
                var data = res.data;
                showErrMsg(data.message, "", "", function () {
                    if (data.enMessage == enMessage.NotFound) {
                        UJ.BackToList();
                    }
                });
            }
        },
        SelectAll(ckd) {
            var v = this;
            v.checkAll = ckd;
            v.ltChecked = [];
            v.list.forEach(async function (item) {
                if (ckd) {
                    v.ltChecked.push(item.id);
                }
            });
        },
        isExistsCheckList(productId) {
            var v = this;
            var isExists = false;
            v.ltChecked.forEach(function (id) {
                if (id == productId) {
                    isExists = true;
                }
            });

            return isExists;
        },
        changeCKD(item, ckd) {
            var v = this;

            var isExists = false;
            v.ltChecked.forEach(function (id, idx) {
                if (id == item.id) {
                    isExists = true;
                    if (!ckd) {
                        v.ltChecked.splice(idx, 1);
                    }
                }
            });

            if (ckd && !isExists) {
                v.ltChecked.push(item.id);
            }
            if (!ckd) {
                v.checkAll = false;
            }
        },
        async deleteItems() {
            var v = this;
            if (v.ltChecked != null && v.ltChecked.length > 0) {
                let res = await ProductRelationDataService.RemoveProduct({ productId: v.mainObj.id, enRelationType: v.relationType, list: v.ltChecked });
                if (res.status != RESPONSE_STATUS.OK) {
                    alert('刪除失敗');
                    v.isFinishRequest = true;
                    return;
                } else {
                    v.checkAll = false;
                    v.getList(1);
                }
            } else {
                showErrMsg("請勾選要加入的項目", "", "warning", function () {
                    return false;
                });
            }
        },
        async updateQty() {
            var v = this;
            let res = await ProductRelationDataService.UpdateQty({ productId: v.mainObj.id, enRelationType: v.relationType, list: v.list });
            if (res.status != RESPONSE_STATUS.OK) {
                alert('數量更新失敗');
                v.isFinishRequest = true;
                return;
            } else {
                alert('數量已儲存');
                v.getList(1);
            }
        }
    }
});


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