<template>
  <div :style="{'height':(heightValue ? (heightValue + +'px') : 'auto')}">
    <flow-res-preview  v-if="isShow" :default-tab="defaultTab" :flow-id="flowId" :operate-root-arr="operateRootArr" :data-permis="dataPermis">
    </flow-res-preview>
  </div>
</template>
<script>
export default {
  name: "work-flow",
  props:{
    flowId:{type:String,default:"",required:true},//工作流id
    tabSettingArr:{type:Array,default:() => {return [];},required:true},//页签
    heightValue:{type:String,default:"",required:true},//高度
  },
  data(){
    return {
      isShow:false,
      operateRootArr:[],//操作权限
      dataPermis:"",//数据权限
      // flowObj:{},//工作流对象
      defaultTab:"pending",
    };
  },
  created(){
   // this.getTabSettingArr(this.flowId,this.tabSettingArr);
   // this.getFlowObj(this.flowId);
   this.getResourceData(this.flowId,this.tabSettingArr);
  },
  methods:{
    /*getTabSettingArr(id,tabArr){//获取选项卡
      let dataArr = [];
      this.axios.post(this.api.plFlowDefine.getById,{"id":id}).then(res=>{
        if(res && res.data){
          let data =  JSON.parse(res.data.tabSetting) || [];
          data.forEach((item,index)=>{
            tabArr.forEach((data,index)=>{
              if(item.name == data){
                dataArr.push(item);
              }
            })
          })
        }
      });
      // this.defaultTab =  JSON.stringify(dataArr);
      this.defaultTab="pending";
    },*/
    getFlowObj(id){//工作流对象
      let data = {};
      this.axios.post(this.api.plFlowDefine.getById,{"id":id}).then(res=>{
        if(res && res.data){
          data = res.data || {};
        }
      });
      this.flowObj =  data;
    },

    getResourceData(rKey,tabArr){//获取资源
      let resourceObj = {};//当前资源池
      let operateRootArr = [];
      let dataPermis ="";
      let menuId = this.$route.query.menuId;
      this.axios.post(this.api.resources.findResources,{'rSource':'flow','rStatus':'1','rKey':rKey}).then(res=>{//获取绑定资源数据
        if(res.data && res.data.operateObj){
          resourceObj = res.data.operateObj[0];
          let loginUser = this.CACHE_GLOBAL.getLoginUser(); //获取当前用户
          //根据 当前用户查询 数据权限和操作权限
          //判断当前用户类型
          /*if(loginUser.userType == 'sup'){*/ //开发者
            //查询数据权限和操作权限
            //操作权限
            let checkOperateKeysRoot = []; //操作权限 根节点
            let checkOperateKeysLeaf = []; //操作权限 叶子节点
            let operatePermisList = resourceObj.operatePermisList;
            operatePermisList.forEach(item =>{
              //处理操作权限
              if(item.powerCode=='children'){//叶子节点
                checkOperateKeysLeaf.push(item);
              }else{
                checkOperateKeysRoot.push(item);
              }
            });
            operateRootArr=checkOperateKeysRoot;

            //数据权限
            let dataPermisList = resourceObj.dataPermisList || [];
            dataPermisList.forEach(data => {
              dataPermis += "," + data.powerKey;
            });
            if(dataPermis){
              dataPermis = dataPermis.substring(1);
            }
          let dataArr = [];
          let falg = false;
          let vm = this;
          operateRootArr.forEach(item=>{
            tabArr.forEach(data=>{
              if(item.powerName == data){
                if(item.powerCode == 'pending'){//选中的页签有待处理defaultTab就不赋值
                  falg = true;
                }
                dataArr.push(item);
              }
            });
          });
          if(!falg && dataArr.length>0){//选中的页签没有待处理defaultTab赋值第一个tab
            this.defaultTab = dataArr[0].powerCode;
          }
          this.operateRootArr = dataArr;
          this.dataPermis = dataPermis;
          this.isShow = true;
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
