<template>
    <!---自定义表单-->
    <div style="height: 100%;margin-top: 10px;margin-bottom: 10px;overflow-y: hidden">
        <list-view-preview-remid v-if="isShow"
                                 :form-id="formId"
                                 :condition-id="conditionId"
                                 :operate-authority="operateAuthority"
                                 :list-view-id="listViewId"
                                 :extra-condition="rparam"
                                 :equilibrium-data="equilibriumData">
        </list-view-preview-remid>
    </div>
</template>

<script>
    import ListViewPreviewRemid from "./pageShow/listViewPreviewRemid";
    export default {
        name: "formResRouter",
        components: {ListViewPreviewRemid},
        props:{
            loginUser:{type:Object,default:null}, // 登录用户
            loginDept:{type:Object,default:null}, // 登录用户组织部门信息
        },
        data(){
            return{
                resourceId:this.$route.params.resourceId,
                rparam:this.$route.params.rparam,
                equilibriumData:this.$route.query,
                optType:this.$route.query.optType,
                menuId:'',
                operatePermis: '', //根据 当前用户查询权限
                dataPermis:'', //根据当前用户查询权限
                resourceObj:{}, //自定义表单资源池
                formId:'',
                listViewId:'', //列表id
                conditionId: '',//条件定义id 数据权限
                operateAuthority:{}, //操作权限
                isShow:false, //展示
                menuObj:{},
            }
        },
        watch:{
            '$route.params.resourceId' (to, from) {
                this.getResources();
            }
        },
        created(){
            this.isShow = false;
            if(this.loginUser){
                this.CACHE_GLOBAL.setLoginUser(this.loginUser);
            }
            if(this.loginDept){
                this.CACHE_GLOBAL.setLoginDept(this.loginDept);
            }
            this.getResources();
        },
        methods:{
            //自定义表单展示
            executShow(){
             // 跟据 menuId 和 当前用
             // 传递
              this.isShow = false;
              let resourceObj = this.resourceObj;
              // this.formId = resourceObj.rclassId || '';
              // this.listViewId = resourceObj.rkey || '';
              let listViewDefineKey = resourceObj.rkey || ''; //列表 definkeKey
              let formDefineKey = resourceObj.rclassId || ''; //列表deifineKey
              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 = window.resourceToMemu[this.resourceId];
                      // this.menuId = menuObj.id;
                      //当前数据资源
                      let loginUser = this.CACHE_GLOBAL.getLoginUser(); //获取当前用户
                      //根据 当前用户查询 数据权限和操作权限
                      //判断当前用户类型
                      // if(loginUser.userType == 'sup'){ //开发者
                      //查询数据权限和操作权限
                      //操作权限
                      let dataArr = []; //操作权限
                      (resourceObj.operatePermisList || []).forEach(item =>{
                          this.operateAuthority[item.powerKey] = true; //操作权限
                      });
                      (resourceObj.dataPermisList || []).forEach(item =>{
                          dataArr.push(item.powerKey); //数据权限 arr
                      });

                      this.conditionId = dataArr.join(",");
                      this.isShow = true;
                  }
              });

              // }else{ //根据 resourceId查询当前用户是否存在  资源权限  resourceId 如果存在获取数据权限和操作权限
              //     this.axios.post(this.api.auth.getOwnMenuOpt,{userId:loginUser.id,userType:loginUser.userType,optType:'source'}).then(res =>{
              //         if(res && res.data){
              //             let roleOptArr = [];
              //             (res.data || []).forEach(item =>{
              //                 if(this.resourceId == 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);
              //                 (resourceObj.operatePermisList || []).forEach(item =>{
              //                       if((optPowerArr || []) .indexOf(item.id) > -1){
              //                           this.operateAuthority[item.powerKey] = true; //操作权限
              //                       }
              //                 });
              //                 let dataPowerKeyArr = [];
              //                 (resourceObj.dataPermisList || []).forEach(item =>{ //数据权限
              //                     if((dataPowerArr || []) .indexOf(item.id) > -1){
              //                         dataPowerKeyArr.push(item.powerKey); //数据权限 arr
              //                     }
              //                 });
              //                 this.conditionId = dataPowerKeyArr.join(",");
              //                 this.isShow = true;
              //             }else{
              //                 //强制跳转到首页
              //                 let url = "/commonHome/welcome";
              //                 this.routerPush(url);
              //             }
              //         }

              //     });
              // }

            },

            /**
             * 获取资源池对象
             */
            getResources(){
                return new Promise(resolve => {
                    this.RES_GLOBAL.getById(this.resourceId).then(resData => {
                        this.resourceObj = resData; //当前资源池
                        this.executShow();
                        resolve();
                    });
                })
                // let res = await this.axios.post(this.api.resources.findResources,{id:this.resourceId}); //查询自定义表单中资源池数据
                // if(res.data && res.data.operateObj){

                // }
            },


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

            },

        }
    }
</script>

<style scoped>

</style>
