<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" show-overflow-tooltip />
        <el-table-column align="center" label="字段" prop="code" show-overflow-tooltip />
        <el-table-column align="center" label="描述" prop="description" show-overflow-tooltip />
        <el-table-column align="center" label="操作" width="120">
          <template #default="{ row }">
            <el-button v-if="row.status != 1" link type="primary" @click="edit(row)">
              编辑
            </el-button>

            <el-button v-if="row.status != 1" 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"
      :categoryList="categoryList"
      @closed="isShowDialog = false"
      @on-success="getList"
    />
  </div>
</template>

<script setup>
import { useTableRPC } from '@/utils/hook'
import HandleDialog from './HandleDialog.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: '/template/list',
  deleteConfig: {
    remote: '/template/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>
