<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="stuNo">
        <el-input v-model="queryParams.stuNo" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <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="departmentName">
        <el-input v-model="queryParams.departmentName" 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="['routine:rtStuDropOutSchool: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:rtStuDropOutSchool: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:rtStuDropOutSchool: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:rtStuDropOutSchool:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rtStuDropOutSchoolList" @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="departmentName" />
      <el-table-column label="年级" align="center" prop="gradeName" />
      <el-table-column label="状态" align="center" prop="applyStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rt_drop_out_status" :value="scope.row.applyStatus" />
        </template>
      </el-table-column>
      <el-table-column label="提交状态" align="center" prop="submissionStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rt_submission_status" :value="scope.row.submissionStatus" />
        </template>
      </el-table-column>
      <el-table-column label="退学类型" align="center" prop="dropOutType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rt_quit_type" :value="scope.row.dropOutType" />
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="createTime" />
      <el-table-column label="退学年份" align="center" prop="quitYear" />
      <el-table-column label="退学文号" align="center" prop="quitNumber" />
      <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:rtStuDropOutSchool:edit']" v-if="scope.row.submissionStatus == 0">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['routine:rtStuDropOutSchool:remove']" v-if="scope.row.submissionStatus == 0">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-info" @click="detail(scope.row)" v-if="scope.row.submissionStatus == 1">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-info" @click="dropOutProve(scope.row)" v-if="scope.row.applyStatus == 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" />

    <!-- 添加或修改学生退学申请记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="学号" prop="stuNo">
              <el-input v-model="form.stuNo" placeholder="请输入学号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学生姓名" prop="stuName">
              <el-input v-model="form.stuName" placeholder="请输入学生姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性别" prop="gender">
              <el-input v-model="form.gender" placeholder="请输入性别" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="部门" prop="departmentName">
              <el-input v-model="form.departmentName" placeholder="请输入部门" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="年级" prop="gradeName">
              <el-input v-model="form.gradeName" placeholder="请输入年级" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="班级" prop="className">
              <el-input v-model="form.className" placeholder="请输入班级" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="出生日期" prop="birthday">
              <el-input v-model="form.birthday" placeholder="请输入出生日期" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="家长姓名" prop="fatherName">
              <el-input v-model="form.fatherName" placeholder="请输入家长姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="家长电话" prop="fatherRelation">
              <el-input v-model="form.fatherRelation" placeholder="请输入家长电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="民族" prop="mz">
              <el-input v-model="form.mz" placeholder="请输入民族" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="政治面貌" prop="politicalStatus">
              <el-select v-model="form.politicalStatus" placeholder="请选择政治面貌" clearable>
                <el-option label="群众" value="群众"></el-option>
                <el-option label="团员" value="团员"></el-option>
                <el-option label="中共党员" value="中共党员"></el-option>
                <el-option label="其他" value="其他"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="籍贯" prop="jg">
              <el-cascader :options="areaOptions" v-model="form.jg" clearable filterable> </el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="市/县" prop="hksz2">
              <el-input v-model="form.hksz2" placeholder="请输入市/县" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="休学类型" prop="dropOutType">
              <el-radio-group v-model="form.dropOutType">
                <el-radio v-for="dict in dict.type.rt_reentry_type" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="附件上传" prop="attachmentUpload">
              <span style="color: red">备注:需要学生本人手写的书面申请.</span>
              <Affix @input="handleAffix" v-model="form.attachmentUpload"></Affix>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="申请原因" prop="reasonApplying">
              <el-input v-model="form.reasonApplying" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="签名" prop="applySignature">
              <signature :url="form.applySignature" @save-success="saveSuccessHandle"></signature>

              <!-- <el-upload class="avatar-uploader" :action="upload.url" :headers="upload.headers" :show-file-list="false" :on-success="handleAvatarSuccess">
                <img v-if="form.applySignature" :src="baseurl + form.applySignature" width="200px" height="50px" class="avatar" />
                <span v-else> 点击上传</span>
              </el-upload> -->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save">保 存</el-button>
        <el-button type="primary" @click="submitForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addRtStuDropOutSchool, delRtStuDropOutSchool, getRtStuDropOutSchool, listRtStuDropOutSchool, save } from '@/api/routine/rtStuDropOutSchool'
import { getMyStuInfo } from '@/api/routine/rtStuQuitSchool'
import signature from '@/components/signature/index.vue'
import { getTokenKeySessionStorage as getToken } from '@/utils/auth'
import {
  pcaTextArr, // 省市区联动数据,纯汉字
} from 'element-china-area-data'

export default {
  name: 'RtStuDropOutSchool',
  dicts: ['rt_quit_type', 'rt_submission_status', 'rt_quit_apply_status', 'rt_drop_out_status', 'rt_reentry_type'],
  components: {
    signature,
  },
  data() {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'))
      } else {
        const reg = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入正确的手机号'))
        }
      }
    }

    return {
      //签名上传
      upload: {
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/common/upload',
      },
      baseurl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生退学申请记录表格数据
      rtStuDropOutSchoolList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicantId: null,
        applicantName: null,
        stuNo: null,
        stuName: null,
        stuId: null,
        gender: null,
        departmentName: null,
        gradeName: null,
        className: null,
        mz: null,
        politicalStatus: null,
        birthday: null,
        parentPhone: null,
        parentName: null,
        jg: null,
        hksz2: null,
        attachmentUpload: null,
        reasonApplying: null,
        applySignature: null,
        ideologicalEducation: null,
        instructionSchoolHours: null,
        applyStatus: null,
        submissionStatus: null,
        dropOutType: null,
        processInstanceId: null,
        deployId: null,
        quitStarttime: null,
        quitEndtime: null,
        quitYear: null,
        quitNumber: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stuNo: [{ required: true, message: '学号不能为空', trigger: 'blur' }],
        stuName: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        gender: [{ required: true, message: '性別不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'blur' }],
        gradeName: [{ required: true, message: '年级不能为空', trigger: 'blur' }],
        className: [{ required: true, message: '班级不能为空', trigger: 'blur' }],
        birthday: [{ required: true, message: '出生日期不能为空', trigger: 'blur' }],
        parentName: [{ required: true, message: '家长姓名不能为空', trigger: 'blur' }],
        parentPhone: [{ required: true, message: '家长电话不能为空', trigger: 'blur' }],
        mz: [{ required: true, message: '民族不能为空', trigger: 'blur' }],
        jg: [{ required: true, message: '籍贯不能为空', trigger: 'blur' }],
        politicalStatus: [{ required: true, message: '请选择政治面貌', trigger: 'blur' }],
        attachmentUpload: [{ required: true, message: '请上传附件', trigger: 'blur' }],
        reasonApplying: [{ required: true, message: '请输入申请原因', trigger: 'blur' }],
        applySignature: [{ required: true, message: '请上传申请人签名', trigger: 'blur' }],
        dropOutType: [{ required: true, message: '请选择退学类别', trigger: 'blur' }],
        fatherName: [{ required: true, message: '请输入家长姓名', trigger: 'blur' }],
        fatherRelation: [
          { required: true, message: '请输入家长电话', trigger: 'blur' },
          { validator: checkPhone, trigger: 'blur' },
        ],
      },
      // 省市区
      areaOptions: pcaTextArr,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    saveSuccessHandle(url) {
      this.form.applySignature = url
    },
    /** 查询学生退学申请记录列表 */
    getList() {
      this.loading = true
      listRtStuDropOutSchool(this.queryParams).then((response) => {
        this.rtStuDropOutSchoolList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        dropOutSchoolId: null,
        applicantId: null,
        applicantName: null,
        stuNo: null,
        stuName: null,
        stuId: null,
        gender: null,
        departmentName: null,
        gradeName: null,
        className: null,
        mz: null,
        politicalStatus: null,
        birthday: null,
        parentPhone: null,
        parentName: null,
        jg: null,
        hksz2: null,
        attachmentUpload: null,
        reasonApplying: null,
        applySignature: null,
        ideologicalEducation: null,
        instructionSchoolHours: null,
        applyStatus: null,
        submissionStatus: null,
        dropOutType: null,
        processInstanceId: null,
        deployId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        quitStarttime: null,
        quitEndtime: null,
        quitYear: null,
        quitNumber: 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.dropOutSchoolId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.getMyStuInfo()
      this.title = '添加学生退学申请'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const dropOutSchoolId = row.dropOutSchoolId || this.ids
      getRtStuDropOutSchool(dropOutSchoolId).then((response) => {
        this.form = response.data
        this.form.jg = this.form.jg.split(',')
        this.form.fatherName = this.form.parentName
        this.form.fatherRelation = this.form.parentPhone
        this.open = true
        this.title = '修改学生退学申请'
      })
    },
    getMyStuInfo() {
      getMyStuInfo().then((res) => {
        console.log(res.data)
        this.form = res.data
        this.form.stuName = res.data.studentName
        this.form.applySignature = res.data.assigneeSign
      })
    },
    handleAffix(affixId) {
      this.form.attachmentUpload = affixId
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$modal.loading('正在努力加载中,请稍等...')

          this.form.jg = this.form.jg.toString()
          this.form.parentName = this.form.fatherName
          this.form.parentPhone = this.form.fatherRelation
          addRtStuDropOutSchool(this.form).then((response) => {
            this.$modal.closeLoading()

            this.$modal.msgSuccess('提交成功')
            this.open = false
            this.getList()
          })
        }
      })
    },
    /** 提交按钮 */
    save() {
      this.$refs['form'].validate((valid) => {
        this.form.jg = this.form.jg.toString()
        this.form.parentName = this.form.fatherName
        this.form.parentPhone = this.form.fatherRelation
        if (valid) {
          save(this.form).then((response) => {

            this.$modal.msgSuccess('保存成功')
            this.open = false
            this.getList()
          })
        }
      })
    },
    // 签名上传
    handleAvatarSuccess(response, file, fileList) {
      this.form.signature = response.fileName
      this.$forceUpdate()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dropOutSchoolIds = row.dropOutSchoolId || this.ids
      this.$modal
        .confirm('是否确认删除学生退学申请记录编号为"' + dropOutSchoolIds + '"的数据项?')
        .then(function () {
          return delRtStuDropOutSchool(dropOutSchoolIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'routine/rtStuDropOutSchool/export',
        {
          ...this.queryParams,
        },
        `rtStuDropOutSchool_${new Date().getTime()}.xlsx`
      )
    },
    detail(row) {
      console.log(row)
      this.$router.push({
        path: '/routine/rtStuDropOutSchool/detail',
        query: {
          procInsId: row.processInstanceId,
          deployId: row.deployId,
          dropOutSchoolId:row.dropOutSchoolId,
          category: 'dropOut',
        },
      })
    },
    dropOutProve(row) {
      this.$router.push({
        path: '/routine/rtStuDropOutSchool/dropOutProve',
        query: {
          dropOutSchoolId: row.dropOutSchoolId,
        },
      })
    },
  },
}
</script>
