<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="学校名称" prop="schoolName">
        <el-input
          v-model="queryParams.schoolName"
          placeholder="请输入学校名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学院名称" prop="collegeName">
        <el-input
          v-model="queryParams.collegeName"
          placeholder="请输入学院名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属赛区" prop="division">
        <el-input
          v-model="queryParams.division"
          placeholder="请输入所属赛区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="团队名称" prop="teamName">
        <el-input
          v-model="queryParams.teamName"
          placeholder="请输入团队名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="大赛年份" prop="competitionYear">-->
<!--        <el-select v-model="queryParams.createTime" placeholder="请选择">-->
<!--          <el-option-->
<!--            v-for="item in yearList"-->
<!--            :key="item.value"-->
<!--            :label="item.label"-->
<!--            :value="item.value">-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <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-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['hit:hitRegistrationStudentInfo: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="['hit:hitRegistrationStudentInfo:export']"
        >导出</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-switch
          v-model="registerStatus"
          @change="updateRegisterStatus"
          active-value="1"
          inactive-value="0"
          active-text="开启报名"
          inactive-text="关闭报名">
        </el-switch>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  style="width: 100%; table-layout: auto" v-loading="loading" :data="HitRegistrationStudentInfoList" @selection-change="handleSelectionChange">

      <el-table-column type="expand">
        <template slot-scope="props">
          <el-descriptions :title="'领队老师'"  style="margin-left: 10%" >
            <el-descriptions-item label="老师姓名">{{props.row.teacherInfoList[0].teacherName}}</el-descriptions-item>
            <el-descriptions-item label="老师职务">{{props.row.teacherInfoList[0].teacherJob}}</el-descriptions-item>
            <el-descriptions-item label="老师手机号">{{props.row.teacherInfoList[0].teacherNumber}}</el-descriptions-item>
            <el-descriptions-item label="老师邮箱">{{props.row.teacherInfoList[0].teacherEmail}}</el-descriptions-item>
            <el-descriptions-item label="老师所在系及专业">{{props.row.teacherInfoList[0].teacherSchool}}</el-descriptions-item>
          </el-descriptions>
          <el-descriptions :title="'指导老师'+(index)" v-if="index>0"  v-for="(item, index) in props.row.teacherInfoList" style="margin-left: 10%" >
            <el-descriptions-item label="老师姓名">{{item.teacherName}}</el-descriptions-item>
            <el-descriptions-item label="老师职务">{{item.teacherJob}}</el-descriptions-item>
            <el-descriptions-item label="老师手机号">{{item.teacherNumber}}</el-descriptions-item>
            <el-descriptions-item label="老师邮箱">{{item.teacherEmail}}</el-descriptions-item>
            <el-descriptions-item label="老师所在系及专业">{{item.teacherSchool}}</el-descriptions-item>
          </el-descriptions>
          <el-descriptions :title="'学生信息'+(index+1)" v-for="(item, index) in props.row.studentInfoList"  style="margin-left: 10%">
            <!--            <el-descriptions-item label="比赛名称">{{props.row.competitionName}}</el-descriptions-item>-->
            <el-descriptions-item label="学生姓名">{{item.stuName}}</el-descriptions-item>
            <el-descriptions-item label="学生性别">
              <dict-tag :options="dict.type.sys_user_sex" :value="item.sex"/>
            </el-descriptions-item>
            <el-descriptions-item label="学生专业">{{item.major}}</el-descriptions-item>
            <el-descriptions-item label="学生手机号">{{item.phoneNumber}}</el-descriptions-item>
          </el-descriptions>

        </template>
      </el-table-column>
<!--      <el-table-column label="比赛名称" align="center" prop="competitionName" />-->
      <el-table-column label="所属赛区" align="center" prop="division"  />
      <el-table-column label="学校名称" align="center" prop="schoolName" />
      <el-table-column label="院系名称" align="center" prop="collegeName" />
      <el-table-column label="团队名称" align="center"  prop="teamName" />
      <el-table-column label="盲样邮寄地址" align="center"  prop="sampleAddress" />
      <el-table-column label="收件人" align="center" prop="sampleConcat"  />
      <el-table-column label="联系电话" align="center" prop="sampleAddress"  />
      <el-table-column label="附件" align="center" prop="uploadFile"  >
        <template slot-scope="props">
          <el-link :href="`${baseUrl}${props.row.uploadFile}`" type="primary" :underline="false" target="_blank">
            <span class="el-icon-document"> 下载 </span>
          </el-link>

        </template>
      </el-table-column>
      <el-table-column label="报名时间" align="center" prop="createTime"  >
        <template slot-scope="props">
          {{props.row.createTime.substring(0,11)}}
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus" >
        <template slot-scope="props">
          <div v-if="props.row.auditStatus=='0'" style="color:#007bff;"> 待审核</div>
          <div v-if="props.row.auditStatus=='1'" style="color:green;"> 通过</div>
          <div v-if="props.row.auditStatus=='2'" style="color:red;"> 拒绝</div>

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

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-popconfirm
            title="确定当前操作？"
            v-if="scope.row.auditStatus=='0'||scope.row.auditStatus=='2'"
            @confirm="auditData(scope.row,'1')"
          >
            <el-button
              size="mini"
              type="text"
              slot="reference"


            >通过</el-button>

          </el-popconfirm>
          <el-popconfirm
            title="确定当前操作？"
            style="margin-left: 10px"
            v-if="scope.row.auditStatus=='0'||scope.row.auditStatus=='1'"
            @confirm="auditData(scope.row,'2')"
          >
            <el-button
              size="mini"
              type="text"
              slot="reference"
            >拒绝</el-button>

          </el-popconfirm>

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

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

  </div>
</template>

<script>
import {
  listHitRegistrationStudentInfo,
  getHitRegistrationStudentInfo,
  delHitRegistrationStudentInfo,
  addHitRegistrationStudentInfo,
  updateHitRegistrationStudentInfo,
  getTeachInfoByIds
} from "@/api/hit/registrationStudentInfo";
import {selectBaseInfo, editStatus} from '@/api/cms/baseInfo'

export default {
  name: "HitRegistrationStudentInfo",
  dicts: ['sys_user_sex', 'competition_type'],
  data() {
    return {
      yearList: [],
      baseUrl: process.env.VUE_APP_BASE_API,
      registerStatus:"",
      leaderTeachList:[],
      guideTeachList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报名信息表格数据
      HitRegistrationStudentInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        competitionName: null,
        stuName: null,
        stuGender: null,
        stuMajor: null,
        stuNumber: null,
        schoolName: null,
        division: null,
        teamName: null,
        leaderIds: null,
        guideIds: null,
        teachName:null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        competitionId: [
          { required: true, message: "大赛ID不能为空", trigger: "blur" }
        ],
        stuName: [
          { required: true, message: "学生姓名不能为空", trigger: "blur" }
        ],
        stuGender: [
          { required: true, message: "学生性别不能为空", trigger: "blur" }
        ],
        stuMajor: [
          { required: true, message: "学生专业不能为空", trigger: "blur" }
        ],
        stuNumber: [
          { required: true, message: "学生手机号不能为空", trigger: "blur" }
        ],
        schoolName: [
          { required: true, message: "学校及院系名称不能为空", trigger: "blur" }
        ],
        division: [
          { required: true, message: "所属赛区不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "逻辑删除0未删除1真删除不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    selectBaseInfo().then(res => {
      this.registerStatus = res.data.registerStatus
    })
    this.yearList = this.years();
  },
  methods: {
    /** 查询报名信息列表 */
    getList() {
      this.loading = true;
      listHitRegistrationStudentInfo(this.queryParams).then(response => {
        this.HitRegistrationStudentInfoList = response.rows;

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        competitionId: null,
        stuName: null,
        stuGender: null,
        stuMajor: null,
        stuNumber: null,
        schoolName: null,
        division: null,
        teamName: null,
        leaderIds: null,
        guideIds: null,
        remark: null,
        delFlag: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.createTime = null
      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.title = "添加报名信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHitRegistrationStudentInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报名信息";
      });
    },
    auditData(data,status){
      data.auditStatus = status
      updateHitRegistrationStudentInfo(data).then(response => {
        this.$modal.msgSuccess("成功");

        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHitRegistrationStudentInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHitRegistrationStudentInfo(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 delHitRegistrationStudentInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hit/hitRegistrationStudentInfo/export', {
        ...this.queryParams
      }, `HitRegistrationStudentInfo_${new Date().getTime()}.xlsx`)
    },
    getTeachInfo(row){
      if (row.leaderIds !== null && row.leaderIds.length !== 0){
        getTeachInfoByIds(row.leaderIds).then(res => {
          this.leaderTeachList = res.data
        })
      }
      if (row.guideIds !== null && row.guideIds.length !== 0){
        getTeachInfoByIds(row.guideIds).then(res => {
          this.guideTeachList = res.data
        })
      }
    },
    updateRegisterStatus(){
      editStatus(this.registerStatus).then(res => {
        if (res.code === 200 ){
          this.$modal.msgSuccess(this.registerStatus === "1" ? "报名已开启" : "报名已关闭")
        }
      })
    },
    years() {
      const currentYear = new Date().getFullYear();
      const startYear = currentYear - 40; // 假设从当前年份往前 20 年
      const endYear = currentYear;
      const years = [];
      for (let year = startYear; year <= endYear; year++) {
        years.push({
          label: year,
          value: year + "-01-01",
        });
      }
      return years.sort((a, b) => b.label - a.label);
    },
  }
};
</script>

<style scoped lang="scss">
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
