<template>
    <div class="relative h-full flex">
        <div :id="`hc_tree_card_${uuid}`">
            <hc-new-card scrollbar>
                <template #header>
                    <el-button hc-btn type="primary">重新设置treeCode</el-button>
                </template>
                <hc-lazy-tree :h-props="treeProps" is-load-menu @load="treeLoadNode" @loadMenu="treeLoadMenu" @menuTap="treeMenuTap" @nodeTap="treeNodeTap" />
            </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" @click="editModalShow = true">修改</el-button>
                    <el-button hc-btn type="danger">删除</el-button>
                    <el-button hc-btn type="warning" @click="treeModalShow = true">增补单元</el-button>
                    <el-button hc-btn type="success">导入</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" :is-table="isInfoView" />

        <!-- 调整排序 -->
        <hc-new-dialog v-model="sortModalShow" is-table widths="1100px" title="调整排序" @save="sortModalSave">
            <hc-table
                ui="hc-table-row-drop"
                :column="sortTableColumn" :datas="sortTableData" :loading="sortTableLoading"
                is-row-drop quick-sort is-new :index-style="{ width: 80 }"
                @row-drop="sortTableRowDrop" @row-sort="sortTableRowDrop"
            >
                <template #key2="{ row }">
                    <span class="text-link">{{ row?.key2 }}</span>
                </template>
                <template #action="{ index }">
                    <span class="text-link text-xl" @click="upSortClick(index)">
                        <HcIcon name="arrow-up" fill />
                    </span>
                    <span class="text-link ml-2 text-xl" @click="downSortClick(index)">
                        <HcIcon name="arrow-down" fill />
                    </span>
                </template>
            </hc-table>
        </hc-new-dialog>
    </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: 'ProjectDebitContractUnit',
})

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 isInfoView = ref(false)
const treeNodeTap = ({ node }) => {
    isInfoView.value = node.isLeaf
}

//菜单
const treeLoadMenu = ({ item, level }, resolve) => {
    if (level === 1) {
        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' },
        ])
    } else {
        return resolve([
            { icon: 'lock', label: '锁定', key: 'lock' },
            { icon: 'upload-cloud', label: '导入', key: 'lead' },
            { 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') {
        treeModalShow.value = true
    }
    if (key === 'edit') {
        treeModalShow.value = true
    }
    if (key === 'sort') {
        sortModalShow.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)


//调整排序
const sortModalShow = ref(false)
//表格数据
const sortTableColumn = ref([
    { key:'key1', name: '节点名称' },
    { key:'action', name: '排序', width: 90 },
])
const sortTableLoading = ref(false)
const sortTableData = ref([
    { key1: '1' }, { key1: '2' }, { key1: '3' },
])
//拖动完成
const sortTableRowDrop = (rows) => {
    sortTableData.value = [] // 先清空，否则排序会异常
    nextTick(() => {
        sortTableData.value = rows
    })
}

//向下
const downSortClick = (index) => {
    const indexs = index + 1
    const data = sortTableData.value
    if (indexs !== data.length) {
        const tmp = data.splice(indexs, 1)
        sortTableData.value.splice(index, 0, tmp[0])
    } else {
        window?.$message?.warning('已经处于置底，无法下移')
    }
}

//向上
const upSortClick = (index) => {
    const data = sortTableData.value || []
    if (index !== 0) {
        const tmp = data.splice(index - 1, 1)
        sortTableData.value.splice(index, 0, tmp[0])
    } else {
        window?.$message?.warning('已经处于置顶，无法上移')
    }
}
const sortModalSave = () => {
    sortModalShow.value = false
}
</script>
