<template>
    <el-dialog title="班级选择" v-model="data.showFlag">
        <div class="app-container">
            <el-row :gutter="20">
                <splitpanes class="class-theme">
                    <!--部门数据-->
                    <pane size="16">
                        <el-col>
                            <div class="head-container">
                                <el-input v-model="deptName" placeholder="请输入部门名称" clearable prefix-icon="Search"
                                          style="margin-bottom: 20px"/>
                            </div>
                            <div class="head-container">
                                <el-tree :data="deptOptions" :props="{ label: 'label', children: 'children' }"
                                         :expand-on-click-node="false" :filter-node-method="filterNode"
                                         ref="deptTreeRef"
                                         node-key="id" highlight-current default-expand-all
                                         @node-click="handleNodeClick"/>
                            </div>
                        </el-col>
                    </pane>
                    <pane size="84">
                        <el-col>
                            <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch"
                                     label-width="68px">
                                <el-form-item label="年级" prop="gradeYear">
                                    <el-select v-model="queryParams.gradeYear" placeholder="请选择年级" clearable
                                               style="width: 168px">
                                        <el-option
                                                v-for="dict in sys_user_grade"
                                                :key="dict.value"
                                                :label="dict.label"
                                                :value="dict.value"
                                        />
                                    </el-select>
                                </el-form-item>
                                <el-form-item label="名称" prop="name">
                                    <el-input
                                            v-model="queryParams.name"
                                            placeholder="请输入名称"
                                            clearable
                                            @keyup.enter="handleQuery"
                                    />
                                </el-form-item>
                                <el-form-item label="班主任" prop="teacherId">
                                    <el-input
                                            v-model="queryParams.teacherId"
                                            placeholder="请输入班主任"
                                            clearable
                                            @keyup.enter="handleQuery"
                                    />
                                </el-form-item>
                                <el-form-item label="班级编码" prop="classCode">
                                    <el-input
                                            v-model="queryParams.classCode"
                                            placeholder="请输入班级编码"
                                            clearable
                                            @keyup.enter="handleQuery"
                                    />
                                </el-form-item>

                                <el-form-item>
                                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                                </el-form-item>
                            </el-form>


                            <el-table v-loading="loading" :data="classmanagerList"
                                      @selection-change="handleSelectionChange">
                                <el-table-column width="55" align="center" v-if="isRadio=='1'" fixed>
                                    <template v-slot="scope">
                                        <el-radio-group v-model="scope.row.selected">
                                            <el-radio :value="scope.row.id" @click="handleRowChange(scope.row)">
                                            </el-radio>
                                        </el-radio-group>

                                    </template>
                                </el-table-column>


                                <el-table-column label="年级" align="center" prop="gradeYear">
                                    <template #default="scope">
                                        <dict-tag :options="sys_user_grade" :value="scope.row.gradeYear"/>
                                    </template>
                                </el-table-column>
                                <el-table-column label="名称" align="center" prop="name"/>
                                <el-table-column label="班主任" align="center" prop="teacherId"/>
                                <el-table-column label="班级编码" align="center" prop="classCode"/>
                                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                                    <template #default="scope">
                                        <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                                                   v-hasPermi="['platform:classmanager:edit']">修改
                                        </el-button>
                                        <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                                                   v-hasPermi="['platform:classmanager:remove']">删除
                                        </el-button>
                                    </template>
                                </el-table-column>
                            </el-table>

                            <pagination
                                    v-show="total>0"
                                    :total="total"
                                    v-model:page="queryParams.pageNum"
                                    v-model:limit="queryParams.pageSize"
                                    @pagination="getList"
                            />
                        </el-col>
                    </pane>
                </splitpanes>
            </el-row>
        </div>
    </el-dialog>

</template>

<script setup name="Classmanager">
    import {
        listClassmanager,
        listClassmanagerBy,
        getClassmanager,
        delClassmanager,
        addClassmanager,
        updateClassmanager
    } from "@/api/xjc-platform/classmanager"

    import {deptTreeSelect, deptTreeSelectDataScope} from "@/api/system/user"

    defineExpose({
        show, setIsRadio, setGradeYear, setOfficeId,getList
    })
    const emit = defineEmits(["onSelected"])

    const props = defineProps({
        isRadio: Number,
        officeId: String,
        gradeYear: Number
    })

    function show() {
        data.showFlag = true
        classmanagerList.value.forEach(function (item, index) {
            item.selected = false
        })
    }

    function setIsRadio(value) {
        data.isRadio = value
    }

    function setGradeYear(value) {
        data.queryParams.gradeYear = value
    }

    function setOfficeId(value) {
        data.queryParams.officeId = value
    }


    const {proxy} = getCurrentInstance()
    const {sys_user_grade} = proxy.useDict('sys_user_grade')

    const classmanagerList = ref([])
    const open = ref(false)
    const loading = ref(true)
    const showSearch = ref(true)
    const ids = ref([])
    const single = ref(true)
    const multiple = ref(true)
    const total = ref(0)
    const title = ref("")

    const deptOptions = ref(undefined)
    const enabledDeptOptions = ref(undefined)

    const data = reactive({
        showFlag: false,
        form: {},
        queryParams: {
            pageNum: 1,
            pageSize: 10,
            officeId: null,
            gradeYear: null,
            name: null,
            teacherId: null,
            classCode: null,
            stageCode: null,
            studentnumset: null,
            samesubjects: null
        },
        rules: {},
    })

    const {queryParams, form, rules} = toRefs(data)


    /** 查询班级管理列表 */
    function getList() {
        loading.value = true
        listClassmanagerBy(queryParams.value).then(response => {
            classmanagerList.value = response.rows
            classmanagerList.value.forEach(function (item,index) {
                item.selected = false
            })
            total.value = response.total
            loading.value = false
        })
    }

    // 取消按钮
    function cancel() {
        open.value = false
        reset()
    }

    // 表单重置
    function reset() {
        form.value = {
            id: null,
            officeId: null,
            gradeYear: null,
            name: null,
            teacherId: null,
            classCode: null,
            stageCode: null,
            delFlag: null,
            updateBy: null,
            updateTime: null,
            createBy: null,
            createTime: null,
            studentnumset: null,
            samesubjects: null
        }
        proxy.resetForm("classmanagerRef")
        //
    }

    /** 搜索按钮操作 */
    function handleQuery() {
        queryParams.value.pageNum = 1
        getList()
    }

    /** 重置按钮操作 */
    function resetQuery() {
        proxy.resetForm("queryRef")
        handleQuery()
    }

    // 多选框选中数据
    function handleSelectionChange(selection) {
        ids.value = selection.map(item => item.id)
        single.value = selection.length != 1
        multiple.value = !selection.length
    }

    /** 新增按钮操作 */
    function handleAdd() {
        reset()
        open.value = true
        title.value = "添加班级管理"
    }


    /** 修改按钮操作 */
    function handleUpdate(row) {
        reset()
        const _id = row.id || ids.value
        getClassmanager(_id).then(response => {
            form.value = response.data
            open.value = true
            title.value = "修改班级管理"
        })
    }

    /** 提交按钮 */
    function submitForm() {
        proxy.$refs["classmanagerRef"].validate(valid => {
            if (valid) {
                if (form.value.id != null) {
                    updateClassmanager(form.value).then(response => {
                        proxy.$modal.msgSuccess("修改成功")
                        open.value = false
                        getList()
                    })
                } else {
                    addClassmanager(form.value).then(response => {
                        proxy.$modal.msgSuccess("新增成功")
                        open.value = false
                        getList()
                    })
                }
            }
        })
    }


    /** 删除按钮操作 */
    function handleDelete(row) {
        const _ids = row.id || ids.value
        proxy.$modal.confirm('是否确认删除班级管理编号为"' + _ids + '"的数据项？').then(function () {
            return delClassmanager(_ids)
        }).then(() => {
            getList()
            proxy.$modal.msgSuccess("删除成功")
        }).catch(() => {
        })
    }

    /** 导出按钮操作 */
    function handleExport() {
        proxy.download('platform/classmanager/export', {
            ...queryParams.value
        }, `classmanager_${new Date().getTime()}.xlsx`)
    }

    /** 查询部门下拉树结构 */
    function getDeptTree() {
        deptTreeSelectDataScope().then(response => {
            deptOptions.value = response.data
            enabledDeptOptions.value = filterDisabledDept(JSON.parse(JSON.stringify(response.data)))
        })
    }

    /** 过滤禁用的部门 */
    function filterDisabledDept(deptList) {
        return deptList.filter(dept => {
            if (dept.disabled) {
                return false
            }
            if (dept.children && dept.children.length) {
                dept.children = filterDisabledDept(dept.children)
            }
            return true
        })
    }

    /** 节点单击事件 */
    function handleNodeClick(data) {
        queryParams.value.deptId = data.id
        handleQuery()
    }

    function handleRowChange(row) {
        row.selected = true
        data.showFlag = false
        emit('onSelected', row)
    }

    onMounted(() => {
        nextTick(() => {
            getDeptTree()
        })
        // getList()
        loading.value = false
    })


</script>

<style>
    .class-theme {
        display: flex;
        flex-direction: row;
    }
</style>
