<template>
    <div class="app-container">
        <el-form :model="queryForm" ref="queryForm" size="small" :inline="true" label-width="68px">

            <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-row class="mb8" :gutter="8">
            <el-col :span="1.5">
                <el-button size="mini" type="primary" icon="el-icon-plus" @click="handleOption(0)"> 新增
                </el-button>
            </el-col>
        </el-row>
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column align="center" type="index" label="序号" width="55" />
            <el-table-column align="center" prop="grade" label="级部" />
            <el-table-column align="center" prop="schoolYearSemester" label="学年" />
            <el-table-column align="center" prop="plannedTime" 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="experimentName" label="实验名称" />
            <el-table-column align="center" prop="sub" label="学科分类">
                <template slot-scope="{ row }">
                    <div>{{ selectDictLabel(dict.type.lab_sub, row.sub) }}</div>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="chapterContent" label="章节内容" />
            <el-table-column align="center" prop="isAppointment" label="是否已预约">
                <template slot-scope="scope">
                    <span v-if="scope.row.isAppointment === '0'">未预约</span>
                    <span v-else-if="scope.row.isAppointment === '1'">已预约</span>
                </template>
            </el-table-column>

            <el-table-column align="center" fixed="right" label="操作" width="150">
                <template slot-scope="{ row }">
                    <div v-if="row.isAppointment === '0'">
                        <el-button type="text" size="small" @click="handleOption(2, row)">查看</el-button>
                        <el-button type="text" size="small" @click="handleOption(1, row)">修改</el-button>
                        <el-button type="text" size="small" @click="handleDelete(row.id)">删除</el-button>
                    </div>
                    <div v-else>
                        <el-button type="text" size="small" @click="handleOption(2, row)">查看</el-button>

                    </div>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryForm.pageNum" :limit.sync="queryForm.pageSize"
            @pagination="getList" />
        <!-- 新增/查看 -->
        <el-dialog title="详细信息" :visible.sync="dialogTableVisible" width="60%">
            <el-form :model="form" ref="form" size="small" :rules="rules" label-width="108px">
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="学科" prop="sub">
                            <el-select v-model="form.sub" placeholder="请选择" :disabled="nowType === 2" clearable
                                style="width: 100%;">
                                <el-option v-for="(item, index) in subList" :key="index" :label="item.dictLabel"
                                    :value="item.dictValue"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="级部" prop="gradeId">
                            <el-select v-model="form.gradeId" placeholder="请选择" clearable @change="onGradeChange"
                                style="width: 100%;" :disabled="nowType === 2">
                                <el-option v-for="(item, index) in gradeList" :key="index" :label="item.gradeName"
                                    :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="学年" prop="schoolYear">
                            <el-input v-model="form.schoolYear" placeholder="请输入" clearable
                                :disabled="nowType === 2"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="学期" prop="semester">
                            <el-select v-model="form.semester" placeholder="请选择" clearable style="width: 100%;"
                                :disabled="nowType === 2">
                                <el-option label="上学期" value="1"></el-option>
                                <el-option label="下学期" value="2"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="20">
                        <el-form-item label="班级" prop="classId">
                            <el-checkbox-group v-model="form.classId" :disabled="nowType === 2">
                                <el-checkbox v-for="classItem in classList" :key="classItem.classId"
                                    :label="classItem.classId">
                                    {{ classItem.className }}
                                </el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-col>


                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="实验分类" prop="experimentClassify">
                            <el-select v-model="form.experimentClassify" placeholder="请选择" clearable style="width: 100%;"
                                :disabled="nowType === 2">
                                <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="experimentName">
                            <el-input v-model="form.experimentName" placeholder="请输入" clearable
                                :disabled="nowType === 2"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="20">
                        <el-form-item label="章节内容" prop="chapterContent">
                            <el-input v-model="form.chapterContent" placeholder="请输入" clearable
                                :disabled="nowType === 2"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="计划开始时间" prop="plannedStartTime">
                            <el-date-picker v-model="form.plannedStartTime" type="month" placeholder="选择月"
                                value-format="yyyy-MM" style="width: 100%;" :disabled="nowType === 2">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="计划结束时间" prop="plannedEndTime">
                            <el-date-picker v-model="form.plannedEndTime" type="month" placeholder="选择月"
                                value-format="yyyy-MM" style="width: 100%;" :disabled="nowType === 2">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="20">
                        <el-form-item label="实验用品" prop="experimentUseGoods">
                            <el-input v-model="form.experimentUseGoods" type="textarea" placeholder="请输入" clearable
                                :disabled="nowType === 2"></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 @click="cancel"> 取 消 </el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import {
    getNewYear,
    getGrade,
    getClass,
    addExperimentPlan,
    updateExperimentPlan,
    getExperimentPlan,
    getExperimentPlanxq,
    deleteExperimentPlan,
    subExperimentPlan,
    getSemester
} from '@/api/smartSchool/gradeWork/laboratoryManagement/experimentalPlan'
export default {
    name: 'index',
    dicts: ['experiment_classify', 'lab_sub'],
    data() {
        return {
            queryForm: {
                schoolYear: "",
                semester: "",
                experimentClassify: "",
                pageNum: 1,
                pageSize: 10,

            },
            classList: [], // 班级列表数据
            subList: [],// 学科列表
            //级部
            gradeList: [],
            //表格数据
            tableData: [

            ],
            //查看弹窗表格
            ExperimentalData: [],
            form: {
                id: '',
                sub: '',
                gradeId: '',
                grade: '',
                schoolYear: '',
                semester: '',
                experimentClassify: '',
                experimentName: '',
                chapterContent: '',
                plannedStartTime: '',
                plannedEndTime: '',
                experimentUseGoods: '',
                classId: '',
                className: '',
                schoolExperimentPlanClassList: [],

            },

            //实验分类下拉框
            options: {

            },
            rules: {
                sub: [
                    { required: true, message: "学科不能为空", trigger: "blur" }
                ],
                gradeId: [
                    { required: true, message: "级部不能为空", trigger: "blur" }
                ],
                schoolYear: [
                    { required: true, message: "学年不能为空", trigger: "blur" }
                ],
                semester: [
                    { required: true, message: "学期不能为空", trigger: "blur" }
                ],
                experimentClassify: [
                    { required: true, message: "实验分类不能为空", trigger: "blur" }
                ],
                experimentName: [
                    { required: true, message: "实验名称不能为空", trigger: "blur" }
                ],
                chapterContent: [
                    { required: true, message: "章节内容不能为空", trigger: "blur" }
                ],
                plannedStartTime: [
                    { required: true, message: "计划开始时间不能为空", trigger: "blur" }
                ],
                plannedEndTime: [
                    { required: true, message: "计划结束时间不能为空", trigger: "blur" }
                ],
                experimentUseGoods: [
                    { required: true, message: "实验用品不能为空", trigger: "blur" }
                ],
                classId: [
                    { required: true, message: "班级不能为空", trigger: "blur" }
                ],


            },
            nowType: 0, // 0新增、1修改、2查看
            //弹窗
            dialogTableVisible: false,
            total: 0,
        }
    },
    created() {
        this.getListWithSemester();
        this.getGrade();


    },

    methods: {
        getListWithSemester() {
            this.getSemester(() => {
                this.getList();
            });
        },
        getSemester(callback) {
            getSemester().then((res) => {
                this.form.semester = res.semester;
                this.form.schoolYear = res.schoolYear + '';
                this.queryForm.semester = res.semester;
                this.queryForm.schoolYear = res.schoolYear + '';
                console.log(this.form.semester, typeof this.form.semester);

                if (callback && typeof callback === 'function') {
                    callback();
                }
            });
        },
        getList() {
            getExperimentPlan(this.queryForm).then(response => {
                this.tableData = response.rows;
                console.log('this.tableData', this.tableData);
                this.total = response.total;
                this.loading = false;
            });
        },

        //选择级部自动获取年级
        onGradeChange() {
            const selectedGrade = this.gradeList.find(grade => grade.id === this.form.gradeId);
            this.classList = []; // 清空班级列表
            getClass(selectedGrade.id).then(response => {
                this.loading = false;
                this.classList = response.data;
                if (selectedGrade.gradeValue == 1) {
                    this.form.classId = this.classList.map(classItem => classItem.classId);
                } else {
                    this.form.classId = []
                }
            });
        },



        //删除按钮操作
        handleDelete(id) {
            this.$confirm("此操作将永久删除该数据，是否继续？", '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteExperimentPlan(id).then(res => {
                    if (res.code == 200) {
                        this.$message.success("删除成功！")
                    }
                    this.getList()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除！'
                });
            });

        },



        //查询级部
        getGrade() {
            getGrade().then(response => {
                this.loading = false;
                this.gradeList = response.data;
                console.log("this.gradeList", this.gradeList);
                if (this.gradeList.length > 0) {
                    const gradeId = this.gradeList[0].id;
                    this.getClass(gradeId);
                    this.getSub(gradeId);
                }
            });
        },
        //查询班级
        getClass(gradeId) {
            getClass(gradeId).then(response => {
                this.loading = false;
                this.classList = response.data;
                console.log('this.classList', this.classList);
            });
        },
        //查询学科
        getSub(gradeId) {
            subExperimentPlan(gradeId).then(response => {
                this.loading = false;
                this.subList = response.data;
                console.log('this.subList', this.subList);
            });
        },

        //重置按钮
        resetQuery() {
            this.queryForm = {
                schoolYear: "",
                semester: "",
                experimentClassify: "",
                pageNum: 1,
                pageSize: 10,
            },
                this.getList();
        },

        // 0新增、1修改、2查看
        handleOption(type, row) {
            this.form = {
                id: '',
                sub: '',
                gradeId: '',
                grade: '',
                schoolYear: '',
                semester: '',
                experimentClassify: '',
                experimentName: '',
                chapterContent: '',
                plannedStartTime: '',
                plannedEndTime: '',
                experimentUseGoods: '',
                classId: '',
                className: '',
                schoolExperimentPlanClassList: [],
            }
            this.nowType = type;
            this.getSemester();
            if (type != 0) {
                const id = row.id
                getExperimentPlanxq(id).then(response => {
                    if (response.code == 200) {
                        console.log(response);
                        Object.keys(this.form).forEach(key => {
                            if (response.data[key]) {
                                this.$set(this.form, key, response.data[key]);
                                this.form.classId = response.data.schoolExperimentPlanClassList.map(item => {
                                    return item.classId;
                                });
                            }
                        })
                    }
                })
            }


            this.dialogTableVisible = true;
        },

        /** 导入按钮操作 */
        handleImport() {

        },


        //取消按钮
        cancel() {
            this.dialogTableVisible = false;
        },
        //提交按钮
        submitForm() {
            if (Array.isArray(this.form.classId)) {
                this.form.schoolExperimentPlanClassList = this.form.classId.map(item => {
                    return { classId: item }
                })
            }
            const gradeId = this.form.gradeId
            this.form.grade = gradeId == '' ? '' : this.gradeList.find(item => item.id == gradeId).gradeName
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id !== "") {
                        updateExperimentPlan(this.form).then(response => {
                            console.log("this.form", this.form);
                            this.$modal.msgSuccess("修改成功");
                            this.dialogTableVisible = false;
                            this.getList();
                        });
                    } else {
                        addExperimentPlan(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.dialogTableVisible = false;
                            this.getList();
                        });
                    }
                }
            });
        },




    },
}
</script>

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