<template>
    <hc-new-card>
        <template #extra>
            <HcNewSwitch :datas="tabData" :keys="tabKey" @change="tabChange" />
        </template>
        <HcTable ref="tableRef" :column="tableColumn" :datas="tableData" :loading="tableLoading" is-new :index-style="{ width: 60 }">
            <template #sourceType="{ row }">
                <span class="text-green">{{ row?.sourceType == 0 ? '巡检' : '验收' }}</span>
            </template>
            <template #fileName="{ row }">
                <span class="text-link" @click="tableRowName(row)">{{ row?.fileName }}</span>
            </template>
        </HcTable>
        <template #action>
            <HcPages :pages="searchForm" :sizes="[20, 50, 100, 200, 300, 500]" @change="pageChange" />
        </template>
    </hc-new-card>
</template>

<script setup>
import { nextTick, ref, watch } from 'vue'
import archiveFileApi from '~api/archiveFile/archiveFile'
import earlyApi from '~api/custody/early'
import { getArrValue } from 'js-fast-way'
import { toPdfPage } from '~uti/btn-auth'

//参数
const props = defineProps({
    projectId: {
        type: [String, Number],
        default: '',
    },
    contractId: {
        type: [String, Number],
        default: '',
    },
    treeData: {
        type: Object,
        default: () => ({}),
    },
})

//变量
const projectId = ref(props.projectId)
const contractId = ref(props.contractId)
const nodeData = ref(props.treeData)

//监听
watch(() => [
    props.treeData,
], ([treeData]) => {
    nodeData.value = treeData
    setQueryData(treeData)
},
{
    deep: true,
})

//渲染完成
nextTick(() => {
    // setQueryData(props.treeData)
})

//获取相关数据
const setQueryData = (data) => {
    searchForm.value.nodeIds = data.id
    nodeData.value.id = data.id
    getTableData()
    /*const cid = data?.contractIdRelation || ''
    const wbsId = data['contractIdRelation'] ? data['id'] : data['primaryKeyId']
    if (wbsId) {
        searchInternalForm.value.contractId = cid ? cid : contractId.value;
        searchInternalForm.value.contractIdRelation = data['contractIdRelation']
        searchInternalForm.value.wbsIds = [wbsId]
        searchInternalClick()
    }*/
}

//搜索表单
const searchForm = ref({
    current: 1, size: 20, total: 0,
})

//tab数据和相关处理
const tabKey = ref('1')
const tabData = ref([
    { key:'1', name: '需要整改的文件' },
    { key:'2', name: '已整改记录' },
])
const tabChange = (item) => {
    tabKey.value = item?.key
    getTableData()
}

//名称被点击
const tableRowName = (row) => {
    if (row['fileUrl']) {
        toPdfPage(row['fileUrl'])
    } else {
        window.$message?.warning('文件不存在')
    }
}

//分页被点击
const pageChange = ({ current, size }) => {
    searchForm.value.current = current
    searchForm.value.size = size
    getTableData()
}

//内业台账表头
const tableRef = ref(null)
const tableColumn = ref([
    { key:'sourceType', name: '来源', width:150 },
    { key:'archiveName', name: '所属案卷' },
    { key:'fileName', name: '具体文件' },
    { key:'allopinion', name: '问题描述' },
])
const tableData = ref([])

//获取数据
const tableLoading = ref(false)
const getTableData = async () => {
    if (tabKey.value !== '1') {
        if (nodeData.value.id) {
            tableLoading.value = true
            const { error, code, data } = await archiveFileApi.getarchiveFilePage({
                ...searchForm.value,
                nodeIds: nodeData.value.id,
                projectId: projectId.value,
                contractId: contractId.value,
                rectification:tabKey.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
            }
        }
    } else {
        tableLoading.value = true
            const { error, code, data } = await earlyApi.warningPage({
                ...searchForm.value,
                nodeIds: nodeData.value.id,
                projectId: projectId.value,
                contractId: contractId.value,
                rectification:tabKey.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
            }
        }
    }



// 暴露出去
defineExpose({
    getTableData,

})
</script>
