<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>
        <template #extra>
            <el-button type="danger" :disabled="tableCheckedKeys.length <= 0" @click="batchDispose">处理样品</el-button>
        </template>
        <hc-table
            :column="tableColumn" :datas="tableData" :loading="tableLoading" is-check
            :index-style="{ width: 60 }" :check-style="{ width: 29 }" @selection-change="tableSelection"
        >
            <template #action="{ row }">
                <el-link type="primary" @click="rowView(row)">查看报告</el-link>
                <el-link type="danger" @click="rowDispose(row)">处理样品</el-link>
            </template>
        </hc-table>
        <template #action>
            <hc-pages :pages="searchForm" @change="pageChange" />
        </template>

        <!-- 样品处理 -->
        <hc-new-dialog v-model="disposeModal" is-footer-center title="样品处理" widths="50rem" @close="disposeClose">
            <div v-if="batchList.length > 0" class="relative mb-24px">
                <hc-table :column="batchColumn" :datas="batchList" :is-index="false" />
            </div>
            <el-form ref="formRef" :model="formModel" :rules="formRules" label-position="left" label-width="auto">
                <el-form-item label="处理原因：" prop="key1">
                    <el-input v-model="formModel.key1" type="textarea" :rows="4" resize="none" />
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button @click="disposeClose">取消</el-button>
                <el-button hc-btn type="primary" @click="disposeSave">确定</el-button>
            </template>
        </hc-new-dialog>
    </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: 5, 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: 'expCount', name: '试验数量', align: 'center' },
    { key: 'key5', name: '留样数量', align: 'center' },
    { key: 'calculationUnit', name: '计算单位', align: 'center' },
    { key: 'proposedPosition', name: '拟用部位' },
    { key: 'representativeCount', name: '代表数量', align: 'center' },
    { key: 'resamTime', name: '留样时间', align: 'center' },
    { key: 'action', name: '操作', width: 150, 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
    }
}

//多选
const tableCheckedKeys = ref([])
const tableSelection = (rows) => {
    tableCheckedKeys.value = rows
}

//表单数据
const formRef = ref(null)
const formModel = ref({})
const formRules = {
    key1: {
        required: true,
        trigger: 'blur',
        message: '请填写处理原因',
    },
}

//弹窗
const disposeModal = ref(false)
const batchColumn = ref([
    { key: 'key1', name: '取样名称' },
    { key: 'key2', name: '留样数量' },
    { key: 'key3', name: '拟用部位' },
    { key: 'key4', name: '留样时间' },
])
const batchList = ref([])

//批量处理样品
const batchDispose = () => {
    batchList.value = tableCheckedKeys.value
    disposeModal.value = true
}

//处理样品
const rowDispose = (row) => {
    batchList.value = []
    disposeModal.value = true
}

//提交保存
const disposeSave = async () => {
    disposeClose()
}

//关闭弹窗
const disposeClose = () => {
    disposeModal.value = false
    batchList.value = []
}

//查看报告
const rowView = (row) => {

}
</script>
