<template>
  <div class="admin-role">
    <el-form :inline="true">
      <el-form-item label="角色名称">
        <el-input v-model="queryParams.name" size="small" placeholder="请输入角色名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="" size="small" @click="getList">查询</el-button>
        <el-button type="primary" size="small" @click="handleAdd(0)">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="tableData"
        border
        header-row-class-name="custom-table-header"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="name" label="角色名称" align="" show-overflow-tooltip></el-table-column>
      <el-table-column prop="gongneng" label="功能权限" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="shuju" label="数据权限" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="remark" label="备注" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-dropdown split-button type="primary" size="small" @click="handleEdit(scope.row)">
            编辑
            <el-dropdown-menu slot="dropdown" class="text-align_center">
              <el-dropdown-item @click.native="handleAdd(scope.row.id)">新增</el-dropdown-item>
              <el-dropdown-item class="color-red" @click.native="handleDelete(scope.row)" divided>删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
     <div class="pagination">
      <el-pagination :current-page.sync="queryParams.current" :page-size="queryParams.size" :total="total" @current-change="handleCurrentChange" layout="total, prev, pager, next, jumper" background></el-pagination>
    </div>
    <el-dialog :title="title" :visible.sync="visible" width="500px" :before-close="close">
      <el-form v-if="visible" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" label-position="left">
      
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="功能权限" prop="function">
           <el-cascader
           v-model="ruleForm.function"
              :options="functionList"
              :props="{multiple:true, checkStrictly: false,value:'id',label:'name' }"
              collapse-tags
              placeholder="请输入功能权限"
              clearable
              @visible-change="getDataList"
              style="width:100%"></el-cascader>
          <!-- <el-input v-model="ruleForm.function" placeholder="请输入功能权限"></el-input> -->
        </el-form-item>
         <el-form-item label="数据权限" prop="data" v-if="showDataList">
            <el-cascader
           v-model="ruleForm.data"
              :options="dataList"
              :props="{multiple:true, checkStrictly: false,value:'id',label:'dataName' }"
              collapse-tags
              placeholder="请输入数据权限"
              clearable
              style="width:100%"></el-cascader>
          <!-- <el-input v-model="ruleForm.data" placeholder="请输入数据权限"></el-input> -->
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="ruleForm.remark" placeholder="请输入备注" type="textarea" maxlength="100" show-word-limit></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: {
    Treeselect
  },
  data() {
    return {
      visible: false,
      loading: false,
      btnLoading: false,
      total: 0,
      tableData: [],
      title: '',
      showDataList:false,
      functionList:[],
      dataList:[],
      queryParams: {
        size: 10,
        current: 1,
        name: ''
      },
      ruleForm: {
        function:[]
      },
      rules: {
        name: [{ required: true, message: '请输入部门名称', trigger: 'blur' },],
        function: [{ required: true, message: '请选择功能权限', trigger: 'change' },],
      },
    }
  },
  mounted() {
    this.getList()
    this.getFunctionList()
  },
  watch:{
    "ruleForm.function"(val){
      if(val.length!=0){
        this.showDataList=true
      }else{
        this.showDataList=false
      }
    }
  },
  methods: {
    normalizer(node) {
      return {
        id: node.id,
        label: node.name,
      }
    },
    handleAdd(id) {
      this.title = '新增角色'
      this.visible = true
     
    },
    handleEdit(row) {
      this.title = '编辑角色'
      this.visible = true
      this.ruleForm = { ...row }
    },
    close() {
      this.visible = false
    },
     handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getList();
    },
    getDataList(val){
      console.log(val,this.ruleForm.function )
      // var functionList = {
      //   functions:this.ruleForm.function 
      // }
      if(val==false){
        this.$api.getDataList({functionList:this.ruleForm.function}).then(response => {
        this.dataList = response.data
      })
      }
   
    },
    getFunctionList() {
      this.$api.getFunctionList().then(response => {
        this.functionList = response.data
      })
    },
    getList() {
      this.loading = true
      this.$api.getRoleList(this.queryParams).then(response => {
        this.tableData = response.data
        this.total=response.pageColumn.total
        this.loading = false
      }).catch(error => {
        this.loading = false
      })
    },
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true
          if (this.ruleForm.id) {
            this.$api.editRole(this.ruleForm).then(response => {
              this.$message.success('编辑成功')
              this.btnLoading = false
              this.getList()
              this.close()
            }).catch(error => {
              this.btnLoading = false
            })
          } else {
            console.log(this.ruleForm)
            this.$api.addRole(this.ruleForm).then(response => {
              this.$message.success('新增成功')
              this.btnLoading = false
              this.getList()
              this.close()
            }).catch(error => {
              this.btnLoading = false
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleDelete(row) {
      this.$confirm('确认删除本条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$api.deleteRole({ id: row.id }).then(response => {
          this.$message.success('删除成功')
          this.loading = false
          this.getList()
        }).catch(error => {
          this.loading = false
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.admin-role {

}
</style>