<template>
    <div>
        <div class="flex-1" style="padding-left:20px">
            <HcNewSwitch :datas="tabTypeTab" :keys="tabTypeKey" :round="false" size="default"
                         @change="tabTypeChange"/>
            <el-select v-model="testReportId" :loading="insertDataSelectoading" :placeholder="placeholderType"
                       class="ml-2 w-80"
                       clearable @change="testReportIdchange">
                <el-option v-for="item in testReportData" :key="item.pKeyId" :label="item['nodeName']"
                           :value="item['pKeyId']"/>
            </el-select>
        </div>
        <div class="dialog-table-box">
            <div class="copy-node-many-table">
                <el-scrollbar v-loading="insertDataTableLoading">
                    <el-table :data="insertDataTable" border stripe @selection-change="insertDataTableCheck">
                        <el-table-column type="selection" width="55"/>
                        <el-table-column label="字段名称" prop="key"/>
                        <el-table-column label="数值" prop="value"/>
                    </el-table>
                </el-scrollbar>
            </div>
        </div>
    </div>
</template>

<script setup>
import {ref, watch, onMounted} from "vue";
import {getArrValue, getObjValue, setPosInsert} from "vue-utils-plus"
import dataApi from "~api/tentative/detect/test";

const props = defineProps({
    projectId: [String, Number],
    contractId: [String, Number],
    tableId: [String, Number],
    treeId: [String, Number],
})

//参数变量
const projectId = ref(props.projectId);
const contractId = ref(props.contractId);
const table_id = ref(props.tableId)
const tree_id = ref(props.treeId)

//监听
watch(() => [
    props.projectId,
    props.contractId,
    props.tableId,
    props.treeId,
], ([pid, cid, tableId, treeId]) => {
    projectId.value = pid
    contractId.value = cid
    table_id.value = tableId
    tree_id.value = treeId
})

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

//事件
const emit = defineEmits(['change'])

//类型tab数据和相关处理
const tabTypeKey = ref('2')
const tabTypeTab = ref([
    {key: '1', name: '试验记录表'},
    {key: '2', name: '试验报告单'},
]);
const placeholderType = ref('试验报告单')
const tabTypeChange = ({key}) => {
    tabTypeKey.value = key
    if (tabTypeKey.value === '1') {
        placeholderType.value = '试验记录表'
    } else {
        placeholderType.value = '试验报告单'
    }
    getSearchNodeTables()
}

const insertDataSelectoading = ref(false)
const testReportData = ref([])

const getSearchNodeTables = async () => {
    insertDataSelectoading.value = true
    const {error, code, data} = await dataApi.searchNodeTables({
        id: table_id.value,
        projectId: projectId.value,
        contractId: contractId.value,
        primaryKeyId: tree_id.value,
        // type: authBtnTabKey.value,
        tableType: tabTypeKey.value
    })
    //处理数据
    insertDataSelectoading.value = false
    if (!error && code === 200) {
        testReportData.value = getArrValue(data)
        if (testReportData.value.length > 0) {
            testReportId.value = testReportData.value[0].pKeyId
            getBussddataInfotrialData()
        }
    } else {
        testReportData.value = []
    }
}

const testReportId = ref('');
const checPkd = ref('')
const testReportIdchange = (key) => {
    testReportData.value.forEach((item) => {
        if (item.id == key) {
            checPkd.value = item.pKeyId
        }
    })
    getBussddataInfotrialData()
}

const insertDataTableLoading = ref(false)
const insertDataTable = ref([])
const multipleSelection = ref([])
const insertDataTableCheck = (val) => {
    multipleSelection.value = val
}

//获取试验表中的data数据接口：
const getBussddataInfotrialData = async () => {
    insertDataTableLoading.value = true;
    const {error, code, data} = await dataApi.getBussddataInfotrialList({
        // id:testReportId.value,
        id: table_id.value,
        pkeyId: testReportId.value,
    })
    insertDataTableLoading.value = false
    if (!error && code === 200) {
        let arrobj = getObjValue(data)
        let arr = []
        for (let key of Object.keys(arrobj)) {
            arr.push({
                key: key,
                value: arrobj[key]
            })
        }
        insertDataTable.value = arr
    } else {
        insertDataTable.value = []
    }
}

//确定关联试验数据数据
const submitinsertData = ({KeyName, startPos, endPos, pkeyId}, itemForm) => {
    if (multipleSelection.value.length > 0) {
        const val = []
        multipleSelection.value.forEach((item) => {
            val.push(item['value'])
        })
        const newval = val.join('、')
        const formValue = setPosInsert(startPos, endPos, itemForm, newval)
        let posVal = startPos + newval.length;
        return {code: 200, val: formValue, posVal}
    } else {
        window?.$message?.warning('请选择你要关联的数据')
        return {code: 300, val: '', posVal: ''}
    }
}

// 暴露出去
defineExpose({
    submitinsertData
})
</script>
