<script lang="ts">
export default {
    name: "document",//文档管理
};
</script>

<script setup lang="ts">
import { ref, onMounted, nextTick } from "vue";
import { useAppStore } from '@/store/modules/app';
import { ElMessageBox, ElMessage } from "element-plus";
import Page from '@/components/Pagination/page.vue';
import { projectList, getNodesTree, addNodes, updateNodes, deleteNodesById } from "@/api/document";
import uploadFiles from '@/components/uploadFiles/index.vue'

const handleSuccess = (file: any) => {
    console.log('上传成功:', file.name)
}

const handleError = ({ file, error }) => {
    console.error('上传失败:', file.name, error)
}
onMounted(() => {
    getProject()
});
//拖动条
const vMove = {
    mounted(el: any) {
        el.onmousedown = function (e: any) {
            var init = e.clientX;
            var parent: any = document.getElementById("silderLeft");
            const initWidth: any = parent.offsetWidth;
            document.onmousemove = function (e) {
                var end = e.clientX;
                var newWidth = end - init + initWidth;
                parent.style.width = newWidth + "px";
            };
            document.onmouseup = function () {
                document.onmousemove = document.onmouseup = null;
            };
        };
    }
}
//左侧树的选择框
const projectId = ref()
const projectArr = ref([])
//获取所有项目
function getProject() {
    projectList().then((res: any) => {
        projectArr.value = res.data
        projectId.value = projectArr.value[0].id
        gettreedata()
    })
}
//左侧树配置
const treedata = ref([])
const treeloading = ref(false)
const defaultProps = {
    children: "children",
    label: "nodeName"
};
const treeForm = ref({
    projectId: '',
    nodeName: '',
})
//获取树数据
function gettreedata() {
    treeloading.value = true
    treeForm.value.projectId = projectId.value
    getNodesTree(treeForm.value).then((res: any) => {
        treedata.value = res.data
        treeloading.value = false
    })
}
function handleNodeClick(data: any, node: any) {

}
//子项目配置
const frame = ref(false)
const title = ref()
const ruleFormRef = ref()
const projectForme: any = ref({
    projectId: '',//所属项目id
    parentId: "",//父节点id
    nodeType: "",//节点类型
    nodeName: "",//节点名称
})
//用户弹窗规则定义
const moderules = ref({
    nodeName: [{ required: true, message: "请输入节点名称", trigger: "blur" }],
});
//新增子项目
function addSubItem(row: any) {
    frame.value = true
    projectForme.value = {
        projectId: '',//所属项目id
        parentId: "",//父节点id
        nodeType: "",//节点类型
        nodeName: "",//节点名称
    }
    projectForme.value.projectId = projectId.value
    if (row.nodeType == '00') {
        projectForme.value.parentId = '00'
    } else {
        projectForme.value.parentId = row.id
    }
    if (row.nodeType == '00') {
        title.value = "添加子项目"
        projectForme.value.nodeType = '01'
    } else if (row.nodeType == '01') {
        title.value = "添加课题"
        projectForme.value.nodeType = '02'
    } else if (row.nodeType == '02') {
        title.value = "添加年度"
        projectForme.value.nodeType = '03'
    } else if (row.nodeType == '03') {
        title.value = "添加主题"
        projectForme.value.nodeType = '04'
    }



}
//修改子项目
function editSubItem(row: any) {
    if (row.nodeType == '01') {
        title.value = "修改子项目"
    } else if (row.nodeType == '02') {
        title.value = "修改课题"
    } else if (row.nodeType == '03') {
        title.value = "修改年度"
    } else if (row.nodeType == '04') {
        title.value = "修改主题"
    }
    // title.value = "修改子项目"
    frame.value = true
    if (row.subRegions) {
        delete row.subRegions
    }
    if (row.createTime) {
        delete row.createTime
    }

    projectForme.value = JSON.parse(JSON.stringify(row))
}
//表单确定
async function submitForm(formEl: any) {
    if (!formEl) return
    await formEl.validate((valid: any, fields: any) => {
        if (valid) {
            if (projectForme.value.id) {
                updateNodes(projectForme.value).then((res: any) => {
                    if (res.code == 0) {
                        gettreedata()
                        ElMessage.success("修改成功")
                        frame.value = false
                    }

                })
            } else {
                addNodes(projectForme.value).then((res: any) => {
                    if (res.code == 0) {
                        gettreedata()
                        ElMessage.success("添加成功")
                        frame.value = false
                    }

                })
            }


        }
    })
}
//弹框关闭
function handleClose(formEl: any) {
    // if (!formEl) return
    // formEl.resetFields()
    frame.value = false
}
//删除子节点
function delSubItem(row: any) {
    ElMessageBox.confirm(
        '您确定要删除该项目吗?',
        '警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            deleteNodesById({ id: row.id }).then((res: any) => {
                if (res.code == 0) {
                    gettreedata()
                    ElMessage({
                        type: 'success',
                        message: '删除成功',
                    })
                }

            })

        })

}
//表格数据配置
const queryParams: any = ref({
    current: 1,
    size: 20,
    name: ''
});
//定义表格数据
const tableData: any = ref([{}, {}]);
const total = ref(0);
// 表格加载
const loading = ref(false)
//获取表格数据
function getdata() {

}
//上传组件
const upfile = ref(false)
function openFile() {
    upfile.value = true
}
function fileClose() {
    upfile.value = false
}

</script>

<template>
    <div class="faulttemplate-box">
        <aside id="silderLeft">
            <div>
                <el-select v-model="projectId" placeholder="请选择项目" @change="gettreedata()">
                    <el-option v-for="item in projectArr" :key="item.id" :label="item.projectName" :value="item.id" />
                </el-select>
                <div class="tree_sou">
                    <el-input v-model="treeForm.nodeName" style="width:100%;margin:10px 9px 10px 0px;"
                        placeholder="节点名称" clearable />
                    <el-button type="primary" @click="gettreedata()">搜索</el-button>
                    <el-button type="primary" @click="addSubItem({ nodeType: '00' })">新增子项目</el-button>
                </div>

                <el-tree ref="treeRef" node-key="id" :data="treedata" :highlight-current="true" :props="defaultProps"
                    v-loading="treeloading" @node-click="handleNodeClick"
                    :class="useAppStore().size === 'default' ? 'silderLeft-large' : 'silderLeft-default'"
                    style="height: calc(100vh - 280px); overflow: auto;margin-top: 10px;">
                    <template #default="{ data }">
                        <span class="custom-tree-node">
                            <span>{{ data.nodeName }}</span>
                            <span class="img_tree">
                                <img src="@/assets/project/xiu.png" alt="" title="编辑" @click="editSubItem(data)">
                                <img src="@/assets/project/del.png" alt="" title="删除" @click="delSubItem(data)">
                                <img v-if="data.nodeType == '01'" src="@/assets/project/jia.png" alt="" title="添加课题"
                                    @click="addSubItem(data)">
                                <img v-if="data.nodeType == '02'" src="@/assets/project/jia.png" alt="" title="添加年度"
                                    @click="addSubItem(data)">
                                <img v-if="data.nodeType == '03'" src="@/assets/project/jia.png" alt="" title="添加主题"
                                    @click="addSubItem(data)">
                            </span>
                        </span>
                    </template>
                </el-tree>
            </div>
            <div class="moveBtn" v-move>
                <div class="moveBtn-line"></div>
            </div>
        </aside>
        <section class="silderRight">
            <div class="sou_title">
                <div class="sou_title_left">
                    <el-input style="margin-right: 10px ;" v-model="queryParams.name" clearable @change="getdata()"
                        placeholder="项目编号"></el-input>
                    <el-input style="margin-right: 10px ;" v-model="queryParams.name" clearable @change="getdata()"
                        placeholder="项目类型"></el-input>
                    <el-input style="margin-right: 10px ;" v-model="queryParams.name" clearable @change="getdata()"
                        placeholder="项目名称"></el-input>
                    <el-button type="primary" @click="getdata()">搜索</el-button>
                </div>
                <div>
                    <el-button type="primary" @click="openFile">上传</el-button>
                    <el-button type="primary">新增</el-button>
                    <el-button type="primary">删除</el-button>
                </div>
            </div>

            <el-table v-loading="loading" :data="tableData"
                :header-cell-style="{ background: 'rgb(250 250 250)', height: '50px' }"
                style="width: 100%; height: calc(100vh - 275px);margin-bottom: 20px;" border>
                <el-table-column type="index" label="序号" width="70" align="center"></el-table-column>
                <el-table-column prop="usercode" label="文件名称"></el-table-column>
                <el-table-column prop="username" label="关键字"></el-table-column>
                <el-table-column prop="requestip" label="上传人"></el-table-column>
                <el-table-column prop="requestip" label="上传时间"></el-table-column>
                <el-table-column fixed="right" label="操作" width="80" align="center">
                    <template #default="scope">
                        <span
                            style="display: flex;display: -webkit-flex;justify-content: space-around;-webkit-justify-content: space-around; ">
                            {{ scope }}
                            <img src="@/assets/MenuIcon/lbcz_xg.png" alt="" title="修改" style="cursor: pointer;">
                            <img src="@/assets/MenuIcon/lbcz_sc.png" alt="" title="删除" style="cursor: pointer;">
                        </span>
                    </template>
                </el-table-column>
            </el-table>
            <Page :total="total" v-model:size="queryParams.size" v-model:current="queryParams.current"
                @pagination="getdata()">
            </Page>

        </section>
        <el-dialog :title="title" v-model="frame" width="30%" :before-close="handleClose" top="30px" draggable
            destroy-on-close>
            <el-form ref="ruleFormRef" style="max-width: 600px" :model="projectForme" :rules="moderules"
                label-width="auto" class="demo-ruleForm" status-icon>
                <el-form-item label="节点名称" prop="nodeName">
                    <el-input v-model="projectForme.nodeName" />
                </el-form-item>
                <el-form-item>
                    <div style="width: 100%;display: flex;justify-content: end;">
                        <el-button type="primary" @click="submitForm(ruleFormRef)">确定</el-button>
                        <el-button @click="handleClose(ruleFormRef)">取消</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </el-dialog>
        <el-dialog title="上传文件" v-model="upfile" width="35%" :before-close="fileClose" top="30px" draggable
            destroy-on-close>
            <el-form ref="ruleFormRef" style="max-width: 600px" :model="projectForme" :rules="moderules"
                label-width="auto" class="demo-ruleForm" status-icon>
                <el-form-item label="文件上传" prop="nodeName">
                    <uploadFiles upload-url="/api/upload"  :max-size="20"
                        @upload-success="handleSuccess" @upload-error="handleError" />

                </el-form-item>
                <el-form-item label="关键字" prop="nodeName">
                    <el-input v-model="projectForme.nodeName" />
                </el-form-item>
                <el-form-item label="描述" prop="nodeName">
                    <el-input v-model="projectForme.nodeName" :rows="2" type="textarea" />
                </el-form-item>
                <el-form-item>
                    <div style="width: 100%;display: flex;justify-content: end;">
                        <el-button type="primary" @click="submitForm(ruleFormRef)">确定</el-button>
                        <el-button @click="handleClose(ruleFormRef)">取消</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<style scoped lang="scss">
.faulttemplate-box {
    height: 100%;
    display: flex;
    display: -webkit-flex;
    background-color: #f2f4f9;

}

#silderLeft {
    width: 300px;
    padding: 10px;
    box-sizing: border-box;
    background: #fff;
    border-radius: 3px;
    position: relative;

    .tree_sou {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    &:hover {
        .moveBtn {
            opacity: 1;
        }
    }
}

/* 拖动条 */
.moveBtn {
    height: 100%;
    width: 15px;
    padding: 0 6px;
    opacity: 0;
    position: absolute;
    right: -15px;
    top: 0;
}

.moveBtn-line {
    width: 100%;
    height: 100%;
    cursor: col-resize;
    user-select: none;
    background-color: #60bfff;
}

.silderRight {
    flex: 1;
    width: 100%;
    height: calc(100vh - 130px);
    overflow: auto;
    background-color: rgba(255, 255, 255, 1);
    border-radius: 3px;
    padding: 15px;
    padding-bottom: 0px;
    box-sizing: border-box;
    margin-left: 15px;

    .sou_title {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;

        .sou_title_left {
            width: 50%;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

    }
}

.custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;

    .img_tree {
        display: flex;
        width: 25%;
        align-items: center;
        justify-content: space-between;

        img {
            cursor: pointer;
        }
    }
}
</style>
