<template>
    <div class="app-container" v-loading.fullscreen.lock="fullLoading">
        <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" label-width="68px">
            <el-form-item label="学号" prop="stuNo">
                <el-input v-model="queryParams.stuNo" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="学生姓名" prop="stuName">
                <el-input v-model="queryParams.stuName" placeholder="请输入学生姓名" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="奖项" prop="code">
                <el-select v-model="queryParams.code">
                    <el-option v-for="(v, i) in code_list" :key="i" clearable :value="v.code"
                        :label="v.name"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label=" ">
                <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-table v-loading="loading" :data="table_data">
            <el-table-column label="奖项" prop="typeName" align="center" />
            <el-table-column label="学生" prop="stuName" align="center" />
            <el-table-column label="学号" prop="stuNo" align="center" />
            <el-table-column label="提交时间" prop="createTime" align="center" />
            <el-table-column label="申请状态" align="center">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.audius_detail_type" :value="scope.row.applyStatus" />
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button v-if="scope.row.applyStatus == 1" type="text" size="mini" icon="el-icon-view"
                        @click="auditVClick(scope.row)">审核</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="申请" :visible.sync="auditV" fullscreen :append-to-body="true">
            <el-steps :active="getStep(auditForm)">
                <el-step title="学生提交申请" description=""></el-step>

                <el-step title="辅导员审核"
                    :status="isEmpty(auditForm.fdyComment) ? 'wait' : getStepStatus(auditForm.fdyComment)">
                    <template slot="description">
                        <div>
                            {{ isEmpty(auditForm.fdyNoName) ? '' : auditForm.fdyNoName }}
                        </div>
                        <div>
                            {{ isEmpty(auditForm.fdyComment) ? '' : auditForm.fdyComment }}
                        </div>
                        <div>
                            {{ isEmpty(auditForm.fdyTime) ? '' : auditForm.fdyTime }}
                        </div>
                    </template>
                </el-step>

                <el-step title="学院审核"
                    :status="isEmpty(auditForm.deptComment) ? 'wait' : getStepStatus(auditForm.deptComment)">
                    <template slot="description">
                        <div>
                            {{ isEmpty(auditForm.deptNoName) ? '' : auditForm.deptNoName }}
                        </div>
                        <div>
                            {{ isEmpty(auditForm.deptComment) ? '' : auditForm.deptComment }}
                        </div>
                        <div>
                            {{ isEmpty(auditForm.deptTime) ? '' : auditForm.deptTime }}
                        </div>
                    </template>
                </el-step>

                <el-step title="学工审核"
                    :status="isEmpty(auditForm.finalComment) ? 'wait' : getStepStatus(auditForm.finalComment)">
                    <template slot="description">
                        <div>
                            {{ isEmpty(auditForm.finalNoName) ? '' : auditForm.finalNoName }}
                        </div>
                        <div>
                            {{ isEmpty(auditForm.finalComment) ? '' : auditForm.finalComment }}
                        </div>
                        <div>
                            {{ isEmpty(auditForm.finalTime) ? '' : auditForm.finalTime }}
                        </div>
                    </template>
                </el-step>
            </el-steps>
            <hr />
            <el-form class="detail" label-width="160px">
                <el-row>
                    <div style="display: flex;">
                        <el-col :span="12" style="margin-right: 50px;">
                            <el-form-item label="班级">
                                <el-input readonly size="mini" v-model="auditForm.className"
                                    class="auditForm-input"></el-input>
                            </el-form-item>
                            <el-form-item label="姓名">
                                <el-input readonly size="mini" v-model="auditForm.stuName"
                                    class="auditForm-input"></el-input>
                            </el-form-item>

                            <el-form-item label="政治面貌">
                                <el-input readonly size="mini" class="auditForm-input"
                                    v-model="auditForm.zzmm"></el-input>
                            </el-form-item>
                            <!-- 
                            <el-form-item v-if="!isEmpty(auditForm.youthPercent)" label="青年大学习学习率">
                                <el-input readonly size="mini" class="auditForm-input"
                                    v-model="auditForm.youthPercent"></el-input>
                            </el-form-item> -->
                            <el-form-item v-if="!isEmpty(auditForm.classtwoSure)" label="第二课堂学分认定">
                                <el-input readonly size="mini" class="auditForm-input"
                                    v-model="auditForm.classtwoSure"></el-input>
                            </el-form-item>

                            <el-form-item label="职务">
                                <el-input readonly size="mini" class="auditForm-input"
                                    v-model="auditForm.classPost"></el-input>
                            </el-form-item>
                            <el-form-item v-if="!isEmpty(auditForm.bankCard)" label="中国农业银行银行卡号">
                                <el-input readonly size="mini" class="auditForm-input"
                                    v-model="auditForm.bankCard"></el-input>
                            </el-form-item>
                            <el-form-item v-if="!isEmpty(auditForm.bankAddr)" label="开户行">
                                <el-input readonly size="mini" class="auditForm-input"
                                    v-model="auditForm.bankAddr"></el-input>
                            </el-form-item>
                            <el-form-item label="获奖情况">
                                <el-input readonly type="textarea" :maxlength="1000"
                                    :autosize="{ minRows: 10, maxRows: 10 }" class="auditForm-input"
                                    v-model="auditForm.goodHis"></el-input>
                            </el-form-item>
                            <el-form-item label="主要事迹">
                                <el-input readonly type="textarea" :maxlength="1000"
                                    :autosize="{ minRows: 10, maxRows: 10 }" class="auditForm-input"
                                    v-model="auditForm.mainHis"></el-input>
                            </el-form-item>
                            <el-form-item label="您的审核意见">
                                <el-row>
                                    <el-col :span="12">
                                        <el-radio-group v-model="doForm.status">
                                            <el-radio :label="10">驳回</el-radio>
                                            <el-radio :label="11">拒绝</el-radio>
                                            <el-radio :label="6">通过</el-radio>
                                        </el-radio-group>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-button @click="doAudit" size="mini">提交审核</el-button>
                                    </el-col>

                                </el-row>

                                <el-input placeholder="请输入审核意见" type="textarea" :maxlength="1000"
                                    :autosize="{ minRows: 10, maxRows: 10 }" class="auditForm-input"
                                    v-model="doForm.common"></el-input>
                            </el-form-item>
                        </el-col>


                        <el-col :span="12">
                            <div>
                                <el-row>
                                    <el-col :span="4">
                                        <span style="font-weight: bolder;">该生申请奖项:</span>
                                    </el-col>
                                    <el-col :span="8">
                                        <span style="color: red; font-weight: bolder;">
                                            {{ auditForm.typeName }}
                                        </span>
                                    </el-col>
                                    <el-col :span="6">
                                        <el-button @click="lookNeedV = true;" size="mini">查看评优要求</el-button>
                                    </el-col>
                                </el-row>
                            </div>


                            <el-drawer :modal="false" size="50%" title="评优要求" :visible.sync="lookNeedV" direction="ltr"
                                :append-to-body="true">
                                <div class="sign">
                                    <ol>
                                        <h3>学业奖学金</h3>
                                        <li>
                                            一等奖学金：平均学分绩班级排名前3%，学年综合素质测评班级排名前50%；
                                        </li>
                                        <li>
                                            二等奖学金：平均学分绩级排名前6%，学年综合素质测评班级排名前50%；
                                        </li>
                                        <li>
                                            三等奖学金：平均学分绩班级排名前9%，学年综合素质测评班级排名前50%
                                        </li>
                                    </ol>

                                </div>

                                <div class="sign">
                                    <ol>
                                        <h3>三好学生</h3>
                                        <li>
                                            德、智、体、美、劳全面发展，平时注重道德修养，勤俭节约，弘扬正能量，树立良好形象。
                                        </li>
                                        <li>
                                            以身作则，为人表率，具有较强的集体观念和集体荣誉感。热心为同学服务，待人诚恳、谦虚、有礼貌。
                                        </li>
                                        <li>
                                            积极参加学校开展的第二课堂活动，第二课堂成绩单学分认定为优秀。在各级各类活动（竞赛）中成绩突出。
                                        </li>
                                        <li>
                                            积极参加体育锻炼及其他文体活动，身体健康，体能考核成绩为良好及以上。(体测成绩80分以上)
                                        </li>
                                        <li>
                                            参评学年各科平均学分绩75分以上（含75分），考查课“中等”及以上，平均学分绩班级排名前30%，学年综合素质测评班级排名前30%
                                            <!-- ，学年青年大学习学习率达100%。 -->
                                        </li>
                                    </ol>
                                </div>

                                <div class="sign">
                                    <ol>
                                        <h3>优秀学生干部</h3>
                                        <li>
                                            关心集体，吃苦在前，以身作则，能起到模范带头作用。
                                        </li>
                                        <li>
                                            具有较强的工作能力和组织能力，工作认真负责，责任心强，团结同学，热心为同学服务，工作效果好。社会实践工作取得较好成绩，在学生中有较高威信。
                                        </li>
                                        <li>
                                            积极参加学校开展的第二课堂活动，第二课堂成绩单学分认定为优秀。在各级各类活动（竞赛）中成绩突出。
                                        </li>
                                        <li>
                                            积极参加体育锻炼及其他文体活动，身体健康，体能考核成绩为良好及以上。
                                        </li>
                                        <li>
                                            参评学年各科平均学分绩为70分以上（含70分），平均学分绩班级排名前40%，学年综合素质测评班级排名前50%
                                            <!-- ，学年青年大学习学习率达100%。 -->
                                        </li>
                                        <span @click="lookMore"
                                            style="text-decoration: underline;color: lightskyblue;cursor: pointer;">查看更多</span>
                                    </ol>

                                </div>

                            </el-drawer>


                            <el-table :data="score_list">
                                <el-table-column label="学年" prop="stuYearName" align="center" />
                                <el-table-column label="平均学分绩" prop="stuScore" align="center" />
                                <el-table-column label="综合素质总成绩" prop="cphScore" align="center" />
                                <el-table-column label="体测成绩" prop="sportScore" align="center" />
                                <el-table-column label="平均学分绩班级排名" align="center">
                                    <template slot-scope="scope">
                                        {{ scope.row.stuClassRank + '/' + scope.row.classCount }}
                                    </template>
                                </el-table-column>
                                <el-table-column label="综合素质总成绩班级排名" align="center">
                                    <template slot-scope="scope">
                                        {{ scope.row.cphClassRank + '/' + scope.row.classCount }}
                                    </template>
                                </el-table-column>
                                <el-table-column label="平均学分绩班级排名百分比" align="center">
                                    <template slot-scope="scope">
                                        {{ Math.round(scope.row.stuClassRank / scope.row.classCount * 10000) / 100 + "%"
                                        }}
                                    </template>
                                </el-table-column>
                                <el-table-column label="学年综合素质测评班级排名百分比" align="center">
                                    <template slot-scope="scope">
                                        {{ Math.round(scope.row.cphClassRank / scope.row.classCount * 10000) / 100 + "%"
                                        }}
                                    </template>
                                </el-table-column>
                            </el-table>

                            <el-table :data="count_list">
                                <el-table-column label="学年" prop="stuYearName" align="center" />
                                <el-table-column label="课程总门数" prop="courseCount" align="center" />
                                <el-table-column label="通过门数" prop="passCount" align="center" />
                                <el-table-column label="未通过门数" prop="unpassCount" align="center" />
                            </el-table>

                            <el-row>
                                <el-col :span="12" v-for="(v, i) in course_list" :key="i">
                                    <el-table :header-row-style="{ padding: '0' }" :data="v.value"
                                        :row-style="{ height: '0' }" :cell-style="{ padding: '0' }">
                                        <el-table-column :label="v.name" align="center">
                                            <el-table-column label="课程" prop="courseName" align="center" />
                                            <el-table-column label="成绩" prop="courseScore" align="center" />
                                        </el-table-column>
                                    </el-table>
                                </el-col>
                            </el-row>

                        </el-col>
                    </div>


                </el-row>
            </el-form>

        </el-dialog>

        <el-dialog title="导出奖项审核结果" :visible.sync="exportV" width="500px">

            <el-select placeholder="请选择学年" v-model="exportForm.stuYearId">
                <el-option v-for="(v, i) in year_list" :key="i" clearable :value="v.id"
                    :label="v.stuYearName"></el-option>
            </el-select>

            <el-select placeholder="请选择奖项" v-model="exportForm.typeName">
                <el-option v-for="(v, i) in code_list" :key="i" clearable :value="v.code" :label="v.name"></el-option>
            </el-select>

            <el-select style="width: 442px;" placeholder="请选择班级" v-model="exportForm.classId">
                <el-option v-for="(v, i) in class_list" :key="i" clearable :value="v.classId"
                    :label="v.className"></el-option>
            </el-select>
            <div style="font-size: small;margin: 10px 0 10px 10px; color: red;">*学年、奖项是必选项，班级不选就默认导出全部。</div>
            <el-button @click="doExport">导出</el-button>
        </el-dialog>


        <el-dialog :visible.sync="lookMoreV" :append-to-body="true">
            <MoreDetail></MoreDetail>
        </el-dialog>

    </div>
</template>
<script>
import {
    listFdyAudit, lookDetail, listStuScoreClassRank, listStuYearCourseScore, countStuPass, countStuIam, fdyAudit
    , fdyExport, applyStatus
} from "@/api/stuCQS/good/audit";
import { listOwnClass } from "@/api/stuCQS/info-fill/stu_eva_task";
import { listAllYear as listYear } from "@/api/stuCQS/basedata/year";

import * as XLSX from 'xlsx';
import { workbook2blob, openDownloadDialog, file2Xce } from "@/api/helpFunc";
import lodash from "lodash";
import { isEmpty, groupBy } from "@/api/helpFunc";
import MoreDetail from '@/views/stuCQS/good/MoreDetail.vue';


export default {
    name: "fdy-audit",
    dicts: ['audius_detail_type'],
    components: {
        MoreDetail
    },
    data() {
        return {
            applyStatus,

            code_list: [
                {
                    name: "学业奖学金",
                    code: "XYJXJ"
                },
                {
                    name: "三好学生",
                    code: "SHXS"
                },
                {
                    name: "优秀学生干部",
                    code: "YXXSGB"
                }
            ],

            isEmpty,

            fullLoading: false,

            loading: false,
            table_data: [],
            total: 0,
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                code: null,
                status: null,
                stuNo: "",
                stuName: ""
            },

            auditV: false,
            auditForm: {},
            material_list: [],

            score_list: [],
            myScoreV: false,
            course_list: [],
            courseParams: {
                pageNum: 1,
                pageSize: 10
            },
            courseTotal: 0,
            course_loading: false,

            count_list: [],

            iamV: false,
            iam_count: [],

            doForm: {
                auditId: null,
                status: null,
                common: ""
            },

            exportForm: {
                typeName: null,
                classId: null,
                stuYearId: null
            },
            exportV: false,

            class_list: [],
            year_list: [],

            lookNeedV: false,
            lookMoreV: false,
        }
    },
    created() {
        this.listOwnClass();
        this.listAllStuYear();
        this.getList();
    },
    methods: {
        getStepStatus(data) {
            if ((data.includes('同意') && !data.includes('不同意')) || data.includes('通过') || (data.includes('符合') && !data.includes('不符合'))) {
                return 'success';
            } else {
                return 'error';
            }
        },
        getStep(data) {
            if (!isEmpty(data.finalComment)) {
                return 4;
            } else if (!isEmpty(data.deptComment)) {
                return 3;
            } else if (!isEmpty(data.fdyComment)) {
                return 2;
            } else {
                return 1;
            }
        },
        lookMore() {
            this.lookMoreV = true
            window.open('http://zhxg.gxsdxy.cn/file/pypxnotify.pdf')
        },
        async listAllStuYear() {
            let res = await listYear();
            if (res.code == 200) {
                this.year_list = [...res.data];
            }
        },
        async listOwnClass() {
            let res = await listOwnClass();
            if (res.code == 200) {
                this.class_list = [...res.data];
            }
        },
        async doExport() {
            let sdata = { ...this.exportForm };
            if (isEmpty(sdata.stuYearId)) {
                this.$message.info("请选择学年");
                return;
            }
            if (isEmpty(sdata.typeName)) {
                this.$message.info("请选择奖项");
                return;
            }

            this.loading = true;
            let res = await fdyExport(sdata);
            this.loading = false;
            if (res.code == 200) {
                let data = res.data;
                let fileName = "";
                let fileHeader = [];
                let fileHeaderZh = {};
                let fileCol = [];
                switch (sdata.typeName) {
                    case "XYJXJ":
                        fileHeader = ['stuYearName', 'deptName', 'majorName', 'className', 'stuNo', 'stuName', 'typeName', 'applyStatus', 'bankCard', 'bankAddr', 'importTime', 'teacherName'];
                        fileHeaderZh = {
                            'stuYearName': "学年",
                            "deptName": "学院",
                            "majorName": "专业",
                            "className": "班级",
                            "stuNo": "学号",
                            "stuName": "姓名",
                            "typeName": "奖项",
                            "applyStatus": "审核状态",
                            "bankCard": "银行卡号",
                            "bankAddr": "开户行",
                            "importTime": "导出时间",
                            'teacherName': "辅导员"
                        };
                        fileCol = [
                            { wch: 15 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 10 },
                            { wch: 10 },
                            { wch: 15 },
                            { wch: 20 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 40 }
                        ];
                        fileName = "学业奖学金名单";
                        break;
                    case "SHXS":
                        fileHeader = ['stuYearName', 'deptName', 'majorName', 'className', 'stuNo', 'stuName', 'typeName', 'applyStatus', 'importTime', 'teacherName'];
                        fileHeaderZh = {
                            'stuYearName': "学年",
                            "deptName": "学院",
                            "majorName": "专业",
                            "className": "班级",
                            "stuNo": "学号",
                            "stuName": "姓名",
                            "typeName": "奖项",
                            "applyStatus": "审核状态",
                            "importTime": "导出时间",
                            'teacherName': "辅导员"
                        };
                        fileCol = [
                            { wch: 15 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 10 },
                            { wch: 10 },
                            { wch: 15 },
                            { wch: 15 },
                            { wch: 40 },
                            { wch: 40 }
                        ];
                        fileName = "优秀学生干部名单";
                        break;
                    case "YXXSGB":
                        fileHeader = ['stuYearName', 'deptName', 'majorName', 'className', 'stuNo', 'stuName', 'typeName', 'applyStatus', 'importTime', 'teacherName'];
                        fileHeaderZh = {
                            'stuYearName': "学年",
                            "deptName": "学院",
                            "majorName": "专业",
                            "className": "班级",
                            "stuNo": "学号",
                            "stuName": "姓名",
                            "typeName": "奖项",
                            "applyStatus": "审核状态",
                            "importTime": "导出时间",
                            'teacherName': "辅导员"
                        };
                        fileCol = [
                            { wch: 15 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 10 },
                            { wch: 10 },
                            { wch: 15 },
                            { wch: 15 },
                            { wch: 40 },
                            { wch: 40 }
                        ];
                        fileName = "学业奖学金名单";
                        break;
                }

                const arrayWithHeader = [fileHeaderZh, ...data];

                var sheet1 = XLSX.utils.json_to_sheet(arrayWithHeader,
                    {
                        header: fileHeader,
                        skipHeader: true
                    });
                sheet1["!cols"] = fileCol;

                // 创建一个新的空的workbook
                var wb = XLSX.utils.book_new();
                // 为每一个工作簿设置名称并添加到workbook（excel表）中
                XLSX.utils.book_append_sheet(wb, sheet1, fileName);
                const workbookBlob = workbook2blob(wb);  // 辅助函数workbook2blob
                // 下载文档并添加文件名称
                openDownloadDialog(workbookBlob, fileName + ".xlsx");  // 辅助函数openDownloadDialog


            } else {
                this.$message.info("出错啦，刷新试试");
            }


        },
        async exportVClick() {
            this.exportV = true;
        },
        async getList() {
            this.loading = true;
            let res = await listFdyAudit(this.queryParams);
            this.loading = false;
            if (res.code == 200) {
                this.table_data = [...res.rows];
                this.total = res.total;
            }
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        async auditVClick(v) {
            let id = v.applyId;
            this.fullLoading = true;
            let res = await lookDetail(id);
            this.fullLoading = false;
            if (res.code == 200) {
                if (!isEmpty(res.data.material)) {
                    this.material_list = res.data.material.split(",");
                } else {
                    this.material_list = [];
                }
                this.auditForm = res.data;
                this.auditV = true;
            }
            //
            this.myScoreVClick()
            //this.iamVClick()
        },
        async countStuIam() {
            let stuNo = this.auditForm.stuNo;
            let res = await countStuIam(stuNo);
            if (res.code == 200) {
                this.iam_count = res.data;
            }
        },

        async countStuPass() {
            let stuNo = this.auditForm.stuNo;

            let res = await countStuPass(stuNo);
            if (res.code == 200) {
                this.count_list = [...res.data];
            }
        },
        async listStuYearCourseScore() {
            let stuNo = this.auditForm.stuNo;

            this.course_loading = true;
            let res = await listStuYearCourseScore(stuNo, this.courseParams);
            this.course_loading = false;

            if (res.code == 200) {

                let groupdata = groupBy([...res.rows], x => {
                    return x.termName;
                });

                let temp = [];
                for (let i in groupdata) {
                    let needAddTemp = {};
                    needAddTemp.name = i.replace(/\"/g, "");
                    needAddTemp.value = groupdata[i];
                    temp.push(needAddTemp);
                }
                this.course_list = [...temp];
                this.courseTotal = res.total;
            }
        },
        async listStuScoreClassRank() {
            let stuNo = this.auditForm.stuNo;
            let res = await listStuScoreClassRank(stuNo);
            if (res.code == 200) {
                this.score_list = lodash.cloneDeep(res.data);
            }
        },
        iamVClick() {
            this.countStuIam();
            this.countStuPass();
            this.listStuScoreClassRank();
            this.listStuYearCourseScore();
            this.iamV = true;
        },
        myScoreVClick() {
            this.countStuPass();
            this.listStuScoreClassRank();
            this.listStuYearCourseScore();
            this.myScoreV = true;
        },
        async doAudit() {
            let sdata = { ...this.doForm };
            if (isEmpty(sdata.status)) {
                this.$message.info("请选择是否通过");
                return;
            }
            if (isEmpty(sdata.common)) {
                this.$message.info("请填写审核意见");
                return;
            }
            sdata.auditId = parseInt(this.auditForm.auditId);
            this.fullLoading = true;
            let res = await fdyAudit(sdata);
            this.fullLoading = false;
            if (res.code == 200) {
                this.getList();
                this.auditForm = {};
                this.doForm = {
                    auditId: null,
                    status: null,
                    common: ""
                };
                this.auditV = false;
                this.$message.success(res.msg);
            }
        }
    }
}

</script>
<style lang="scss" scoped>
.addForm-input {
    width: 400px;
}

.detail {
    .el-form-item {
        margin-bottom: 0px;
    }
}

.sign {
    width: 100%;
    border: 1px solid rgb(163, 211, 255);
    font-size: small;
    margin-bottom: 10px;
    border-radius: 2px;
}
</style>