<!--  -->
<template>
    <div class="hc-page-box">
        <HcNewCard>
            <HcTable is-new :column="tableColumn" :datas="tableData">
                <template #inspectName="{ row }">
                    <span class="text-link" @click="tableRowEdit(row)">{{ row?.inspectName }}</span>
                </template>
                <template #isRectify="{ row }">
                    <!-- <span v-if="row.isRectify == 1">需要</span>
                    <span v-else>不需要</span> -->
                    <el-tag
                        v-if="row?.isRectify"
                        :type="`${row.isRectify === 3 ? 'success' : row.isRectify === 1 ? 'warning' : 'info'}`" class="mx-1" effect="dark"
                    >
                        {{ row.isRectify === 3 ? '已整改' : row.isRectify === 2 ? '不需要' : '需要' }}
                    </el-tag>
                </template>
                <template #action="{ row }">
                    <el-link v-if="row.submitRectify == 2 && row.isRectify == 1" type="primary" @click="reviewRow(row)">复核</el-link>
                    <el-link type="primary" :disabled="row.submitRectify === '2'" @click="updateRow(row)">修改</el-link>
                    <el-link type="primary" :disabled="row.submitRectify === '2'" @click="delRow(row)">删除</el-link>
                </template>
            </HcTable>
            <template #action>
                <HcPages :pages="searchForm" @change="pageChange" />
            </template>
        </HcNewCard>
    </div>
</template>

<script setup>
import { onActivated, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import patrolApi from '~api/patrol/patrol'
import { useAppStore } from '~src/store'
import { getArrValue } from 'js-fast-way'
import { delMessageV2 } from '~com/message/index.js'
const useAppState = useAppStore()
const projectId = ref(useAppState.getProjectId)
const contractId = ref(useAppState.contractId)
//初始变量
const router = useRouter()
const tableColumn = [
    { key: 'projectName', name: '项目' },
    { key: 'inspectTypeName', name: '检查类别' },
    { key: 'inspectName', name: '检查名称' },
    { key: 'reviewInspectStatusName', name: '复核检查状态' },
    { key: 'isRectify', name: '是否需要整改' },
    { key: 'rectifyDate', name: '需求整改完成日期' },
    { key: 'actualRectifyDate', name: '实际整改完成日期' },
    { key: 'action', name: '操作' },
]
const tableData = ref([])
//搜索表单
const searchForm = ref({
    current: 1, size: 20, total: 0,
})
//分页被点击
const pageChange = ({ current, size }) => {
    searchForm.value.current = current
    searchForm.value.size = size
    getTableData()
}
const tableLoading = ref(false)
const getTableData = async () => {
    tableLoading.value = true
    const { error, code, data } = await patrolApi.queryPage({
        ...searchForm.value,
        projectId: projectId.value,
        contractId:contractId.value,

    })
    //判断状态
    tableLoading.value = false
    if (!error && code === 200) {
        tableData.value = getArrValue(data['records'])
        searchForm.value.total = data['total'] || 0
    } else {
        tableData.value = []
        searchForm.value.total = 0
    }
}
onActivated(()=>{
    getTableData()
})
const tableRowEdit = (row)=>{
    router.push({
        path: '/patrol/add',
        query: {
            type: 'view',
            id:row.id,
        },
    })
}
const updateRow = (row)=>{
    router.push({
        path: '/patrol/add',
        query: {
            type: 'confirmChange',
            id:row.id,
        },
    })
}
const reviewRow = (row)=>{
    router.push({
        path: '/patrol/add',
        query: {
            type: 'review',
            id:row.id,
        },
    })
}
//删除
const delRow = (row)=>{
    delMessageV2(async (action, instance, done) => {
            if (action === 'confirm') {
                instance.confirmButtonLoading = true
                delInfo(row)
                instance.confirmButtonLoading = false
                done()
            } else {
                done()
            }
     })
}
const delInfo = async (row)=>{
    const { error, code, msg } = await patrolApi.delete({
        id:row.id,

    })
    //判断状态
    if (!error && code === 200) {
        window.$message.success(msg)
        getTableData()
    } 
}
</script>

<style lang='scss' scoped>
</style>