<template>
  <div class="page-container teacher-add-page" @click="checkedButton">
    <div class="public-form-box">
      <div class="page-top-title-box">
        <p class="page-top-title-name-p">PC端权限配置</p>
        <p class="page-top-title-out-p" @click="outPageButton">返回</p>
        <p class="page-top-title-submit-p" @click="upDataButton">提交</p>
      </div>
      <!--权限模块-->
      <div class="table-for-max-box">
        <div class="table-title-box">
          <p>模块菜单</p>
          <p>权限</p>
        </div>
        <div class="table-for-max-big-box scrollbar-box">
          <div class="table-for-big-box" v-for="(maxItem,maxIndex) in menuList" :key="maxIndex">
            <!--一级目录-->
            <div class="max-title-box" :class="!maxItem.children?'max-title-box-null':''">
              <p><el-checkbox v-model="maxItem.checked" @change="(type)=>itemCheckClick(type,maxItem)">{{maxItem.menuName}}</el-checkbox></p>
            </div>
            <div class="max-right-box">
              <div class="big-box" v-for="(bigItem,bigIndex) in maxItem.children" :key="bigIndex">
                <!--二级页面-->
                <div class="big-title-box" :class="bigItem.menuType == '1'?'big-title-box-null':''">
                  <p><el-checkbox v-model="bigItem.checked" @change="(type)=>itemCheckClick(type,bigItem,maxItem)">{{bigItem.menuName}}</el-checkbox></p>
                </div>
                <!--三级页面/四级按钮-->
                <div class="big-right-box" v-if="bigItem.menuType !='1'">
                  <div class="min-box" v-for="(minItem,minIndex) in bigItem.children" :key="minIndex">
                    <div class="min-title-box">
                      <!--三级页面-->
                      <div class="title-left-box">
                        <p class="min-title-box-null"></p>
                        <p><el-checkbox v-model="minItem.checked" @change="(type)=>itemCheckClick(type,minItem,bigItem,maxItem)">{{minItem.menuName}}</el-checkbox></p>
                        <p class="min-title-box-null"></p>
                      </div>
                    </div>
                    <div class="min-right-box">
                      <!--四级按钮-->
                      <div v-for="(btnItem,btnIndex) in minItem.children">
                        <el-checkbox v-model="btnItem.checked"
                                     @change="(type)=>itemCheckClick(type,btnItem,minItem,bigItem,maxItem)">
                          {{btnItem.menuName}}
                        </el-checkbox>
                      </div>
                      <p v-if="!minItem.children" style="font-weight:500;color:#999;text-align: center">该页面没有按钮</p>
                    </div>
                  </div>
                </div>
                <!--三级按钮-->
                <div class="big-right-box-null" v-if="bigItem.menuType == '1'">
                  <div v-for="(btnItem,btnIndex) in bigItem.children">
                    <el-checkbox v-model="btnItem.checked"
                                 @change="(type)=>itemCheckClick(type,btnItem,minItem,bigItem,maxItem)">
                      {{btnItem.menuName}}
                    </el-checkbox>
                  </div>
                  <p v-if="!bigItem.children" style="font-weight:500;color:#999;text-align: center">该页面没有按钮</p>
                </div>
              </div>
              <p class="big-box-null" v-if="!maxItem.children&&maxItem.menuName == '数据可视化'">数据可视化大屏查看权限</p>
            </div>
          </div>
        </div>
      </div>
      <el-dialog title="选择指定部门（可多选）" @close="deptCancel" :visible.sync="deptOpen"
                 :close-on-click-modal="false" v-if="deptOpen" width="500px"
                 append-to-body class="managePermissionTemplates-dept-dialog-box">
        <el-form :model="deptForm" ref="deptForm" :inline="true" :rules="rules" class="addCheckPage-min">
          <el-form-item label="指定部门" prop="deptIds" label-width="90px" class="el-form-item-bottom">
            <el-cascader
              style="width:300px;"
              :options="treeselectList"
              :props="{multiple: true,value: 'id', label: 'label'}"
              collapse-tags
              v-model="deptForm.deptIds"
              clearable>
            </el-cascader>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer dialog-footer-box">
          <p class="dialog-footer-button-null"></p>
          <p class="dialog-footer-button-info" @click="deptCancel">取消</p>
          <p class="dialog-footer-button-primary" @click="deptSure">提交</p>
          <p class="dialog-footer-button-null"></p>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { addRoleByScope, editRoleByScope } from '@/apiDemo/system/role'
import { listMenuAll} from "@/apiDemo/system/menu";
import { treeselect } from "@/apiDemo/system/dept";

import { systemRoleGetRoleMenu,systemRoleSaveRolePermission } from "@/api/systemManagement/index";
export default {
  name: "addPage",
  props:{
    propsData:{},
  },
  data() {
    return {
      menuList:[],
      originalMenuList:[],
      form:{
        roleName:"",     //姓名
        roleKey:"",
        dataScope:null,
      },
      rules:{
        roleName: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { required: true, message: "请输入姓名", validator: this.spaceJudgment, trigger: "blur" }
        ],
        roleKey: [
          { required: true, message: "请输入权限标识", trigger: "blur" },
          { required: true, message: "请输入权限标识", validator: this.spaceJudgment, trigger: "blur" }
        ],
        dataScope: [
          { required: true, message: "请选择数据范围", trigger: "blur" },
        ],
        deptIds: [
          { required: true, message: "请选择指定部门", trigger: "blur" },
        ],
      },
      //数据范围（1：全部, 2：部门及下级, 3：本部门, 4：仅本人，5：自定义）
      optionsDataList:[
        {type: 1, value: "所有数据",},
        {type: 2, value: "本部门及下级部门数据",},
        {type: 3, value: "本部门",},
        {type: 4, value: "当前账号数据",},
        {type: 5, value: "自定义",},
      ],
      //数据范围部门弹窗
      deptOpen:false,
      deptOpenType:'',
      //部门数据结构树
      treeselectList:[],
      //指定部门数据
      deptForm:{
        deptIds:[],
      },
      checkDeptType:null,
      checkAllType:false,
    }
  },
  created(){

  },
  mounted(){
    this.getMenu();
  },
  methods:{
    //提交按钮
    upDataButton(){
      let obj = {
        roleId:this.propsData.roleId,
        menuIds : this.toArray(),
      }
      systemRoleSaveRolePermission(obj).then(response => {
        if(response.code == 200){
          this.msgSuccess(response.message);
          this.$parent.goPage(4);
        }
      });
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if(this.propsData.roleId){
      //       //修改
      //       this.editRoleByScope()
      //     }else{
      //       //发布
      //       this.addRoleByScope()
      //     }
      //   }
      // });
    },
    //新增
    addRoleByScope(){
      let self = this;
      let obj = JSON.parse(JSON.stringify(this.form))
      obj.roleSort = 1;
      obj.menuCheckStrictly = 0;
      obj.status = 0;
      obj.menuIds = this.toArray();
      obj.viewDeptIds = JSON.stringify(self.deptForm.deptIds);
      obj.deptIds = [];
      let list = [];
      for(let i=0;i<self.deptForm.deptIds.length;i++){
        for(let o=0;o<self.deptForm.deptIds[i].length;o++){
          list.push(self.deptForm.deptIds[i][o])
        }
      }
      for (var i = 0,len=list.length; i < len; i++) {
        if(obj.deptIds.indexOf(list[i]) === -1){
          obj.deptIds.push(list[i]);
        }
      }
      addRoleByScope(obj).then(response => {
        if(response.code == 200){
          this.msgSuccess(response.message);
          this.$parent.goPage(3);
        }
      });
    },
    //编辑
    editRoleByScope(){
      let self = this;
      let obj = JSON.parse(JSON.stringify(this.form))
      obj.roleSort = 1;
      obj.menuCheckStrictly = 0;
      obj.status = 0;
      obj.menuIds = this.toArray();
      obj.viewDeptIds = JSON.stringify(self.deptForm.deptIds);
      obj.deptIds = [];
      let list = [];
      for(let i=0;i<self.deptForm.deptIds.length;i++){
        for(let o=0;o<self.deptForm.deptIds[i].length;o++){
          list.push(self.deptForm.deptIds[i][o])
        }
      }
      for (var i = 0,len=list.length; i < len; i++) {
        if(obj.deptIds.indexOf(list[i]) === -1){
          obj.deptIds.push(list[i]);
        }
      }
      editRoleByScope(obj).then(response => {
        if(response.code == 200){
          this.msgSuccess(response.message);
          this.$parent.goPage(3);
        }
      });
    },
    checkedButton(){
      this.$set(this,'checkAllType',false);
    },
    //数据范围选中
    dataScopeFun(type){
      let self = this;
      if(!type){
        setTimeout(function(){
          if(self.checkAllType){
            if(self.form.dataScope == 5){
              //指定范围弹窗
              self.$set(self,'deptOpen',true)
            }else{
              self.$set(self,'checkDeptType',self.form.dataScope)
              self.$set(self.deptForm,'deptIds',[])
            }
          }
        },50);
      }else{
        this.$set(this,'checkAllType',true);
      }
    },
    //数据范围选择清除
    dataScopeClearFun(){
      this.$set(this.form,'dataScope',null)
      this.$set(this.deptForm,'deptIds',[])
    },
    //指定部门弹窗取消
    deptCancel(){
      this.$set(this.form,'dataScope',this.checkDeptType)
      this.$set(this,'deptOpen',false)
    },
    //指定部门弹窗确定
    deptSure(){
      this.$refs["deptForm"].validate(valid => {
        if (valid) {
          this.$set(this,'checkDeptType',this.form.dataScope)
          this.$set(this,'deptOpen',false);
        }
      });
    },
    //模块勾选逻辑
    itemCheckClick(type,item,minItem,bigItem,maxItem){
      if(item.children){
        for(let i=0;i<item.children.length;i++){
          item.children[i].checked = !!type;
          if(item.children[i].children){
            for(let o=0;o<item.children[i].children.length;o++){
              item.children[i].children[o].checked = !!type;
              if(item.children[i].children[o].children){
                for(let x=0;x<item.children[i].children[o].children.length;x++){
                  item.children[i].children[o].children[x].checked = !!type;
                }
              }
            }
          }
        }
      }
      if(minItem&&type){
        minItem.checked = type;
      }
      if(bigItem&&type){
        bigItem.checked = type;
      }
      if(maxItem&&type){
        maxItem.checked = type;
      }
    },
    getMenu() {
      let self = this;
      this.loading = true;
      systemRoleGetRoleMenu({roleId:this.propsData.roleId}).then(response => {
        let newList = JSON.parse(JSON.stringify(response.data));
        // for(let i=0;i<response.data.length;i++){
        //   if(response.data[i].status == 0 && response.data[i].visible == 0){
        //     newList.push(response.data[i]);
        //   }
        // }
        // if(this.propsData.type == 1){
        //   //新增
        //     this.menuList = JSON.parse(JSON.stringify(this.handleTree(newList, "menuId")))
        // }else {
        //   if(this.propsData.type == 2){
        //     //编辑
        //     this.$set(this.form,'roleId',this.propsData.roleId);
        //     this.$set(this.form,'roleName',this.propsData.name);
        //     this.$set(this.form,'roleKey',this.propsData.key);
        //   }
        //   //页面权限处理
        //   for(let i=0;i<self.propsData.list.length;i++){
        //     for(let o=0;o<newList.length;o++){
        //       if(self.propsData.list[i] == newList[o].menuId){
        //         newList[o].checked = true;
        //       }
        //     }
        //   }
        //   //数据权限范围
        //   this.$set(this.form,'dataScope',this.propsData.dataScope);
        //   this.$set(this,'checkDeptType',this.propsData.dataScope)
        //   //如果权限范围为指定部门
        //   if (this.propsData.dataScope == 5){
        //     this.$set(this.deptForm,'deptIds',JSON.parse(this.propsData.viewDeptIds));
        //   }
        // }
        this.menuList = JSON.parse(JSON.stringify(this.handleTree(newList, "menuId")))
        // this.getTreeselect();
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
        this.$set(this,'treeselectList',response.data[0].children);
      });
    },
    //返回事件
    outPageButton(){
      this.$parent.goPage(1);
    },
    //结构树转数组并筛选选中项
    toArray(){
      let list = [];
      let newList = JSON.parse(JSON.stringify(this.menuList));
      for(let i=0;i<newList.length;i++){
        pushNode(newList[i]);
      }
      function pushNode(node){
        if(node.children){
          for (let nodeItem of node.children){
            pushNode(nodeItem)
          }
          delete node.children;
          if(node.checked){
            list.push(node)
          }
        }else{
          if(node.children){
            delete node.children
          }
          if(node.checked){
            list.push(node)
          }
        }
      }
      let idList = [];
      for(let i=0;i<list.length;i++){
        idList.push(list[i].menuId)
      }
      return idList
    }
  }
}
</script>

<style scoped lang="scss">
.teacher-add-page{
  .public-form-box{
    flex:1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    padding-bottom:20px;
  }
  .top-title-box{
    border-bottom:1px solid #E0E0E0;
    margin-bottom:20px;
    display: flex;
    p:nth-child(1){
      color:#0045AF;
      line-height:80px;
      margin-left:24px;
      font-size:18px;
      flex:1;
    }
    p:nth-child(2){
      border:1px solid #0045AF;
      color:#0045AF;
      width:80px;
      height:30px;
      text-align: center;
      font-size:16px;
      border-radius: 6px;
      line-height:30px;
      margin:25px 20px 0 0;
      cursor: pointer;
    }
  }
  .top-info-box{
    display: flex;
    padding:40px 20px 0;
    .left-info-box{
      margin-right:20px;
      img{
        width:100px;
        height:120px;
      }
      p{
        width: 80px;
        height: 26px;
        border: 1px solid #0183FA;
        border-radius: 6px;
        line-height:24px;
        font-size:12px;
        color:#0183FA;
        text-align: center;
        margin:24px 10px;
      }
    }
  }
  .top-tow-box{
    display: flex;
    align-items: center;
    height: 50px;
    background: #F5F5F5;
    >p{
      color:#0045AF;
      line-height:80px;
      margin-left:24px;
      font-size:18px;
      flex:1;
    }
  }
  /*数据范围*/
  .data_scope{
    margin: 40px 20px;
    display: flex;
    justify-content: space-between;
    .data_scope_l{
      display: flex;
      justify-content: flex-start;
      .data_scope_l_l{
        font-size: 16px;
        font-family: Microsoft YaHei-Regular, Microsoft YaHei;
        font-weight: 400;
        color: #333333;
        line-height: 24px;
        margin-right: 8px;
        >i{
          font-size: 14px;
          color: #FF0000;
          font-style: normal;
          margin-right: 4px;
        }
      }
      .data_scope_l_r{
        display: flex;
        justify-content: flex-start;
        >i{
          height: 40px;
          font-style: normal;
          display: inline-block;
          font-size: 14px;
          font-family: Microsoft YaHei-Regular, Microsoft YaHei;
          font-weight: 400;
          color: #CCCCCC;
          line-height: 40px;
          padding: 0 14px;
          box-sizing: border-box;
          border: 1px dashed #E0E0E0;
          margin-right: 20px;
          cursor: pointer;
          &.on{
            color:#0183FA;
            border: 1px dashed #0183FA;
          }
        }

      }
    }
    .data_scope_r{
      width: 70px;
      height: 40px;
      border-radius: 6px 6px 6px 6px;
      opacity: 1;
      border: 1px solid #0045AF;
      font-size: 14px;
      font-family: Microsoft YaHei-Regular, Microsoft YaHei;
      font-weight: 400;
      color: #0045AF;
      line-height: 40px;
      text-align: center;
      cursor: pointer;
    }
  }
  .top-title-one{
    display: flex;
    border-top:1px solid #E0E0E0;
    position: relative;
    .title-p{
      font-size:18px;
      color:#0045AF;
      line-height:80px;
      margin-left:24px;
    }
    .img-p{
      font-size:18px;
      margin:0 10px;
      line-height:80px;
      color:#FFC000;
    }
    .position-box{
      width: 420px;
      height: 194px;
      padding:20px;
      background: #F5F5F5;
      position: absolute;
      top:10px;
      left:134px;
      p{
        font-size:12px;
        line-height:18px;
        color:#333;
      }
    }
  }
  .name-input-box{
    height:100px;
  }
  .for-button-list{
    background: #E5F2FE;
    margin:0 20px;
    display: flex;
    .for-title-p{
      width:175px;
      height:80px;
      line-height:80px;
      font-size:16px;
      color:#333;
      text-align: center;
    }
    .for-button-max-box{
      flex:1;
      .for-button-min-box{
        font-size:16px;
        display: inline-block;
        overflow: hidden;
        height:30px;
        line-height:30px;
        border-radius:6px;
        margin:25px 28px 0 0;
        cursor: pointer;
        i{
          height:30px;
          line-height:30px;
          font-size:16px;
          color:#fff;
          margin-right:6px;
        }
      }
      .colorAA{
        color: #ffffff;
        background: #0183FA;
        padding:0 26px 0 16px;
      }
      .colorBB{
        padding:0 26px;
        color: #333;
        background: #E0E0E0;
      }
    }
  }
  .template-name-box{
    display: flex;
    .template-name-p{
      font-weight:500;
      height:80px;
      line-height:80px;
      font-size:16px;
      color:#333;
      margin-left:20px;
      flex: 1;
    }
    .template-name-button{
      width:80px;
      height:40px;
      line-height:40px;
      margin:20px 20px 0 0;
    }
    .template-name-button-one{
      width:180px;
      height:40px;
      line-height:40px;
      margin:20px 40px 0 0;
      color:#fff;
      background: #0183FA;
      border: 1px solid #E0E0E0;
      cursor: pointer;
      font-size: 14px;
      text-align: center;
      border-radius: 6px;
    }
  }
  .table-for-max-box{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border:1px solid #D7D7D7;
    margin:20px 20px 0;
    *{
      margin:0;
      padding:0;
    }
    .table-title-box{
      background: rgba(1,131,250,0.1);
      display: flex;
      p{
        font-size:14px;
        font-weight:700;
        color:#333;
        line-height:48px;
        padding-left:20px;
      }
      p:nth-child(1){
        width:616px;
        border-right:1px solid #D7D7D7;
      }
    }
    .table-for-max-big-box{
      flex:1;
      display: flex;
      flex-direction: column;
      .table-for-big-box{
        flex:1;
        border-top:1px solid #D7D7D7;
        border-right:1px solid #D7D7D7;
        display: flex;
        font-size:14px;
        .max-title-box-null{
          width:616px!important;
        }
        .max-title-box{
          width:230px;
          border-right:1px solid #D7D7D7;
          min-height:48px;
          position: relative;
          p{
            position: absolute;
            top:50%;
            left:20px;
            height:18px;
            line-height:18px;
            margin-top:-9px;
          }
        }
        .max-right-box{
          flex:5;
          min-height:48px;
          .big-box:nth-child(1){
            border:none!important;
          }
          .big-box-null{
            line-height:48px;
            margin-left:20px;
            color:#606266;
          }
          .big-box{
            display: flex;
            border-top:1px solid #D7D7D7;
            .big-title-box-null{
              width:386px!important;
            }
            .big-title-box{
              width:180px;
              border-right:1px solid #D7D7D7;
              min-height:48px;
              position: relative;
              overflow: hidden;
              p{
                position: absolute;
                top:50%;
                left:20px;
                height:48px;
                line-height:48px;
                margin-top:-24px;
              }
            }
            .big-right-box-null{
              flex:1;
              padding:14px 0 0 20px;
              div{
                display: inline-block;
                margin:0 20px 14px 0;
              }
            }
            .big-right-box{
              flex:5;
              min-height:48px;
              .min-box:nth-child(1){
                border:none!important;
              }
              .min-box{
                display: flex;
                border-top:1px solid #D7D7D7;
                .min-title-box{
                  display: flex;
                  flex-direction: column;
                  border-right:1px solid #D7D7D7;
                  min-height:48px;
                  position: relative;
                  overflow: hidden;
                  .title-left-box{
                    width:205px;
                    display: flex;
                    .min-title-box-null{
                      flex:1;
                    }
                    p{
                      width:205px;
                      position: absolute;
                      top:50%;
                      left:20px;
                      height:48px;
                      line-height:48px;
                      margin-top:-24px;
                    }
                  }
                }
                .min-right-box{
                  flex:1;
                  padding:14px 0 0 20px;
                  div{
                    display: inline-block;
                    margin:0 20px 14px 0 ;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  .bottom-button-box{
    display: flex;
    width:600px;
    margin:30px auto 10px;
    .null-p{
      flex:1;
    }
    .button-p-1{
      width:100px;
      margin-right:20px;
    }
    .button-p-2{
      width:100px;
      margin-right:20px;
    }
    .button-p-3{
      width:180px;
    }
  }
}
</style>
<style lang="scss">
.teacher-add-page{
  .top-info-box{
    .el-form-item{
      height:70px;
    }
    .switch .el-switch__label {
      position: absolute;
      display: none;
      color: #fff !important;
    }
    .switch .el-switch__label--right {
      z-index: 1;
    }
    .switch .el-switch__label--right span{
      margin-left: 10px;
    }
    .switch .el-switch__label--left {
      z-index: 1;
    }
    .switch .el-switch__label--left span{
      margin-left: 24px;
    }
    .switch .el-switch__label.is-active {
      display: block;
    }
    .switch.el-switch .el-switch__core,
    .el-switch .el-switch__label {
      width: 64px !important;
      margin: 0;
    }
  }
}
.managePermissionTemplates-permission-dialog-box{
  *{
    margin:0;
  }
  .title-p{
    font-size:15px;
    font-weight:700;
    margin-bottom:30px;
  }
  .managePermissionTemplates-permission-dialog-form-box{
    .title-box{
      display: flex;
      height: 60px;
      background: #F5F5F5;
      .left-title-p{
        width:198px;
        border-right:1px solid #E0E0E0;
        font-size:15px;
        font-weight:700;
        padding-left:26px;
        line-height:60px;
        color:#333;
      }
      .right-box{
        flex:1;
        .right-box-min{
          height:20px;
          margin:20px 0 0 50px;
          .el-checkbox__label{
            font-size:15px;
            font-weight:700;
          }
        }
      }
    }
    .check-box{
      display: flex;
      .left-title-box{
        width:198px;
        border:1px solid #E0E0E0;
        position: relative;
        .left-title-p{
          position: absolute;
          left:27px;
          height:20px;
          line-height:20px;
          top:50%;
          margin-top:-10px;
        }
      }
      .right-box{
        padding:0 50px 41px 0;
        flex:1;
        border:1px solid #E0E0E0;
        border-left:none;
        .for-check-box{
          margin:41px 0 0 50px;
        }
      }
    }
  }
  .managePermissionTemplates-permission-dialog-button-box{
    display: flex;
    width:160px;
    margin:0 auto;
    p{
      margin:0;
      width:70px;
      height:30px;
      line-height:30px;
      font-size:14px;
    }
    p:nth-child(1){
      margin-right:20px;
    }
  }
}
.managePermissionTemplates-dept-dialog-box{
  *{
    margin:0;
  }
  .managePermissionTemplates-dept-dialog-button-box{
    display: flex;
    width:190px;
    margin:0 auto;
    p{
      margin:0;
      width:70px;
      height:30px;
      line-height:30px;
      font-size:14px;
    }
    p:nth-child(1){
      margin-right:50px;
    }
  }
}
</style>
