<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-input v-model="queryForm.experimentName" placeholder="请输入实验名称" clearable></el-input>
            </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>
                <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="grade" label="级部" />
            <el-table-column align="center" prop="schoolYear" label="学年" />
            <el-table-column align="center" prop="plannedTime" 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="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="chapterContent" label="章节内容" />
            <el-table-column align="center" fixed="right" label="操作" width="150">
                <template slot-scope="scope">
                    <el-button @click="handleLook(scope.row)" type="text" size="small">查看</el-button>
                </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" label-width="108px" :disabled="isEdit">
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="级部">
                            <el-input v-model="form.grade" placeholder="请输入" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="学年">
                            <el-input v-model="form.schoolYear" placeholder="请输入" clearable></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%;">
                            </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%;">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <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-col :span="10">
                        <el-form-item label="实验分类">
                            <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>
                    <el-col :span="10">
                        <el-form-item label="实验名称">
                            <el-input v-model="form.experimentName" placeholder="请输入" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="章节内容">
                            <el-input v-model="form.chapterContent" 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 @click="cancel"> 取 消 </el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>

import {
    getExperimentPlan,
    getExperimentPlaxq,
    getSemester

} from '@/api/smartSchool/laboratoryManagement/experimentalPlan'
export default {
    name: 'index',
    dicts: ['experiment_classify', 'lab_sub'],
    data() {
        return {
            queryForm: {
                pageNum: 1,
                pageSize: 10,
                schoolYear: '',
                semester: '',
                experimentClassify: '',
                experimentName: '',
            },
            //表格数据
            tableData: [
                {

                }
            ],
            form: {

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

            },
            //弹窗
            dialogTableVisible: false,
            total: 0,
            isEdit: false,
        }
    },

    created() {
        this.getList();
        this.getSemesterAndList();
    },
    methods: {

        async getSemesterAndList() {
            await this.getSemester(); // 等待获取学年和学期信息
            this.getList(); // 获取数据列表
        },
        getSemester() {
            return new Promise((resolve, reject) => {
                getSemester()
                    .then(response => {
                        this.queryForm.semester = response.semester;
                        this.queryForm.schoolYear = response.schoolYear + '';
                        resolve(); // 成功获取学年和学期信息
                    })
                    .catch(error => {
                        console.log(error);
                        reject(error);
                    });
            });
        },
        getList() {

            getExperimentPlan(this.queryForm)
                .then(response => {
                    this.tableData = response.rows;
                    console.log('response', response);
                    this.total = response.total;
                    this.loading = false;
                })
                .catch(error => {
                    this.loading = false;
                    console.log(error);
                });
        },

        resetQuery() {
            this.queryForm = {
                pageNum: 1,
                pageSize: 10,
                schoolYear: '',
                semester: '',
                experimentClassify: '',
                experimentName: '',
            };
            this.getList();
        },
        //查看按钮操作
        handleLook(row) {
            this.dialogTableVisible = true;
            const id = row.id || this.ids;
            getExperimentPlaxq(id).then(response => {
                this.form = response.data;
                this.isEdit = true;
                console.log('this.tableData', this.tableData);
                this.loading = false
            })
        },
        //取消按钮
        cancel() {
            this.dialogTableVisible = false;
        },

    },
}
</script>

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