<template>
  <div class="admin-role">
    <el-form :inline="true">
      <el-form-item v-if="$permission('/admin/role/check')">
        <el-input v-model="queryParams.name" size="small" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="" size="small" @click="getList" v-if="$permission('/admin/role/check')">查询</el-button>
        <el-button type="primary" size="small" @click="handleAdd()" v-if="$permission('/admin/role/add')">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
        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 label="功能权限" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-for="item in scope.row.functionList">{{item.name}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="数据权限" align="center" show-overflow-tooltip>
         <template slot-scope="scope">
          <span v-for="item in scope.row.dataList" :key="item.id">{{item.name}}
          </span>
        </template>
      </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">
            <span v-if="$permission('/admin/role/modify')"  @click="handleEdit(scope.row)">编辑</span>
            <span v-else :disabled="true">编辑</span>
            <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 v-if="$permission('/admin/role/delete')">删除</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="1100px" max-height="800" :before-close="close">
      <div style="height: 500px">
        <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-table
              :row-key="functionList.id"
              :data="functionList"
              v-loading="loading"
              max-height="370"
            >
              <el-table-column type="expand">
                <template style="width:80%;" slot-scope="scope">
                  <div>
                    <el-table
                        border
                        :data="scope.row.children"
                        row-key="id"
                        ref="multiTable"
                        
                        @selection-change="handleSelectionChange"
                      >
                      <el-table-column label="功能名称" align="left" prop="name">
                        <template slot-scope="scope">
                          <el-checkbox-group v-model="checkList" style="display:inline-block" @change="handleCheckedCitiesChange">
                            <el-checkbox :label="scope.row.id" @change="getFunInfo(scope.row)"
                            >
                              {{scope.row.name}}
                            </el-checkbox>
                          </el-checkbox-group>
                        </template>
                      </el-table-column>
                      <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
                        <span v-if="scope.row.pdata">{{scope.row.pdata}}</span>
                          <i v-if="checkList.filter(item=>item==scope.row.id)[0]" class="el-icon-plus" @click="premissVisibale(scope.row)">添加</i>
                        </template>
                      </el-table-column>
                    </el-table>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="应用名称" align="left">
                <template slot-scope="scope">
                  <span>{{scope.row.name}}</span>
                </template>
              </el-table-column>
            </el-table>
          </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>
        <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>
    <!-- 新增中选中功能后的添加弹窗 -->
    <el-dialog :title="permissTitle" :visible.sync="permissVisible" width="1100px" :before-close="premissClose" custom-class="permiss">
    <div style="height: 500px;">
      <DataPermission 
      :fun="fun"

      @getPremissData="getPremissData"
      >
      </DataPermission>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="premissClose">取 消</el-button>
      <el-button type="primary" @click="premissSubmit" :loading="btnLoading">确 定</el-button>
    </div>
    </el-dialog>
  </div>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import DataPermission from '../dataPermission'
export default {
  components: {
    Treeselect,
    DataPermission
  },
  data() {
    return {
      fun: '',
      functionName: '',
      functionInfo: [],
      checkList:[],
      checked: true,
      visible: false,
      loading: false,
      btnLoading: false,
      permissVisible: false,
      total: 0,
      tableData: [],
      title: '',
      permissTitle: '',
      showDataList:false,
      functionList:[],
      dataList: [],
      multipleSelection: [],
      expands:[],
      pdata:'',
      queryParams: {
        size: 10,
        current: 1,
        name: ''
      },
      ruleForm: {
        function:[]
      },
      rules: {
        name: [{ required: true, message: '请输入部门名称', trigger: 'blur' },],
      },
    }
  },
  mounted() {
    this.getList()
    this.getFunctionList()
  },
  watch:{
    "ruleForm.function"(val){
      if(val.length!=0){
        this.showDataList=true
      }else{
        this.showDataList=false
      }
    }
  },
  methods: {
    getPremissData(id, name, data) {
      console.log(id, name, data)
      // for(var i = 0; i<this.functionList.length;i++){
      //   for(var j=0;j<this.functionList[i].children.length;j++){
      //     if(this.functionList[i].children[j].id==id){
      //       this.functionList[i].children[j].pdata = data.name
      //     }else{
      //       if(this.functionList[i].children[j].hasOwnProperty('children')){
              
      //       }
      //     }
      //   }
      // }
      this.pdata=data
      // if (this.functionInfo.length === 0) {
      //   this.functionInfo.push({
      //     'functionId': id,
      //     'functionName': name,
      //     'dataPremiss': data
      //   })
      // } 
      // else if (this.functionInfo.length > 0) {
      //   console.log(this.functionInfo.some(item => item.dataPremiss === data))
      // }
      // console.log(this.functionInfo)
    },
    getFunInfo(val) {
      this.fun = val
      this.functionName = val.name
    },
    handleCheckedCitiesChange(value) {
        // console.log(value)
    },
    handleSelectionChange(val) {
      // console.log(val,this.$refs.multipleTable.selection)
      this.multipleSelection = val;
      // console.log(val)
    },
    normalizer(node) {
      return {
        id: node.id,
        label: node.name,
      }
    },
    handleAdd() {
      this.title = '新增角色'
      this.visible = true
      this.checkList=[]
      this.ruleForm={
        function:[]
      }
    },
    handleEdit(row) {
      this.title = '编辑角色'
      this.visible = true
      this.ruleForm = { ...row }
    },
    premissVisibale() {
      this.permissTitle = '数据权限管理'
      this.permissVisible = true
    },
    close() {
      this.visible = false
    },
    premissSubmit() {
      this.fun.pdata = this.pdata.name
      
      console.log('')
    },
    premissClose() {
      this.permissVisible = false
      console.log(this.functionId)
    },
     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
        // console.log(response.data)
        this.total=response.pageColumn.total
        this.loading = false
      }).catch(error => {
        this.loading = false
      })
    },
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.ruleForm.functions=[]
        for(var i =0;i<this.ruleForm.function.length;i++){
          var a =this.ruleForm.function[i].join(',')
          this.ruleForm.functions.push(a)
        }
          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 {

}
.permiss {
  height: 2000px;
}
</style>