<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="角色名称">
              <a-input allowClear v-model.trim="queryParam.name" @pressEnter="$refs.table.refresh(true)" id="roleList-name" placeholder="请输入角色名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="状态">
              <v-select code="ENABLE_FLAG" v-model="queryParam.isEnable" id="roleList-isEnable" allowClear placeholder="请选择状态" ></v-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin-right: 10px;" id="roleList-handSubmit">查询</a-button>
            <a-button @click="reset" id="roleList-reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-divider/>
    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="openModal" v-hasPermission="'B_supervision_role_add'" id="roleList-openModal">新增</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      :rowKey="(row)=>row.id"
      :columns="columns"
      :data="loadData"
      bordered
    >
      <span slot="status" slot-scope="text, record">
        <a-switch
          checkedChildren="启用"
          unCheckedChildren="禁用"
          v-model="record.isEnable"
          @change="changeFlagHandle(text, record)"
          v-hasPermission="'B_supervision_role_enable'"
          id="roleList-changeFlagHandle"/>
      </span>
      <!-- 角色描述 -->
      <template slot="remarks" slot-scope="text, record">
        <span :title="record.remarks" style="display: inline-block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{{ record.remarks }}</span>
      </template>
      <span slot="action" slot-scope="text, record">
        <a-icon
          type="setting"
          title="菜单权限"
          class="actionBtn orange"
          @click="openMenuModal(record)"
          v-hasPermission="'M_supervision_role_menu'" />
        <a-icon
          type="edit"
          title="编辑"
          class="actionBtn blue"
          @click="handleEdit(record)"
          v-hasPermission="'B_supervision_role_edit'" />
        <a-icon
          type="delete"
          title="删除"
          class="actionBtn red"
          v-if="!record.isEnable"
          v-hasPermission="'B_supervision_role_del'"
          @click="delect(record)"/>
      </span>
    </s-table>
    <!-- 新增、编辑角色弹窗 -->
    <roleModal :visible="showModal" :data="itemData" @refresh="refreshTable" @close="showModal = false"></roleModal>
    <!-- 菜单权限弹窗 -->
    <menuModal :visible="showMenuModal" :data="menuData" @close="showMenuModal = false"></menuModal>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import roleModal from './roleModal.vue'
import menuModal from './menuModal.vue'
import { getPowerRoleList, updateEnableStatus, delectRolePower, getMenuList } from '@/api/supervision-role.js'
export default {
  name: 'RoleList',
  components: {
    STable, VSelect, roleModal, menuModal
  },
  data () {
    return {
      // 查询参数
      queryParam: {
        name: '',
        isEnable: ''
      },
      showModal: false,
      showMenuModal: false,
      itemData: {}, // 编辑行数据
      menuData: {},
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'no',
          width: 60,
          align: 'center'
        },
        {
          title: '创建时间',
          dataIndex: 'createDate',
          width: 200,
          align: 'center'
        },
        {
          title: '角色名称',
          dataIndex: 'name',
          width: 150,
          align: 'center'
        },
        {
          title: '角色描述',
          dataIndex: 'remarks',
          width: 200,
		  ellipsis: true, // 内容超过宽度 自动出现省略号
          align: 'center',
		  scopedSlots: { customRender: 'remarks' }
        },
        {
          title: '状态',
          width: 150,
          align: 'center',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          width: 200,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getPowerRoleList(Object.assign(parameter, this.queryParam)).then(res => {
          const no = (res.data.pageNo - 1) * res.data.pageSize
          for (let i = 0; i < res.data.list.length; i++) {
            const _item = res.data.list[i]
            _item.no = no + i + 1
            _item.isEnable = _item.isEnable == 1
          }
          return res.data
        })
      },
      optionAlertShow: false
    }
  },
  methods: {
    // 刷新列表
    refreshTable () {
      this.$refs.table.refresh(true)
    },
    // 新建
    openModal () {
      this.showModal = true
      this.itemData = {}
    },
    // 打开分配权限弹窗
    openMenuModal (row) {
      this.getMenuList(row.id)
    },
    // 获取菜单树列表
    getMenuList (id) {
      const _this = this
      getMenuList({ id: id }).then(res => {
        if (res.status == 200) {
          _this.menuData = res.data
          this.showMenuModal = true
        }
      })
    },
    //  重置
    reset () {
      this.queryParam = {
        name: '',
        isEnable: ''
      }
      this.$refs.table.refresh(true)
    },
    // 删除
    delect (row) {
      const _this = this
      this.$confirm({
        title: '警告',
        content: '删除后无法恢复，确认删除?',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          delectRolePower({
            id: row.id
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
          })
        }
      })
    },
    handleEdit (row) {
      this.showModal = true
      this.itemData = row
    },
    // 修改状态
    changeFlagHandle (text, record) {
      const _data = {
        id: record.id,
        flag: record.isEnable ? '1' : '0'
      }
      updateEnableStatus(_data).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
        } else {
          record.isEnable = !record.isEnable
        }
      })
    }
  }
}
</script>
<style scoped>
  .table-page-search-wrapper .ant-form-inline .ant-form-item{
    margin-bottom: 0;
  }
  .action-button{
    line-height: 40px;
    white-space: nowrap;
    padding: 5px 10px;
    background-color: #1890ff;
    border-radius: 4px;
    color: #fff;
    margin-right: 5px;
  }
  .red-text{
    background-color: red;
  }
  .menu-text {
    background-color: #f90;
  }
</style>
