<template>
  <div class="data-info">
    <el-card>
      <el-form :model="dataForm" label-width="140px">

        <el-form-item label="角色名称:">
          <el-input v-model="dataForm.name"></el-input>
        </el-form-item>
        <el-form-item label="是否为管理员:">
          <el-select v-model="dataForm.is_admin"  placeholder="请选择是否为管理员" size="large"  style="width:100%;">
              <el-option
                v-for="item in adminOptions"
                :key="item.status"
                :label="item.name"
                :value="item.status"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="权限配置:">
          <div style="width:100%;height:150px;overflow: hidden; border:1px solid #CCC;overflow-y: auto;padding:10px;">
             <el-tree
                 ref="nodeTree"
                 style="max-width: 600px;"
                 :props="defaultProps"
                 :data="treeData"
                 default-expand-all
                 node-key="value"
                 show-checkbox
               />
            </div>
        </el-form-item>

        <el-form-item label="模板分类授权:">
          <div style="width:100%;height:150px;overflow: hidden; border:1px solid #CCC;overflow-y: auto;padding:10px;">
             <el-tree
                 ref="templateCate"
                 style="max-width: 600px;"
                 :props="defaultProps"
                 :data="templateCate"
                 default-expand-all
                 node-key="value"
                 show-checkbox
               />
            </div>
        </el-form-item>
        <el-form-item label="文档分类授权:">
          <div style="width:100%;height:150px;overflow: hidden; border:1px solid #CCC;overflow-y: auto;padding:10px;">
             <el-tree
                 ref="docCate"
                 style="max-width: 600px;"
                 :props="defaultProps"
                 :data="documentCate"
                 default-expand-all
                 node-key="value"
                 show-checkbox
               />
            </div>
        </el-form-item>
        <el-form-item label="原始数据分类授权:">
          <div style="width:100%;height:150px;overflow: hidden; border:1px solid #CCC;overflow-y: auto;padding:10px;">
             <el-tree
                 ref="sourceDataTree"
                 style="max-width: 600px;"
                 :props="defaultProps"
                 :data="sourceDataCate"
                 default-expand-all
                 node-key="value"
                 show-checkbox
               />
            </div>
        </el-form-item>
        <el-form-item label="角色状态:">
          <el-select v-model="dataForm.status"  placeholder="请选择状态" size="large"  style="width:100%;">
              <el-option
                v-for="item in statusOptions"
                :key="item.status"
                :label="item.name"
                :value="item.status"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="介绍:">
            <el-input type="textarea" v-model="dataForm.intro"></el-input>
        </el-form-item>
      </el-form>

    </el-card>


      <div style="text-align:right; margin-top:20px;">
          <el-button type="warning" @click="btnSave">确认保存</el-button>
      </div>
  </div>
</template>

<script>
  import {createAdminRole,updateAdminRole,getAdminRoleInfo} from'@/api/AdminRole'

  import {searchDocumentCategory} from "@/api/document"
  import {searchSourceDataCategory} from "@/api/sourceData"
  import {searchAdminAuth} from"@/api/AdminAuth"
  import {getAllCategory} from "@/api/template"
  export default{
    emits:['onClose'],
    props:{
      id:{
        type:Number,
        default:0
      },
    },
    watch:{
      id:{
        handler(v){
            let _this=this;
            if(v==null || v<=0)return;
            _this.getInfo(v);
        },
        immediate: true,
        deep: true
      }
    },
    data(){
      return{
        activeName:'base',
        defaultProps :{
           children: 'children',
           label: 'label',
        },
        treeData:[],
        dataForm:{
          id:'',
          name:'',
          is_admin:'',
          intro:'',
          status:5
        },
        statusOptions:[
          {
            status:5,
            name:'启用'
          },
          {
            status:6,
            name:'禁用'
          },

        ],
        adminOptions:[
          {
            status:1,
            name:'是'
          },
          {
            status:2,
            name:'否'
          },
        ],
        pickedItem:[],
        templateCate:[],
        documentCate:[],
        sourceDataCate:[],

      }
    },

    mounted() {

      this.initNodeList();
      this.initTemplateCate();
      this.initDocCategory();
      this.initSourceDataCate();
    },
    methods:{


      initSourceDataCate(){
          let _this=this;
          searchSourceDataCategory({page:1,pageSize:9999,status:5}).then(res=>{
            if(res.status!=200)return;
            _this.sourceDataCate=res.data.dataList.map(res=>{
              let item={
                id:res.id,
                label:res.name,
                name:res.name,
                children:[
                        {
                          action:"view",
                          cateId:res.id,
                          value:'view-source-data'+res.id,
                          type:'sourceCate',
                          isMain:true,
                          name:'查看',
                          label:'查看'
                        },
                        {
                          action:"create",
                          cateId:res.id,
                          value:'create-source-data'+res.id,
                          type:'sourceCate',
                          isMain:true,
                          name:'创建',
                          label:'创建'
                        },
                        {
                          action:"edit",
                          cateId:res.id,
                          value:'edit-source-data'+res.id,
                          type:'sourceCate',
                          isMain:true,
                          name:'编辑',
                          label:'编辑'
                        },
                        {
                          action:"delete",
                          cateId:res.id,
                          value:"delete-source-data"+res.id,
                          type:'sourceCate',
                          isMain:true,
                          name:'删除',
                          label:'删除'
                        },
                      ]
              }
              return item;
            })
          })

      },
      initDocCategory(){
        let _this=this;
        searchDocumentCategory({page:1,pageSize:9999,status:5}).then(res=>{
          if(res.status!=200)return;
          _this.documentCate=res.data.dataList.map(res=>{
            let item={
              id:res.id,
              label:res.name,
              name:res.name,
              children:[
                      {
                        action:"view",
                        cateId:res.id,
                        type:'docCate',
                        value:"view-doc-cate-"+res.id,
                        isMain:true,
                        name:'查看',
                        label:'查看'
                      },
                      {
                        action:"create",
                        cateId:res.id,
                        type:'docCate',
                        value:"create-doc-cate-"+res.id,
                        isMain:true,
                        name:'创建',
                        label:'创建'
                      },
                      {
                        action:"edit",
                        cateId:res.id,
                        type:'docCateg',
                        value:"edit-doc-cate-"+res.id,
                        isMain:true,
                        name:'编辑',
                        label:'编辑'
                      },
                      {
                        action:"delete",
                        cateId:res.id,
                        type:'docCateg',
                        value:"delete-doc-cate-"+res.id,
                        isMain:true,
                        name:'删除',
                        label:'删除'
                      },
                    ]
            }
            return item;
          })
        })
      },


      //初始化模板分类
      initTemplateCate(){
        let _this=this;
        getAllCategory({}).then(res=>{
          if(res.status!=200) return;
          _this.templateCate=res.data.map((item)=>{
              let data={
                id:item.id,
                label:item.name,
                name:item.name,
                children:item.children.map(subItem=>{
                  return{
                    id:subItem.id,
                    name:subItem.name,
                    label:subItem.name,
                    children:[
                      {
                        action:"view",
                        cateId:subItem.id,
                        pCateId:item.id,
                        isMain:true,
                        value:"view-template-cate-"+res.id,
                        type:'templateCategory',
                        name:'捍看',
                        label:'查看'
                      },
                      {
                        action:"create",
                        cateId:subItem.id,
                        pCateId:item.id,
                        isMain:true,
                        value:"create-template-cate-"+res.id,
                        type:'templateCategory',
                        name:'创建',
                        label:'创建'
                      },
                      {
                        action:"edit",
                        cateId:subItem.id,
                        pCateId:item.id,
                        isMain:true,
                        value:"edit-template-cate-"+res.id,
                        type:'templateCategory',
                        name:'编辑',
                        label:'编辑'
                      },
                      {
                        action:"delete",
                        cateId:subItem.id,
                        pCateId:item.id,
                        isMain:true,
                        value:"delete-template-cate-"+res.id,
                        type:'templateCategory',
                        name:'删除',
                        label:'删除'
                      },
                    ]
                  }
                })
              }
              return data;
          });
        })
      },

      initNodeList(){
        let _this=this;
        searchAdminAuth({page:1,pageSize:999,status:5}).then(res=>{
           if(res.status!=200){
             _this.treeData=[];
             return;
           }
           let data=[
             {
               value:1,
               label:'项目管理',
               children:[],
               sub:false
             },
             {
               value:2,
               label:'客户管理',
               children:[],
               sub:false
             },
             {
               value:3,
               label:'产品管理',
               children:[],
               sub:false
             },
             {
               value:4,
               label:'文档管理',
               children:[],
               sub:false
             },
             {
               value:5,
               label:'系统设置',
               children:[],
               sub:false
             },
           ];

           let viewAuth=res.data.dataList.filter(o=>o.type!=99 && o.type!=999)
           let controlAuth=res.data.dataList.filter(o=>o.type==999);

            let items=[];
            data.map(item=>{
               item.children=viewAuth.filter(o=>o.type==item.value).map((subItem)=>{
                   let sub={
                     value:subItem.id,
                     label:subItem.name,
                     sub:true,
                     children:controlAuth.filter(o=>o.parent_id==subItem.id).map(res=>{
                       let item={
                           value:res.id,
                           label:res.name,
                           sub:true,
                         };
                         return item;
                     })
                   }
                   return sub;
               });
               if(item.children.length>0){
                  items.push(item);
               }
           })
           _this.treeData=items;
        })
      },

      //保存更新
      btnSave(e){
          let _this=this;
          let items=_this.$refs.nodeTree.getCheckedNodes();

          let templateCate=_this.$refs.templateCate.getCheckedNodes();
          let docCate=_this.$refs.docCate.getCheckedNodes();
          let sourceDataTree=_this.$refs.sourceDataTree.getCheckedNodes();


          templateCate=templateCate.filter(o=>o.isMain);


          let pickedItem=items.filter(o=>o.sub==true);
          _this.dataForm.authList=JSON.stringify(pickedItem);

          _this.dataForm.template_cate_auth=JSON.stringify(templateCate);
          _this.dataForm.doc_cate_auth=JSON.stringify(docCate);
          _this.dataForm.source_cate_auth=JSON.stringify(sourceDataTree);


          let data={..._this.dataForm};
          data.attrs=JSON.stringify(data.attrs);
          if(_this.dataForm.id>0){
            updateAdminRole(data).then(res=>{
              if(res.status!=200)return;
              _this.$alert("更新角色信息成功");
              _this.$emit("onClose")
            })
          }else{
            createAdminRole(data).then(res=>{
              if(res.status!=200)return;
              _this.$alert("创建角色信息成功");
              _this.$emit("onClose")
            })
          }
      },

      //产品详情
      getInfo(id){
          let _this=this;
          let par={
            id:id,
          };
          getAdminRoleInfo(par).then(res=>{
              if(!res)return;
              if(res.status!=200)return;
              _this.dataForm=res.data;
              if(res.data.linkData.length>0){
                _this.$refs.nodeTree.setCheckedNodes(res.data.linkData);
              }else{
                _this.$refs.nodeTree.setCheckedNodes([]);
              }


              let tempCate=JSON.parse(res.data.template_cate_auth);
              if(tempCate.length>0){
                _this.$refs.templateCate.setCheckedNodes(tempCate);
              }else{
                _this.$refs.templateCate.setCheckedNodes([]);
              }


              let sourceCate=JSON.parse(res.data.source_cate_auth);
              if(sourceCate.length>0){
                _this.$refs.sourceDataTree.setCheckedNodes(sourceCate);
              }else{
                _this.$refs.sourceDataTree.setCheckedNodes([]);
              }


              let docCate=JSON.parse(res.data.doc_cate_auth);
              if(docCate.length>0){
                _this.$refs.docCate.setCheckedNodes(docCate);
              }else{
                _this.$refs.docCate.setCheckedNodes([]);
              }

          //     _this.$refs.docCate.setCheckedNodes(JSON.parse(res.data.doc_cate_auth));
          //     _this.$refs.sourceDataTree.setCheckedNodes(JSON.parse(res.data.source_cate_auth));



          })
      },

      onChangeStatus(e){
          this.dataForm.status=e;
      },



    }
  }
</script>

<style lang="scss">
 @import './dataInfo.scss'
</style>
