<script lang="ts" setup name="SystemDictList">
import type { Ref } from 'vue'
import { getCurrentInstance, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { DictListInfo } from './dict-interface'
import SystemEditDict from './editDict.vue'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { delDict, getDictList } from '@/api/system/dict'

const { proxy } = getCurrentInstance() as any

const editDialog = ref() // 组件

// 默认查询条件
const defaultQuery = {
  condition: '',
  offset: 1,
  limit: 10,
  sort: '',
  order: '',
}
const total = ref(0)
// 查询条件
const listQuery = reactive({ ...defaultQuery })
// 搜索重置
function reset() {
  Object.assign(listQuery, defaultQuery)
  fetchData()
}
// 表格表头
const columns: Ref<TableColumn[]> = ref([])
columns.value = [
  { text: '字典名称', value: 'name', width: 180, align: 'center' },
  { text: '字典编码', value: 'code', width: 180, align: 'center' },
  { text: '字典描述', value: 'tips', width: 180, align: 'center' },
  { text: '字典详情', value: 'detail', align: 'center' },
]
// 数据列表
const list: Ref<DictListInfo[]> = ref([])
const loading = ref(false)

// 搜索按钮
function search() {
  fetchData()
}
// 查询数据
function fetchData() {
  loading.value = true
  getDictList(listQuery).then((res: { data: { rows: []; total: number } }) => {
    list.value = res.data.rows
    total.value = res.data.total
    loading.value = false
  })
}

// 编辑资源
function edit(row: DictListInfo) {
  editDialog.value.initDialog('update', row)
}
// 删除资源
function del(row: DictListInfo) {
  ElMessageBox.confirm(
    `确定要删除${row.name}吗？`,
    '确认删除',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    delDict(row.id).then(() => {
      ElMessage.success('删除成功')
      fetchData()
    })
  })
}
// 添加资源
function add() {
  editDialog.value.initDialog('create')
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
function changePage(val: { size: number; page: number }) {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}

onMounted(() => {
  fetchData()
})
</script>

<template>
  <app-container>
    <search-area @search="search">
      <search-item>
        <el-input v-model="listQuery.condition" placeholder="字典名称" />
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <icon-button v-if="proxy.hasPerm('/sys/dict/add')" icon="icon-add" title="新增" @click="add" />
      </template>
      <normal-table :query="listQuery" :loading="loading" :columns="columns" :data="list" :total="total" :border="false" @change="changePage">
        <template #columns>
          <el-table-column v-if="proxy.hasPerm('/sys/dict/update') || proxy.hasPerm('/sys/dict/delete')" label="操作" width="140" align="center">
            <template #default="scope">
              <el-button v-if="proxy.hasPerm('/sys/dict/update')" type="primary" link size="small" class="table-text-button" @click="edit(scope.row)">
                修改
              </el-button>
              <el-button v-if="proxy.hasPerm('/sys/dict/delete')" type="primary" link size="small" class="table-text-button" @click="del(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
    <system-edit-dict ref="editDialog" @close-refresh="search" />
  </app-container>
</template>
