<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="辅导员姓名" prop="applicantId">
        <el-input v-model="queryParams.applicantName" 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="penaltyStatus">
        <el-select clearable v-model="queryParams.penaltyStatus" placeholder="请选择处分状态" style="width: 100%">
          <el-option v-for="dict in dict.type.rt_penalty_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </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:disciplinaryApplication: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:disciplinaryApplication: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:disciplinaryApplication: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:disciplinaryApplication:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="disciplinaryApplicationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请人(辅导员)" align="center" prop="applicantName" />
      <el-table-column label="处分文号" align="center" prop="penaltyNumber" />
      <el-table-column label="学号" align="center" prop="stuNo" />
      <el-table-column label="学生姓名" align="center" prop="stuName" />
      <el-table-column label="处分等级:" align="center" prop="penaltyType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rt_penalty_type" :value="scope.row.penaltyType" />
        </template>
      </el-table-column>
      <el-table-column label="处分状态" align="center" prop="penaltyStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rt_penalty_status" :value="scope.row.penaltyStatus" />
        </template>
      </el-table-column>
      <el-table-column label="违纪日期" align="center" prop="violationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.violationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处分到期时间" align="center" prop="expirationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expirationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="createTime" />
      <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" 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:disciplinaryApplication: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:disciplinaryApplication: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>
        </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="85%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="学号" prop="stuNo">
              <el-input v-model="form.stuNo" placeholder="请输入学号" @blur="handleBlur" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="stuName">
              <el-input v-model="form.stuName" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-radio-group v-model="form.gender">
                <el-radio label="男">男</el-radio>
                <el-radio label="女">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="院部" prop="departmentName">
              <el-input v-model="form.departmentName" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="年级" prop="gradeName">
              <el-input v-model="form.gradeName" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <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="12">
            <el-form-item label="民族" prop="mz">
              <el-input v-model="form.mz" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birthday">
              <el-input v-model="form.birthday" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="籍贯" prop="jg">
              <el-cascader :options="areaOptions" v-model="form.jg" clearable filterable> </el-cascader>
              <!-- <el-input v-model="form.jg" placeholder="" /> -->
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <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="12">
            <el-form-item label="处分等级" prop="penaltyType">
              <el-select clearable v-model="form.penaltyType" placeholder="请选择处分等级" style="width: 100%">
                <el-option v-for="dict in dict.type.rt_penalty_type" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="违纪时间" prop="violationDate">
              <el-date-picker clearable v-model="form.violationDate" type="date" value-format="yyyy-MM-dd"
                placeholder="">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="违纪材料" prop="evidenceUpload">
              <span style="color: red">请上传:违纪询问记录,违纪申辩记录,学生检讨书等扫描件.</span>
              <Affix @input="handleAffix" v-model="form.evidenceUpload"></Affix>
              <!-- <image-upload v-model="form.evidenceUpload" :disabled="true" /> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="处分建议" prop="penaltyRecommendation">
              <el-input v-model="form.penaltyRecommendation" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="违纪条例" prop="violationRegulations">
              <el-input v-model="form.violationRegulations" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveForm">保 存</el-button>
        <el-button type="primary" @click="submitForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addDisciplinaryApplication, delDisciplinaryApplication, getDisciplinaryApplication, getStuInfo, getStuList, listDisciplinaryApplication, save } from '@/api/routine/disciplinaryApplication';
import {
  pcaTextArr, // 省市区联动数据,纯汉字
} from 'element-china-area-data';

export default {
  name: 'DisciplinaryApplication',
  dicts: ['rt_penalty_type', 'rt_penalty_status', 'rt_submission_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生处分申请表格数据
      disciplinaryApplicationList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicantId: null,
        penaltyNumber: null,
        stuNo: null,
        stuId: null,
        penaltyType: null,
        penaltyStatus: null,
        violationDate: null,
        expirationDate: null,
        evidenceUpload: null,
        penaltyRecommendation: null,
        violationRegulations: null,
        submissionStatus: null,
        processInstanceId: null,
      },
      // 表单参数
      form: {
        stuNo: '',
      },
      // 表单校验
      rules: {
        stuNo: [{ required: true, message: '学号不能为空', trigger: 'blur' }],
        studentName: [{ 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' }],
        mz: [{ required: true, message: '请输入民族', trigger: 'blur' }],
        birthday: [{ required: true, message: '请输入出生日期', trigger: 'blur' }],
        jg: [{ required: true, message: '请选择籍贯', trigger: 'blur' }],
        violationDate: [{ required: true, message: '请选择违纪时间', trigger: 'blur' }],
        penaltyType: [{ required: true, message: '请选择违纪等级', trigger: 'blur' }],
        evidenceUpload: [{ required: true, message: '请上传违纪材料', trigger: 'blur' }],
        penaltyRecommendation: [{ required: true, message: '请输入违纪建议', trigger: 'blur' }],
        violationRegulations: [{ required: true, message: '请输入违纪条例', trigger: 'blur' }],
      },
      // 学生信息
      stuInfo: {},
      // 省市区
      areaOptions: pcaTextArr,
    }
  },
  created() {
    // 如果学生,查询学生自己的信息
    this.getList()
  },
  methods: {
    /** 查询学生处分申请列表 */
    getList() {
      this.loading = true
      listDisciplinaryApplication(this.queryParams).then((response) => {
        this.disciplinaryApplicationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询学生处分申请列表 */
    getStuList() {
      this.loading = true
      getStuList(this.queryParams).then((response) => {
        this.disciplinaryApplicationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        applicationId: null,
        applicantId: null,
        penaltyNumber: null,
        stuNo: null,
        stuId: null,
        penaltyType: null,
        penaltyStatus: null,
        violationDate: null,
        expirationDate: null,
        evidenceUpload: null,
        penaltyRecommendation: null,
        violationRegulations: null,
        submissionStatus: null,
        processInstanceId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: 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.applicationId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加学生处分申请'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const applicationId = row.applicationId || this.ids
      getDisciplinaryApplication(applicationId).then((response) => {
        this.form = response.data
        this.form.jg = this.form.jg.split(',');
        this.form.penaltyType = this.form.penaltyType.toString()
        this.open = true
        this.title = '修改学生处分申请'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$modal.loading('正在努力加载中,请稍等...')
          this.form.jg = this.form.jg.toString()
          addDisciplinaryApplication(this.form).then((response) => {
            this.$modal.closeLoading()

            if (response.code == 500) {
              return
            }
            this.$modal.msgSuccess('提交成功')

            this.open = false
            this.getList()
          })
        }
      })
    },
    // 保存
    saveForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$modal.loading('正在努力加载中,请稍等...')
          this.form.jg = this.form.jg.toString()
          save(this.form).then((response) => {
            this.$modal.closeLoading()

            if (response.code == 500) {
              return
            }
            this.$modal.msgSuccess('保存成功')

            this.open = false
            this.getList()
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const applicationIds = row.applicationId || this.ids
      this.$modal
        .confirm('是否确认删除学生处分申请编号为"' + applicationIds + '"的数据项?')
        .then(function () {
          return delDisciplinaryApplication(applicationIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'routine/disciplinaryApplication/export',
        {
          ...this.queryParams,
        },
        `disciplinaryApplication_${new Date().getTime()}.xlsx`
      )
    },
    handleBlur() {
      if (this.form.stuNo != null || this.form.stuNo != '') {
        getStuInfo(this.form.stuNo).then((res) => {
          if (res.data == null) {
            this.$message.error('学号不存在')

            return
          }
          this.form = res.data
          this.form.stuName = res.data.studentName
        })
      }
    },
    detail(row) {
      console.log("row", row)
      this.$router.push({
        path: '/routine/disciplinaryApplication/detail',
        query: {
          procInsId: row.processInstanceId,
          deployId: row.deployId,
          applicationId: row.applicationId,
          category: 'disposal',
        },
      })
    },
    handleAffix(affixId) {
      this.form.evidenceUpload = affixId
    },
  },
}
</script>
