<template>
    <div class="app-container">
        <el-form :model="queryForm" ref="queryForm" size="small" :inline="true" label-width="88px">
            <el-form-item label="实验分类">
                <el-select v-model="queryForm.experimentClassify" placeholder="请选择" clearable>
                    <el-option v-for="dict in dict.type.experiment_classify" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="申请老师">
                <el-input v-model="queryForm.applyName" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="实验级部">
                <el-select v-model="queryForm.gradeId" placeholder="请选择级部" clearable>
                    <el-option v-for="(item, index) in gradeLists" :key="index" :label="item.gradeName"
                        :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column align="center" type="index" label="序号" width="55" />
            <el-table-column align="center" prop="sub" label="学科" width="60">
                <template slot-scope="{ row }">
                    <div>{{ selectDictLabel(dict.type.lab_sub, row.sub) }}</div>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="grade" label="级部" />
            <el-table-column align="center" prop="plannedTime" label="实验时间" />
            <el-table-column align="center" prop="experimentName" label="实验名称" />
            <el-table-column align="center" prop="experimentClassify" label="实验分类">
                <template slot-scope="{ row }">
                    <div>{{ selectDictLabel(dict.type.experiment_classify, row.experimentClassify) }}</div>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="schoolTeacherExperimentApplyLabsList" label="实验室">
                <template slot-scope="scope">
                    <div v-if="scope.row.schoolTeacherExperimentApplyLabsList">
                        {{ scope.row.schoolTeacherExperimentApplyLabsList.map(lab => lab.labName).join(", ") }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="experimentUseGoods" label="实验用品" />
            <el-table-column align="center" prop="applyName" label="申请教师" />
            <el-table-column align="center" prop="declareState" label="申报状态">
                <template slot-scope="{ row }">
                    <div>{{ selectDictLabel(dict.type.declare_state, row.declareState) }}</div>
                </template>
            </el-table-column>
            <el-table-column align="center" fixed="right" label="操作" width="150">
                <template slot-scope="scope">
                    <el-button v-if="scope.row.declareState === '1' || scope.row.declareState === '2'"
                        @click="handleLook(scope.row)" type="text" size="small">查看</el-button>
                    <el-button v-if="scope.row.declareState === '1' || scope.row.declareState === '2'"
                        @click="handleAllocation(scope.row)" type="text" size="small">分配实验室</el-button>
                    <el-button v-if="scope.row.declareState === '3'" @click="handleLook(scope.row)" type="text"
                        size="small">查看</el-button>
                </template>
            </el-table-column>

        </el-table>
        <!-- 分配实验室弹窗 -->
        <el-dialog title="申请信息" :visible.sync="dialogTableVisible" width="75%">
            <el-form :model="form" ref="form" size="small" label-width="110px">
                <el-row :gutter="30">
                    <el-col :span="8">
                        <el-form-item label="学科" prop="sub">
                            <el-select v-model="form.sub" placeholder="请选择" clearable style="width: 100%;"
                                :disabled="isEdits">
                                <el-option v-for="dict in dict.type.lab_sub" :key="dict.value" :label="dict.label"
                                    :value="dict.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实验名称">
                            <el-input v-model="form.experimentName" placeholder="请输入" clearable :disabled="isEdits" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实验分类" prop="experimentClassify">
                            <el-select v-model="form.experimentClassify" placeholder="请选择" clearable style="width: 100%;"
                                :disabled="isEdits">
                                <el-option v-for="dict in dict.type.experiment_classify" :key="dict.value"
                                    :label="dict.label" :value="dict.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="30">
                    <el-col :span="8">
                        <el-form-item label="学年">
                            <el-input v-model="form.schoolYear" placeholder="请输入" clearable :disabled="isEdits" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="学期">
                            <el-select v-model="form.semester" placeholder="请选择学期" style="width: 100%;" :disabled="isEdits">
                                <el-option label="上学期" value="1"></el-option>
                                <el-option label="下学期" value="2"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="级部">
                            <el-input v-model="form.grade" placeholder="请输入" clearable :disabled="isEdits" />
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row :gutter="30">
                    <el-col :span="8">
                        <el-form-item label="申请老师">
                            <el-input v-model="form.applyName" placeholder="请输入" clearable :disabled="isEdits" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开始时间" prop="plannedStartTime">
                            <el-date-picker v-model="form.plannedStartTime" type="date" placeholder="选择"
                                value-format="yyyy-MM-dd" style="width: 100%;">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="结束时间" prop="plannedEndTime">
                            <el-date-picker v-model="form.plannedEndTime" type="date" placeholder="选择"
                                value-format="yyyy-MM-dd" style="width: 100%;">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="30">
                    <el-col :span="24">
                        <el-form-item label="实验室选择">
                            <el-checkbox-group v-model="form.schoolTeacherExperimentApplyLabsList">
                                <el-checkbox v-for="classItem in labs" :key="classItem.labId" :label="classItem.labId"
                                    >
                                    {{ classItem.labName }}- {{ classItem.useState }}
                                </el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="30">
                    <el-col :span="24">
                        <el-form-item label="实验用品">
                            <el-input v-model="form.experimentUseGoods" type="textarea" placeholder="请输入"
                                clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

            </el-form>
            <div slot="footer" class="dialog-footer" style="text-align: center;">
                <el-button type="primary" @click="save">分 配</el-button>
                <el-button type="danger" @click="submitForm">提 交</el-button>
            </div>

        </el-dialog>
        <!-- 查看 -->
        <el-dialog title="申请信息" :visible.sync="open" width="75%">
            <el-form :model="form" ref="form" size="small" label-width="110px" :disabled="isEdit">
                <el-row :gutter="30">
                    <el-col :span="8">
                        <el-form-item label="学科" prop="sub">
                            <el-select v-model="form.sub" placeholder="请选择" clearable style="width: 100%;">
                                <el-option v-for="dict in dict.type.lab_sub" :key="dict.value" :label="dict.label"
                                    :value="dict.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实验名称">
                            <el-input v-model="form.experimentName" placeholder="请输入" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实验分类" prop="experimentClassify">
                            <el-select v-model="form.experimentClassify" placeholder="请选择" clearable style="width: 100%;">
                                <el-option v-for="dict in dict.type.experiment_classify" :key="dict.value"
                                    :label="dict.label" :value="dict.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="30">
                    <el-col :span="8">
                        <el-form-item label="学年">
                            <el-input v-model="form.schoolYear" placeholder="请输入" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="学期">
                            <el-select v-model="form.semester" placeholder="请选择学期" style="width: 100%;">
                                <el-option label="上学期" value="1"></el-option>
                                <el-option label="下学期" value="2"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="级部">
                            <el-input v-model="form.grade" placeholder="请输入" clearable />
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row :gutter="30">
                    <el-col :span="8">
                        <el-form-item label="申请老师">
                            <el-input v-model="form.applyName" placeholder="请输入" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开始时间" prop="plannedStartTime">
                            <el-date-picker v-model="form.plannedStartTime" type="date" placeholder="选择"
                                value-format="yyyy-MM-dd" style="width: 100%;">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="结束时间" prop="plannedEndTime">
                            <el-date-picker v-model="form.plannedEndTime" type="date" placeholder="选择"
                                value-format="yyyy-MM-dd" style="width: 100%;">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="30">
                    <el-col :span="24">
                        <el-form-item label="实验室选择">
                            <el-checkbox-group v-model="form.schoolTeacherExperimentApplyLabsList">
                                <el-checkbox v-for="classItem in schoolTeacherExperimentApplyLabsList"
                                    :key="classItem.labId" :label="classItem.labId">
                                    {{ classItem.labName }}
                                </el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="30">
                    <el-col :span="24">
                        <el-form-item label="实验用品">
                            <el-input v-model="form.experimentUseGoods" type="textarea" placeholder="请输入"
                                clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

            </el-form>
            <div slot="footer" class="dialog-footer" style="text-align: center;">
                <el-button type="primary" @click="cancel">取 消</el-button>

            </div>

        </el-dialog>


        <pagination v-show="total > 0" :total="total" :page.sync="queryForm.pageNum" :limit.sync="queryForm.pageSize"
            @pagination="getList" />
    </div>
</template>

<script>
import {
    getTeacherList,// 查看列表
    getLabList,// 获取实验室
    getExperimentLabs,// 分配实验室
    getTeacherapplyxq// 查看详情
} from '@/api/smartSchool/laboratoryManagement/experimentApproval'
import {
    getGradelist
} from '@/api/smartSchool/laboratoryManagement/leadlaboratoryCompletion'
export default {
    name: 'index',
    dicts: ['experiment_classify', 'lab_sub', 'declare_state'],
    data() {
        return {
            queryForm: {
                pageNum: 1,
                pageSize: 10,
                experimentClassify: "",
                applyName: "",
                grade: "",
            },
            selectedLabs: [], // 已选择的实验室
            //表格数据
            tableData: [

            ],
            form: {

            },
            isEdit: false,
            isEdits: false,
            labs: [],
            schoolTeacherExperimentApplyLabsList: [],
            total: 0,
            dialogTableVisible: false,
            open: false,
        }
    },
    created() {
        this.getList();
        this.getGradelist();
    },
    methods: {
        //表单查询
        getList() {
            getTeacherList(this.queryForm).then(response => {
                this.tableData = response.rows
                console.log(response);
                this.total = response.total
                this.loading = false
            })
        },
        //查询级部搜索
        getGradelist() {
            getGradelist().then(response => {
                this.loading = false;
                this.gradeLists = response.data;
                console.log("this.gradeList", this.gradeList);

            });
        },
        //分配实验室
        handleAllocation(row) {
            this.loading = true;
            const id = row.id || this.ids;
     
            getTeacherapplyxq(id).then(response => {
                this.form = response.data;
                // 清空已经选择的实验室
                this.selectedLabs = [];
                for (let i = 0; i < this.form.schoolTeacherExperimentApplyLabsList.length; i++) {
                    const labId = this.form.schoolTeacherExperimentApplyLabsList[i].labId;
                    this.selectedLabs.push(labId);
                }

                this.form.schoolTeacherExperimentApplyLabsList = this.selectedLabs; // 仅将已经选择的实验室作为关联数据提交
                this.loading = false;
                const plannedStartTime = this.form.plannedStartTime
                const plannedEndTime = this.form.plannedEndTime
                const sub = this.form.sub
                getLabList(plannedStartTime, plannedEndTime, sub).then(response => {
                    const labs = response.data;
                    console.log(labs, 'qb');
                    const selectedLabs = this.form.schoolTeacherExperimentApplyLabsList; // 获取已选择的实验室列表
                    console.log(selectedLabs, 'xz');

                    // 将已选择的实验室信息存储在新的数组中
                    const selectedLabInfo = labs.filter(lab => {
                        const isSelected = selectedLabs.some(selectedLab => selectedLab.labId === lab.labId);
                        if (isSelected) {
                            lab.isSelected = true;
                            return true;
                        }
                        return false;
                    });

                    // 更新 labs 数组，将已选择的实验室信息存储在对应实验室对象中
                    this.labs = labs.map(lab => {
                        const selectedLab = selectedLabInfo.find(selectedLab => selectedLab.labId === lab.labId);
                        if (selectedLab) {
                            lab.selectedLabInfo = selectedLab;
                        }
                        return lab;
                    }).map(lab => {
                        if (lab.useState === '使用中') {
                            lab.isDisabled = true;
                        }
                        return lab;
                    })
                    this.isEdits = true;
                    this.loading = false;
                });
            });
            this.dialogTableVisible = true;
        },
        //查看按钮操作
        handleLook(row) {
            this.open = true;
            const id = row.id || this.ids;
            getTeacherapplyxq(id).then(response => {
                this.form = response.data;
                if (response.data.schoolTeacherExperimentApplyLabsList) { // 判断 schoolLabClassYearRelationList 存在且不为空
                    this.schoolTeacherExperimentApplyLabsList = response.data.schoolTeacherExperimentApplyLabsList;
                    console.log('this.form.schoolTeacherExperimentApplyLabsList', this.form.schoolTeacherExperimentApplyLabsList);

                }
                this.form.schoolTeacherExperimentApplyLabsList = [];
                for (let i = 0; i < this.schoolTeacherExperimentApplyLabsList.length; i++) {
                    const labId = this.schoolTeacherExperimentApplyLabsList[i].labId;
                    const labName = this.schoolTeacherExperimentApplyLabsList[i].labName;
                    this.form.schoolTeacherExperimentApplyLabsList.push(labId, labName);
                    console.log(this.form.schoolTeacherExperimentApplyLabsList, '查看 ');
                }
                console.log('this.form', this.form);
                this.loading = false
                this.isEdit = true;
            })
        },
        resetQuery() {
            this.queryForm = {
                pageNum: 1,
                pageSize: 10,
                experimentClassify: "",
                applyName: "",
                grade: "",
            },
                this.getList();
        },
        // 保存按钮
        save() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        const selectedLabs = []; // 临时数组
                        for (let i = 0; i < this.labs.length; i++) {
                            const labId = this.labs[i].labId;
                            const labName = this.labs[i].labName;
                            if (this.form.schoolTeacherExperimentApplyLabsList.includes(labId)) { // 判断是否选中
                                selectedLabs.push({ labId: labId, labName: labName }); // 将选中的实验室push进临时数组
                            }
                        }

                        const params = {
                            id: this.form.id,
                            plannedStartTime: this.form.plannedStartTime,
                            plannedEndTime: this.form.plannedEndTime,
                            experimentUseGoods: this.form.experimentUseGoods,
                            declareState: '2',
                            schoolTeacherExperimentApplyLabsList: selectedLabs, // 将临时数组赋值给params.schoolLabClassYearRelationList
                        };

                        getExperimentLabs(params).then(response => {
                            this.$modal.msgSuccess('保存成功');
                            this.dialogTableVisible = false;
                            this.getList();
                        });
                    }
                }
            });
        },


        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        const params = {
                            id: this.form.id,
                            plannedStartTime: this.form.plannedStartTime,
                            plannedEndTime: this.form.plannedEndTime,
                            experimentUseGoods: this.form.experimentUseGoods,
                            declareState: '3',
                            schoolTeacherExperimentApplyLabsList: [],
                        };
                        for (let i = 0; i < this.labs.length; i++) {
                            const labId = this.labs[i].labId;
                            const labName = this.labs[i].labName;
                            if (this.form.schoolTeacherExperimentApplyLabsList.includes(labId)) { // 判断是否选中
                                params.schoolTeacherExperimentApplyLabsList.push({ labId: labId, labName: labName }); // 将选中的实验室push进数组
                            }
                        }
                        getExperimentLabs(params).then(response => {
                            console.log(params, params);
                            this.$modal.msgSuccess('提交成功');
                            this.dialogTableVisible = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        //取消按钮
        cancel() {
            this.open = false;
        },

    },
}
</script>

<style lang="scss" scoped></style>