<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="primary">增补清单</el-button>
                    <el-button hc-btn type="primary">导入</el-button>
                    <el-button hc-btn type="primary">导出</el-button>
                </template>
                <div class="relative">
                    <infoTable />
                    <HcTitle title="下级节点列表" />
                    <hc-table :column="tableColumn" :datas="tableData" :loading="tableLoading" is-new :index-style="{ width: 60 }" />
                </div>
            </hc-new-card>
        </div>
        <!-- 节点数据 -->
        <hc-new-dialog v-model="treeModalShow" widths="1100px" :title="treeModalTitle" @save="treeModalSave">
            <el-form ref="formRef" class="p-4" label-position="left" label-width="auto" :model="formModel" :rules="formRules">
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item label="清单编号：">
                            <el-input v-model="formModel.key1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="清单名称：">
                            <el-input v-model="formModel.key2" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="单位：">
                            <el-input v-model="formModel.key3" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="清单节点：">
                            <el-select v-model="formModel.key4" block>
                                <el-option label="是" :value="1" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="中标单价：">
                            <el-input v-model="formModel.key5" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同数量：">
                            <el-input v-model="formModel.key6" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="章编号：">
                            <el-input v-model="formModel.key7" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="清单标识：">
                            <el-input v-model="formModel.key8" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="清单类型：">
                            <el-select v-model="formModel.key9" block>
                                <el-option label="普通清单" :value="1" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="备注：">
                            <el-input v-model="formModel.key10" :autosize="{ minRows: 4, maxRows: 8 }" type="textarea" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </hc-new-dialog>
    </div>
</template>

<script setup>
import { nextTick, onMounted, ref } from 'vue'
import { getRandom } from 'js-fast-way'
import infoTable from './components/check-list/info-table.vue'

defineOptions({
    name: 'ProjectPeriodsBaseList',
})

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' },
            { icon: 'add', label: '新增', key: 'add' },
            { icon: 'arrow-up-down-line', label: '排序', key: 'sort' },
        ])
    } else {
        return resolve([
            { icon: 'lock', label: '锁定', key: 'lock' },
            { icon: 'add', label: '新增', key: 'add' },
            { icon: 'pencil', label: '修改', key: 'edit' },
            { icon: 'arrow-up-down-line', label: '排序', key: 'sort' },
            { icon: 'close', label: '删除', key: 'del' },
        ])
    }
}

const treeMenuTap = ({ key, node, data }) => {
    if (key === 'add') {
        treeModalTitle.value = '新增合同工程清单'
        treeModalShow.value = true
    }
    if (key === 'edit') {
        treeModalTitle.value = '修改合同工程清单'
        treeModalShow.value = true
    }
}

//表格数据
const tableLoading = ref(false)
const tableColumn = ref([
    { key: 'formNumber', name: '清单编号' },
    { key: 'formName', name: '清单名称' },
    { key: 'unit', name: '单位' },
    { key: 'formNumber', name: '现行单价' },
    { key: 'contractTotal', name: '合同数量' },
    { key: 'contractMoney', name: '合同金额' },
    { key: 'changePrice', name: '变更后单价' },
    { key: 'changeTotal', name: '变更后数量' },
    { key: 'changeMoney', name: '变更后金额' },
    { key: 'isSupplement', name: '是否增补' },
])
const tableData = ref([
    { key1: '1111' },
])

//弹窗
const treeModalShow = ref(false)
const treeModalTitle = ref('')

//弹窗表单
const formRef = ref(null)
const formModel = ref({})
const formRules = ref({})

//弹窗保存
const treeModalSave = () => {

}
</script>
