<template>
    <div class="relative h-full flex">
        <div :id="`hc_tree_card_${uuid}`">
            <hc-new-card scrollbar>
                <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" @click="leadModalShow = true">导入</el-button>
                    <el-button hc-btn type="primary">导出</el-button>
                </template>
                <div class="relative">
                    <infoTable />
                    <el-divider border-style="dashed">下级节点列表</el-divider>
                    <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>

        <!-- 导入清单 -->
        <hc-new-dialog v-model="leadModalShow" scrollbar widths="1100px" title="合同工程清单导入" @save="leadModalSave">
            <div class="hc-el-upload-drag">
                <el-upload
                    ref="leadUploadRef" hc drag :show-file-list="false" :auto-upload="false" :headers="getHeader()"
                    action="/api/blade-resource/oss/endpoint/upload-file" :data="leadUploadData"
                    accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
                >
                    <div class="hc-ui-upload-btn">
                        <div class="hc-uploader-drop">
                            <div class="upload-icon">
                                <HcIcon name="upload-cloud" />
                            </div>
                            <div class="text">将文件拖动到此处，或点击上传</div>
                        </div>
                        <div class="hc-uploader-accept">支持的文件格式： Excel(xls、xlsx)</div>
                    </div>
                </el-upload>
            </div>
            <hc-title>
                <span>范例模板</span>
                <template #extra>
                    <el-link type="primary">下载范例模板.xls</el-link>
                </template>
            </hc-title>
            <el-table :data="tableLeadData" style="width: 100%;" row-key="id" border>
                <el-table-column prop="k1" label="导入编号" width="90" />
                <el-table-column prop="k2" label="清单编号" width="90" />
                <el-table-column prop="k3" label="清单名称" />
                <el-table-column prop="k4" label="单位" width="80" />
                <el-table-column prop="k5" label="合同数量" width="90" />
                <el-table-column prop="k6" label="中标单价" width="90" />
                <el-table-column prop="k7" label="清单类型" width="90" />
                <el-table-column prop="k8" label="章编号" width="80" />
                <el-table-column prop="k9" label="备注" width="90" />
            </el-table>
            <div class="mt-4 text-orange">特别提示：清单类型分为：1、普通清单，2、计日工清单，3、暂定金清单，请填写具体数字对应</div>
        </hc-new-dialog>
    </div>
</template>

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

defineOptions({
    name: 'ProjectDebitContractList',
})

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: 'key1', name: '清单编号' },
    { key: 'key2', name: '清单名称' },
    { key: 'key3', name: '单位' },
    { key: 'key4', name: '现行单价' },
    { key: 'key5', name: '合同数量' },
    { key: 'key6', name: '合同金额' },
    { key: 'key7', name: '变更后单价' },
    { key: 'key8', name: '变更后数量' },
    { key: 'key9', name: '变更后金额' },
    { key: 'key10', 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 = () => {

}

//合同工程清单导入
const leadModalShow = ref(false)
const leadUploadRef = ref(null)
const leadUploadData = ref({})

const tableLeadData = ref([
    { id: 1, k1: '100', k2: '100', k3: '总则', k4: '', k5: '', k6: '', k7: '1', k8: '100', k9: '' },
    { id: 2, k1: '101-1', k2: '101-1', k3: '保险费', k4: '', k5: '', k6: '', k7: '1', k8: '100', k9: '' },
    { id: 3, k1: '101-1a', k2: '101-1a', k3: '按合同条款规定，提供建筑工程一切险', k4: '总额', k5: '27', k6: '1.2', k7: '1', k8: '100', k9: '' },
    { id: 4, k1: '101-1b', k2: '101-1b', k3: '按合同条款规定，提供建筑工程一切险', k4: '总额', k5: '20', k6: '3', k7: '1', k8: '100', k9: '' },
    { id: 5, k1: '101-1c', k2: '101-1c', k3: '按合同条款规定，提供建筑工程一切险', k4: '总额', k5: '12', k6: '3.9', k7: '1', k8: '100', k9: '' },
])

const leadModalSave = () => {
    leadModalShow.value = false
}
</script>
