<template>
    <div class="app-container">
        <el-form :model="queryForm" ref="queryForm" size="small" :inline="true" label-width="88px">
            <el-form-item label="实验室">
                <el-input v-model="queryForm.labName" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="实验室状态">
                <el-select v-model="queryForm.labState" placeholder="请选择" clearable>
                    <el-option v-for="dict in dict.type.lab_state" :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-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-form>
        <el-table border :data="tableData" style="width: 100%">
            <el-table-column align="center" type="index" label="序号" width="55" />
            <el-table-column align="center" prop="labName" label="实验室名称" />
            <el-table-column align="center" prop="labSub" label="实验室学科">
                <template slot-scope="{ row }">
                    <div>{{ selectDictLabel(dict.type.lab_sub, row.labSub) }}</div>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="inChargeName" label="实验室负责人" />
            <el-table-column align="center" prop="labUseGoods" label="实验用品" />
            <el-table-column align="center" prop="labState" label="实验室状态">
                <template slot-scope="{ row }">
                    <div>{{ selectDictLabel(dict.type.lab_state, row.labState) }}</div>
                </template>
            </el-table-column>
            <el-table-column align="center" fixed="right" label="操作" width="180">
                <template slot-scope="{ row }">
                    <div>
                        <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>
                        <el-button @click="handleRecord(row)" type="text" size="small">查看记录</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="98px">
                <el-row :gutter="30">
                    <el-col :span="10">
                        <el-form-item label="实验名称" prop="labName">
                            <el-input v-model="form.labName" placeholder="请输入" clearable
                                :disabled="nowType === 2"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="10">
                        <el-form-item label="学科" prop="labSub">
                            <el-select v-model="form.labSub" placeholder="请选择" clearable style="width: 100%;"
                                :disabled="nowType === 2">
                                <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-row>
                <el-row :gutter="30">
                    <el-col :span="10">
                        <el-form-item label="负责人" prop="inChargeId">
                            <el-select v-model="form.inChargeId" placeholder="请选择" style="width: 100%;"
                                :disabled="nowType === 2" filterable>
                                <el-option v-for="item in adminList" :key="item.userId" :label="item.userName"
                                    :value="item.userId">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="状态" prop="labState">
                            <el-radio-group v-model="form.labState" style="width: 100%;" :disabled="nowType === 2">
                                <el-radio label="1">良好</el-radio>
                                <el-radio label="2">正常</el-radio>
                                <el-radio label="3">待检查</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="30">
                    <el-col :span="20">
                        <el-form-item label="实验用品" prop="labUseGoods">
                            <el-input v-model="form.labUseGoods" 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>

        <!-- 查看记录弹窗 -->
        <el-dialog :title="title" :visible.sync="open" width="90%">
            <el-form :model="formlook" ref="formlook" size="small" :inline="true" label-width="98px">
                <el-form-item label="实验分类">
                    <el-select v-model="formlook.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-select v-model="formlook.gradeId" placeholder="请选择" clearable>
                        <el-option v-for="item in gradeList" :key="item.id" :label="item.gradeName" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="班级">
                    <el-input v-model="formlook.className" placeholder="请输入" clearable></el-input>
                </el-form-item>
                <el-form-item label="开始时间" prop="startTime">
                    <el-date-picker v-model="formlook.startTime" type="date" placeholder="选择" value-format="yyyy-MM-dd"
                        style="width: 100%;">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间" prop="endTime">
                    <el-date-picker v-model="formlook.endTime" type="date" placeholder="选择" value-format="yyyy-MM-dd"
                        style="width: 100%;">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="getListtc">搜索</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="schoolYear" label="学年" />
                <el-table-column align="center" prop="grade" label="实验级部" />
                <el-table-column align="center" prop="className" label="实验班级" />
                <el-table-column align="center" prop="labName" label="实验室" />
                <el-table-column align="center" prop="section" label="节次" />
                <el-table-column align="center" prop="sub" label="学科分类" />
                <el-table-column align="center" prop="experimentName" label="实验名称" />
                <el-table-column align="center" prop="chapterContent" label="章节内容" />
                <el-table-column align="center" prop="experimentClassify" label="实验分类" />
                <el-table-column align="center" prop="experimentTime" label="实验时间" />
            </el-table>

        </el-dialog>
    </div>
</template>

<script>
import {
    addSchoolLab,
    getSchoolLab,
    updateSchoolLab,
    deleteSchoolLab,
    departUser,
    subSchoolLab,
    getSchoolLabxq,
    getClassDetails
} from '@/api/smartSchool/laboratoryManagement/laboratoryManagementlist'
// import {
//     getGrade,
// } from '@/api/smartSchool/gradeWork/laboratoryManagement/experimentalPlan'
export default {
    name: 'index',
    dicts: ['lab_state', 'lab_sub', 'experiment_classify',],
    data() {
        return {
            queryForm: {
                pageNum: 1,
                pageSize: 10,
                labName: '',
                labState: '',
            },
            //表格数据
            tableData: [

            ],
            nowType: 0, // 0新增、1修改、2查看
            // 弹出层标题
            title: '',
            //查看弹窗表格
            ExperimentalData: [],
            form: {
                id: '',
                labName: '',
                labSub: '',
                inChargeId: '',
                inChargeName: '',
                labState: '',
                labUseGoods: '',

            },
            formlook: {
                pageNum: 1,
                pageSize: 10,
                labId: '',
                sub: '',
                gradeId: '',
                className: '',
                startTime: '',
                endTime: ''
            },
            //实验分类下拉框
            options: {

            },
            //学科列表
            subList: [],
            rules: {
                labName: [
                    { required: true, message: "实验名称不能为空", trigger: "blur" }
                ],
                labSub: [
                    { required: true, message: "学科不能为空", trigger: "blur" }
                ],
                inChargeId: [
                    { required: true, message: "负责人不能为空", trigger: "blur" }
                ],
                labState: [
                    { required: true, message: "状态不能为空", trigger: "blur" }
                ],
                labUseGoods: [
                    { required: true, message: "实验用品不能为空", trigger: "blur" }
                ],
            },
            //弹窗
            dialogTableVisible: false,
            //查看记录弹窗
            open: false,
            //负责人下拉框
            adminList: [],
            total: 0,
            //级部
            gradeList: [],
        }
    },
    created() {
        this.getList();
        // this.getGrade();
        this.getListtc();
    },
    methods: {
        getList() {
            getSchoolLab(this.queryForm).then(response => {
                this.tableData = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        resetQuery() {
            this.queryForm = {
            };
            this.getList();
        },
        //负责人下拉框
        getAdmin() {
            departUser().then(res => {
                if (res.code === 200) {
                    this.adminList = res.data;
                    console.log(this.adminList, 'this.adminList');
                }
            });
        },
        //查询级部
        // getGrade() {
        //     getGrade().then(response => {
        //         this.loading = false;
        //         this.gradeList = response.data;
        //         console.log("this.gradeList", this.gradeList);

        //     });
        // },


        //弹窗搜索
        getListtc() {
            const params = {
                labId: this.formlook.labId,
                sub: this.formlook.sub,
                gradeId: this.formlook.gradeId,
                experimentPlanId: this.form.id,
                experimentClassify: this.formlook.experimentClassify,
                className: this.formlook.className,
                startTime: this.formlook.startTime,
                endTime: this.formlook.endTime,
                pageNum: this.formlook.pageNum,
                pageSize: this.formlook.pageSize
            };

            console.log('实验', params);
            getClassDetails(params).then(response => {
                this.ExperimentalData = response.rows;
                this.loading = false;
            });
        },

        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                labName: undefined,
                labSub: undefined,
                inChargeId: undefined,
                inChargeName: undefined,
                labState: undefined,
                labUseGoods: undefined,
            };
            this.resetForm("form");
        },
        //弹窗重置
        resetQuerytc() {
            const savedLabId = this.formlook.labId; // 保存 labId 的值
            console.log(savedLabId, 'savedLabId');
            this.formlook = {
                pageNum: 1,
                pageSize: 10,
                labId: savedLabId,
                sub: '',
                gradeId: '',
                experimentClassify: '',
                className: '',
                startTime: '',
                endTime: ''
            };

            this.getListtc();
        },

        // 0新增、1修改、2查看
        handleOption(type, row) {
            this.form = {
                id: '',
                labName: '',
                labSub: '',
                inChargeId: '',
                inChargeName: '',
                labState: '',
                labUseGoods: '',
            }
            this.nowType = type;
            this.getAdmin();
            this.getSub();
            if (type != 0) {
                const id = row.id
                getSchoolLabxq(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.dialogTableVisible = true;
        },

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

        },
        //查询学科
        getSub() {
            subSchoolLab().then(response => {
                this.loading = false;
                this.subList = response.data;
                console.log('this.subList', this.subList);
            });
        },


        //查看记录操作
        handleRecord(row) {
            console.log(row);
            this.open = true;
            this.title = "实验室使用记录明细";
            const params = {
                labId: row.id,
                sub: this.formlook.sub,
                gradeId: this.formlook.gradeId,
                experimentPlanId: this.form.id,
                experimentClassify: this.formlook.experimentClassify,
                className: this.formlook.className,
                startTime: this.formlook.startTime,
                endTime: this.formlook.endTime,
            };

            console.log('实验', params);
            getClassDetails(params).then(response => {
                this.ExperimentalData = response.rows;
                console.log('this.formjl', this.formjl);
                this.loading = false
            })
        },
        //取消按钮
        cancel() {
            this.dialogTableVisible = false;
            this.reset();
        },
        //提交按钮
        submitForm: function () {
            const inChargeId = this.form.inChargeId
            this.form.inChargeName = inChargeId == '' ? '' : this.adminList.find(item => item.userId == inChargeId).userName
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != "") {
                        updateSchoolLab(this.form).then(response => {
                            console.log('this.form', this.form);
                            this.$modal.msgSuccess("修改成功");
                            this.dialogTableVisible = false;
                            this.getList();
                        });
                    } else {
                        addSchoolLab(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.dialogTableVisible = false;
                            this.getList();
                        });
                    }
                }
            });
        },



    },
}
</script>

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