<template>
  <div class="app-container">
    <!--角色查询条件-->
    <el-form ref="queryForm" :model="model" :inline="true" label-width="68px">
      <el-form-item label="角色编码" prop="number">
        <el-input
          v-model="model.number"
          placeholder="请输入角色编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="角色名称" prop="name">
        <el-input
          v-model="model.name"
          placeholder="请输入角色名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="enabled">
        <el-select v-model="model.enabled" placeholder="角色状态" clearable size="small">
          <el-option
            v-for="dict in dictValues['enabled']"
            :key="dict.key"
            :label="dict.value"
            :value="dict.key"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="model = {}">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="20">
      <!--左树数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="leftTreeSearch"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            ref="leftTree"
            :data="leftTreeData"
            :props="leftTreeProps"
            :expand-on-click-node="false"
            :filter-node-method="filterLeftTreeNode"
            default-expand-all
            @node-click="handleLeftTreeNodeClick"
          />
        </div>
      </el-col>
      <!--右表数据-->
      <el-col :span="20" :xs="24">
        <!--工具栏按钮-->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd(null)"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleBatchDelete"
            >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-upload"
              size="mini"
              @click="handleImport"
            >导入
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
            >导出
            </el-button>
          </el-col>
        </el-row>
        <!--角色列表数据-->
        <el-table
          v-loading="loading"
          :data="roleList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="角色编码" align="center" prop="number" />
          <el-table-column label="角色名称" align="center" prop="name" />
          <el-table-column label="状态" align="center" prop="enabled" :formatter="columnFormat" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="{row}">
              <span>{{ parseTime(row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="{row}">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(row)"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-circle-check-outline"
                @click="handlePermission(row)"
              >分配权限</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="page"
          :limit.sync="size"
          @pagination="handleQuery"
        />
      </el-col>
    </el-row>
    <!--角色新增编辑弹出框-->
    <role-dialog :dialog="dialog" @refresh="handleQuery" />
    <!--权限分配弹出框-->
    <select-permission-dialog :dialog="permissionDialog" @refresh="handleQuery" />
    <!--角色导入弹出框-->
    <import-dialog :dialog="importDialog" @onSuccess="importSuccess" />
  </div>
</template>

<script>
import { getRolePage, getRoleDict, deleteRole, batchOperation, leftTreeData } from '@/api/sys/role' // 角色api
import RoleDialog from './components/RoleDialog'
import SelectPermissionDialog from './components/SelectPermissionDialog' // 选择权限对话框
import ImportDialog from '@/components/ImportDialog' // 导入文件弹出框

export default {
  name: 'RoleList',
  components: { ImportDialog, SelectPermissionDialog, RoleDialog },
  data() {
    return {
      // 表格是否在加载中
      loading: false,
      // 当前第几页
      page: 1,
      // 每页多少条数据
      size: 20,
      // 一共多少条数据
      total: 0,
      // 查询条件
      model: {},
      // 左树搜索条件
      leftTreeSearch: '',
      // 左树数据
      leftTreeData: undefined,
      // 左树属性
      leftTreeProps: {
        children: 'children',
        label: 'name'
      },
      // 当前选中行id
      ids: [],
      // 当前选中是否是单行
      single: false,
      // 当前选中是否是多行
      multiple: false,
      // 表格数据
      roleList: [],
      // 角色数据字典
      roleDict: {},
      // 缓存每列的详细信息 {fieldName:columnInfo}
      columnInfo: {},
      // 缓存每列的数据字典 {fieldName:dictValues}
      dictValues: {},
      // 角色新增编辑页面 参数
      dialog: {
        // 默认不显示 dialog 新增或编辑时显示
        show: false,
        // dialog 标题
        title: '',
        // 数据字典
        dictValues: {},
        // dialog 数据
        form: {
          enabled: 1,
          treeId: 0,
          dataScope: 0
        },
        // dialog 默认数据
        defaultForm: {
          enabled: 1,
          treeId: 0,
          dataScope: 0
        }
      },
      // 分配权限页面数据
      permissionDialog: {
        // 默认不显示 permissionDialog 分配权限时时显示
        show: false,
        // dialog 标题
        title: '分配权限',
        permission: []
      },
      // 导入文件弹出框
      importDialog: {
        show: false,
        action: null // 导入文件Url
      }
    }
  },
  watch: {
    /**
     * 根据输入的值过滤左树列表
     */
    leftTreeSearch(value) {
      this.$refs.leftTree.filter(value)
    }
  },
  created() {
    this.getLeftTreeData() // 获取左树数据
    this.handleQuery() // 获得表格数据
    this.handleDict() // 获得数据字典
  },
  methods: {
    /**
     * 获得左树数据
     */
    getLeftTreeData() {
      leftTreeData().then(res => {
        this.leftTreeData = res
      })
    },
    /**
     * 过滤左树节点
     */
    filterLeftTreeNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    /**
     * 左树节点单击事件
     */
    handleLeftTreeNodeClick(data) {
      // 增加搜索条件
      this.model.treeId = data && data.id
      // 刷新表格数据
      this.handleQuery()
    },
    /**
     * 将菜单转化为树形数据
     */
    handleRoles(roles) {
      for (let i = 0; i < roles.length; i++) {
        const role = roles[i]
        role.menus = this.listToTree(role.menus)
      }
      return roles
    },
    /**
     * 获得表格数据
     */
    handleQuery() {
      this.loading = true
      getRolePage(this.page, this.size, this.model).then(res => {
        this.roleList = this.handleRoles(res.list)
        this.page = res.page
        this.total = res.total
        this.loading = false
      })
    },
    /**
     * 获取数据字典
     */
    handleDict() {
      // 获得本单据的详细定义信息
      getRoleDict().then(res => {
        this.roleDict = res // 本单据的详细定义信息
        const result = this.parseTableMeta(res) // 解析本单据的详细定义信息
        this.columnInfo = result.columnInfo // 每列的详细信息
        this.dictValues = result.dictValues // 每列的数据字典
        this.dialog.dictValues = result.dictValues // 每列的数据字典
      })
    },
    /**
     * 默认的列字段显示格式化，从数据字典中查找该值在前台显示什么信息
     * @param row 当前行数据
     * @param column 当前列信息
     */
    columnFormat(row, column) {
      const key = column.property // 当前列信息 绑定的属性名称
      const dictValues = this.dictValues[key] // 当前列的数据字典
      if (dictValues != null && dictValues.length > 0) {
        // 将 key 转为 int 才查找, Boolean型后台返回实体上为 true false, 而数据字典中是 1，0
        const dict = dictValues.find(dict => dict.key === +row[key])
        return (dict && dict.value) || '数据错误'
      }
      return row[key]
    },
    /**
     * 新增按钮操作
     * @param row 当前行数据
     */
    handleAdd(row) {
      this.dialog.show = true
      this.dialog.title = '添加角色'
      this.dialog.form.treeId = this.model.treeId
    },
    /**
     * 表格内编辑按钮
     * @param row 当前行数据
     */
    handleUpdate(row) {
      if (row != null) {
        this.dialog.form = this.deepClone(row)
        this.dialog.form.enabled = +row.enabled // Boolean 转 int
      } else {

      }
      this.dialog.show = true
      this.dialog.title = '编辑角色'
      this.dialog.form.treeId = this.model.treeId
    },
    /**
     * 分配权限
     * @param row 当前行数据
     */
    handlePermission(row) {
      this.permissionDialog.roleId = row.id
      this.permissionDialog.permissions = row.menus
      this.permissionDialog.show = true
    },
    /**
     * 表格内删除按钮
     * @param row 当前行的数据
     */
    handleDelete(row) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRole(row.id).then(res => {
          this.$notify.success({
            title: '成功',
            message: '删除角色成功'
          })
          this.page = 1
          this.handleQuery()
        }).catch(err => {
          console.log(err)
          this.$notify.error({
            title: '失败',
            message: '删除角色失败'
          })
        })
      })
    },
    /**
     * 批量删除
     */
    handleBatchDelete() {
      this.$confirm('此操作将永久所选择的角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        batchOperation({ delete: this.ids }).then(res => {
          this.$notify.success({
            title: '成功',
            message: '批量删除角色成功'
          })
          this.page = 1
          this.handleQuery()
        }).catch(err => {
          console.log(err)
          this.$notify.error({
            title: '失败',
            message: '批量删除角色失败'
          })
        })
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1 // 是否只选择了一个
      this.multiple = !selection.length // 是否有选择数据
    },
    // 导入
    handleImport() {
      this.importDialog.show = true
      this.importDialog.action = `${this.baseURL}/${this.$route.path}/import`
    },
    // 导入成功
    importSuccess(data) {
      this.handleQuery() // 获得表格数据
      this.importDialog.show = false
    },
    // 导出
    handleExport() {
      window.open(`${this.baseURL}/${this.$route.path}/export`)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
