<template>
    <hc-card-item class="hc-test-sample-card-item">
        <template #header>
            <div class="w-200px">
                <el-select v-model="searchForm.contractId" placeholder="选择合同段" filterable block>
                    <el-option v-for="item in contractData" :key="item.id" :label="item.contractName" :value="item.id" />
                </el-select>
            </div>
            <div class="ml-2 w-250px">
                <hc-date-picker :dates="betweenTime" clearable @change="betweenTimeUpdate" />
            </div>
            <div class="ml-2 w-250px">
                <hc-search-input v-model="searchForm.materialName" @search="searchClick" />
            </div>
        </template>
        <hc-table :column="tableColumn" :datas="tableData" :loading="tableLoading" :index-style="{ width: 60 }">
            <template #key9="{ row }">
                <el-button v-if="row.key9 === 1" type="success" size="small">已同意</el-button>
                <el-button v-if="row.key9 === 2" type="info" size="small">待审批</el-button>
                <el-button v-if="row.key9 === 3" type="danger" size="small">已废除</el-button>
            </template>
        </hc-table>
        <template #action>
            <hc-pages :pages="searchForm" @change="pageChange" />
        </template>
    </hc-card-item>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue'
import { useAppStore } from '~src/store'
import { getErtractInfo } from '~api/other'
import { getArrValue, getObjValue, isNullES } from 'js-fast-way'
import mainApi from '~api/tentative/material/testSample'

//参数
const props = defineProps({
    tree: {
        type: Object,
        default: () => ({}),
    },
})

//变量
const store = useAppStore()
const projectId = ref(store.getProjectId)
const contractId = ref(store.getContractId)

//渲染完成
onMounted(() => {
    getContractData()
})

//监听数据
const treeInfo = ref(props.tree)
watch(() => props.tree, (obj) => {
    treeInfo.value = getObjValue(obj)
    searchClick()
})

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

//获取合同段信息
const contractData = ref([])
const getContractData = async () => {
    const { data } = await getErtractInfo({
        projectId: projectId.value,
        contractId: contractId.value,
    })
    const res = getArrValue(data)
    contractData.value = res
    if (res.length > 0) {
        searchForm.value.contractId = res[0].id
        searchClick()
    }
}

//日期时间被选择
const betweenTime = ref(null)
const betweenTimeUpdate = ({ arr }) => {
    betweenTime.value = arr
    if (arr.length > 0) {
        searchForm.value.startTime = arr[0]
        searchForm.value.endTime = arr[1]
    } else {
        searchForm.value.startTime = null
        searchForm.value.endTime = null
    }
}

//搜索
const searchClick = () => {
    searchForm.value.current = 1
    getTableData()
}

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

//表格数据
const tableColumn = ref([
    { key: 'materialName', name: '样品名称' },
    { key: 'specificationModel', name: '规格型号', align: 'center' },
    { key: 'materialCount', name: '试样数量', align: 'center' },
    { key: 'calculationUnit', name: '计算单位', align: 'center' },
    { key: 'proposedPosition', name: '拟用部位' },
    { key: 'representativeCount', name: '代表数量', align: 'center' },
    { key: 'userName', name: '取样人', align: 'center' },
    { key: 'entrustTime', name: '委托单上报时间', align: 'center' },
])
const tableData = ref([])

//获取表格数据
const tableLoading = ref(false)
const getTableData = async () => {
    const { primaryKeyId } = treeInfo.value
    if (isNullES(primaryKeyId)) return
    tableLoading.value = true
    const { error, code, data } = await mainApi.queryPage({
        ...searchForm.value,
        projectId: projectId.value,
        contractId: contractId.value,
        nodeId: primaryKeyId || '',
    })
    //处理数据
    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
    }
}
</script>
