<style lang="scss">
	.area{
		.el-select{
			width: 120px;
			display: inline-block;
			margin-right: 6px;
		}
	}
</style>
<template>
  <section class="content">
    <div class="breadcrumb">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item><a href="/">系统管理</a></el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <el-form :model="queryParams" size="mini" label-width="80px" class="filter-form" :inine="true">
      <el-row>
        <el-col :span="4">
          <el-form-item label="名称">
			  <el-input clearable placeholder="请输入名称" v-model="queryParams.name"></el-input>
          </el-form-item>
        </el-col><el-col :span="11">
          <el-form-item label-width="10" style="margin-left: 10px">
            <el-button type="primary" @click="getData" size="mini"
              >搜索</el-button
            >
			<el-button 
			  type="primary"
			  plain 
			  icon="el-icon-plus"
			  size="mini"
			  @click="handleAdd"
			  >添加角色</el-button>
			  <el-button
			     type="primary"
			     size="mini"
			     @click="del(-1)"
			  >批量删除</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%; margin-top: 10px"
      height="55vh"
    @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column align="center" prop="name" label="名称" />
      <el-table-column align="center" prop="ctime" label="创建时间" />
      <el-table-column align="center" prop="date" label="操作" width="320">
        <template slot-scope="scope">
          <el-button @click="edit(scope.row)" size="mini" type="info" plain
            >编辑</el-button
          >
          <el-button @click="del(scope.row.id)" size="mini" type="info" plain
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Page
      ref="pageButton"
      :current="form.page"
      :page_size="form.page_size"
      :total="total"
      @pageChange="gopage"
    />
    <!-- 新增-->
	<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
	  <el-form   ref="form" :model="form" :rules="rules" label-width="80px">
	
	    <el-form-item label="角色名称" prop="name">
	      <el-input
	        clearable
	        v-model="form.name"
	        placeholder="请输入角色名称"
	      ></el-input>
	    </el-form-item>
		<el-form-item label="角色类别" prop="sex">
		  <el-select
		    v-model="form.role_type"
		    placeholder="请选择"
		    clearable
		    filterable>
			<el-option label="系统角色" :value="1"></el-option>
		  </el-select>
		</el-form-item>
		<el-form-item label="所属学校" prop="school_id">
		  <el-select
		    v-model="form.school_id"
		    placeholder="请选择学校"
		    clearable
		    filterable  
		  >
		    <el-option
		      v-for="item in schoolList"
		      :key="item.value"
		      :label="item.name"
		      :value="item.id"
		    >
		    </el-option>
		  </el-select>
		</el-form-item>
		<el-form-item  label="权限" prop="permissions">
			<el-tree
			  ref="tree"
			  :data="permissionList"
			  show-checkbox
			  :default-expand-all='true'
			  node-key="code"
			  :default-checked-keys="permissions"
			  :props="defaultProps">
			</el-tree>
		</el-form-item>
	  </el-form>
	  <div slot="footer" class="dialog-footer">
	    <el-button type="primary" @click="submitForm">保存</el-button>
	    <el-button @click="open = false">取 消</el-button>
	  </div>
	</el-dialog>
  </section>
</template>
<script>
import Page from "../../components/Page";
export default {
  components: {
    Page,
  },
  data() {
    return {
      loading: false,
      queryParams: {
        page: 1,
      },
      form: {},
      form1: {},
      form2: {},
      list: [{}, {}],
      total: 0,
      title: "新增",
      open: false,
	  schoolList:[],
      permissionList: [],
	  permissions:[],
      rules: {},
	  ids:'',
	  defaultProps: {
	        children: 'children',
	        label: 'name'
	  }
    };
  },
  methods: {
	  handleSelectionChange(val) {
	  	const ids=[]
	  	val.forEach(item=>{
	  		ids.push(item.id)
	  	})
	  	this.ids=ids.join(',')
	  },
    del(id) {
		if(id == -1){
			if(this.ids){
				id=this.ids
			}else{
				this.msgInfo('请选择删除的数据');
				return
			}
		}
      this.$confirm("确认删除?", "提示", {
        type: "warning",
      }).then(() => {
        this.$api
          .delRole({
            id: id,
          })
          .then((res) => {
            if (!res.data.code) {
              this.msgSuccess("删除成功");
              this.getData();
            } else {
              this.msgError(res.data.message);
            }
          });
      });
    },
    gopage(size) {
      if (size) {
        this.queryParams.page_size = size;
      }
      this.queryParams.page = this.$refs.pageButton.page;
      this.getData();
    },
	// 获取学校 年级
	getList(){
		this.$api.schoolList().then((res) => {
		  this.schoolList = res.data.data.list;
		});
		this.$api.permissionList().then((res) => {
		  this.permissionList = res.data.data;
		});
	},
    getData() {
      this.loading = true;
      this.$api.roleList(this.queryParams).then((res) => {
        this.list = res.data.data.list;
        this.total = res.data.data.total;
        this.loading = false;
      });
    },
    handleAdd() {
      this.open = true;
      this.title = "新增角色";
      this.form = {};
    },
    edit(item) {
      this.title = "编辑";
      this.form = item;
	  if(item.permissions){
		this.permissions=item.permissions.split(',')  
	  }
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
		// console.log(this.$refs.tree.getCheckedNodes());
	  let per=this.$refs.tree.getCheckedNodes()
	  let permissions=[]
	  if(per.length > 0){
		  per.forEach(item=>{
		  		permissions.push(item.code)
		  })
		  this.form.permissions=permissions.join(',')
	  }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.$api.editRole(this.form).then((res) => {
              if (res.data.code == 0) {
                this.msgSuccess("成功!");
                this.open = false;
                this.getData();
              } else {
                this.msgError(res.data.message);
              }
            });
          } else {
            this.$api.addRole(this.form).then((res) => {
              if (res.data.code == 0) {
                this.msgSuccess("成功!");
                this.open = false;
                this.getData();
              } else {
                this.msgError(res.data.message);
              }
            });
          }
        }
      });
    }
  },
  created() {
	  this.getList()
	  this.getData();
    
  },
};
</script>