<template>
    <hc-new-card>
        <template #header>
            <hc-new-switch :datas="tabTab" :keys="tabKey" :round="false" size="default" @change="tabChange" />
        </template>
        <template #extra>
            <el-button hc-btn type="primary" @click="isTableInfoShow = true">
                <HcIcon name="add" />
                <span>新增</span>
            </el-button>
        </template>
        <div class="relative h-full flex">
            <div :id="`hc_tree_card_${uuid}`">
                <hc-card-item scrollbar>
                    <hc-data-tree :h-props="treeProps" :datas="treeLoadNode" :menus="treeMenus" :root-menu="treeRootMenu" default-expand-all @menuTap="treeMenuTap" />
                </hc-card-item>
            </div>
            <div :id="`hc_table_card_${uuid}`" class="flex-1">
                <hc-card-item>
                    <hc-table :column="tableColumn" :datas="tableData" :loading="tableLoading" is-new :index-style="{ width: 60 }">
                        <template #action="{ row }">
                            <el-link type="primary">扫描</el-link>
                            <el-link type="success">修改</el-link>
                            <el-link type="danger">删除</el-link>
                        </template>
                    </hc-table>
                    <template #action>
                        <hc-pages :pages="searchForm" @change="pageChange" />
                    </template>
                </hc-card-item>
            </div>
        </div>

        <!-- 节点树的新增和编辑 -->
        <treeInfoModal v-model="isTreeInfoShow" />

        <!-- 表格的新增和编辑 -->
        <tableInfoModal v-model="isTableInfoShow" />
    </hc-new-card>
</template>

<script setup>
import { nextTick, onMounted, ref } from 'vue'
import treeInfoModal from './components/treeInfoModal.vue'
import tableInfoModal from './components/tableInfoModal.vue'
import { getRandom } from 'js-fast-way'

defineOptions({
    name: 'AlterCollectionScan',
})

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],
        })
    })
}

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

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

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

//数据
const treeLoadNode = ref([
    {
        label: '变更归档目录',
        children: [
            { label: '归档目录1-1' },
            { label: '归档目录2-1' },
            { label: '归档目录2-2' },
            { label: '归档目录3-1' },
            { label: '归档目录3-2' },
        ],
    },
])

//根节点菜单
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 }) => {
    console.log(key, node, data)
    if (key === 'add' || key === 'edit') {
        isTreeInfoShow.value = true
    }
}

const isTreeInfoShow = ref(false)
const isTableInfoShow = ref(false)

//搜索表单
const searchForm = ref({ current: 1, size: 10, total: 0 })

//分页
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: '扫描状态' },
    { key: 'key5', name: '归集状态' },
    { key: 'action', name: '操作', width: 130 },
])
const tableData = ref([
    { key1: '1111' },
])
</script>
