<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
    <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="batchId">
        <el-select v-model="queryParams.batchId" 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 label="学生" prop="studentId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.studentId"-->
      <!--          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 dict.type.m_apply_special_flag" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="调剂分配" prop="adjustFlag">
        <el-select v-model="queryParams.adjustFlag" placeholder="请选择是否调剂分配" clearable size="small">
          <el-option v-for="dict in dict.type.m_apply_adjust_flag" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="学院" prop="collegeId">
        <el-select v-model="queryParams.collegeId" placeholder="请选择学院id" 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="majorName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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" v-if=showColumn>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['major:adjustResult: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="['major:adjustResult: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="['major:adjustResult: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="['major:adjustResult:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-download" size="mini" @click="handleAdjust"
          v-hasPermi="['major:adjustResult:export']">分流</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-edit" size="mini" @click="handleAudit"
          v-hasPermi="['major:adjustResult:audit']">审批发布</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="adjustResultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分流批次" align="center" prop="batchId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.m_year" :value="scope.row.batchId" />
        </template>
      </el-table-column>
      <el-table-column label="学生" align="center" prop="studentName" />
      <el-table-column label="是否特殊审批" align="center" prop="specialFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.m_apply_special_flag" :value="scope.row.specialFlag" />
        </template>
      </el-table-column>
      <el-table-column label="是否调剂分配" align="center" prop="adjustFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.m_apply_adjust_flag" :value="scope.row.adjustFlag" />
        </template>
      </el-table-column>
      <el-table-column label="专业" align="center" prop="admitMajorName" />
      <el-table-column label="学院" align="center" prop="collegeId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.m_college" :value="scope.row.collegeId" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if=showColumn>
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['major:adjustResult:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['major:adjustResult: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="100px">
        <el-form-item label="分流批次" prop="batchId">
          <el-select v-model="form.batchId" placeholder="请选择分流批次" disabled>
            <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-item label="学生" prop="studentId" hidden>
          <el-input v-model="form.studentId" placeholder="请输入学生" />
        </el-form-item>
        <!-- <el-form-item label="是否特殊审批" prop="specialFlag">
          <el-select v-model="form.specialFlag" placeholder="请选择是否特殊审批">
            <el-option
              v-for="dict in dict.type.m_apply_special_flag"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否调剂分配" prop="adjustFlag">
          <el-select v-model="form.adjustFlag" placeholder="请选择是否调剂分配">
            <el-option
              v-for="dict in dict.type.m_apply_adjust_flag"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="分配专业" prop="adjustFlag">
          <el-select v-model="form.admitMajorId" 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="collegeId">
          <el-select v-model="form.collegeId" placeholder="请选择学院" disabled>
            <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>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改分流结果对话框 -->
    <el-dialog :title="title" :visible.sync="adjustOpen" width="500px" append-to-body>
      <el-form ref="formAdjust" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分流批次" prop="batchId">
          <el-select v-model="form.batchId" 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-item label="学院" prop="collegeId">
          <el-select v-model="form.collegeId" placeholder="请选择学院">
            <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>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="clickAdjust">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改审批发布对话框 -->
    <el-dialog :title="title" :visible.sync="auditOpen" width="500px" append-to-body>
      <el-form ref="formAudit" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="批次" prop="batchId">
          <el-select v-model="form.batchId" 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-item label="学院" prop="collegeId">
          <el-select v-model="form.collegeId" placeholder="请选择学院">
            <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>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="clickAudit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAdjustResult,
  getAdjustResult,
  delAdjustResult,
  addAdjustResult,
  updateAdjustResult,
  startAdjust,
  audit
} from "@/api/major/adjustResult";

import { listMajorInfo } from "@/api/major/majorInfo";
import { listDataFile } from "@/api/major/dataFile";


export default {
  name: "AdjustResult",
  dicts: ['m_year', 'm_apply_adjust_flag', 'm_apply_special_flag', 'm_college'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 显示操作
      showColumn: true,
      // 分流结果表格数据
      adjustResultList: [],
      // 归档数据
      dataFileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      adjustOpen: false,
      auditOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        batchId: null,
        studentId: null,
        specialFlag: null,
        adjustFlag: null,
        collegeId: null,
        studentName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        collegeId: [
          { required: true, message: '请选择学院', trigger: 'change' }
        ],
        batchId: [
          { required: true, message: '请选择批次', trigger: 'change' }
        ]
      },
      majors: [
      ]
    };
  },
  created() {
    this.queryParams.batchId = new Date().getFullYear();
    this.getList();
  },
  methods: {
    /** 查询分流结果列表 */
    getList() {
      const form = {};
      this.loading = true;
      listAdjustResult(this.queryParams).then(response => {
        this.adjustResultList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(this.adjustResultList)
        // 归档校验
        listDataFile(form).then(respone => {
          this.dataFileList = respone.rows;
          for (let i = 0; i < this.adjustResultList.length; i++) {
            for (let j = 0; j < this.dataFileList.length; j++) {
              if (this.adjustResultList[i].collegeId === this.dataFileList[j].collegeId && this.adjustResultList[i].batchId === this.dataFileList[j].year) {
                this.showColumn = false
                this.$modal.msgError("包含不可修改的数据，操作栏无法正常显示")
                return
              }else{
                this.showColumn = true
              }
            }
          }
        });
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.adjustOpen = false;
      this.auditOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        batchId: null,
        studentId: null,
        admitMajorId: null,
        specialFlag: null,
        adjustFlag: null,
        collegeId: null,
        studentName: 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 = "添加分流结果";
    },
    handleAdjust() {
      // this.reset();
      this.adjustOpen = true;
      this.title = "分流";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const studentId = row.studentId || this.ids


      getAdjustResult(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) {
            updateAdjustResult(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAdjustResult(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const studentIds = row.studentId || this.ids;
      this.$modal.confirm('是否确认删除？').then(function () {
        return delAdjustResult(studentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    handleAudit() {
      this.auditOpen = true;
      this.title = "审批发布";
    },
    /**审批发布按钮操作 */
    clickAudit() {
      const _self = this
      this.$modal.confirm('是否确认审批并发布？发布后学生可查看结果').then(function () {
        _self.$refs["formAudit"].validate(valid => {
          console.log("valid", valid)
          if (valid) {
            console.log(_self.form)
            return audit(_self.form)
          } else {
            throw '校验不通过'
          }
        });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("发布成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('major/adjustResult/export', {
        ...this.queryParams
      }, `adjustResult_${new Date().getTime()}.xlsx`)
    },
    clickAdjust() {
      const _self = this
      this.$modal.confirm('是否确认进行分流').then(function () {
        _self.$refs["formAdjust"].validate(valid => {
          console.log("valid", valid, _self)
          console.log(_self)
          if (valid) {
            _self.loading = true;
            _self.adjustOpen = false;
            _self.$modal.msgSuccess("正在分流中");
            startAdjust(_self.form)
            .then(res=>{
              _self.getList();
              _self.$modal.msgSuccess("分流成功");
            }).finally(()=>{
              _self.loading = false;
            })
          } else {
            throw '校验不通过'
          }
        });
      })
    }
  }
};
</script>
