<!--  -->
<template>
    <div class="hc-page-box">
        <HcNewCard>
            <HcTable is-new :column="tableColumn" :datas="tableData">
                <template #inspectName="{ row }">
                    <span :class="row.isRectifyUser === 0 ? 'cursor-not-allowed' : 'text-link'" @click="tableRowEdit(row)">{{ row?.inspectName }}</span>
                </template>
                <template #isRectify="{ row }">
                    <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 == 1" type="primary" :disabled="row.isRectifyUser === 0" @click="changeRow(row)">整改</el-link>
                    <el-link v-else type="primary" :disabled="row.isRectifyUser === 0" @click="cancleSubmit(row)">撤回提交</el-link>
                </template>
            </HcTable>
            <template #action>
                <HcPages :pages="searchForm" @change="pageChange" />
            </template>
        </HcNewCard>
    </div>
</template>

<script setup>
import { onActivated, onMounted, ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import patrolApi from '~api/patrol/patrol'
import { useAppStore } from '~src/store'
import { getArrValue } from 'js-fast-way'
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([
    { key1: 'xx至xx高速公路-演示项（本地测试）', key2: '安全巡检', key3: '原地面检查', key4:'检查状态', key5:'需要整改', key6:'2023-09-27', key7:'2023-09-27', isType:true },
    { key1: 'xx至xx高速公路-演示项（本地测试）', key2: '安全巡检', key3: '原地面检查', key4:'检查状态', key5:'需要整改', key6:'2023-09-27', key7:'2023-09-27', isType:false },
    { key1: 'xx至xx高速公路-演示项（本地测试）', key2: '安全巡检', key3: '原地面检查', key4:'检查状态', key5:'需要整改', key6:'2023-09-27', key7:'2023-09-27', isType:true },
])
//搜索表单
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.page2({
        ...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)=>{
    const { isRectifyUser } = row
    if (isRectifyUser !== 0) {
        changeRow(row)
    }
   
}
//提交整改
const changeRow = (row)=>{
    router.push({
        path: '/patrol/add',
        query: {
            type: 'changeRow',
            id:row.id,
        },
    })
}
//撤回提交
const cancleSubmit = async (row)=>{
    tableLoading.value = true
    const { error, code, msg } = await patrolApi.revokeSubmit({
       id:row.id,

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

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