<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="学生新姓名" prop="studentName">
        <el-input v-model="queryParams.studentName" placeholder="请输入学生新姓名" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <!--      <el-form-item label="民族" prop="nation">-->
      <!--        <el-input v-model="queryParams.nation" placeholder="请输入民族" clearable @keyup.enter.native="handleQuery" />-->
      <!--      </el-form-item>-->
      <el-form-item label="学生新民族" prop="newNation">
        <el-select v-model="queryParams.newNation" placeholder="请选择学生新民族" style="width: 100%">
          <el-option v-for="item in Nations" :key="item.label" :label="item.label" :value="item.label"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学生新身份证号" prop="newIdCard">
        <el-input v-model="queryParams.newIdCard" 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">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['studentChange:change: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="['studentChange:change: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="['studentChange:change: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="['studentChange:change:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="changeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="姓名" align="center" minWidth="100" prop="studentName"/>
      <el-table-column label="学生身份证" minWidth="180" align="center" prop="idCard"/>
      <el-table-column label="民族" align="center" prop="nation"/>
      <el-table-column label="变更后学生姓名" minWidth="150" align="center" prop="newStudentName"/>
      <el-table-column label="变更后学生身份证" minWidth="180" align="center" prop="newIdCard"/>
      <el-table-column label="变更后民族" minWidth="100" align="center" prop="newNation"/>
      <!--      <el-table-column label="附件地址" align="center" prop="dataUrl"/>-->
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div v-show="scope.row.state == 0">
            <el-button size="mini" type="text" icon="el-icon-edit"
                       @click="handleUpdate(scope.row)" v-hasPermi="['studentChange:change:edit']">编辑
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-finished"
                       @click="handleSubmit(scope.row)" v-hasPermi="['studentChange:change:edit']">提交
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
                       v-hasPermi="['studentChange:change:edit']">查看
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-delete"
                       @click="handleDelete(scope.row)"
                       v-hasPermi="['studentChange:change:remove']">删除
            </el-button>
          </div>
          <div v-show="scope.row.state == 1">
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
                       v-hasPermi="['studentChange:change:edit']">查看
            </el-button>
          </div>
        </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="800px" append-to-body @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <b>变更前</b>
        <el-row :gutter="20">
          <br/>
          <el-col :span="12">
            <el-form-item label="学生姓名" prop="studentName">
              <el-input v-model="form.studentName" placeholder="请选择学生" readonly @focus="selectStudent"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="民族" prop="nation">
              <el-input v-model="form.nation" readonly placeholder="请输入民族"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="学生身份证" prop="idCard">
          <el-input v-model="form.idCard" readonly placeholder="请输入学生身份证"/>
        </el-form-item>
        <el-divider/>
        <b>变更后</b>
        <el-row>
          <br/>
          <el-col :span="12">
            <el-form-item label="学生姓名" prop="newStudentName">
              <el-input v-model.trim="form.newStudentName" placeholder="请输入变更后学生姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="民族" prop="newNation">
              <el-select v-model.trim="form.newNation" placeholder="请选择民族" style="width: 100%">
                <el-option v-for="item in Nations" :key="item.label" :label="item.label"
                           :value="item.label"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="学生身份证" prop="newIdCard">
          <el-input v-model.trim="form.newIdCard" placeholder="请输入变更后学生身份证"/>
        </el-form-item>
        <el-form-item v-if="this.title == '添加学生关键信息变更'" label="附件" prop="dataUrl">
          <!--          <el-input v-model="form.dataUrl" placeholder="请输入附件地址"/>-->
          <el-upload ref="upload" class="upload-demo" action="#" :before-remove="beforeRemove"
                     :before-upload="beforeUpload" multiple :limit="1" :on-exceed="handleExceed"
                     :http-request="uploadFile">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item v-if="this.title == '修改学生关键信息变更'" label="附件" prop="dataUrl">
          <div style="display:flex ;">
            <div v-if="isShow"
                 style="max-width: 200px; overflow: hidden;  white-space: nowrap; text-overflow: ellipsis; ">
              {{ form.dataName }}
            </div>
            <div>
              <el-upload ref="upload" class="upload-demo" action="#" :before-remove="beforeRemove" multiple :limit="1"
                         :on-exceed="handleExceed" :before-upload="beforeUpload" :http-request="uploadFile">
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>
            </div>
          </div>

        </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="openView" width="800px" append-to-body>
      <el-form :model="form" label-width="100px">
        <b>变更前</b>
        <el-row :gutter="20">
          <br/>
          <el-col :span="12">
            <el-form-item label="学生姓名" prop="studentName">
              <el-input v-model="form.studentName" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="民族" prop="nation">
              <el-input v-model="form.nation" readonly placeholder="请输入民族"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="学生身份证" prop="idCard">
          <el-input v-model="form.idCard" readonly placeholder="请输入学生身份证"/>
        </el-form-item>
        <el-divider/>
        <b>变更后</b>
        <el-row>
          <br/>
          <el-col :span="12">
            <el-form-item label="学生姓名" prop="newStudentName">
              <el-input v-model="form.newStudentName" readonly placeholder="请输入变更后学生姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="民族" prop="newNation">
              <el-input v-model="form.newNation" readonly placeholder="请输入变更后民族"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="学生身份证" prop="newIdCard">
          <el-input v-model="form.newIdCard" readonly placeholder="请输入变更后学生身份证"/>
        </el-form-item>
        <el-form-item label="附件" prop="dataUrl">
          <div style="display:flex ;">
            <div style="max-width: 200px; overflow: hidden;  white-space: nowrap; text-overflow: ellipsis; ">{{
                form.dataName
              }}
            </div>
            <div>
              <el-button size="small" type="primary" @click="imageLoad()">下载附件</el-button>
            </div>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 选择学生 -->
    <StudentInfoSelect v-if="studentOptions.open" :openSelect.sync="studentOptions.open" :title="studentOptions.title"
                       @getStudentList="getStudentList" @setStudentInfo="setStudentInfo"></StudentInfoSelect>
  </div>
</template>

<script>
import {
  listChange,
  getChange,
  delChange,
  addChange,
  updateChange,
  listStudent,
  commonUpload,
  submit,
} from "@/api/smartSchool/teachAffairAdministration/stuEssentialInfAlert/index.js";
import StudentInfoSelect from '@/views/components/moduleComponets/studentInfoSelect/index';
import {Nations} from "@/enums/common";

export default {
  name: "stuEssentialInfAlert",
  components: {StudentInfoSelect},
  data() {
    return {
      /* 民族 */
      Nations,
      fileList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生关键信息变更表格数据
      changeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      openView: false,
      // 是否显示附件名称
      isShow: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // studentName: null,
        // nation: null,
        // idCard: null,
        newStudentName: null,
        newNation: null,
        newIdCard: null,
        // dataUrl: null,
      },
      // 表单参数
      form: {},
      // 选择学生对话框
      studentOptions: {
        open: false,
        title: '选择学生'
      },
      //
      isUpload: false,
      // 表单校验
      rules: {
        newStudentName: [
          {required: true, message: "学生姓名不能为空", trigger: "blur"}
        ],
        newNation: [
          {required: true, message: "民族不能为空", trigger: "blur"}
        ],
        newIdCard: [
          {required: true, message: "学生身份证不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学生关键信息变更列表 */
    getList() {
      this.loading = true;
      listChange(this.queryParams).then(response => {
        this.changeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset()
      this.$refs['upload'].clearFiles()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentName: null,
        nation: null,
        idCard: null,
        newStudentName: null,
        newNation: null,
        newIdCard: null,
        dataUrl: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.isShow = true
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    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
      getChange(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) {
            updateChange(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.$refs['upload'].clearFiles()
            });
          } else {
            console.log(this.form);
            addChange(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.$refs['upload'].clearFiles()
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学生关键信息变更编号为"' + ids + '"的数据项？').then(function () {
        return delChange(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('studentChange/change/export', {
        ...this.queryParams
      }, `change_${new Date().getTime()}.xlsx`)
    },
    handleSubmit(row) {
      const id = row.id
      this.$modal.confirm('确认提交学生关键信息变更？').then(function () {

        // return submit(id);
      }).then(() => {
        this.$modal.confirm('再次确认提交学生关键信息变更？').then(function () {
          return submit(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("提交成功");
        }).catch(() => {
        });
        // this.getList();
        // this.$modal.msgSuccess("提交成功");
      }).catch(() => {
      });
    },
    // 查看
    handleView(row) {
      getChange(row.id).then(response => {
        this.form = response.data;
        this.openView = true;
        this.title = "查看学生关键信息";
      });
    },
    // 图片上传
    uploadFile(file) {
      const fileData = file.file
      const formData = new FormData()
      formData.append('file', fileData)
      this.uploadLoading = true
      commonUpload(formData)
        .then(response => {
          console.log(response)
          this.uploadLoading = false
          this.$modal.msgSuccess('上传成功')
          this.form.dataUrl = response.fileName
          this.form.dataName = response.newFileName
          this.isShow = false
        })
        .catch(error => {
          this.uploadLoading = false
        })
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件`);
    },
    /** 上传前 */
    beforeUpload(file) {
      // //1MB=1024*1024(1MB=1024KB 1KB=1024MB)
      // const is1M = file.size / 1024 / 1024 < 1;
      // //限制文件上传大小
      // if (!is1M) {
      //   this.$message.error("上传文件大小不能超过 1MB!");
      //   return false
      // }
      // this.isUpload = true
      // return is1M
    },
    beforeRemove(file, fileList) {
      if (this.isUpload) {
        this.isUpload = false
        return this.$confirm(`确定移除 ${file.name}？`);
      }

    },
    // 选择学生
    selectStudent() {
      this.studentOptions.open = true;
    },
    /** 获取学生信息列表 */
    getStudentList(queryForm, callback) {
      listStudent({...queryForm, gradeId: this.gradeId}).then(response => {
        callback(response.rows, response.total);
      }).catch(error => {
        callback([], 0);
      })
    },
    /** 设置学生信息 */
    setStudentInfo(item) {
      this.$set(this.form, 'studentName', item.studentName);
      this.$set(this.form, 'idCard', item.idCard);
      this.$set(this.form, 'studentId', item.id);
      this.$set(this.form, 'nation', item.nation);
      this.$set(this.form, 'newIdCard', item.idCard);
      this.$set(this.form, 'newNation', item.nation);
      this.$set(this.form, 'newStudentName', item.studentName);
    },
    // 附件下载
    imageLoad() {
      if (this.form.dataName) {
        this.download(`studentChange/downloadFile/${this.form.id}`, {
          // id: this.form.id
        }, this.form.dataName)
      } else {
        this.$message('没有附件可下载');
      }

    },
  }
};
</script>
