<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="studentNo">
        <el-input
          v-model="queryParams.studentNo"
          placeholder="请输入学号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级名称" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入班级名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="绩点" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入绩点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排名" prop="rank">
        <el-input
          v-model="queryParams.rank"
          placeholder="请输入排名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入联系号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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 label="专业id" prop="majorId">-->
<!--        <el-input-->
<!--          v-model="queryParams.majorId"-->
<!--          placeholder="请输入专业id"-->
<!--          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:info:add']"-->
<!--        >新增</el-button>-->
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleImport"
          v-hasPermi="['major:info: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:info: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:info: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:info:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="studentName" />
      <!-- <el-table-column label="学号" align="center" prop="studentNo" /> -->
<!--      <el-table-column label="源数据id" align="center" prop="orginId" />-->
<!--      <el-table-column label="微信关联ip" align="center" prop="openId" />-->
<!--      <el-table-column label="头像" align="center" prop="avatar" />-->
      <el-table-column label="年级" align="center" prop="year">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.m_year" :value="scope.row.year"/>
        </template>
      </el-table-column>
      <el-table-column label="班级名称" align="center" prop="className" />
      <el-table-column label="绩点" align="center" prop="score" :formatter="rounding"/>
      <el-table-column label="排名" align="center" prop="rank,totalMan">
        <template slot-scope="scope">
          {{scope.row.rank}}/{{scope.row.totalMan}}
        </template>
      </el-table-column>
      <el-table-column label="高等数学" align="center" prop="mathScore" :formatter="rounding"/>
      <el-table-column label="工程制图与识图" align="center" prop="projectScore" :formatter="rounding"/>
      <el-table-column label="有无不及格课程" align="center" prop="failedFlag">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.m_student_course_flag"
            :value="scope.row.failedFlag"
          />
        </template>
      </el-table-column>
      <el-table-column label="有无处分" align="center" prop="punishFlag">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.m_student_punish_flag"
            :value="scope.row.punishFlag"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="联系号码" align="center" prop="phonenumber" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column> -->
<!--      <el-table-column label="监护人号码" align="center" prop="guardianPhone" />-->
<!--      <el-table-column label="创建用户" align="center" prop="createUserId" />-->
<!--      <el-table-column label="更新用户id" align="center" prop="updateUserId" />-->
      <!-- <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="专业id" align="center" prop="majorId" />-->
<!--      <el-table-column label="头像图片路径" align="center" prop="portraitPath" />-->
      <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:info:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['major:info: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="punishFlag">
          <el-select v-model="form.punishFlag" placeholder="请选择是否处分">
            <el-option
              v-for="dict in dict.type.m_student_punish_flag"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        
        <!-- <el-form-item label="学号" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="学生名称" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入学生名称" />
        </el-form-item>
        <el-form-item label="学号" prop="studentNo">
          <el-input v-model="form.studentNo" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="源数据id" prop="orginId">
          <el-input v-model="form.orginId" placeholder="请输入源数据id" />
        </el-form-item>
        <el-form-item label="微信关联ip" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入微信关联ip" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="班级名称" prop="className">
          <el-input v-model="form.className" 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="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入联系号码" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="监护人号码" prop="guardianPhone">
          <el-input v-model="form.guardianPhone" placeholder="请输入监护人号码" />
        </el-form-item>
        <el-form-item label="创建用户" prop="createUserId">
          <el-input v-model="form.createUserId" placeholder="请输入创建用户" />
        </el-form-item>
        <el-form-item label="更新用户id" prop="updateUserId">
          <el-input v-model="form.updateUserId" placeholder="请输入更新用户id" />
        </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-item label="专业id" prop="majorId">
          <el-input v-model="form.majorId" placeholder="请输入专业id" />
        </el-form-item>
        <el-form-item label="头像图片路径" prop="portraitPath">
          <el-input v-model="form.portraitPath" placeholder="请输入头像图片路径" />
        </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="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/major/info";
import {getToken} from "@/utils/auth";
import { listDataFile } from "@/api/major/dataFile";

export default {
  name: "Info",
  dicts: ['m_year', 'm_student_punish_flag', 'm_student_course_flag', 'm_college'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      showColumn: true,
      // 总条数
      total: 0,
      // 学生信息表格数据
      infoList: [],
      dataFileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
        studentNo: null,
        className: null,
        collegeId: null,
        year: null,
        /* orginId: null,
        openId: null,
        avatar: null,
        score: null,
        rank: null,
        phonenumber: null,
        email: null,
        gender: null,
        guardianPhone: null,
        createUserId: null,
        updateUserId: null,
        majorId: null,
        portraitPath: null */
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/major/info/importData"
      },
    };
  },
  created() {
    this.queryParams.year = new Date().getFullYear();
    this.getList();
  },
  methods: {
    /** 查询学生信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
        // 归档校验
        const form = {};
        listDataFile(form).then(respone => {
          this.dataFileList = respone.rows;
          for (let i = 0; i < this.infoList.length; i++) {
            for (let j = 0; j < this.dataFileList.length; j++) {
              if (this.infoList[i].collegeId === this.dataFileList[j].collegeId && this.infoList[i].year === this.dataFileList[j].year) {
                this.showColumn = false
                this.$modal.msgError("包含不可修改的数据，操作栏无法正常显示")
                return
              }else{
                this.showColumn = true
              }
            }
          }
        });
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        studentId: null,
        studentName: null,
        studentNo: null,
        orginId: null,
        openId: null,
        avatar: null,
        className: null,
        score: null,
        rank: null,
        phonenumber: null,
        email: null,
        gender: null,
        guardianPhone: null,
        createTime: null,
        updateTime: null,
        createUserId: null,
        updateUserId: null,
        collegeId: null,
        year: null,
        majorId: null,
        portraitPath: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('major/info/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 多选框选中数据
    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
      getInfo(studentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.studentId != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(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 delInfo(studentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('major/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
