<template>
  <div class="content">
    <el-card shadow="never">
      <div class="project-name">
        <!-- <p>{{ $route.matched[1].meta.title }}</p> -->
      </div>
      <div class="search-header">
        <div class="tool-common-wrap">
          <div class="search-wrap">
            <quick-query ref="quick" :placeholder="placeholder" :is-date="false" @quickSearch="quickSearch" />
          </div>
          <div class="btn-wrap">
            <el-button type="primary" @click="userHandle('添加')">添加</el-button>
            <el-button type="primary" @click="handleAllDelete" :disabled="multipleSelection.length === 0">批量删除
            </el-button>
            <el-button type="primary" @click="rest()"> 刷新</el-button>
          </div>
        </div>
      </div>
      <el-table
        :data="tableList"
        v-loading="tableLoading"
        @selection-change="handleSelectionChange"
        :header-cell-style="{'text-align':'center'}"
        :cell-style="{'text-align':'center'}"
        style="width: 100%; margin: 15px 0">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column label="名称" show-overflow-tooltip prop="name" >
          <template slot-scope="scope">
                <el-link class="table-link" @click.native.prevent="userHandle('编辑', scope.row)"
                     :type="currentRow && currentRow.id && scope.row.id && currentRow.id === scope.row.id ? 'primary' : ''">
                 {{ scope.row.name }}
                </el-link>
         
              </template>
          </el-table-column>
        <el-table-column label="类型" show-overflow-tooltip prop="type" />
        <el-table-column label="默认" show-overflow-tooltip prop="defaults">
          <template slot-scope="scope">
            <div>{{ scope.row.defaults === 1 ? '是' : '否' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="禁用" show-overflow-tooltip prop="delete">
          <template slot-scope="scope">
            <div>{{ scope.row.delete === 1 ? '是' : '否' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="分配成员" :enterable="false" placement="top">
              <el-button
                class="icon-button"
                type="text"
                icon="el-icon-user"
                @click.native.prevent="userHandle('分配成员', scope.row)">
              </el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="分配权限" :enterable="false" placement="top">
              <el-button
                class="icon-button"
                type="text"
                icon="el-icon-setting"
                @click.native.prevent="userHandle('分配权限', scope.row)">
              </el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="编辑" :enterable="false" placement="top">
              <el-button
                class="icon-button"
                type="text"
                icon="el-icon-edit-outline"
                @click.native.prevent="userHandle('编辑', scope.row)">
              </el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除" :enterable="false" placement="top">
              <el-button
                class="icon-button"
                type="text"
                icon="el-icon-delete"
                @click.native.prevent="handleDelete(scope.row)">
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 15px"
        :page-size="page.pageSize"
        :current-page.sync="page.pageNum"
        background
        :total="page.total"
        @current-change="getData" />
    </el-card>
    <AddRoleManage v-model="dialogVisible" :item="editItem" @confirm="getData" />
    <tree v-model="treeDialog" :title="editItem.name+'--'+ '权限设置'" :item="editItem" />
    <allocationDialog v-model="businessDialogVisible" :title="editItem.name+'--'+ '分配成员'" :item="editItem" />
  </div>
</template>

<script>
import { queryRole, batchDeleteRole, deleteRole } from '@/api/system_manage'
import AddRoleManage from './AddRoleManage'
import tree from './tree'
import allocationDialog from './allocationDialog'
import EsbServiceListMixins from '@/mixins/esb_service_list_mixins'// 列表操作需要依赖
import QuickQuery from '@/components/quickquery/QuickQuery'

export default {
  mixins: [EsbServiceListMixins],
  components: {
    AddRoleManage,
    tree,
    allocationDialog,
    QuickQuery
  },
  data() {
    return {
      // 高级搜索框
      isPowerSearch: false,
      searchForm: {},
      tableLoading: false,
      tableList: [],
      dialogVisible: false,
      treeDialog: false,
      businessDialogVisible: false,
      multipleSelection: [],
      editItem: {},
      quickQueryKeys: ['name', 'type'],
      sort: {
        orderby: 'createon',
        sort: 'desc'
      },
      placeholder: '名称、类型'
    }
  },
  computed: {},
  created() {},
  methods: {
    handleDelete(item) {
      this.$confirm('是否确认删除', '提示', {
        type: 'warning'
      }).then(() => {
        deleteRole(item.id).then(rt => {
          if (rt.status === '200') {
            this.getData()
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: '删除失败',
              type: 'error'
            })
          }
        })
      }).catch(_ => {
      })
    },
    handleAllDelete() {
      this.$confirm('是否确认批量删除', '提示', {
        type: 'warning'
      }).then(() => {
        batchDeleteRole(this.multipleSelection.map(item => item.id)).then(rt => {
          if (rt.status === '200') {
            this.getData()
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: '删除失败',
              type: 'error'
            })
          }
        })
      }).catch(_ => {
      })
    },
    async getData() {
      this.tableLoading = true
      queryRole({
        logic: this.logic,
        filters: this.filters,
        ...this.sort,
        ...this.page
      }).then((rt) => {
        this.tableList = rt.data.list || []
        this.page.total = rt.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    userHandle(item, data) {
      if (item === '添加') {
        this.editItem = {}
        this.dialogVisible = true
      } else if (item === '编辑') {
        this.editItem = Object.assign({}, data)
        this.dialogVisible = true
      } else if (item === '分配成员') {
        this.editItem = Object.assign({}, data)
        this.businessDialogVisible = true
      } else if (item === '分配权限') {
        this.editItem = Object.assign({}, data)
        this.treeDialog = true
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    quickQueryData() {
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>
  .content{
    padding: 10px 10px;
  }
</style>
