<template>
    <div class="app-container">
        <el-dialog title="查询" :visible.sync="query" width="60%" append-to-body>
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                label-width="68px">
                <el-form-item label="学年" prop="schoolYear">
                    <el-select v-model="queryParams.schoolYear" clearable placeholder="请选择">
                        <el-option v-for="dict in dict.type.yearda" :key="dict.value" :label="dict.label"
                            :value="dict.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="学期" prop="semester">
                    <el-select v-model="queryParams.semester" clearable placeholder="请选择">
                        <el-option v-for="dict in dict.type.semester_jsdzda" :key="dict.value" :label="dict.label"
                            :value="dict.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <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-form-item label="年级" prop="grade">
                    <el-select v-model="queryParams.grade" clearable placeholder="请选择">
                        <el-option v-for="dict in dict.type.grade_da" :key="dict.value" :label="dict.label"
                            :value="dict.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="班级" prop="className">
                    <el-input v-model="queryParams.className" placeholder="请输入班级" clearable
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="嘉奖类别" prop="awardType">
                    <el-input v-model="queryParams.awardType" placeholder="请输入嘉奖级别" clearable
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="嘉奖级别" prop="awardRank">
                    <el-select v-model="queryParams.awardRank" clearable placeholder="请选择">
                        <el-option v-for="dict in dict.type.student_award_rank" :key="dict.value" :label="dict.label"
                            :value="dict.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="嘉奖时间" prop="awardTime">
                    <el-date-picker clearable v-model="queryParams.awardTime" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择嘉奖时间" style="width: 192px;">
                    </el-date-picker>
                </el-form-item>
                <el-form-item class="dialog-footer" style="display: flex; justify-content: center; align-items: center">
                    <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="['school:studentAward: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="['school:studentAward: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="['school:studentAward:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-download" size="mini" @click="handleImport"
                    v-hasPermi="['school:studentAward:import']">导入</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button plain type="warning" icon="el-icon-upload2" size="mini" @click="handleExport"
                    v-hasPermi="['school:studentAward:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table :row-style="{ height: '0' }" :cell-style="{ padding: '3px' }" :data="studentAwardList"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="学年" align="center" prop="schoolYear" width="100px" show-overflow-tooltip />
            <el-table-column label="学期" align="center" prop="semester" width="80px" show-overflow-tooltip>
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.semester_jsdzda" :value="scope.row.semester" />
                </template>
            </el-table-column>
            <el-table-column label="届别" align="center" prop="year" width="60px" />
            <el-table-column label="年级" align="center" prop="grade" width="60px">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.grade_da" :value="scope.row.grade" />
                </template>
            </el-table-column>
            <el-table-column label="班级" align="center" prop="className" width="60px" />
            <el-table-column label="班级类型" align="center" prop="classType" width="80px" />
            <el-table-column label="姓名" align="center" prop="name" width="80px" show-overflow-tooltip />
            <el-table-column label="班主任" align="center" prop="classTeacher" width="80px" show-overflow-tooltip />
            <el-table-column label="嘉奖级别" align="center" prop="awardRank" width="80px">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.student_award_rank" :value="scope.row.awardRank" />
                </template>
            </el-table-column>
            <el-table-column label="嘉奖类别" align="center" prop="awardType" width="80px" show-overflow-tooltip />
            <el-table-column label="嘉奖时间" align="center" prop="awardTime" width="160">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.awardTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="审核状态" align="center" prop="auditState" fixed="right">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.audit_states" :value="scope.row.auditState" />
                </template>
            </el-table-column>
            <el-table-column align="center" fixed="right" label="操作" width="180">
                <template slot-scope="scope">
                    <div v-if="scope.row.auditState === '0' || scope.row.auditState === '3'">
                        <el-button @click="handleExamine(scope.row)" type="text" size="small">提交</el-button>
                        <el-button @click="handleUpdate(scope.row)" type="text" size="small">编辑</el-button>
                        <el-button @click="handleLook(scope.row)" type="text" size="small">查看</el-button>
                        <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
                    </div>
                    <div v-else>
                        <el-button @click="handleLook(scope.row)" type="text" size="small">查看</el-button>
                    </div>
                </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="90px" :disabled="isEdit">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="学年" prop="schoolYear">
                            <el-select v-model="form.schoolYear" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.yearda" :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="semester">
                            <el-select v-model="form.semester" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.semester_jsdzda" :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="year">
                            <el-select v-model="form.year" clearable placeholder="请选择" style="width: 100%;">
                                <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="grade">
                            <el-select v-model="form.grade" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.grade_da" :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="className">
                            <el-select v-model="form.className" 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-col :span="12">
                        <el-form-item label="班级类型" prop="classType">
                            <el-select v-model="form.classType" clearable placeholder="请选择" style="width: 100%">
                                <el-option v-for="dict in dict.type.class_type_wj" :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="name">
                            <el-input v-model="form.name" placeholder="请输入姓名" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="身份证号" prop="idCard">
                            <el-input v-model="form.idCard" placeholder="请输入身份证号" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="班主任" prop="classTeacher">
                            <el-input v-model="form.classTeacher" placeholder="请输入班主任" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="级部主任" prop="departmentDirector">
                            <el-input v-model="form.departmentDirector" placeholder="请输入级部主任" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="嘉奖类别" prop="awardType">
                            <el-input v-model="form.awardType" placeholder="请输入嘉奖类别" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="嘉奖级别" prop="awardRank">
                            <el-select v-model="form.awardRank" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.student_award_rank" :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="awardTime">
                            <el-date-picker clearable v-model="form.awardTime" type="date" value-format="yyyy-MM-dd"
                                placeholder="请选择嘉奖时间" style="width: 100%;">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer">
                <el-button v-if="!isEdit" type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">{{ isEdit ? '取 消' : '取 消' }}</el-button>
            </div>
        </el-dialog>

        <!-- 导入对话框 -->
        <el-dialog :title="title" :visible.sync="uploadOpen" width="520px" append-to-body inline>
            <el-form ref="uploadForm" :model="uploadForm" :rules="rules" label-width="70px">
                <el-form-item prop="dataUrl">
                    <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
                        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
                        <i class="el-icon-upload"></i>
                        <div class="el-upload__text">
                            将文件拖到此处，或<em>点击上传</em>
                        </div>
                        <div class="el-upload__tip text-center" slot="tip">
                            <span>仅允许导入xls、xlsx格式文件。</span>
                            <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline"
                                @click="importTemplate">下载模板
                            </el-link>
                        </div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div class="mask" v-if="importing">
                <div class="mask-content">
                    <i class="el-icon-loading"></i>
                    <span>正在导入，请稍候...</span>
                </div>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button @click="cancelFile">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import { checkIdcard } from '@/utils/utilLibrary/validate'
import { listStudentAward, listStudentAwardgr, getStudentAward, delStudentAward, addStudentAward, addStudentAwardgr, updateStudentAward, reviewStudentAward } from "@/api/smartSchool/classAdviser/studentCommendation";
export default {
    name: "StudentAward",
    dicts: ['semester_jsdzda', 'exam_typeda', 'grade_da', 'class_type_wj', 'yearda', 'rankda', 'appointment_situation', 'appointment_positions', 'class_num', 'audit_states', 'student_award_rank'],
    data() {
        return {
            title: "",
            importing: false,
            uploadOpen: false,
            upload: {
                // 是否显示弹出层（用户导入）
                open: false,
                // 弹出层标题（用户导入）
                title: "",
                // 是否禁用上传
                isUploading: false,
                // 是否更新已经存在的用户数据
                updateSupport: 0,
                // 设置上传的请求头部
                headers: { Authorization: "Bearer " + getToken() },
                // 上传的地址
                url:
                    process.env.VUE_APP_BASE_API + "/school/studentAward/importDataGr",
            },
            uploadForm: {},
            baseUrl: [process.env.VUE_APP_BASE_API],
            uploadLoading: false,
            uploadFileLoading: false,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 学生嘉奖表格数据
            studentAwardList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            isEdit: false,
            query: false,
            look: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                schoolYear: null,
                semester: null,
                year: null,
                grade: null,
                className: null,
                classType: null,
                name: null,
                idCard: null,
                sex: null,
                classTeacherId: null,
                classTeacher: null,
                departmentDirectorId: null,
                departmentDirector: null,
                awardType: null,
                awardRank: null,
                awardTime: null,
                auditState: null,
                createById: null,
                updateById: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                schoolYear: [
                    { required: true, message: "学年不能为空", trigger: "blur" }
                ],
                semester: [
                    { required: true, message: "学期不能为空", trigger: "blur" }
                ],
                year: [
                    { required: true, message: "界别不能为空", trigger: "blur" }
                ],
                grade: [
                    { required: true, message: "年级不能为空", trigger: "blur" }
                ],
                className: [
                    { required: true, message: "班级不能为空", trigger: "blur" }
                ],
                classType: [
                    { required: true, message: "班级类型不能为空", trigger: "blur" }
                ],
                name: [
                    { required: true, message: "姓名不能为空", trigger: "blur" }
                ],
                idCard: [{ required: true, validator: checkIdcard, trigger: 'blur' }],
                sex: [
                    { required: true, message: "性别不能为空", trigger: "blur" }
                ],
                classTeacher: [
                    { required: true, message: "班主任不能为空", trigger: "blur" }
                ],
                departmentDirector: [
                    { required: true, message: "级部主任不能为空", trigger: "blur" }
                ],
                awardType: [
                    { required: true, message: "嘉奖类别不能为空", trigger: "blur" }
                ],
                awardRank: [
                    { required: true, message: "嘉奖级别不能为空", trigger: "blur" }
                ],
                awardTime: [
                    { required: true, message: "嘉奖时间不能为空", trigger: "blur" }
                ],
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询学生嘉奖列表 */
        getList() {
            this.loading = true;
            listStudentAwardgr(this.queryParams).then(response => {
                this.studentAwardList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.look = false;
            this.reset();
        },

        //查看
        handleLook(row) {
            console.log(row);
            const id = row.id || this.ids;
            getStudentAward(id).then((response) => {
                this.form = response.data;
                this.isEdit = true;
                this.open = true;
                this.title = "查看学生嘉奖";
            }).catch(error => {
                this.$modal.closeLoading();
            })

        },

        //提交
        handleExamine(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm("是否提交该信息？")
                .then(function () {
                    const params = {
                        id: row.id,
                        auditState: '1'  // 表示同意
                    };
                    return reviewStudentAward(params);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("提交成功");
                })
                .catch((err) => {
                    console.error(err);
                });
        },

        // 表单重置
        reset() {
            this.form = {
                id: null,
                schoolYear: null,
                semester: null,
                year: null,
                grade: null,
                className: null,
                classType: null,
                name: null,
                idCard: null,
                sex: null,
                classTeacherId: null,
                classTeacher: null,
                departmentDirectorId: null,
                departmentDirector: null,
                awardType: null,
                awardRank: null,
                awardTime: null,
                remark: null,
                auditState: null,
                createById: null,
                createBy: null,
                createTime: null,
                updateById: null,
                updateBy: null,
                updateTime: null,
                delFlag: null
            };
            this.resetForm("form");
        },
        handleQuerycx() {
            this.query = true;
        },
        handleQuery() {
            this.getList();
            this.query = false;
        },


        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            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.title = "添加学生嘉奖";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getStudentAward(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.isEdit = false;
                this.title = "修改学生嘉奖";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateStudentAward(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addStudentAwardgr(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 delStudentAward(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        handleImport() {
            this.reset();
            this.title = "导入";
            this.uploadOpen = true;
        },
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
            this.importing = true;

        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit();
        },
        cancelFile() {
            this.uploadOpen = false;
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.uploadOpen = false;
            this.upload.isUploading = false;
            this.importing = false;
            this.$refs.upload.clearFiles();
            this.$alert(
                "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
                response.msg +
                "</div>",
                "导入结果",
                { dangerouslyUseHTMLString: true }
            );
            this.getList();
        },
        //下载模版
        importTemplate() {
            this.download(
                "/school/studentAward/importTemplate",
                {},
                `学生嘉奖_${Date.now()}.xlsx`
            );
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('/school/studentAward/exportGr', {
                ...this.queryParams
            }, `学生嘉奖_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>

<style scoped lang="scss" >
.mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
}

.mask-content {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #1a74be;
    font-size: 16px;
}

::v-deep .el-form-item {
    margin-bottom: 12px;
}

::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;
}

.search ::v-deep .el-form-item__content {
    width: 200px
}
</style>