<template>
  <div :id="winId" class="hidden go_win_obj" :style="extendStyle">
      <div :id="msgIdStr"  class="toast-ctl toast-top-right" aria-live="polite" role="alert" :style="getMaxZIndex" style="position: absolute"></div>
    <Row class="wh100b">
      <slot></slot>
    </Row>
  </div>
</template>

<script>
import { GoingUtils, AssertUtils } from "goingutils";
import WinMixins from "../ctlMixins/WinMixins";

export default {
    name: "go-add-mod-win",
    mixins: [WinMixins],
    data() {
        return {
            winId: GoingUtils.getUUid(32),
            msgIdStr:GoingUtils.getUUid(10)
        };
    },
    props: {
        loadByBackFun: { type: Boolean, default: false },
    //是否相应底部工具栏
        footerBar: { type: Boolean, default: true },
    //  是否需要继续按钮
        continueBtn: { type: Boolean, default: true },
    //  是否需要确定按钮
        confirmBtn: { type: Boolean, default: true },
    //  窗口高度
        height: { type: [String, Number], default: 400 },
    //  窗口宽度
        width: { type: [String, Number], default: 450 },
    //  窗口标题
        title: { type: String, default: "" },
        value: {},
        recordId: { type: String, default: "" },
        loadRecordAction: { type: String, default: "" },
    //  操作类型 主要是修改 、删除2种类型
        optionType: { type: String, default: "add" },
    //  表格对象的名称
        gridObjName: { type: String, default: "gridObj" },
        modLoadData: { type: Boolean, default: true }
    },
    computed: {
    //扩展样式信息
        extendStyle() {
            let json = {};
            json["width"] = this.width + "px";
            let curHeight = this.height;
            if (this.footerBar) {
                curHeight = parseFloat(this.height) - 40;
            }
            curHeight = curHeight - 42;
            json["height"] = curHeight + "px";
      //缓存宽高
            let winInfo = { height: curHeight, width: this.width };
            GoingUtils.setTotalParams(this.winId, JSON.stringify(winInfo));
            return json;
        }
    },
    methods: {
    //获取窗口的id
        getWinId() {
            return this.winId;
        },
        hideWin() {
      //    关闭窗口
            this.$WinUtils.closeParentCurWin();
        },
        showWin() {
            let opts = {
                width: this.width,
                height: this.height,
                url: this.winId,
                showBottomBar: this.footerBar,
                title: this.title,
                anim: null
            };
      //关闭函数
            opts["closeWin"] = () => {
                this.$emit("input", false);
                this.$emit("close", true);
            };
      //判断是否存在 继续的按钮
            if (this.continueBtn) {
                opts["continueBtn"] = (winId, closeWinFun) => {
                    if (this.optionType === "add") {
                        this.addRecordToDb(winId, closeWinFun, "continue");
                    } else {
                        this.modRecordToDb(winId, closeWinFun, "continue");
                    }

                    this.$emit("continueBtn", false);
                };
            }
      //判断是否存在确定按钮
            if (this.confirmBtn) {
                opts["confirmBtn"] = (winId, closeWinFun) => {
                    if (this.optionType === "add") {
                        this.addRecordToDb(winId, closeWinFun, "confirm");
                    } else if(this.optionType === "mod"){
                        this.modRecordToDb(winId, closeWinFun, "confirm");
                    }else{
                        this.$emit(this.optionType, {winId, closeWinFun});
                    }
                    this.$emit("confirmBtn", false);
                };
            }
      //如果新增则弹出窗口  如果是修改 先加载数据再弹出窗口
            if (this.optionType === "add") {
                this.$WinUtils.showInfoOnWin(opts);
            } else {
                this.loadDataAndOpenWin(opts);
            }
        },
    //加载数据 并 打开窗口
        loadDataAndOpenWin(opts) {
            let loadRecordId = this.recordId;
            if (loadRecordId.split(";").length > 1) {
                GoingUtils.showErrorMessage("每次只能修改一条记录，请确认！");
                return void 0;
            }
      //如果需要加载数据 则需要定义选择的记录的id咯
            if (this.modLoadData) {
                if (AssertUtils.isNotEmpty(loadRecordId)) {
                    opts["continueBtn"] = null;
                    //通过回调方法来加载数据
                    if(this.loadByBackFun){
                        //是否需要加载数据
                        this.$emit("loadRecordDataAction", {loadRecordId,backFun:(data)=>{
                                //修改操作 加载数据完成后的操作
                            this.$emit("modAfterLoadData", { data});
                            this.$WinUtils.showInfoOnWin(opts);
                        }});
                       
                    }else{
                        //是否需要加载数据
                        this.$store.dispatch(this.getActionName(), {
                            recordId: loadRecordId,
                            backFun: (data) => {
                                this.$emit('loadRecordAfter',{data});
                                this.$WinUtils.showInfoOnWin(opts);
                            }
                        });
                    }

                } else {
                    GoingUtils.showErrorMessage("请定义修改的recordId");
                }
            } else {
                opts["continueBtn"] = null;
        //可能不需要加载数据 只是显示窗口而已
                this.$WinUtils.showInfoOnWin(opts);
            }
        },
    //获取新增的action的名字
        getActionName() {
            let addActionAry = this.loadRecordAction.split(".");
            return addActionAry[0]+ "/" +addActionAry[1];
        },
        modRecordToDb(winId, closeWinFun, btnType) {
      //执行校验函数 如果返回true才需要继续执行
            this.$emit("modRecordToDb", { winId, closeWinFun, btnType });
        },
    //保存数据到数据库
        addRecordToDb(winId, closeWinFun, btnType) {
      //执行校验函数 如果返回true才需要继续执行
            this.$emit("addRecordToDb", { winId, closeWinFun, btnType });
        }
    },
    mounted() {},
    watch: {
        value(newVal) {
            if (newVal) {
                this.showWin();
            } else {
                this.hideWin();
            }
        }
    }
};
</script>
