<template>
  <div class="roleList">
    <template v-if="roleIsShowAdd">
      <div class="head-title">
        角色列表
        <el-button @click="roleIsShowAdd=false,clearInfo()" style="float:right;margin-top: -5px;" size="small" type="primary">
          添加角色
        </el-button>
      </div>
      <div class="content">
        <div>
          <div class="searchInput">
            <el-input style="display:inline-block;width:225px;height:30px" placeholder="请输入角色名称"
              v-model="msg.RoleName" size="small" clearable @keyup.enter.native="msg.pageIndex=1,getList()">
            </el-input>
            <span class="el-icon-search" style="color:#979dad;font-size:14px;position:relative;top:1px"
              @click="msg.pageIndex=1,getList()"> </span>
          </div>
        </div>
        <el-table :data="dataList" v-loading="loading" border style="width: 100%;margin:20px 0">
          <el-table-column prop="RoleId" label="ID" width="80">
          </el-table-column>
          <el-table-column prop="RoleName" label="角色名称" width="120">
          </el-table-column>
          <el-table-column prop="RoleIntro" label="备注/描述">
          </el-table-column>
          <el-table-column prop="CreateByName" label="创建者" width="150">
          </el-table-column>
          <el-table-column prop="CreateDateStr" label="添加日期" width="220">
          </el-table-column>
          <el-table-column label="操作" width="260">
            <template slot-scope="scope">
              <img @click="EditRole(scope.row)" src="../../assets/img/userman/edit.png" alt="">
              <img @click="RemmoveRole(scope.row)" style="margin-left:10px;" src="../../assets/img/userman/del.png" alt="">
            </template>
          </el-table-column>
        </el-table>
        <el-pagination style="text-align:right" background @current-change="handleCurrentChange"
          :page-size="msg.pageSize" layout="prev, pager, next" :total="total">
        </el-pagination>
      </div>
    </template>
    <template v-else>
      <div class="head-title">
        <span @click="roleIsShowAdd=true" style="color:rgb(64, 158, 255);cursor:pointer;">角色管理</span><span
          style="margin:0 9px;color:#C0C4CC">/</span><span>角色编辑</span>
      </div>
      <div class="content">
        <el-form :model="addMsg" :rules="rules" ref="addMsg" label-width="120px" style="padding:0 20px;">
          <el-form-item label="角色名称" prop="RoleName">
            <el-input v-model="addMsg.RoleName" class="w400" size="small" maxlength="20" />
          </el-form-item>
          <el-form-item label="备注/描述">
            <el-input type="text" v-model="addMsg.RoleIntro" size="small" class="w400"/>
          </el-form-item>
          <el-form-item label="角色权限">
              <el-tree
                :data="MenuList"
                node-key="MenuId"
                ref="tree"
                show-checkbox>
              </el-tree>
          </el-form-item>
        </el-form>
      </div>
      <el-button size="small" style="margin-top:20px;padding:9px 25px;" type="primary" @click="submitform('addMsg')">保存
      </el-button>
    </template>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        loading: false,
        dataList: [],
        msg: {
          pageIndex: 1,
          pageSize: 15,
          RoleName:"",
        },
        total: 0,
        roleIsShowAdd: true,
        MenuList: [], //菜单列表
        addMsg: {
          RoleName: '', //角色名称
          RoleIntro: '', //备注/描述
          RoleAuth: '', //角色权限
        },
        rules: {
          RoleName: [{
            required: true,
            message: "请输入角色名称",
            trigger: "blur"
          }]
        },
      };
    },
    created() {

    },
    methods: {
      handleCurrentChange(val) {
        this.msg.pageIndex = val;
        this.getList();
      },
      getList() {
        this.apipost("/api/Employee/GetRolePageList", this.msg, res => {
          if (res.data.resultCode == 1) {
            this.dataList = res.data.data.pageData;
            this.total = res.data.data.count;
          } else {
            this.Info(res.data.message);
          }
        })
      },
      submitform(addMsg) {
        //提交创建、修改表单
        this.$refs[addMsg].validate(valid => {
          if (valid) {
            this.saveMsg();
          } else {
            return false;
          }
        });
      },
      //保存
      saveMsg() {
        var ckedKeys = this.$refs.tree.getCheckedKeys();
        this.addMsg.RoleAuth=ckedKeys.join(',');
        this.apipost("/api/Employee/SetRole", this.addMsg, res => {
          if (res.data.resultCode == 1) {
            this.getList();
            this.clearInfo();
            this.roleIsShowAdd = true;
          } else {
            this.Info(res.data.message);
          }
        })
      },
      //清空
      clearInfo(){
        this.addMsg.RoleId = 0;
        this.addMsg.RoleName = '';
        this.addMsg.RoleIntro = '';
        this.addMsg.RoleAuth = '';
      },
      //修改
      EditRole(item) {
        this.apipost("/api/Employee/GetRole", {
          RoleId: item.RoleId
        }, res => {
          if (res.data.resultCode == 1) {
            this.addMsg = res.data.data;
            this.roleIsShowAdd = false;
            let newArr = this.addMsg.RoleAuth.split(',');
            setTimeout(()=>{
               this.$refs.tree.setCheckedKeys(newArr);
            },10)
          } else {
            this.Info(res.data.message);
          }
        })
      },
      //删除
      RemmoveRole(item) {
        var that = this;
        that.Confirm("是否要删除?", function () {
          that.apipost("/api/Employee/RemoveRole", {
            RoleId: item.RoleId
          }, res => {
            if (res.data.resultCode == 1) {
              that.getList();
            } else {
              that.Info(res.data.message);
            }
          })
        })
      },
      //获取菜单列表
      getMenuList() {
        this.apipost("/api/Tenant/GetMenuList", {}, res => {
          if (res.data.resultCode == 1) {
            this.MenuList = res.data.data;
            this.MenuList.forEach(x=>{
              x.children=x.SubList;
              x.label=x.MenuName
              if(x.children.length>0){
                x.children.forEach(y=>{
                  y.children=y.ThirdList;
                  y.label=y.MenuName
                  if(y.children.length>0){
                    y.children.forEach(z=>{
                      z.label=z.MenuName;
                    })
                  }
                })
              }
            })
          } else {
            this.Info(res.data.message);
          }
        })
      },
    },
    mounted() {
     this.getList();
     this.getMenuList();
    }
  };

</script>
<style>


  .roleList .content .searchInput {
    border: 1px solid #DCDFE6;
    border-radius: 4px;
  }

  .roleList .content .searchInput .el-input__inner {
    border: none;
    outline: none;
    height: 30px;
    line-height: 30px;
  }

  .roleList .content .searchInput {
    line-height: normal;
    display: inline-table;
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    width: 250px;
    margin-right: 20px;
  }

  .roleList .content {
    background: #fff;
    margin-top: 10px;
    padding: 20px;
    box-sizing: border-box;
  }

</style>
