<template>
    <hc-new-dialog is-table widths="90%" :show="isShow" title="变更清单添加" :loading="addSaveLoad" @save="addModalSave" @close="addModalClose">
        <hc-card-item>
            <template #header>
                <div class="w-60">
                    <hc-search-input v-model="searchForm.searchValue" placeholder="请输入清单编号" @search="searchClick" />
                </div>
            </template>
            <template #extra>
                <el-link type="primary" @click="showTypeClick">{{ searchForm.showType === 1 ? '显示已分解' : '显示所有' }}</el-link>
            </template>
            <hc-table
                :column="tableColumn" :datas="tableData" :loading="tableLoading"
                is-new :index-style="{ width: 60 }" is-check :check-style="{ width: 29 }"
                @selection-change="tableCheckChange"
            />
        </hc-card-item>
    </hc-new-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { arrToKey, getArrValue } from 'js-fast-way'
import mainApi from '~api/alter/admin/order'

const props = defineProps({
    ids: {
        type: [String, Number],
        default: '',
    },
    treeId: {
        type: [String, Number],
        default: '',
    },
    contractId: {
        type: [String, Number],
        default: '',
    },
})

//事件
const emit = defineEmits(['finish', 'close'])

//双向绑定
// eslint-disable-next-line no-undef
const isShow = defineModel('modelValue', {
    default: false,
})

//监听
watch(() => [
    props.ids,
    props.treeId,
    props.contractId,
], ([id, tid, cid]) => {
    searchForm.value.ids = id
    searchForm.value.nodeId = tid
    searchForm.value.contractId = cid
}, { deep: true })

//监听
watch(isShow, (val) => {
    if (val) {
        getTableData()
    }
})

//搜索
const searchClick = () => {
    getTableData()
}

//显示类型
const showTypeClick = () => {
    const { showType } = searchForm.value
    searchForm.value.showType = showType === 1 ? 2 : 1
    getTableData()
}

//搜索表单
const searchForm = ref({
    searchValue: null, showType: 1, nodeId: props.treeId,
    ids: props.ids, contractId: props.contractId,
})

//表格数据
const tableLoading = ref(false)
const tableColumn = ref([
    { key: 'formNumber', name: '清单编号', width: 120 },
    { key: 'formName', name: '清单名称' },
    { key: 'currentPrice', name: '单价(元)', width: 100 },
    { key: 'contractTotal', name: '合同数量', width: 100 },
    { key: 'changeTotal', name: '合同变更后数量', width: 120 },
    { key: 'buildChangeTotal', name: '施工图变更后数量', width: 140 },
    { key: 'resolveResidueTotal', name: '分解剩余量', width: 100 },
])
const tableData = ref([])
const getTableData = async () => {
    tableData.value = []
    tableLoading.value = true
    const { data } = await mainApi.addForm({
        ...searchForm.value,
        contractId: props.contractId,
    })
    tableData.value = getArrValue(data)
    tableLoading.value = false
}

//表格勾选
const tableCheck = ref([])
const tableCheckChange = (rows) => {
    tableCheck.value = rows
}

//保存
const addSaveLoad = ref(false)
const addModalSave = async () => {
    const ids = arrToKey(tableCheck.value, 'id')
    addSaveLoad.value = true
    const { error, code, msg, data } = await mainApi.getSelectForm({
        ids: ids,
        nodeId: props.treeId,
        contractId: props.contractId,
    })
    addSaveLoad.value = false
    if (!error && code === 200) {
        addModalClose()
        emit('finish', getArrValue(data))
    } else {
        window.$message.error(msg ?? '确认失败')
    }
}

//关闭弹窗
const addModalClose = () => {
    isShow.value = false
    emit('close')
}
</script>
