<template>
  <div v-loading.fullscreen.lock="fullLoading" class="app-container">

    <el-form ref="queryForm" :model="queryParams" size="mini" :inline="true" label-width="68px">
      <el-form-item label="学院" prop="deptId">
        <el-select v-model="queryParams.deptId" size="mini" placeholder="学院" clearable
                   @change="changeSearchDept"
        >
          <el-option v-for="(v, i) in dept_list" :key="i" :label="v.label" :value="v.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="专业" prop="majorId">
        <el-select v-model="queryParams.majorId" placeholder="请先选择学院再选择专业" filterable
                   clearable @change="changeSearchMajor"
        >
          <el-option v-for="(v, i) in search_major_list" :key="i" :label="v.majorName" :value="v.majorId" />
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请先选择专业再选择班级" filterable clearable>
          <el-option v-for="(v, i) in search_class_list" :key="i" :label="v.className" :value="v.classId" />
        </el-select>
      </el-form-item>
      <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 search_type_list" :key="i" clearable :value="v.typeCode"
                     :label="v.typeName"
          />
        </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-button icon="el-icon-edit" type="success" size="mini" @click="manyVClick">一键审核通过</el-button>


    <el-table v-loading="loading" :data="table_data" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <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="deptName" align="center" />
      <el-table-column label="专业" prop="majorName" align="center" />
      <el-table-column label="班级" prop="className" align="center" />
      <el-table-column label="申请时间" prop="applyTime" align="center">
        <template slot-scope="scope">
          {{ dayjs(scope.row.applyTime).format("YYYY-MM-DD HH:mm:ss") }}
        </template>
      </el-table-column>
      <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 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 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.xwComment) ? 'wait' : getStepStatus(auditForm.xwComment)"
        >
          <template slot="description">
            <div>
              {{ isEmpty(auditForm.xwNoName) ? '' : auditForm.xwNoName }}
            </div>
            <div>
              {{ isEmpty(auditForm.xwComment) ? '' : auditForm.xwComment }}
            </div>
            <div>
              {{ isEmpty(auditForm.xwTime) ? '' : auditForm.xwTime }}
            </div>
          </template>
        </el-step>

        <el-step title="学工审核"
                 :status="isEmpty(auditForm.jwcComment) ? 'wait' : getStepStatus(auditForm.jwcComment)"
        >
          <template slot="description">
            <div>
              {{ isEmpty(auditForm.xgNoName) ? '' : auditForm.xgNoName }}
            </div>
            <div>
              {{ isEmpty(auditForm.jwcComment) ? '' : auditForm.jwcComment }}
            </div>
            <div>
              {{ isEmpty(auditForm.xgTime) ? '' : auditForm.xgTime }}
            </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 v-model="auditForm.className" readonly size="mini"
                          class="auditForm-input"
                />
              </el-form-item>
              <el-form-item label="姓名">
                <el-input v-model="auditForm.stuName" readonly size="mini"
                          class="auditForm-input"
                />
              </el-form-item>

              <el-form-item label="政治面貌">
                <el-input v-model="auditForm.zzmm" readonly size="mini"
                          class="auditForm-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 v-model="auditForm.classtwoSure" readonly size="mini"
                          class="auditForm-input"
                />
              </el-form-item>

              <el-form-item label="职务">
                <el-input v-model="auditForm.classPost" readonly size="mini"
                          class="auditForm-input"
                />
              </el-form-item>
              <el-form-item v-if="!isEmpty(auditForm.bankCard)" label="中国农业银行银行卡号">
                <el-input v-model="auditForm.bankCard" readonly size="mini"
                          class="auditForm-input"
                />
              </el-form-item>
              <el-form-item v-if="!isEmpty(auditForm.bankAddr)" label="开户行">
                <el-input v-model="auditForm.bankAddr" readonly size="mini"
                          class="auditForm-input"
                />
              </el-form-item>
              <el-form-item label="获奖情况">
                <el-input v-model="auditForm.goodHis" readonly type="textarea"
                          :maxlength="1000" :autosize="{ minRows: 10, maxRows: 10 }"
                          class="auditForm-input"
                />
              </el-form-item>
              <el-form-item label="主要事迹">
                <el-input v-model="auditForm.mainHis" readonly type="textarea"
                          :maxlength="1000" :autosize="{ minRows: 10, maxRows: 10 }"
                          class="auditForm-input"
                />
              </el-form-item>
              <el-form-item label="您的审核意见">
                <el-row>
                  <el-col :span="12">
                    <el-select v-model="doForm.applyStatus" placeholder="请选择审核意见"
                               style="width: 200px;" size="mini" @change="changeAuditStatus"
                    >
                      <el-option v-for="(v, i) in status_list" :key="i" :label="v.label"
                                 :value="v.value"
                      />
                    </el-select>
                  </el-col>
                  <el-col :span="12">
                    <el-button type="success" size="mini" @click="doAudit">提交审核</el-button>
                  </el-col>

                </el-row>

                <el-input v-model="doForm.jwcComment" placeholder="请输入审核意见" type="textarea"
                          :maxlength="1000" :autosize="{ minRows: 10, maxRows: 10 }"
                          class="auditForm-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 type="primary" size="mini"
                               @click="lookNeedV = true;"
                    >查看评优要求</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">
                  <ul>
                    <li>
                      申请要求:综合素质优秀。学年综合测评班级排名前30%；学习成绩优良，无不及格、补考、重修科目；体能成绩合格等
                    </li>
                    <li>
                      学业之星:
                      <ol>
                        <li>学业成绩名列前茅。学年平均学分绩在专业排名前5%；获得校级及以上先进个人项目表彰；</li>
                        <li>专业领域成果突出。在与专业相关的知识竞赛、技能竞赛中获奖。优先推荐发表过专业论文者。</li>
                      </ol>
                    </li>
                    <li>
                      技能之星:
                      <ol>
                        <li>技能或创新创业比赛成绩优异。积极参加技能训练、技能比赛或创新创业类比赛，在省部级及以上举办的技能类、创新创业类比赛中获得三等奖及以上，或在市厅级举办的技能类、创新创业类比赛中获得二等奖及以上，或在校级组织的技能类、创新创业类比赛中获得一等奖及以上；
                        </li>
                        <li>专业技能成绩优良。在本专业技能考核中成绩突出，学年平均学分绩在专业排名前30%；</li>
                        <li>专业技能突出。取得与专业相关的资格、资质证书、专利（排名前五）或者在专业技能某个方面特别突出者优先考虑。</li>
                      </ol>
                    </li>
                    <li>
                      励志之星:
                      <ol>
                        <li>在评选学年中被认定为家庭经济困难学生，自立自强，热爱生活，吃苦耐劳，勇于拼搏，乐观向上，有突出的励志事迹，能够激励他人、影响他人，在同学中认可度高；
                        </li>
                        <li>在专业学习、勤工俭学、社会实践、科技创新、励志感恩、创新创业、公益服务等方面取得突出成绩；</li>
                        <li>学业成绩优良，学年平均学分绩在专业排名前30%，获得校级及以上先进个人项目表彰。</li>
                      </ol>
                    </li>

                    <li>
                      才艺之星:
                      <ol>
                        <li>才艺成绩突出。在校内外各类文体赛事中（如体育、美术、音乐、舞蹈、戏剧、摄影摄像、写作、演讲、辩论等）成绩突出。
                        </li>
                        <li>学业成绩优良。学年平均学分绩在专业排名前30%。</li>
                      </ol>
                    </li>
                    <li>
                      服务之星:
                      <ol>
                        <li>积极主动为师生服务。在团委、学生会、易班、社团、协会、班级等组织中担任主要学生干部，认真履行职责，且获得过校级及以上优秀学生干部称号；
                        </li>
                        <li>积极参与社会服务。积极做好社会服务工作，在校级及以上大型活动中发挥重要作用，受到主办单位的嘉奖或认可，服务成效显著；</li>
                        <li>学业成绩优良。学年平均学分绩在专业排名前30%。</li>
                      </ol>
                    </li>

                    <li>
                      <span style="text-decoration: underline;color: lightskyblue;cursor: pointer;"
                            @click="lookMore"
                      >查看更多</span>
                    </li>
                  </ul>

                </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.stuMajorRank + '/' + scope.row.majorCount }}
                  </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.stuMajorRank / scope.row.majorCount * 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 v-for="(v, i) in course_list" :key="i" :span="12">
                  <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 :visible.sync="lookMoreV" :append-to-body="true">
      <MoreDetail />
    </el-dialog>


    <el-dialog title="一键审核通过" :visible.sync="manyV" :append-to-body="true">
      <el-input v-model="manyForm.common" type="textarea" placeholder="请输入审核意见" />
      <el-button type="primary" @click="doMany">一键审核通过</el-button>
    </el-dialog>


  </div>
</template>
<script>
import { getDeptName } from '@/api/system/dept'
import { listMajors } from '@/api/stuCQS/basedata/majors'
import { listClass } from '@/api/stuCQS/basedata/class'
import { listAllYear as listYear } from '@/api/stuCQS/basedata/year'
import { listSelect } from '@/api/stuCQS/lake/type'
import { listJwc, jwcAudit, manyAudit } from '@/api/stuCQS/lake/apply'

import {
    listStuScoreClassRank,listStuScoreClassRankByTag ,listStuYearCourseScore, countStuPass, countStuIam, applyStatus
} from '@/api/stuCQS/good/audit'

import lodash from 'lodash'
import { isEmpty, groupBy } from '@/api/helpFunc'
import MoreDetail from '@/views/stuCQS/lake/MoreDetail.vue'
import dayjs from 'dayjs'


export default {
    name: 'Lakefdy',
    dicts: ['audius_detail_type'],
    components: {
        MoreDetail
    },
    data() {
        return {
            dayjs,
            applyStatus,
            search_type_list: [],

            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: {
                id: null,
                applyStatus: null,
                jwcComment: '经过审核，同意该生申请。'
            },

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

            class_list: [],
            year_list: [],

            lookNeedV: false,
            lookMoreV: false,

            search_major_list: [],
            search_class_list: [],
            dept_list: [],

            manyV: false,
            applyIds: [],
            manyForm: {
                applyIds: [],
                common: '经过审核，同意该生申请。'
            },

            status_list: [
                {
                    label: '通过',
                    value: 6
                },
                {
                    label: '驳回(学生可以重新提交)',
                    value: 10
                },
                {
                    label: '拒绝(学生不可以再提交)',
                    value: 11
                }
            ]
        }
    },
    created() {
        this.listSelect()
        this.listDept()
        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.xwComment)) {
                return 3
            } else if (!isEmpty(data.fdyComment)) {
                return 2
            } else {
                return 1
            }
        },
        changeAuditStatus(v) {
            switch (v) {
                case 6:
                    this.doForm.jwcComment = '经过审核，同意该生申请。'
                    break
                case 10:
                    this.doForm.jwcComment = '填写有误，请重新提交。'
                    break
                case 11:
                    this.doForm.jwcComment = '拒绝申请。'
                    break
                default:
                    this.doForm.jwcComment = '同意申请。'
                    break
            }
        },
        async doMany() {
            let sdata = lodash.cloneDeep(this.manyForm)
            if (isEmpty(sdata.common)) {
                this.$message.info('请输入审核意见')
                return
            }
            this.fullLoading = true
            let res = await manyAudit(sdata)
            this.fullLoading = false
            if (res.code == 200) {
                this.$message.success(res.msg)
                this.getList()
                this.manyV = false
            }
        },
        handleSelectionChange(selection) {
            console.log(selection)
            this.applyIds = selection.map(item => item.id)
        },
        manyVClick() {
            if (this.applyIds.length == 0) {
                this.$message.info('请选择学生')
                return
            }
            this.manyForm.applyIds = this.applyIds
            this.manyV = true
        },
        async listDept() {
            let res = await getDeptName()
            this.dept_list = [...res.data]
        },
        async changeSearchDept() {
            let sdata = {
                collegeId: this.queryParams.deptId,
                pageNum: 1,
                pageSize: 100
            }
            let res = await listMajors(sdata)
            this.search_major_list = [...res.rows]
        },
        async changeSearchMajor() {
            let sdata = {
                pageNum: 1,
                pageSize: 100,
                majorId: this.queryParams.majorId
            }
            let res = await listClass(sdata)
            if (res.code == 200) {
                this.search_class_list = [...res.rows]
            }
        },
        async listSelect() {
            let res = await listSelect()
            if (res.code == 200) {
                this.search_type_list = [...res.data]
            }
        },
        lookMore() {
            this.lookMoreV = true
            window.open('http://zhxg.gxsdxy.cn/file/jhzx2023.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 getList() {
            this.loading = true
            let res = await listJwc(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) {
            this.auditForm = { ...v }
            this.auditV = true
            this.myScoreVClick()
        },
        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) */
            /* 此处使用硬编码，请根据实际情况修改。
        当前功能是获取'静湖之星---JHZX'标签对应学年的成绩排名信息。
        涉及到学年标签字典module_tag，需要在字典管理中查询对应的信息。
        如果在字典管理更改了 学年标签 的信息，此处也要进行相应修改。
        ---尽量避免学年标签字典的更改!!!! */
        const yearTag = 'JHZX'
            let res = await listStuScoreClassRankByTag(stuNo,yearTag)
            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.applyStatus)) {
                this.$message.info('请选择是否通过')
                return
            }
            if (isEmpty(sdata.jwcComment)) {
                this.$message.info('请填写审核意见')
                return
            }
            sdata.id = parseInt(this.auditForm.id)
            this.fullLoading = true
            let res = await jwcAudit(sdata)
            this.fullLoading = false
            if (res.code == 200) {
                this.getList()
                this.auditForm = {}
                this.doForm = {
                    id: null,
                    applyStatus: null,
                    jwcComment: '同意申请'
                }
                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>