<template>
    <hc-card>
        <template #header>
            <div class="w-60">
                <hc-search-input v-model="searchForm.name" placeholder="请输入模板名称" @search="searchClick" />
            </div>
        </template>
        <template #extra>
            <el-button hc-btn type="primary" @click="addClick">新增</el-button>
            <el-button hc-btn type="danger" @click="delClick">删除</el-button>
        </template>
        <hc-table
            :column="tableColumn" :datas="tableData" :loading="tableLoading" :index-style="{ width: 60 }"
            is-check :check-style="{ width: 29 }" @selection-change="tableCheckChange"
        >
            <template #tableTemplateType="{ row }">
                <span>{{ getTableTempTypeName(row.tableTemplateType) }}</span>
            </template>
            <template #action="{ row }">
                <el-link type="warning" @click="editRowClick(row)">修改</el-link>
                <el-link v-del-com:[delRowClick]="row" type="danger">删除</el-link>
                <el-link type="primary" @click="clearingTableTemp(row)">清表模板</el-link>
                <el-link type="primary" @click="elementRecognition(row)">元素识别</el-link>
            </template>
        </hc-table>
        <template #action>
            <hc-pages :pages="searchForm" @change="pageChange" />
        </template>
        <!-- 新增编辑 -->
        <hc-dialog v-model="isAddEditForm" widths="400px" :title="`${isNullES(addEditForm.id) ? '新增' : '编辑'}清表信息`" is-footer-center @close="addEditFormClose">
            <el-form ref="addEditFormRef" :model="addEditForm" :rules="addEditFormRules" label-position="top" label-width="auto">
                <el-form-item label="模板名称：" prop="name">
                    <el-input v-model="addEditForm.name" clearable placeholder="请输入模板名称" />
                </el-form-item>
                <el-form-item label="模板类型：" prop="tableTemplateType">
                    <el-select v-model="addEditForm.tableTemplateType" filterable block placeholder="请选择模板类型">
                        <el-option v-for="item in tableTempType" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button hc-btn @click="addEditFormClose">取消</el-button>
                <el-button hc-btn type="primary" :loading="addEditFormLoading" @click="addEditFormSubmit">提交</el-button>
            </template>
        </hc-dialog>
        <!-- 清表模板 -->
        <HcClearTemplate v-model="isRowTempShow" :data="rowTempInfo" @close="getTableData" />
        <!-- 元素识别 -->
        <HcElementRecognition v-model="isElementShow" :data="rowElementInfo" @close="getTableData" />
    </hc-card>
</template>

<script setup>
import { HcDelMsg } from 'hc-vue3-ui'
import { onActivated, onDeactivated, ref } from 'vue'
import { getDictionaryData } from '~uti/tools'
import { arrToId, deepClone, formValidate, getArrValue, isNullES } from 'js-fast-way'
import HcClearTemplate from './excel/template.vue'
import HcElementRecognition from './element/index.vue'
import mainApi from '~api/exctab/exceltab'

//激活
onActivated(() => {
    searchForm.value.current = 1
    tableTemplateType()
    searchClick()
})

//获取模板类型
const tableTempType = ref([])
const tableTemplateType = async () => {
    tableTempType.value = await getDictionaryData('table_template_type', true)
}
const getTableTempTypeName = (key) => {
    if (isNullES(key)) return '-'
    return tableTempType.value.find(item => item.value === key)?.label
}

//搜索表单
const searchForm = ref({ parentId: 0, current: 1, size: 30, total: 0 })

//搜索
const searchClick = () => {
    searchForm.value.current = 1
    getTableData()
}

//分页
const pageChange = ({ current, size }) => {
    searchForm.value.current = current
    searchForm.value.size = size
    getTableData()
}

//表格数据
const tableColumn = ref([
    { key: 'name', name: '模板名称' },
    { key: 'tableTemplateType', name: '模板类型', width: 120, align: 'center' },
    { key: 'tabCout', name: '表数量', width: 100, align: 'center' },
    { key: 'createTime', name: '创建时间', width: 180, align: 'center' },
    { key: 'action', name: '操作', width: 220, align: 'center' },
])

//获取表格数据
const tableLoading = ref(false)
const tableData = ref([{}])
const getTableData = async () => {
    tableData.value = []
    tableLoading.value = true
    const { data } = await mainApi.page({
        ...searchForm.value,
        total: null,
    })
    tableLoading.value = false
    tableData.value = getArrValue(data?.records)
    searchForm.value.total = data?.total || 0
}

//表格被选择
const tableCheckKeys = ref([])
const tableCheckChange = (rows) => {
    tableCheckKeys.value = rows
}

//新增编辑清表信息
const isAddEditForm = ref(false)
const addEditFormRef = ref(null)
const addEditForm = ref({})
const addEditFormRules = {
    name: { required: true, trigger: 'blur', message: '请输入模板名称' },
    tableTemplateType: { required: true, trigger: 'blur', message: '请选择模板类型' },
}

//新增
const addClick = () => {
    addEditForm.value = {}
    isAddEditForm.value = true
}

//修改
const editRowClick = (row) => {
    isAddEditForm.value = true
    addEditForm.value = deepClone(row)
}

//提交保存
const addEditFormLoading = ref(false)
const addEditFormSubmit = async () => {
    const isForm = await formValidate(addEditFormRef.value)
    if (!isForm) return
    addEditFormLoading.value = true
    const { isRes } = await mainApi.submit(addEditForm.value)
    addEditFormLoading.value = false
    if (!isRes) return
    window.$message.success('操作成功')
    addEditFormClose()
    getTableData().then()
}

//关闭弹窗
const addEditFormClose = () => {
    isAddEditForm.value = false
    addEditForm.value = {}
}

//单个删除
const delRowClick = async ({ item }, resolve) => {
    await delDataApi(item.id)
    resolve()
}

//批量删除
const delClick = () => {
    const rows = tableCheckKeys.value
    if (rows.length <= 0) {
        window.$message.warning('请先勾选要删除的数据')
        return
    }
    HcDelMsg(async (resolve) => {
        await delDataApi(arrToId(rows, 'id'))
        resolve() //关闭弹窗的回调
    })
}

//删除数据
const delDataApi = async (id) => {
    const { isRes } = await mainApi.del(id)
    if (!isRes) return
    window.$message.success('删除成功')
    getTableData().then()
}

//清表模板
const isRowTempShow = ref(false)
const rowTempInfo = ref({})
const clearingTableTemp = (row) => {
    rowTempInfo.value = row
    isRowTempShow.value = true
}

//元素识别
const isElementShow = ref(false)
const rowElementInfo = ref({})
const elementRecognition = (row) => {
    rowElementInfo.value = row
    isElementShow.value = true
}

//离开了当前页面
onDeactivated(() => {
    isRowTempShow.value = false
    isElementShow.value = false
})
</script>
