<template>
    <hc-card title="试验汇总分类配置">
        <template #extra>
            <el-button hc-btn type="primary" @click="addRowClick">新增</el-button>
        </template>
        <hc-table :column="tableColumn" :datas="tableData" :loading="tableLoading" :index-style="{ width: 60 }">
            <template #action="{ row }">
                <el-link type="primary" @click="editRowClick(row)">编辑</el-link>
                <el-link type="warning" @click="partitioningClick(row)">配置划分</el-link>
                <el-link type="success" @click="associationList(row)">关联清表</el-link>
                <el-link type="info">数据映射配置</el-link>
                <el-link type="danger" @click="delRowClick(row)">删除</el-link>
            </template>
        </hc-table>
        <template #action>
            <hc-pages :pages="searchForm" @change="pageChange" />
        </template>

        <!-- 新增/修改 -->
        <hc-dialog v-model="isRowInfoShow" widths="400px" is-footer-center :title="isNullES(formModel.id) ? '新增' : '修改'" @close="rowInfoClose">
            <el-form ref="formRef" :model="formModel" :rules="formRules" label-position="top" label-width="auto" size="large">
                <el-form-item label="分类名称：" prop="className">
                    <el-input v-model="formModel.className" clearable placeholder="请输入分类名称" />
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button hc-btn @click="rowInfoClose">取消</el-button>
                <el-button hc-btn type="primary" :loading="rowInfoLoading" @click="rowInfoSubmit">提交</el-button>
            </template>
        </hc-dialog>
        <!-- 配置划分 -->
        <HcPartitioning v-model="isPartitioningShow" :data="partitioningData" @close="partitioningClose" @finish="getTableData" />
        <!-- 关联清表 -->
        <HcAssociationList
            v-model="isAssociationShow"
            :info="associationInfo"
            :type="3"
        />
    </hc-card>
</template>

<script setup>
import { nextTick, onActivated, ref } from 'vue'
import { deepClone, formValidate, getArrValue, isNullES } from 'js-fast-way'
import { HcDelMsg } from 'hc-vue3-ui'
import HcPartitioning from './test-collect/partitioning.vue'
import mainApi from '~api/desk/test-collect'
import HcAssociationList from '../project/list/association-list.vue'

//激活
onActivated(() => {
    searchClick()
})

//搜索表单
const searchForm = ref({ 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 tableData = ref([])
const tableColumn = ref([
    { key: 'className', name: '分类名称' },
    { key: 'action', name: '操作', width: 320, align: 'center' },
])

//获取表格数据
const tableLoading = ref(false)
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 addRowClick = () => {
    formModel.value = {}
    isRowInfoShow.value = true
}

//编辑
const editRowClick = (item) => {
    formModel.value = deepClone(item)
    isRowInfoShow.value = true
}

//新增编辑弹窗
const isRowInfoShow = ref(false)
const rowInfoLoading = ref(false)

//表单
const formRef = ref(null)
const formModel = ref({})
const formRules = { className: { required: true, trigger: 'blur', message: '请输入分类名称' } }

//确认提交
const rowInfoSubmit = async () => {
    const isForm = await formValidate(formRef.value)
    if (!isForm) return
    rowInfoLoading.value = true
    const { isRes } = await mainApi.submit(formModel.value)
    rowInfoLoading.value = false
    if (!isRes) return
    window.$message.success('操作成功')
    rowInfoClose()
    getTableData().then()
}

//关闭表单
const rowInfoClose = () => {
    isRowInfoShow.value = false
    rowInfoLoading.value = false
    formModel.value = {}
}

//删除
const delRowClick = (item) => {
    HcDelMsg(async (resolve) => {
        const { isRes } = await mainApi.del(item.id)
        resolve() //关闭弹窗的回调
        if (!isRes) return
        window.$message.success('删除成功')
        getTableData().then()
    })
}

//配置划分
const isPartitioningShow = ref(false)
const partitioningData = ref({})
const partitioningClick = async (item) => {
    partitioningData.value = deepClone(item)
    await nextTick()
    isPartitioningShow.value = true
}

//关闭 配置划分
const partitioningClose = () => {
    isPartitioningShow.value = false
    partitioningData.value = {}
}
//关联清表
const isAssociationShow = ref(false)
const associationInfo = ref({})
const associationList = async (item) => {
    associationInfo.value = item
    await nextTick()
    isAssociationShow.value = true
}
</script>

<style lang="scss">

</style>
