<template>
    <hc-new-card id-ref="hc-certificate-list" div-p="12px">
        <template #header>
            <div class="w-100">
                <el-select v-model="searchForm.projectId" filterable block placeholder="选择项目" @change="searchClick">
                    <el-option v-for="item in projectData" :key="item.id" :label="item.projectName" :value="item.id" />
                </el-select>
            </div>
        </template>
        <template #extra>
            <el-button hc-btn type="primary" @click="addClick">新增</el-button>
        </template>
        <hc-table :loading="tableLoading" :column="tableColumn" :datas="tableData" :index-style="{ width: 60 }" is-new>
            <template #certificateType="{ row }">{{ row.certificateType === 1 ? '个人证书' : '企业证书' }}</template>
            <template #isRegister="{ row }">
                <el-link v-if="row.isRegister === 0" type="primary" @click="registerRowClick(row)">注册</el-link>
                <span v-else>已注册</span>
            </template>
            <template #action="{ row }">
                <el-link type="warning" @click="editRowClick(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-new-drawer v-model="isRowDrawer" to-id="hc-certificate-list">
            <HcListForm v-if="isRowDrawer" :info="tableRowItem" @close="isRowDrawer = false" />
        </hc-new-drawer>
    </hc-new-card>
</template>

<script setup>
import { nextTick, onActivated, ref } from 'vue'
import { getArrValue } from 'js-fast-way'
import { delMessage } from '~uti/tools'
import HcListForm from './modules/list/form.vue'
import adminApi from '~api/certificate/admin'
import mainApi from '~api/certificate/list'

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

//项目列表
const projectData = ref([])
const getProjectData = async () => {
    const { data } = await adminApi.queryProjectList()
    const res = getArrValue(data)
    if (res.length > 0) {
        searchForm.value.projectId = res[0].id
    } else {
        searchForm.value.projectId = ''
    }
    projectData.value = res
    searchClick()
}

//搜索条件
const searchForm = ref({ projectId: '', current: 1, size: 20, 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: 'certificateUserName', name: '证书所有者' },
    { key: 'certificateId', name: '证书ID/企业统一社会信用代码' },
    { key: 'certificateType', name: '证书类型' },
    { key: 'isRegister', name: '注册' },
    { key: 'action', name: '操作', width: 100, align: 'center' },
])
const tableData = ref([])

//获取表格数据
const tableLoading = ref(true)
const getTableData = async () => {
    tableData.value = []
    tableLoading.value = true
    const { error, code, data } = await mainApi.page({
        ...searchForm.value,
        total: null,
    })
    tableLoading.value = false
    if (!error && code === 200) {
        tableData.value = getArrValue(data['records'])
        searchForm.value.total = data['total']
    } else {
        tableData.value = []
        searchForm.value.total = 0
    }
}

const isRowDrawer = ref(false)
const tableRowItem = ref({})

//新增
const addClick = () => {
    tableRowItem.value = {}
    nextTick(() => {
        isRowDrawer.value = true
    })
}

//修改
const editRowClick = (row) => {
    tableRowItem.value = row
    nextTick(() => {
        isRowDrawer.value = true
    })
}

//注册
const registerRowClick = async (row) => {
    const { error, code, msg } = await mainApi.goRegister({
        key: row.id,
    })
    if (!error && code === 200) {
        window.$message.success('操作成功')
        getTableData()
    } else {
        window.$message.error(msg ?? '操作失败')
    }
}

//删除
const delRowClick = (row) => {
    delMessage(async () => {
        const { code, msg } = await mainApi.del(row.id)
        if (code === 200) {
            window.$message.success('删除成功')
            getTableData()
        } else {
            window.$message.error(msg ?? '删除失败')
        }
    })
}
</script>
