<template>
    <div class="app-container">
        <el-dialog title="查询" :visible.sync="query" width="50%" append-to-body>
            <el-form ref="queryParams" :model="queryParams" label-width="96px" class="search">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="届别" prop="year">
                            <el-select v-model="queryParams.year" clearable placeholder="请选择">
                                <el-option v-for="dict in dict.type.rankda" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="学科" prop="sub">
                            <el-select v-model="queryParams.sub" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.teaching_subjects" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="教师姓名" prop="teacherName">
                            <el-input v-model="queryParams.teacherName" placeholder="请输入教师姓名" clearable
                                @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="授课班级" prop="teachingClassName">
                            <el-select v-model="queryParams.teachingClassName" clearable placeholder="请选择"
                                style="width: 100%">
                                <el-option v-for="dict in dict.type.class_num" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="班级类型" prop="classType">
                            <el-select v-model="queryParams.classType" clearable placeholder="请选择" style="width: 100%">
                                <el-option v-for="dict in dict.type.class_type" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="高考评优" prop="gkAppraising">
                            <el-select v-model="queryParams.gkAppraising" clearable placeholder="请选择" style="width: 100%">
                                <el-option v-for="dict in dict.type.gk_appraising" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-search" size="mini" @click="handleQuerycx">查询</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:achievement:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:achievement:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['system:achievement:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:achievement:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table :row-style="{ height: '1px' }" :cell-style="{ padding: '0px' }"
            :header-cell-style="{ height: '50px', fontSize: '12px', padding: '0 0' }" :data="achievementList"
            @selection-change="handleSelectionChange" :default-sort="{ prop: 'year', order: 'descending' }">
            <el-table-column type="selection" width="30" align="center" sortable />
            <el-table-column type="index" label="序号" width="30" align="center" sortable />
            <el-table-column label="届别" align="center" prop="year" sortable width="60" show-overflow-tooltip />
            <el-table-column label="学科" align="center" prop="sub" sortable width="60" show-overflow-tooltip>
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.teaching_subjects" :value="scope.row.sub" />
                </template>
            </el-table-column>
            <el-table-column label="教师姓名" align="center" prop="teacherName" sortable width="80" show-overflow-tooltip />
            <el-table-column label="授课班级" align="center" prop="teachingClassName" sortable width="80"
                show-overflow-tooltip />
            <el-table-column label="班级类型" align="center" prop="classType" sortable show-overflow-tooltip width="90">
                <!-- <template slot-scope="scope">
                    <span v-if="getSelectedTypes(scope.row.classType).length > 0">
                        <span v-for="(option, index) in getSelectedTypes(scope.row.classType)" :key="index">
                            {{ option.label }}
                            <span v-if="index !== getSelectedTypes(scope.row.classType).length - 1">, </span>
                        </span>
                    </span>
                </template> -->
            </el-table-column>
            <el-table-column label="高考评优" align="center" prop="gkAppraising" sortable width="110" show-overflow-tooltip>
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.gk_appraising" :value="scope.row.gkAppraising" />
                </template>
            </el-table-column>
            <el-table-column label="尖子生培养" align="center" prop="topStudentsCulture" sortable width="120"
                show-overflow-tooltip />
            <el-table-column label="增量情况" align="center" prop="incrementSituation" sortable width="120"
                show-overflow-tooltip />
            <el-table-column label="有效数情况" align="center" prop="effectiveNumSituation" sortable width="120"
                show-overflow-tooltip />
            <el-table-column label="其他情况" align="center" prop="other" sortable show-overflow-tooltip />
            <el-table-column label="审核状态" align="center" prop="auditState" sortable width="90" fixed="right">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.audit_states" :value="scope.row.auditState" />
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="right">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:achievement:edit']"
                        v-if="scope.row.auditState === '0' || scope.row.auditState === '3'">修改</el-button>
                    <el-button size="mini" type="text" @click="handleLook(scope.row)"
                        v-hasPermi="['system:achievement:look']"
                        v-if="scope.row.auditState === '0' || scope.row.auditState === '1' || scope.row.auditState === '3' || scope.row.auditState === '2'">查看</el-button>
                    <el-button size="mini" type="text" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:achievement:remove']"
                        v-if="scope.row.auditState === '0' || scope.row.auditState === '3'">删除</el-button>
                    <el-button size="mini" type="text" @click="handleExamine(scope.row)"
                        v-hasPermi="['system:achievement:sub']" v-if="scope.row.auditState === '0'">提交</el-button>

                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改高考成绩对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="92px" :disabled="isEdit" class="add">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="学科" prop="sub">
                            <el-select v-model="form.sub" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.teaching_subjects" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="教师姓名" prop="teacherName">
                            <el-input v-model="form.teacherName" placeholder="请输入教师姓名" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="届别" prop="year">
                            <el-select v-model="form.year" placeholder="请选择" style="width: 100%;" clearable>
                                <el-option v-for="dict in dict.type.rankda" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="授课班级" prop="teachingClassName">
                            <el-select v-model="form.teachingClassName" placeholder="请选择" style="width: 100%" clearable>
                                <el-option v-for="dict in dict.type.class_num" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="班级类型" prop="classType">
                            <el-select v-model="form.classType" placeholder="请选择" clearable style="width: 100%">
                                <el-option v-for="dict in dict.type.class_type" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="高考评优" prop="gkAppraising">
                            <el-select v-model="form.gkAppraising" placeholder="请选择" clearable style="width: 100%">
                                <el-option v-for="dict in dict.type.gk_appraising" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="尖子生培养" prop="topStudentsCulture">
                            <el-input v-model="form.topStudentsCulture" type="textarea" placeholder="请输入内容" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="增量情况" prop="incrementSituation">
                            <el-input v-model="form.incrementSituation" type="textarea" placeholder="请输入内容" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="有效数情况" prop="effectiveNumSituation">
                            <el-input v-model="form.effectiveNumSituation" clearable type="textarea"
                                placeholder="请输入有效数情况" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="其他" prop="other">
                            <el-input v-model="form.other" type="textarea" placeholder="请输入内容" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button v-if="isEdit === false" type="primary" @click="submitForm">确定</el-button>
                <el-button @click="cancel">{{ isEdit ? '取消' : '关闭' }}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listAchievement, getAchievement, delAchievement, addAchievement, updateAchievement, audit, listAchievementgr } from "@/api/smartSchool/electronicArchives/examinationScore";;

export default {
    name: "Achievement",
    dicts: ["teaching_subjects", 'class_type', 'rankda', 'gk_appraising', 'class_num', 'audit_states'],

    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 高考成绩表格数据
            achievementList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            isEdit: false,
            query: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                sub: null,
                teacherName: null,
                year: null,
                teachingClassName: null,
                classType: null,
                gkAppraising: null,
                topStudentsCulture: null,
                incrementSituation: null,
                effectiveNumSituation: null,
                other: null,
                auditState: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                sub: [
                    { required: true, message: "学科不能为空", trigger: "blur" }
                ],
                teacherName: [
                    { required: true, message: "教师姓名不能为空", trigger: "blur" }
                ],
                year: [
                    { required: true, message: "届别不能为空", trigger: "blur" }
                ],
                teachingClassName: [
                    { required: true, message: "授课班级不能为空", trigger: "blur" }
                ],
                classType: [
                    { required: true, message: "班级类型不能为空", trigger: "blur" }
                ],
                gkAppraising: [
                    { required: true, message: "高考评优不能为空", trigger: "blur" }
                ],
                topStudentsCulture: [
                    { required: true, message: "尖子生培养不能为空", trigger: "blur" }
                ],
                incrementSituation: [
                    { required: true, message: "增量情况不能为空", trigger: "blur" }
                ]


            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询高考成绩列表 */
        getList() {
            this.loading = true;
            const userId = this.$store.state.user.userId;
            const params = Object.assign({}, this.queryParams, { userId }); // 将 userId 添加到 queryParams 中
            listAchievementgr(params).then(response => {
                this.achievementList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },

        // getSelectedTypes(classType) {
        //     const selectedTypes = classType.split(','); // 将逗号分隔的字符串转换为数组
        //     return this.dict.type.class_type.filter(option => selectedTypes.includes(option.value.toString()));
        // },
        handleExamine(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm("是否提交该信息？")
                .then(function () {
                    const params = {
                        id: row.id,
                        auditState: '1'
                    };
                    return audit(params);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("提交成功");
                })
                .catch((err) => {
                    console.error(err);
                });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                sub: null,
                teacherName: null,
                year: null,
                teachingClassName: null,
                classType: null,
                gkAppraising: null,
                topStudentsCulture: null,
                incrementSituation: null,
                effectiveNumSituation: null,
                other: null,
                auditState: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                delFlag: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuerycx() {
            this.query = true;

        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
            this.query = false;
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryParams");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.isEdit = false;
            this.form.teacherName = this.$store.state.user.name;
            this.form.userId = this.$store.state.user.userId;
            this.title = "添加高考成绩";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            this.isEdit = false;
            const id = row.id || this.ids
            getAchievement(id).then(response => {
                this.form = response.data;
                // this.form.classType = this.form.classType.split(',');
                this.open = true;
                this.title = "修改高考成绩";
            });
        },
        // 查看按钮操作
        handleLook(row) {
            this.reset();
            this.open = true;
            this.isEdit = true;
            const id = row.id || this.ids
            getAchievement(id).then(response => {
                this.form = response.data;
                // this.form.classType = this.form.classType.split(',');
                this.title = "查看";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.form.auditState = '0';
            this.$refs["form"].validate(valid => {
                // const classTypeStr = this.form.classType.join(',');
                // console.log(classTypeStr); // 输出转换后的字符串
                if (valid) {
                    if (this.form.id != null) {
                        updateAchievement({ ...this.form }).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addAchievement({ ...this.form }).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除高考成绩编号为"' + ids + '"的数据项？').then(function () {
                return delAchievement(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            const userId = this.$store.state.user.userId;
            this.download('/achievement/export', {
                ...this.queryParams,
                userId: userId
            }, `高考成绩_${new Date().getTime()}.xlsx`)
        }

    }
};
</script>
<style scoped lang="scss" >
::v-deep .el-form-item {
    margin-bottom: 0px;
}

::v-deep .el-input--medium .el-input__inner {
    height: 26px;
    line-height: 26px;
}

::v-deep .el-dialog__body {
    padding: 10px 20px
}

::v-deep .el-dialog:not(.is-fullscreen) {
    margin-top: 4vh !important;
}

.add ::v-deep .el-textarea__inner {
    margin-bottom: 10px;
}

.search ::v-deep .el-form-item__content {
    width: 200px
}

::v-deep .el-table .el-table__cell {
    padding: 0;
}

::v-deep .el-table .cell {
    padding: 0;
}

::v-deep .el-table th.el-table__cell>.cell {
    padding: 0;

}
</style>
