<template>
    <span></span>
</template>

<script>
    import WinMixins from "../ctlMixins/WinMixins";
    import {AssertUtils,GoingUtils} from 'goingutils';
    import goDialog from "../../jsCom/go-dialog";


    export default {
        name: "go-win-acl",
        mixins: [WinMixins],
        data() {
            return {
                treeObj:null,
            };
        },
        props: {
            //返回的id是否需要带类型
            hasType: {
                type: Boolean,
                default: false
            },
            initDatas:{ type: Array, default: ()=>[]},
            moreSel:{ type: Boolean, default: true },
            tabs:{ type: Array, default: ()=>{
                return [{id: "departUser", active: true, accountSel: false}];
            }},
        },
        computed: {

        },
        methods: {
            //点击确定按钮
            executeConfirm(vueObj, aclCtl) {
                const count=this.moreSel?100:1;
                //获取返回的ids
                let {selIds, selNames, accountId, backFlag,dataFroms} = aclCtl.getBackInfos(count, this.hasType);
                this.dataFroms=dataFroms;
                //如果返回false 则表示出现了提示信息
                if (backFlag === false) {
                    return backFlag;
                }
                //对选择的数据进行校验
                if (AssertUtils.isNotNull(this.validateSelRecord) && AssertUtils.isFunction(this.validateSelRecord)) {
                    let flat = this.validateSelRecord({selIds: selIds.join(""), selNames});
                    if (!flat) {
                        return flat;
                    }
                }
                //需要返回数组的情况 才进行操作
                if (AssertUtils.isNotNull(selNames) && AssertUtils.isArray(selNames)) {
                    this.$emit('setValues',{selIds, selNames, accountId});
                }
                return true;
            },
            showWin(){
                const count=this.moreSel?100:1;

                let config={
                    count:count,
                    //标题
                    title: this.title,
                    //显示的tab页
                    tabs: this.tabs
                };
                //确定操作的事件
                config["confirmBtn"] = (aclCtl) => {
                    var flag = this.executeConfirm(this, aclCtl);
                    //如果返回了false 则表示有提示信息出现
                    if (flag !== false) {
                        //关闭窗口
                        this.$WinUtils.closeParentCurWin();
                        this.$emit("input", false);
                        this.$emit("close", true);
                    }
                };
                //关闭窗口的事件
                config["closeWin"]=() => {
                    this.$emit("input", false);
                    this.$emit("close", true);
                };

                let {ids,names,dataForms}=this.getInitDatas(this.initDatas);
                if(typeof(ids)==='string'){
                    ids=ids.split(GoingUtils.relChart);
                }
                if(typeof(names)==='string'){
                    names=names.split(GoingUtils.relChart);
                }
                //初始化函数的事件
                config["initFun"] = ()=>{
                    return {
                        ids: ids.join(GoingUtils.relChart),
                        names: names.join(GoingUtils.relChart),
                        dataForms,
                        hasType:this.hasType,
                        tabs: this.tabs
                    };
                };
                //设置授权窗口信息
                goDialog.attachAclWin(config, this);
            },
            hideWin(){
                this.$WinUtils.closeParentCurWin();
            },
            //获取初始化数据
            getInitDatas(){
                let ids=[],names=[],dataForms=[];
                this.initDatas.forEach((item)=>{
                    ids.push(item["id"]);
                    names.push(item["name"]);
                    dataForms.push(item["dataForm"]);
                })
                return {ids:ids.join(";"),names:names.join(";"),dataForms:dataForms.join(";")};
            },
        },
        mounted() {
            if(this.value){
                this.showWin();
            }
        },
        watch: {
            treeUrl(newVal) {
                if(this.treeObj!=null&&AssertUtils.isNotEmpty(newVal)){
                    this.treeObj.flushTreeByUrl({url:newVal});
                }
            },
            value(newVal) {
                if (newVal) {
                    this.showWin();
                } else {
                    this.hideWin();
                }

            }
        }
    };
</script>
