<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>
        </template>
        <hc-table :column="tableColumn" :datas="tableData" :loading="tableLoading" :index-style="{ width: 60 }" :is-current-row="false">
            <template #action="{ row }">
                <el-link type="warning" @click="editRowClick(row)">修改</el-link>
                <el-link type="primary" @click="setTempShow(row)">编辑计量系统单元</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="isDialogShow" widths="24rem" is-footer-center :title="formModel.id ? '新增' : '修改'" @close="dialogClose">
            <el-form ref="formRef" :model="formModel" :rules="formRules" size="large" label-width="auto">
                <el-form-item label="名称：" prop="name">
                    <el-input v-model="formModel.name" clearable placeholder="请输入模版名称" />
                </el-form-item>
                <el-form-item label="备注：">
                    <el-input v-model="formModel.remarks" clearable placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button hc-btn @click="dialogClose">取消</el-button>
                <el-button hc-btn type="primary" :loading="submitLoading" @click="dialogSubmit">提交</el-button>
            </template>
        </hc-dialog>

        <!-- 编辑计量系统单元 -->
        <HcTemp v-model="isTempShow" :data="tempData" />
    </hc-card>
</template>

<script setup>
import { nextTick, onActivated, onDeactivated, onUnmounted, ref } from 'vue'
import { deepClone, formValidate, getArrValue } from 'js-fast-way'
import HcTemp from './system-unit/temp.vue'
import { HcDelMsg } from 'hc-vue3-ui'
import mainApi from '~api/desk/system-unit'

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

//搜索表单
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 tableColumn = ref([
    { key: 'name', name: '模版名称' },
    { key: 'remarks', name: '备注' },
    { key: 'action', name: '操作', width: 220, align: 'center' },
])

//获取表格数据
const tableLoading = ref(true)
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 isDialogShow = ref(false)
const formRef = ref(null)
const formModel = ref({})
const formRules = { name: { required: true, trigger: 'blur', message: '请输入模版名称' } }

//新增
const addClick = async () => {
    formModel.value = {}
    await nextTick()
    isDialogShow.value = true
}

//修改
const editRowClick = async (row) => {
    formModel.value = deepClone(row)
    await nextTick()
    isDialogShow.value = true
}

//提交表单
const submitLoading = ref(false)
const dialogSubmit = async () => {
    const isForm = await formValidate(formRef.value)
    if (!isForm) return false
    submitLoading.value = true
    const { error, code } = await mainApi.submit(formModel.value)
    submitLoading.value = false
    if (!error && code === 200) {
        window?.$message?.success('操作成功')
        dialogClose()
        getTableData().then()
    }
}

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

//编辑计量系统单元
const isTempShow = ref(false)
const tempData = ref({})

// 编辑计量系统单元
const setTempShow = async (row) => {
    tempData.value = deepClone(row)
    await nextTick()
    isTempShow.value = true
}

//关闭弹窗
const dialogClose = () => {
    isDialogShow.value = false
    formModel.value = {}
}

//关闭
onDeactivated(() => {
    isTempShow.value = false
    isDialogShow.value = false
})

//卸载
onUnmounted(()=> {
    isTempShow.value = false
    isDialogShow.value = false
})
</script>
