<template>
  <div class="table-container">
    <el-card>
      <template #header>
        <div class="header">
          <el-form :inline="true" :model="searchFormState">
            <el-form-item label="角色名称">
              <el-input
                v-model="searchFormState.RoleName"
                clearable
                class="filter-item"
                style="width: 150px"
                placeholder="请输入角色名称"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
              <el-button type="primary" @click="handleReset">重置</el-button>
              <el-button
                type="primary"
                v-permission="['RoleAdd']"
                @click="handleAdd(-1, 1)"
                >新增</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </template>
      <div>
        <el-table
          :data="dataList"
          border
          style="width: 100%"
          v-loading="isLoading"
        >
          <el-table-column label="角色名称" prop="Name"></el-table-column>
          <el-table-column label="描述" prop="Description"></el-table-column>
          <el-table-column label="创建时间" prop="CreateTime"></el-table-column>
          <el-table-column label="修改时间" prop="UpdateTime"></el-table-column>
          <el-table-column fixed="right" label="操作" prop="describe">
            <template #default="scope">
              <el-button
                v-permission="['RoleEdit']"
                type="primary"
                size="mini"
                @click="handleAdd(scope.row.Id, scope.row.Status)"
              >
                编辑
              </el-button>
              <el-button
                v-show="scope.row.IsBase !== 1"
                type="danger"
                size="mini"
                @click="handleDel(scope.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParam.PageNum"
          :page-size="queryParam.PageSize"
          :page-sizes="[10, 20, 30, 40]"
          layout="total,sizes,pager,prev,next,jumper"
          :total="pagination.total"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { useRouter } from 'vue-router'
import { onMounted, toRefs } from 'vue'
import { getRoleList, deleteRole } from '@/api/role'
import { ElMessage } from 'element-plus'
import useLoadTableData from '@/hooks/useLoadTableData'

export default {
  setup() {
    //表格通用条件只是查询参数不一样，其他分页，loading等都一样
    const searchFormData = {
      RoleName: ''
    }

    //获取接口数据
    const fetchData = async (params) => {
      const data = await getRoleList(params)
      return data
    }

    //处理数据-拼接角色
    const handleData = (data) => {
      console.log(data)
    }

    //从钩子函数获取通用方法和对象
    const {
      loadData,
      handleSearch,
      handleSizeChange,
      handleCurrentChange,
      handleReset,
      state
    } = useLoadTableData(searchFormData, fetchData, handleData)

    onMounted(() => {
      //获取表格数据
      loadData(fetchData, handleData)
    })

    const router = useRouter()
    //新增
    const handleAdd = (Id, Status = 0) => {
      //禁用操作
      if (Status === 0) {
        ElMessage.error('用户被禁用')
        return
      }
      const argu = { Id }
      router.push({
        name: 'role_edit',
        params: argu
      })
    }

    //删除用户
    const handleDel = async (rowData) => {
      await deleteRole({ Id: rowData.Id })
      loadData(fetchData, handleData)
      ElMessage.success('操作成功')
    }

    //通用hook处理-重置，搜索，页面切换，数据获取
    return {
      handleAdd,
      ...toRefs(state),
      handleReset,
      handleSearch,
      handleSizeChange,
      handleCurrentChange,
      handleDel
    }
  }
}
</script>

<style lang="scss" scoped>
.table-container {
  .pagination {
    margin-top: 20px;
    text-align: center;
  }
  :deep(.el-form-item) {
    margin-bottom: 10px;
  }
}
</style>
