<template>
    <hc-new-card>
        <template #header>
            <hc-new-switch :datas="tabTab" :keys="tabKey" :round="false" size="default" @change="tabChange" />
            <div v-if="tabKey === 'key2'" class="ml-3 w-40">
                <el-select v-model="searchForm.key1" filterable block placeholder="选择工区">
                    <el-option label="工区1" value="1" />
                    <el-option label="工区2" value="2" />
                    <el-option label="工区3" value="3" />
                </el-select>
            </div>
        </template>
        <div class="relative h-full flex">
            <div :id="`hc_tree_card_${uuid}`">
                <hc-card-item scrollbar>
                    <hc-lazy-tree tree-key="id" :h-props="treeProps" @load="treeLoadNode" @nodeTap="treeNodeTap" />
                </hc-card-item>
            </div>
            <div :id="`hc_table_card_${uuid}`" class="flex-1">
                <hc-card-item class="h-full">
                    <template #header>
                        <div class="font-400 text-orange">零号变更数据列表</div>
                    </template>
                    <template #extra>
                        <el-button hc-btn type="primary" @click="buildZeroChange">
                            <hc-icon name="pencil-ruler-2" />
                            <span>一键生成零号变更</span>
                        </el-button>
                    </template>
                    <hc-table :is-index="false" :column="tableColumn" :datas="tableData" :loading="tableLoading" is-new />
                </hc-card-item>
            </div>
        </div>
    </hc-new-card>
</template>

<script setup>
import { nextTick, onMounted, ref } from 'vue'
import { useAppStore } from '~src/store'
import { getArrValue, getObjValue, getRandom } from 'js-fast-way'
import unitApi from '~api/project/debit/contract/unit'
import mainApi from '~api/alter/admin/zero'

defineOptions({
    name: 'AlterAdminZero',
})

const uuid = getRandom(4)
const useAppState = useAppStore()
const projectId = ref(useAppState.getProjectId || '')
const contractId = ref(useAppState.getContractId || '')

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

//初始化设置拖动分割线
const setSplitRef = () => {
    //配置参考： https://split.js.org/#/?direction=vertical&snapOffset=0
    nextTick(() => {
        window.$split(['#hc_tree_card_' + uuid, '#hc_table_card_' + uuid], {
            sizes: [20, 80],
            snapOffset: 0,
            minSize: [50, 500],
        })
    })
}

//类型tab数据和相关处理
const tabKey = ref('key1')
const tabTab = ref([
    { key: 'key1', name: '普通变更' },
    { key: 'key2', name: '工区变更' },
])

const tabChange = (item) => {
    tabKey.value = item?.key
}

//数据格式
const treeProps = {
    label: 'nodeName',
    children: 'children',
    isLeaf: 'notExsitChild',
}

//懒加载的数据
const treeLoadNode = async ({ item, level }, resolve) => {
    let id = 0
    if (level !== 0) {
        const nodeData = getObjValue(item)
        id = nodeData?.id || ''
    }
    //获取数据
    const { data } = await unitApi.lazyTree({
        contractId: contractId.value,
        id: id,
    })
    resolve(getArrValue(data))
}
const nodeId = ref('')
const treeNodeTap = ({ data }) => {
    nodeId.value = data?.id || ''
    getTableData()
}

//搜索表单
const searchForm = ref({ key1: null, nodeId: null })

//表格数据
const tableLoading = ref(false)
const tableColumn = ref([
    { key: 'formNumber', name: '清单编号' },
    { key: 'formName', name: '清单名称' },
    { key: 'currentPrice', name: '现行单价' },
    { key: 'contractTotal', name: '合同数量' },
    { key: 'contractMoney', name: '清单金额' },
    { key: 'buildChangeTotal', name: '生成变更时划分数量' },
    { key: 'currentChangeTotal', name: '现划分数量' },
    { key: 'updateTotal', name: '修正量' },
    { key: 'updateMoney', name: '修正金额' },
    { key: 'verifyTotal', name: '核实量' },
    { key: 'verifyMoney', name: '核实金额' },
    { key: 'statusName', name: '状态' },
])
const tableData = ref([])
const getTableData = async () => {
    tableData.value = []
    tableLoading.value = true
    const { data } = await mainApi.getZeroChange({
        projectId: projectId.value,
        contractId: contractId.value,
        nodeId: nodeId.value,
    })
    tableData.value = getArrValue(data)
    tableLoading.value = false
}

//一键生成零号变更
const buildZeroLoading = ref(false)
const buildZeroChange = async () => {
    buildZeroLoading.value = true
    const { error, code } = await mainApi.buildZeroChange({
        projectId: projectId.value,
        contractId: contractId.value,
        nodeId: nodeId.value,
    })
    if (!error && code === 200) {
        window.$message.success('生成成功')
        getTableData().then()
    } else {
        window.$message.error('生成失败')
    }
    buildZeroLoading.value = false
}
</script>

<style lang="scss">
.search-form-box .el-form-item {
    margin-bottom: 0;
}
</style>
