<template>
  <div>
    <div class="title">分类管理</div>
    <el-form
      ref="searchFormRef"
      class="form"
      :inline="true"
      label-width="98px"
      :model="queryParams"
    >
      <el-form-item label="分类名称" prop="name">
        <el-input
          clearable
          placeholder="请输入"
          style="width: 220px"
          v-model="queryParams.name"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label-width="100px">
        <el-button icon="Search" type="primary" @click="handleQuery"> 搜索 </el-button>
        <el-button icon="RefreshRight" @click="resetQuery"> 重置 </el-button>
      </el-form-item>
    </el-form>

    <div style="margin-bottom: 20px">
      <el-button icon="Plus" type="primary" @click="add"> 新建 </el-button>
    </div>
    <div ref="tableContainerEl" class="table_border table">
      <el-table
        ref="tableRef"
        :data="tableData"
        v-loading="loading"
        header-row-class-name="table-header"
      >
        <el-table-column align="center" label="分类名称" prop="name" />
        <el-table-column align="center" label="排序" prop="sort" />
        <el-table-column align="center" label="操作">
          <template #default="{ row }">
            <el-button link type="primary" @click="edit(row)"> 编辑 </el-button>

            <el-button link type="danger" @click="handleDelete(row)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:limit="queryParams.limit"
      v-model:page="queryParams.page"
      @pagination="getList"
    />

    <HandleDialog
      v-if="isShowDialog"
      :type="rowType"
      :row="currentRow"
      @closed="isShowDialog = false"
      @on-success="getList"
    />
  </div>
</template>

<script setup>
import { useTableRPC } from '@/utils/hook'
import dayjs from 'dayjs'
import RPC from '@/utils/rpc'
import HandleDialog from './DataManageDialog.vue'

const rowType = ref('')
const currentRow = ref(null)
const add = () => {
  isShowDialog.value = true
  currentRow.value = null
  rowType.value = 'add'
}
const edit = (row) => {
  isShowDialog.value = true
  rowType.value = 'edit'
  currentRow.value = row
}

const {
  loading,
  tableData,
  tableRef,
  searchFormRef,
  total,
  queryParams,
  getList,
  handleQuery,
  resetQuery,
  handleDelete,
  isShowDialog,
  maxHeight,
  tableContainerEl,
} = useTableRPC({
  queryParams: {},
  remoteDataFn: '/content/type/list',
  deleteConfig: {
    remote: '/content/type/delete',
    key: 'id',
    message(row) {
      return `确认删除 “${row.name}”`
    },
  },
})

getList()
</script>

<style lang="scss" scoped>
.title {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 20px;
}
</style>
