<template>
    <div class="app-container">
        <el-form :model="queryForm" ref="queryForm" size="small" :inline="true" label-width="68px">
            <el-form-item label="学科分类" prop="sub">
                <el-select v-model="queryForm.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-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 label="实验名称">
                <el-input v-model="queryForm.experimentName" placeholder="请输入实验名称" clearable></el-input>
            </el-form-item>
            <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>
                <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="experimentName" label="实验名称" />
            <el-table-column align="center" prop="schoolYearSemester" label="学期" />
            <el-table-column align="center" prop="totalClass" label="总班级数" />
            <el-table-column align="center" prop="completeClass" label="已完成班级数" />
            <el-table-column align="center" prop="ratio" 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="80%">
            <el-form :model="form" ref="form" size="small" :inline="true" label-width="88px">
                <el-form-item label="实验分类">
                    <el-select v-model="form.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="form.className" placeholder="请输入班级" clearable></el-input>
                </el-form-item>
                <el-form-item label="开始时间" prop="startTime">
                    <el-date-picker v-model="form.startTime" type="date" placeholder="选择" value-format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="结束时间" prop="endTime">
                    <el-date-picker v-model="form.endTime" type="date" placeholder="选择" value-format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="getListtc">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuerytc">重置</el-button>
                </el-form-item>
            </el-form>
            <el-table :data="ExperimentalData" border style="width: 100%">
                <el-table-column align="center" type="index" label="序号" width="55" />
                <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="className" label="班级" />
                <el-table-column align="center" prop="experimentTime" label="实验时间" />
                <el-table-column align="center" prop="section" label="节次" />
                <el-table-column align="center" prop="state" label="附件">
                    <template slot-scope="scope">
                        <span v-if="scope.row.state === '0'">未上报</span>
                        <span v-else-if="scope.row.state === '1'">已上报</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
            </el-table>


        </el-dialog>

    </div>
</template>

<script>

import {
    getList,
    getSemester,
    getTeacherxq,
    getGradelist
} from '@/api/smartSchool/laboratoryManagement/leadlaboratoryCompletion'
export default {
    name: 'index',
    dicts: ['experiment_classify', 'lab_sub'],
    data() {
        return {
            queryForm: {
                pageNum: 1,
                pageSize: 10,
                schoolYear: "",
                semester: "",
                sub: "",
                grade: "",
                experimentName: "",
            },
            gradeLists: [],
            //表格数据
            tableData: [

            ],
            //查看弹窗表格
            ExperimentalData: [],
            form: {
                pageNum: 1,
                pageSize: 10,
                experimentPlanId: "",
                experimentClassify: "",
                className: "",
                startTime: "",
                endTime: "",
            },
            //弹窗
            dialogTableVisible: false,
            total: 0,
        }
    },
    created() {
        this.getSemesterAndList();
        this.getListtc();
        this.getGradelist();
    },

    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() {

            getList(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);
                });
        },
        //查询级部搜索
        getGradelist() {
            getGradelist().then(response => {
                this.loading = false;
                this.gradeLists = response.data;
                console.log("this.gradeList", this.gradeList);

            });
        },
        //查看按钮
        handleLook(row) {
            console.log(row);
            console.log(this.form.id);
            // 设置 dialogTableVisible 为 true，显示弹窗
            this.dialogTableVisible = true;
            // 根据 row 的数据构建请求参数
            const params = {
                experimentPlanId: row.id,
                experimentClassify: this.form.experimentClassify,
                className: this.form.className,
                startTime: this.form.startTime,
                endTime: this.form.endTime,
            };
            getTeacherxq(params).then(response => {
                this.ExperimentalData = response.rows;
                this.loading = false;
            }).catch(error => {

            });
        },


        resetQuery() {
            this.queryForm = {
                pageNum: 1,
                pageSize: 10,
                schoolYear: "",
                semester: "",
                sub: "",
                grade: "",
                experimentName: "",
            };
            this.getList();
        },
        //弹窗获取表单
        getListtc() {
            const params = {
                experimentPlanId: this.form.id,
                experimentClassify: this.form.experimentClassify,
                className: this.form.className,
                startTime: this.form.startTime,
                endTime: this.form.endTime,
            };
            getTeacherxq(params).then(response => {
                this.ExperimentalData = response.rows;
            }).catch(error => {

            });
        },
        //弹窗重置按钮
        resetQuerytc() {
            this.form = {
                pageNum: 1,
                pageSize: 10,
                experimentPlanId: "",
                experimentClassify: "",
                className: "",
                startTime: "",
                endTime: "",
            };

            this.getListtc();
        },

    },
}
</script>

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