<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <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="reason">
        <el-input v-model="queryParams.reason" placeholder="请输入补办原因" clearable @keyup.enter.native="handleQuery" />
      </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="inspectionProgress">
        <el-select v-model="queryParams.inspectionProgress" placeholder="请选择审核状态" @keyup.enter.native="handleQuery">
          <el-option label="学生提交申请" value="0"></el-option>
          <el-option label="辅导员通过" value="1"></el-option>
          <el-option label="学工通过" value="2"></el-option>
          <el-option label="完成制作" value="3"></el-option>
        </el-select>
        <!-- <el-input v-model="queryParams.inspectionProgress" placeholder="请输入审核状态" clearable
          @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="showRole !== 4">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['routine:stuIdReissue: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="['routine:stuIdReissue: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="['routine:stuIdReissue: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="['routine:stuIdReissue:export']">导出</el-button>
      </el-col>
    </el-row>
    <el-row class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" v-if="showRole <= 2"></right-toolbar>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getListByCounsellorName" v-if="showRole === 3"></right-toolbar>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getListName" v-if="showRole === 4"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="stuIdReissueList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学号" align="center" prop="stuNo" />
      <el-table-column label="学生名称" align="center" prop="stuName" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="学院" align="center" prop="departmentName" />
      <el-table-column label="年级" align="center" prop="gradeName" />
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="民族" align="center" prop="mz">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rt_nation" :value="scope.row.mz" />
        </template>
      </el-table-column>
      <el-table-column label="照片" align="center" prop="photo" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.photo" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="补办原因" align="center" prop="reason" />
      <el-table-column label="审核状态" align="center" prop="inspectionProgress">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.inspectionProgress === 0">未审核</el-tag>
          <el-tag type="success" v-else-if="scope.row.inspectionProgress === 1">辅导员通过</el-tag>
          <el-tag type="success" v-else-if="scope.row.inspectionProgress === 2">学工通过</el-tag>
          <el-tag type="success" v-else-if="scope.row.inspectionProgress === 3">完成制作</el-tag>
          <el-tag type="danger" v-else>辅导员驳回</el-tag>
        </template>
      </el-table-column>
      <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="['routine:stuIdReissue:edit']" v-if="showRole !== 4">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['routine:stuIdReissue:remove']">取消申请</el-button>
          <el-button size="mini" type="text" icon="el-icon-info" @click="detail(scope.row)"
            v-if="scope.row.inspectionProgress >= -1">详情</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" v-if="showRole <= 2"/>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getListByCounsellorName" v-if="showRole === 3"/>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getListName" v-if="showRole === 4"/>

    <!-- 添加或修改学生证补办对话框 -->
    <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="stuName">
          <el-input v-model="form.stuName" placeholder="请输入学生名称" />
        </el-form-item>
        <el-form-item label="照片" prop="photo">
          <image-upload v-model="form.photo" />
        </el-form-item>
        <el-form-item label="补办原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入补办原因" />
        </el-form-item>
        <el-form-item label="学号" prop="stuNo">
          <el-input v-model="form.stuNo" placeholder="请输入学号" />
        </el-form-item>
        <!-- <el-form-item label="审核状态" prop="inspectionProgress">
          <el-select v-model.number="form.inspectionProgress" placeholder="请选择审核状态" style="width: 100%;">
            <el-option  label="学生提交申请" value="0" key="学生提交申请"></el-option>
            <el-option label="辅导员通过" value="1" key="辅导员通过"></el-option>
            <el-option label="学工通过" value="2" key="学工通过"></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 { listStuIdReissue, getStuIdReissue, delStuIdReissue, addStuIdReissue, updateStuIdReissue, listStuIdReissueName, getListByCounsellorName } from "@/api/routine/stuIdReissue";

export default {
  name: "reissuerLIst",
  dicts: ['rt_nation'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生证补办表格数据
      stuIdReissueList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuName: null,
        photo: null,
        reason: null,
        stuId: null,
        inspectionProgress: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        photo: [
          { required: true, message: "照片不能为空", trigger: "blur" }
        ],
        reason: [
          { required: true, message: "补办原因不能为空", trigger: "blur" }
        ],
      },
      inspectionProgressList: [
        { val: 0, name: '学生提交申请' },
        { val: 1, name: '辅导员通过' },
        { val: 2, name: '学工通过' }
      ],
      showRole: ''
    };
  },
  created() {
    let temp = this.$store.getters.roles;
    temp.map(x => {
      if (x == "admin") {
        this.showRole = 1;
        this.getList();
      }

      if (x == "test") { //学工
        this.showRole = 1;
        this.getList();
      }

      if (x == "stumanger") { //学务
        this.showRole = 2;
        this.getList();
      }

      if (x == "testteacher") { //辅导员
        this.showRole = 3;
        this.getListByCounsellorName();
      }

      if (x == "teststu") { //学生
        this.showRole = 4;
        this.getListName()
      }
    });
  },
  methods: {
    /** 查询学生证补办列表 */
    getList() {
      this.loading = true;
      listStuIdReissue(this.queryParams).then(response => {
        this.stuIdReissueList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 根据学校名字查询学生证补办列表 */
    getListName() {
      this.loading = true;
      listStuIdReissueName(this.queryParams).then(response => {
        this.stuIdReissueList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 根据学校名字查询学生证补办列表 */
    getListByCounsellorName() {
      this.loading = true;
      getListByCounsellorName(this.queryParams).then(response => {
        this.stuIdReissueList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stuName: null,
        photo: null,
        reason: null,
        stuId: null,
        createTime: null,
        inspectionProgress: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.handleRole()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      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
      getStuIdReissue(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生证补办";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStuIdReissue(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.handleRole()
            });
          } else {
            addStuIdReissue(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.handleRole()
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      if (row.inspectionProgress >= 3) {
        this.$message.error('当前审核已完成，不可取消')
        return
      }
      this.$modal.confirm('是否确认取消学生证补办申请编号为"' + ids + '"的数据项？').then(function () {
        return delStuIdReissue(ids);
      }).then(() => {
        this.handleRole()
        this.$modal.msgSuccess("取消成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('routine/stuIdReissue/export', {
        ...this.queryParams
      }, `stuIdReissue_${new Date().getTime()}.xlsx`)
    },
    detail(row) {
      this.$router.push({
        path: '/routine/sicr/stuIdReissue',
        query: {
          id: row.id,
          stuNo: row.stuNo,
          inspectionProgress: row.inspectionProgress,
          fdyCmt: row.fdyCmt,
          jwcCmt: row.jwcCmt
        },
      })
    },
    handleRole() {
      if (this.showRole === 4) {
        this.getListName()
      } else if (this.showRole === 3) {
        this.getListByCounsellorName()
      } else {
        this.getList();
      }
    },


  }
};
</script>
