<script setup name="roleManage">
import { reactive, ref, getCurrentInstance, onMounted } from 'vue'
import roleDrawer from './roleDrawer.vue'
import { getUserRoleList, del } from '@/http/apis/systemManage/roleManage'
const { proxy } = getCurrentInstance(),
  searchForm = reactive({
    name: undefined
  }),
  tableListRef = ref(),
  total = ref(0),
  column = reactive([
    {
      label: '序号',
      type: 'index',
      width: '80'
    },
    {
      label: '角色名称',
      key: 'name',
      prop: 'name',
      width: '250'
    },
    {
      label: '角色描述',
      key: 'describe',
      prop: 'describe',
      minWidth: '180',
      showOverflowTooltip: true
    },
    {
      label: '创建时间',
      key: 'createOn',
      prop: 'createOn',
      width: '180'
    },
    {
      label: '操作',
      slot: 'action',
      width: '120',
      fixed: 'right'
    }
  ]),
  data = ref([]),
  getTableData = async (pageParams = tableListRef.value.pageParams) => {
    const res = await getUserRoleList({ ...pageParams, ...searchForm })
    data.value = res.data.records
    total.value = res.data.total
  },
  search = () => {
    getTableData()
  },
  reset = () => {
    searchForm.name = undefined
    tableListRef.value.pageParams.pageNumber = 1
    tableListRef.value.pageParams.pageSize = 10
    getTableData()
  },
  // 操作弹窗
  dialogData = reactive({
    visible: false,
    rowData: undefined
  }),
  add = () => {
    dialogData.visible = true
    dialogData.rowData = undefined
  },
  edit = row => {
    dialogData.visible = true
    dialogData.rowData = row
  },
  delData = data => {
    proxy.$messageBox
      .confirm('是否删除该角色?', '提示！', {
        type: 'warning'
      })
      .then(async () => {
        await del([data.id])
        proxy.$message.success('删除成功！')
        getTableData()
      })
  },
  close = (flag) => {
    dialogData.visible = false
    flag && getTableData()
  }
onMounted(() => {
  getTableData()
})
</script>

<template>
  <el-card shadow="never" class="search mb-16">
    <el-form label-suffix=":" :model="searchForm" size="small">
      <el-row :gutter="16">
        <el-col :span="8">
          <el-form-item label="角色名称">
            <el-input v-model="searchForm.name" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item class="btn">
            <el-button type="primary" @click="search">查询</el-button>
            <el-button @click="reset">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
  <el-card shadow="never">
    <template #header>
      <div class="flex justify-between items-center">
        <span>角色列表</span>
        <el-button
          type="primary"
          icon="Plus"
          size="small"
          @click="add"
        >
          新增角色
        </el-button>
      </div>
    </template>
    <table-list
      ref="tableListRef"
      :column="column"
      :data="data"
      :total="total"
      @get-table-data="getTableData"
    >
      <template #action="{ scope }">
        <a class="text-primary" @click="edit(scope.row)">编辑</a>
        <a class="text-danger" @click="delData(scope.row)">删除</a>
      </template>
    </table-list>
  </el-card>
  <role-drawer
    :visible="dialogData.visible"
    :data="dialogData.rowData"
    @close="close"
  />
</template>
