<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="102px"
    >
      <el-form-item label="学号" prop="studentNo">
        <el-input
          v-model="queryParams.studentNo"
          placeholder="请输入学号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生名称" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否特殊审批" prop="specialFlag">
        <el-select
          v-model="queryParams.specialFlag"
          placeholder="请选择是否特殊审批"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in searchDict.specialFlag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否填报志愿" prop="applyFlag">
        <el-select
          v-model="queryParams.applyFlag"
          placeholder="请选择是否填报志愿"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in searchDict.applyFlag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="第一志愿" prop="majorId1st">
        <el-select
          v-model="queryParams.majorId1st"
          placeholder="请选择第一志愿"
          clearable
          size="small"
        >
          <el-option
            v-for="major in selects"
            :key="major.majorId"
            :label="major.majorName"
            :value="major.majorId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="第二志愿" prop="majorId2st">
        <el-select
          v-model="queryParams.majorId2st"
          placeholder="请选择第二志愿"
          clearable
          size="small"
        >
          <el-option
            v-for="major in selects"
            :key="major.majorId"
            :label="major.majorName"
            :value="major.majorId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="第三志愿" prop="majorId3st">
        <el-select
          v-model="queryParams.majorId3st"
          placeholder="请选择第三志愿"
          clearable
          size="small"
        >
          <el-option
            v-for="major in selects"
            :key="major.majorId"
            :label="major.majorName"
            :value="major.majorId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="第四志愿" prop="majorId4st">
        <el-select
          v-model="queryParams.majorId4st"
          placeholder="请选择第四志愿"
          clearable
          size="small"
        >
          <el-option
            v-for="major in selects"
            :key="major.majorId"
            :label="major.majorName"
            :value="major.majorId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="第五志愿" prop="majorId5st">
        <el-select
          v-model="queryParams.majorId5st"
          placeholder="请选择第五志愿"
          clearable
          size="small"
        >
          <el-option
            v-for="major in selects"
            :key="major.majorId"
            :label="major.majorName"
            :value="major.majorId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学院" prop="collegeId">
        <el-select
          v-model="queryParams.collegeId"
          placeholder="请选择学院"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.m_college"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="年份" prop="year">
        <el-select
          v-model="queryParams.year"
          placeholder="请选择年份"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.m_year"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </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="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['summaryApply:summaryApply:edit']"
          >调剂分配</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['major:specialApply:export']"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="summaryApplyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学号" align="center" prop="studentNo" />
      <el-table-column label="学生名称" align="center" prop="studentName" />
      <el-table-column label="绩点" align="center" prop="score" :formatter="rounding"/>
      <el-table-column label="排名" align="center" prop="rank" />
      <el-table-column label="是否特殊审批" align="center" prop="specialFlag" />
      <el-table-column label="是否填报志愿" align="center" prop="applyFlag" />
      <el-table-column label="第一志愿" align="center" prop="majorName1st" />
      <el-table-column label="第二志愿" align="center" prop="majorName2st" />
      <el-table-column label="第三志愿" align="center" prop="majorName3st" />
      <el-table-column label="第四志愿" align="center" prop="majorName4st" />
      <el-table-column label="第五志愿" align="center" prop="majorName5st" />
      <!--
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['summaryApply:summaryApply:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['summaryApply:summaryApply:remove']"
          >删除</el-button>
        </template>
      </el-table-column>

      -->
    </el-table>

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

    <!-- 添加或修改学生志愿填报情况汇总对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="学生名称" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入学生名称" />
        </el-form-item>
        <el-form-item label="绩点" prop="score">
          <el-input v-model="form.score" placeholder="请输入绩点" />
        </el-form-item>
        <el-form-item label="排名" prop="rank">
          <el-input v-model="form.rank" placeholder="请输入排名" />
        </el-form-item>
        <el-form-item label="是否特殊审批" prop="specialFlag">
          <el-input
            v-model="form.specialFlag"
            placeholder="请输入是否特殊审批"
          />
        </el-form-item>
        <el-form-item label="是否填报志愿" prop="appplyFlag">
          <el-input
            v-model="form.appplyFlag"
            placeholder="请输入是否填报志愿"
          />
        </el-form-item> -->
        <el-form-item label="第一志愿" prop="majorId1st">
          <el-select v-model="form.majorId1st" placeholder="请选择调剂专业">
            <el-option
              v-for="major in majors"
              :key="major.majorId"
              :label="major.majorName"
              :value="major.majorId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="第二志愿" prop="majorId2st">
          <el-select v-model="form.majorId2st" placeholder="请选择调剂专业">
            <el-option
              v-for="major in majors"
              :key="major.majorId"
              :label="major.majorName"
              :value="major.majorId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="第三志愿" prop="majorId3st">
          <el-select v-model="form.majorId3st" placeholder="请选择调剂专业">
            <el-option
              v-for="major in majors"
              :key="major.majorId"
              :label="major.majorName"
              :value="major.majorId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="第四志愿" prop="majorId4st">
          <el-select v-model="form.majorId4st" placeholder="请选择调剂专业">
            <el-option
              v-for="major in majors"
              :key="major.majorId"
              :label="major.majorName"
              :value="major.majorId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="第五志愿" prop="majorId5st">
          <el-select v-model="form.majorId5st" placeholder="请选择调剂专业">
            <el-option
              v-for="major in majors"
              :key="major.majorId"
              :label="major.majorName"
              :value="major.majorId"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="学院id" prop="collegeId">
          <el-select v-model="form.collegeId" placeholder="请选择学院id">
            <el-option
              v-for="dict in dict.type.m_college"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年份" prop="year">
          <el-select v-model="form.year" placeholder="请选择年份">
            <el-option
              v-for="dict in dict.type.m_year"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSummaryApply,
  getSummaryApply,
  delSummaryApply,
  addSummaryApply,
  updateSummaryApply,
} from "@/api/summaryApply/summaryApply";
import { listMajorInfo } from "@/api/major/majorInfo";

export default {
  name: "SummaryApply",
  dicts: ["m_year", "m_college"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生志愿填报情况汇总表格数据
      summaryApplyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentNo: null,
        studentName: null,
        specialFlag: null,
        appplyFlag: null,
        collegeId: null,
        year: null,
        majorId1st: null,
        majorId2st: null,
        majorId3st: null,
        majorId4st: null,
        majorId5st: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      searchDict: {
        specialFlag: [
          { value: 1, label: "是" },
          { value: 0, label: "否" },
        ],
        applyFlag: [
          { value: 1, label: "是" },
          { value: 0, label: "否" },
        ],
      },
      majors: [],
      selects: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学生志愿填报情况汇总列表 */
    getList() {
      this.loading = true;
      listSummaryApply(this.queryParams).then((response) => {
        this.summaryApplyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listMajorInfo().then((res) => {
        this.selects = res && res.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        studentNo: null,
        studentName: null,
        score: null,
        rank: null,
        specialFlag: null,
        appplyFlag: null,
        majorId1st: null,
        majorname1st: null,
        majorId2st: null,
        majorname2st: null,
        majorId3st: null,
        majorname3st: null,
        majorId4st: null,
        majorname4st: null,
        majorId5st: null,
        majorname5st: null,
        collegeId: null,
        year: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.studentId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生志愿填报情况汇总";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const studentId = row.studentId || this.ids;

      getSummaryApply(studentId).then((response) => {
        this.form = response.data;
        const { year, collegeId } = response.data;
        listMajorInfo({ year, collegeId }).then((res) => {
          this.majors = res && res.rows;
        });
        console.log(this.majors);
        this.open = true;
        this.title = "调剂分配";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.studentId != null) {
            updateSummaryApply(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } /* else {
            addSummaryApply(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          } */
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const studentNos = row.studentNo || this.ids;
      this.$modal
        .confirm(
          '是否确认删除？'
        )
        .then(function () {
          return delSummaryApply(studentNos);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "summaryApply/summaryApply/export",
        {
          ...this.queryParams,
        },
        `summaryApply_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
