<template>
    <div class="app-container">
        <el-form :model="queryForm" ref="queryForm" size="small" :inline="true" label-width="88px">
            <el-form-item label="学期">
                <el-date-picker v-model="queryForm.schoolYear" type="year" value-format="yyyy" placeholder="选择年">
                </el-date-picker>
                <el-select v-model="queryForm.semester" placeholder="请选择学期">
                    <el-option label="上学期" value="1"></el-option>
                    <el-option label="下学期" value="2"></el-option>
                </el-select>
            </el-form-item>
            <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.experimentName" placeholder="请输入实验名称" clearable></el-input>
            </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="级部" width="100" />
            <el-table-column align="center" prop="schoolYearSemester" label="学期" width="120" />
            <el-table-column align="center" prop="plannedTime" label="实验时间" width="200" />
            <el-table-column align="center" prop="experimentName" label="实验名称" show-overflow-tooltip />
            <el-table-column align="center" prop="experimentClassify" label="实验分类" width="90">
                <template slot-scope="{ row }">
                    <div>{{ selectDictLabel(dict.type.experiment_classify, row.experimentClassify) }}</div>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="chapterContent" label="章节内容" show-overflow-tooltip />
            <el-table-column align="center" prop="schoolLabClassYearRelationList" label="实验室">
                <template slot-scope="scope">
                    <div v-if="scope.row.schoolLabClassYearRelationList">
                        {{ scope.row.schoolLabClassYearRelationList.map(lab => lab.labName).join(", ") }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="experimentUseGoods" label="实验用品" show-overflow-tooltip />
            <el-table-column align="center" fixed="right" label="操作" width="150">
                <template slot-scope="scope">
                    <el-button @click="handleApply(scope.row)" type="text" size="small">申请</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分配实验室弹窗 -->
        <el-dialog title="教师实验室申请" :visible.sync="dialogTableVisible" width="70%">
            <el-form :model="form" ref="form" size="small" :rules="rules" label-width="100px">
                <el-row :gutter="30">
                    <el-col :span="10">
                        <el-form-item label="实验名称" prop="experimentName">
                            <el-input v-model="form.experimentName" placeholder="请输入" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="实验内容" prop="chapterContent">
                            <el-input v-model="form.chapterContent" placeholder="请输入" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="30">
                    <el-col :span="10">
                        <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-col :span="10">
                        <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-row>
                <el-row :gutter="30">
                    <el-col :span="10">
                        <el-form-item label="级部" prop="grade">
                            <el-input v-model="form.grade" placeholder="请输入" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="学年" prop="schoolYear">
                            <el-input v-model="form.schoolYear" placeholder="请输入" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="30">
                    <el-col :span="10">
                        <el-form-item label="学期" prop="semester">
                            <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="10">
                        <el-form-item label="班级" prop="classId">
                            <el-select v-model="form.classId" placeholder="请选择" clearable style="width: 100%;">
                                <el-option v-for="(item, index) in classData" :key="index" :label="item.className"
                                    :value="item.classId"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="节次" prop="section">
                            <el-select v-model="form.section" placeholder="请选择" clearable style="width: 100%;">
                                <el-option v-for="dict in dict.type.course_arrange" :key="dict.value" :label="dict.label"
                                    :value="dict.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="申请时间" prop="experimentTime">
                            <el-date-picker v-model="form.experimentTime" type="date" placeholder="选择"
                                value-format="yyyy-MM-dd" style="width: 100%;" @change="getTeacherLab">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="30">
                    <el-col :span="20" style="display: flex;">
                        <el-form-item label="实验室选择" prop="selectedLabId">
                            <el-radio-group v-model="form.selectedLabId">
                                <el-radio v-for="classItem in labList" :key="classItem.labId" :label="classItem.labId">
                                    {{ classItem.labName }} - {{ classItem.useState }}
                                </el-radio>
                            </el-radio-group>

                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="30">
                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" 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="submitForm">确 定</el-button>
                <el-button type="primary" @click="cancle">取 消</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 {
    getTeacherlabList,// 查看实验室安排列表
    getSemester,//获取当前学年+学期
    getTeacherLab,// 查询实验室使用状态
    getTeacherClass,// 获取班级
    schoolTeacherapply,// 教师申请实验室
    getTeacherLabxq
} from '@/api/smartSchool/classTeacherWork/laboratoryManagement/laboratoryArrangements';
import {
    getLabplanxlk,//获取实验名称下拉框
} from '@/api/smartSchool/gradeWork/laboratoryManagement/laboratoryAppointment';
export default {
    dicts: ['experiment_classify', 'lab_sub', 'course_arrange', 'declare_state'],
    name: 'index',
    data() {
        return {
            queryForm: {
                pageNum: 1,
                pageSize: 10,
                declareState: "",
                schoolYear: "",
                semester: "",
                experimentClassify: "",
                experimentName: "",
            },

            //班级
            classData: [],
            //实验名称
            experimentList: [],
            //实验室
            labList: [],

            //表格数据
            tableData: [

            ],
            selectedLabId: null,
            //学科分类
            options: {

            },
            form: {
                labClassYearId: "",
                classId: "",
                experimentTime: "",
                section: "",
            },
            rules: {
                experimentName: [
                    { required: true, message: "实验名称不能为空", trigger: "blur" }
                ],
                experimentClassify: [
                    { required: true, message: "实验分类不能为空", trigger: "blur" }
                ],
                chapterContent: [
                    { required: true, message: "章节内容不能为空", trigger: "blur" }
                ],
                sub: [
                    { required: true, message: "学科不能为空", trigger: "blur" }
                ],
                grade: [
                    { required: true, message: "级部不能为空", trigger: "blur" }
                ],
                schoolYear: [
                    { required: true, message: "学年不能为空", trigger: "blur" }
                ],
                semester: [
                    { required: true, message: "学期不能为空", trigger: "blur" }
                ],
                classId: [
                    { required: true, message: "班级不能为空", trigger: "blur" }
                ],
                section: [
                    { required: true, message: "节次不能为空", trigger: "blur" }
                ],
                experimentTime: [
                    { required: true, message: "实验时间不能为空", trigger: "blur" }
                ],
                selectedLabId: [
                    { required: true, message: "实验室不能为空", trigger: "blur" }
                ],
            },
            total: 0,
            dialogTableVisible: false,
        }
    },
    created() {
        this.getList();
        this.getExperimentName();
        this.getSemester();
    },
    methods: {
        async getList() {
            await this.getSemester(); // 等待获取当前学期和学年的值
            const params = {
                declareState: '3',
                semester: this.queryForm.semester,
                schoolYear: this.queryForm.schoolYear,
                experimentClassify: this.queryForm.experimentClassify,
                experimentName: this.queryForm.experimentName,
                pageNum: this.queryForm.pageNum, // 绑定到 queryForm.pageNum 上
                pageSize: this.queryForm.pageSize, // 绑定到 queryForm.pageSize 上
            };

            try {
                const response = await getTeacherlabList(params);
                console.log(this.queryForm, 'this.queryForm');
                this.tableData = response.rows;
                console.log('response', response);
                this.total = response.total;
                this.loading = false;
            } catch (error) {
                console.error(error);
            }
        },

        //获取当前学期
        async getSemester() {
            try {
                const res = await getSemester();
                this.queryForm.semester = res.semester;
                this.queryForm.schoolYear = res.schoolYear + '';
                console.log(this.queryForm.schoolYear, typeof res.schoolYear);
            } catch (error) {
                console.error(error);
            }
        },
        //获取实验室使用情况
        getTeacherLab() {
            const params = {
                labClassYearId: this.form.id,
                classId: this.form.classId,
                experimentTime: this.form.experimentTime,
                section: this.form.section,
            };
            console.log(params);
            getTeacherLab(params).then(response => {
                this.labList = response.data;
                console.log('response', response);
                this.loading = false
            })
        },
        //获取班级
        getClasslist() {
            const params = {
                experimentPlanId: this.form.experimentPlanId,
            }
            getTeacherClass(params).then(response => {
                console.log('获取班级', params);
                this.classData = response.data;
                console.log('response', response);
                this.loading = false
            })
        },
        // 获取实验名称下拉框
        getExperimentName() {
            getLabplanxlk().then(response => {
                this.experimentList = response.data;
                console.log('this.experimentList', this.experimentList);
                this.loading = false
            })

        },
        //查看按钮操作
        handleApply(row) {
            this.reset();
            const id = row.id || this.ids;
            getTeacherLabxq(id).then(response => {
                this.form = response.data;
                this.form.experimentPlanId = response.data.experimentPlanId;
                console.log(this.form.experimentPlanId);
                console.log('response', response);
                this.loading = false;
                this.getClasslist();
                this.dialogTableVisible = true;
            });
        },
        // 表单重置
        reset() {
            this.queryForm = {
                pageNum: 1,
                pageSize: 10,
                declareState: "",
                schoolYear: "",
                semester: "",
                experimentClassify: "",
                experimentName: "",
            },
                this.resetForm("queryForm");
        },
        resetQuery() {
            this.queryForm = {
                pageNum: 1,
                pageSize: 10,
                declareState: "",
                schoolYear: "",
                semester: "",
                experimentClassify: "",
                experimentName: "",
            },
                this.getList();
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        const params = {
                            labClassYearId: this.form.id,
                            experimentPlanId: this.form.experimentPlanId,
                            experimentName: this.form.experimentName,
                            chapterConten: this.form.chapterConten,
                            experimentClassify: this.form.experimentClassify,
                            sub: this.form.sub,
                            gradeId: this.form.gradeId,
                            grade: this.form.grade,
                            schoolYear: this.form.schoolYear,
                            semester: this.form.semester,
                            classId: this.form.classId,
                            section: this.form.section,
                            experimentTime: this.form.experimentTime,
                            remark: this.form.experimentPlanId,
                            labId: this.form.selectedLabId, // 修改为单选的实验室ID
                        };
                        schoolTeacherapply(params)
                            .then(response => {
                                this.$modal.msgSuccess('提交成功');
                                this.dialogTableVisible = false;
                                this.getList();
                            });

                    }
                }
            });
        },

        // 取消按钮
        cancle() {
            this.dialogTableVisible = false;
        },

    },
}
</script>

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