<template>
    <div class="relative h-full flex">
        <div :id="`hc_tree_card_${uuid}`">
            <hc-new-card scrollbar>
                <template #header>
                    <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>
                </template>
                <hc-lazy-tree :h-props="treeProps" is-load-menu @load="treeLoadNode" @loadMenu="treeLoadMenu" @menuTap="treeMenuTap" />
            </hc-new-card>
        </div>
        <div :id="`hc_table_card_${uuid}`" class="flex-1">
            <hc-new-card scrollbar title="合同计量单元">
                <template #extra>
                    <el-button hc-btn type="warning">增补单元</el-button>
                </template>
                <div class="relative">
                    <infoTable />
                    <HcTitle title="清单分解汇总列表">
                        <template #extra>
                            <div class="text-sm text-orange">温馨提示：累计分解量 > 合同变更后量，整行文字红色</div>
                        </template>
                    </HcTitle>
                    <hc-table :column="tableColumn" :datas="tableData" :loading="tableLoading" is-new :index-style="{ width: 60 }">
                        <template #key1="{ row }">
                            <i class="i-iconoir-open-select-hand-gesture inline-block" />
                        </template>
                    </hc-table>
                </div>
            </hc-new-card>
        </div>

        <!-- 节点新增和编辑 -->
        <treeForm v-model="treeModalShow" />

        <!-- 修改合同计量单元 -->
        <rowData v-model="editModalShow" />
    </div>
</template>

<script setup>
import { nextTick, onMounted, ref } from 'vue'
import { getRandom } from 'js-fast-way'
import infoTable from './components/unit/info-table.vue'
import treeForm from './components/unit/tree-form.vue'
import rowData from './components/unit/row-data.vue'

defineOptions({
    name: 'ProjectPeriodsBaseUnit',
})

const uuid = getRandom(4)

//渲染完成
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],
        })
    })
}

//搜索表单
const searchForm = ref({})

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

//懒加载的数据
const treeLoadNode = ({ level }, resolve) => {
    if (level === 0) {
        return resolve([{ name: 'region' }])
    }
    if (level > 3) {
        return resolve([])
    }
    setTimeout(() => {
        resolve([
            { name: 'leaf', leaf: true },
            { name: 'zone' },
        ])
    }, 500)
}

//菜单
const treeLoadMenu = ({ item, level }, resolve) => {
    if (level === 1) {
        return resolve([
            { icon: 'lock', label: '锁定', key: 'lock' },
        ])
    } else {
        return resolve([
            { icon: 'lock', label: '锁定', key: 'lock' },
            { icon: 'upload-cloud', label: '导入', key: 'lead' },
            { icon: 'add', label: '新增', key: 'add' },
            { icon: 'arrow-up-down-line', label: '排序', key: 'sort' },
        ])
    }
}

const treeMenuTap = ({ key, node, data }) => {
    if (key === 'add') {
        treeModalShow.value = true
    }
    if (key === 'edit') {
        treeModalShow.value = true
    }
}

//表格数据
const tableLoading = ref(false)
const tableColumn = ref([
    { key: 'key1', name: '分解明细', width: 80, align: 'center' },
    { key: 'key2', name: '清单编号', width: 120, align: 'center' },
    { key: 'key3', name: '清单名称', align: 'center' },
    { key: 'key4', name: '合同变更数量', width: 110, align: 'center' },
    { key: 'key5', name: '施工图变更后汇总数量', align: 'center' },
    { key: 'key6', name: '单价（元）', width: 90, align: 'center' },
    { key: 'key7', name: '施工图变更后汇总金额（元）', align: 'center' },
])
const tableData = ref([
    { key2: '1111' },
])

//弹窗
const treeModalShow = ref(false)

const editModalShow = ref(false)
</script>
