<template>
    <!---自定义表单-->
    <div style="height: 100%">
        <list-view-preview-remid v-if="isShow"
                                 :form-id="formId"
                                 :isCloudView="isCloudView"
                                 :condition-id="conditionId"
                                 :operate-authority="operateAuthority"
                                 :parentFormData="parentFormData"
                                 :compositeObj="compositeObj"
                                 :isCompositeView="isCompositeView"
                                 :refreshPreview="refreshPreview"
                                 :compositeFormOperate="compositeFormOperate"
                                 :list-view-id="listViewId">
        </list-view-preview-remid>
    </div>
</template>

<script>
    export default {
        name: "formMenuRouter",
        components: {
            ListViewPreviewRemid: () => import("./pageShow/listViewPreviewRemid"),
        },
        props: {
            cresourceId: { //资源id
                type: String,
                default: ''
            },
            cmenuId:{//菜单id
                type: String,
                default: ''
            },
            isCloudView:{
                type: Boolean,
                default: false
            },
            parentFormData:{type:Object,default:() => {return {};}}, //主表单数据
            compositeObj:{type:Object,default:() => {return {};}},//复合视图表单变化对象
            isCompositeView:{type: Boolean, default: false,required:false},//是否是复合视图组件
            compositeFormOperate:{type:String,default:''}, //复合视图表单操作状态
            refreshPreview:{type:String,default:''},
            loginUser:{type:Object,default:null}, // 登录用户
            loginDept:{type:Object,default:null}, // 登录用户组织部门信息
        },
        data(){
            return{
                resourceId:this.$route.params.resourceId,
                menuId:this.$route.query.menuId,
                optType:this.$route.query.optType,
                operatePermis: '', //根据 当前用户查询权限
                dataPermis:'', //根据当前用户查询权限
                resourceObj:{}, //自定义表单资源池
                formId:'',
                listViewId:'', //列表id
                conditionId: '',//条件定义id 数据权限
                operateAuthority:{}, //操作权限
                isShow:false, //展示
                menuObj:{},
            }
        },
        watch:{
            /*'$route.params.resourceId' (to, from) {
                this.getResources().then(() => { //获取资源池
                    this.executShow();
                });
            }*/
        },
        created(){
            if(!this.resourceId){
                this.resourceId = this.cresourceId;
            }
            if((!this.menuId) || (this.menuId == "0")){
                this.menuId = this.cmenuId;
            }
            if(this.loginUser){
                this.CACHE_GLOBAL.setLoginUser(this.loginUser);
            }
            if(this.loginDept){
                this.CACHE_GLOBAL.setLoginDept(this.loginDept);
            }
            this.executShow();//展示
        },
        methods:{
            //自定义表单展示
            executShow(){
                 // 跟据 menuId 和 当前用
                 // 传递
                this.isShow = false;
                //获取资源池对象
                this.RES_GLOBAL.getById(this.resourceId).then(resData => {
                    this.resourceObj = resData;
                    let resourceObj = this.resourceObj;
                    let listViewDefineKey = resourceObj.rkey || ''; //列表 definkeKey
                    let formDefineKey = resourceObj.rclassId || ''; //列表deifineKey
                    //根据 formDefineKey 查询 最新的的
                    this.FORM_GLOBAL.findListNewVersion({formDefineKey:formDefineKey,defineKey:listViewDefineKey}).then(res => {
                        if(res && res.data){
                            this.listViewId = res.data.id || ''; //表单视图的 definkeKey
                            this.formId = res.data.formId || ''; //表单id definkeKey
                            this.operateAuthority = {};
                            this.conditionId = "";
                            let menuObj = {};
                            if(window.menuIdToMenuObj){
                                menuObj = window.menuIdToMenuObj[this.menuId];
                            }else{
                                //新窗口打开时查询菜单信息
                                this.spcpFormAxios.post(this.api.menu.getById,{id:this.menuId}).then(function (res) {
                                    if(res && res.data){
                                        menuObj = res.data;
                                    }
                                });
                            }
                            let loginUser = this.CACHE_GLOBAL.getLoginUser(); //获取当前用户
                            //根据 当前用户查询 数据权限和操作权限
                            //判断当前用户类型
                            if(loginUser.userType == 'sup' || this.isCloudView){ //开发者
                                //查询数据权限和操作权限
                                //操作权限
                                let dataArr = []; //操作权限
                                let operatePermisList = ((menuObj || {}).operatePermis || '').split(","); //操作权限
                                operatePermisList.forEach(key =>{
                                    (resourceObj.operatePermisList || []).forEach(item =>{
                                        if(key == item.id){
                                            this.operateAuthority[item.powerKey] = true; //操作权限
                                        }
                                    });
                                });
                                let dataPermisList = ((menuObj || {}).dataPermis || '').split(",");//数据权限
                                dataPermisList.forEach(key =>{
                                    (resourceObj.dataPermisList || []).forEach(item =>{
                                        if(key == item.id){
                                            dataArr.push(item.powerKey); //数据权限 arr
                                        }
                                    });
                                });
                                this.conditionId = dataArr.join(",");
                                this.isShow = true;
                            }else{ //根据 resourceId查询当前用户是否存在  资源权限  resourceId 如果存在获取数据权限和操作权限
                                this.RES_GLOBAL.getUserOwnMenuOpt().then(datas =>{
                                    if(datas){
                                        let roleOptArr = [];
                                        (datas || []).forEach(item =>{
                                            // if(this.optType == 'source'){
                                            //     if(this.resourceId == item.menuId){ //数据资源
                                            //         roleOptArr.push(item);
                                            //     }
                                            // }
                                            // else if(this.optType == 'menu'){
                                            if(this.menuId == item.menuId){
                                                roleOptArr.push(item);
                                            }
                                            // }
                                        });
                                        if(roleOptArr.length > 0){
                                            //遍历权限
                                            let optPowerArr = []; //操作权限
                                            let dataPowerArr = []; //数据权限
                                            roleOptArr.forEach(item =>{
                                                if(item.optPower){
                                                    optPowerArr = optPowerArr.concat(item.optPower.split(",") || []);
                                                }
                                                if (item.dataPower){
                                                    dataPowerArr = dataPowerArr.concat(item.dataPower.split(",") || []);
                                                }
                                            });
                                            //去重
                                            let optPowerSet = new Set(optPowerArr);
                                            optPowerArr = Array.from(optPowerSet);
                                            //去重
                                            let dataPowerSet = new Set(dataPowerArr);
                                            dataPowerArr = Array.from(dataPowerSet);
                                            let operatePermisList = ((menuObj || {}).operatePermis || '').split(","); //菜单操作权限
                                            (resourceObj.operatePermisList || []).forEach(item =>{
                                                // 菜单与角色权限同时存在时可以显示
                                                if((optPowerArr || []) .indexOf(item.id) > -1 && operatePermisList.indexOf(item.id) > -1){
                                                    this.operateAuthority[item.powerKey] = true; //操作权限
                                                }
                                            });
                                            let dataPowerKeyArr = [];
                                            let dataPermisList = ((menuObj || {}).dataPermis || '').split(",");//菜单数据权限
                                            (resourceObj.dataPermisList || []).forEach(item =>{ //数据权限
                                                if((dataPowerArr || []) .indexOf(item.id) > -1 && dataPermisList.indexOf(item.id) > -1){
                                                    dataPowerKeyArr.push(item.powerKey); //数据权限 arr
                                                }
                                            });
                                            this.conditionId = dataPowerKeyArr.join(",");
                                            this.isShow = true;
                                        }else{
                                            //强制跳转到首页
                                            let url = "/commonHome/welcome";
                                            this.routerPush(url);
                                        }
                                    }
                                });
                            }
                        }
                    });
                });
            },

            /**
             * 请求跳转
             */
            routerPush(url) {
                this.$router.push({
                    path: url
                });

            },

        }
    }
</script>

<style scoped>

</style>
