<template>
  <div>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="68px"
    >
      <el-form-item label="编码" prop="configKey">
        <el-input
          v-model="queryParams.configKey"
          class="!w-240px"
          clearable
          placeholder="请输入"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"> 搜索 </el-button>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="编码" prop="configKey" />
      <el-table-column label="名称" prop="configName" />
      <el-table-column label="值" prop="configValue" />
      <el-table-column label="json" prop="configJson" />
      <el-table-column label="备注" prop="configDesc" />
      <el-table-column label="创建时间" prop="createTime" width="180" />
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button link type="primary" @click="openForm(row)"> 修改 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />

    <!-- 表单弹窗：添加/修改 -->
    <DictTypeForm ref="formRef" @success="getList" />
  </div>
</template>

<script name="SystemDictType" setup>
import { searchConfig } from '@/api/system/dict/config'
import DictTypeForm from './DictTypeForm.vue'

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 字典表格数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 50,
  configKey: ''
})
const queryFormRef = ref() // 搜索的表单

/** 查询字典类型列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await searchConfig(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (val) => {
  formRef.value.open(val)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
