<template>
    <div class="relative h-full flex">
        <div :id="`hc_tree_card_${uuid}`">
            <hc-new-card scrollbar>
                <hc-lazy-tree :h-props="treeProps" :menus="treeMenus" :root-menu="treeRootMenu" @load="treeLoadNode" @menuTap="treeMenuTap" />
            </hc-new-card>
        </div>
        <div :id="`hc_table_card_${uuid}`" class="flex-1">
            <hc-new-card>
                <hc-table :column="tableColumn" :datas="tableData" :loading="tableLoading" is-new :index-style="{ width: 60 }" />
                <template #action>
                    <hc-pages :pages="searchForm" @change="pageChange" />
                </template>
            </hc-new-card>
        </div>

        <!-- 节点数据 -->
        <hc-new-dialog v-model="treeModalShow" widths="500px" :title="treeModalTitle" @save="treeModalSave">
            <el-form ref="formRef" class="p-2" label-position="left" size="large" label-width="auto" :model="formModel" :rules="formRules">
                <el-form-item label="节点编码：">
                    <el-input v-model="formModel.key1" />
                </el-form-item>
                <el-form-item label="节点名称：">
                    <el-input v-model="formModel.key2" />
                </el-form-item>
                <el-form-item label="节点类型：">
                    <el-select v-model="formModel.key3" block>
                        <el-option label="单位工程" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label="工程类型：">
                    <el-select v-model="formModel.key4" block>
                        <el-option label="总则" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label="显示类型：">
                    <el-select v-model="formModel.key5" block>
                        <el-option label="总则" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label="备注：">
                    <el-input v-model="formModel.key6" :autosize="{ minRows: 4, maxRows: 8 }" type="textarea" />
                </el-form-item>
            </el-form>
        </hc-new-dialog>
    </div>
</template>

<script setup>
import { nextTick, onMounted, ref } from 'vue'
import { getRandom } from 'js-fast-way'

defineOptions({
    name: 'ProjectPeriodsProjectUnit',
})

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({ current: 1, size: 10, total: 0 })

//数据格式
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 treeRootMenu = [
    { icon: 'add', label: '新增', key: 'add' },
    { icon: 'arrow-up-down-line', label: '排序', key: 'sort' },
]

//节点菜单
const treeMenus = [
    { 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 pageChange = ({ current, size }) => {
    searchForm.value.current = current
    searchForm.value.size = size
}

//表格数据
const tableLoading = ref(false)
const tableColumn = ref([
    { key: 'key1', name: '节点编号' },
    { key: 'key2', name: '节点名称' },
    { key: 'key3', name: '节点类型' },
    { key: 'key4', 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>
