<template>
  <div class="app-container">
    <span>老师信息</span>
    <hr/>
    <br/>
    <el-form :model="techerInfoForm" ref="reftecherInfoForm" size="small" :inline="true" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="教师名称" clearable prop="teacherName">
            <el-input v-model="techerInfoForm.teacherName" disabled clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别" prop="sex">
            <el-select disabled v-model="techerInfoForm.sex" style="width: 100%" 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-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="教师电话" clearable prop="teacherTel">
            <el-input disabled v-model="techerInfoForm.teacherTel" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作编号" clearable prop="teacherCode">
            <el-input disabled v-model="techerInfoForm.teacherCode" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span>履历信息</span>
    <hr/>
    <br/>
    <el-row>
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="resumeAdd">增加</el-button>
      <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="resumeEdit">修改</el-button>
      <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="resumeDelete">删除
      </el-button>
      <el-button type="info" size="mini" plain icon="el-icon-close" @click="closePage">关闭</el-button>
    </el-row>
    <br/>
    <el-table stripe v-loading="loading" style="width: 100%" :data="resumeList"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" width="55"/>
      <el-table-column label="序号" type="index" align="center" width="55"/>
      <el-table-column label="学校/工作单位" width="width" align="center" prop="learnSchool"/>
      <el-table-column label="履历开始时间" :formatter="dateFormat" width="width" align="center"
                       prop="learnStart"></el-table-column>
      <el-table-column label="履历结束时间" :formatter="dateFormat" width="width" align="center"
                       prop="learnEnd"></el-table-column>
      <el-table-column label="备注" width="width" align="center" prop="remark"/>
    </el-table>
    <ExPagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                  @pagination="getList"/>

    <!--    履历编辑修改，新增弹窗-->
    <el-dialog :title="title" :visible.sync="openTeacherResume" width="800px">
      <el-form :model="resumeForm" :rules="resumeRules" ref="refresumeForm" size="small"
               style="width: 90%; margin: auto" label-width="110px">
        <el-form-item label="学校/工作单位" prop="learnSchool">
          <el-input v-model="resumeForm.learnSchool" clearable style="width: 100%"/>
        </el-form-item>
        <el-form-item label="履历开始时间" prop="learnStart">
          <el-date-picker v-model="resumeForm.learnStart" type="date" style="width: 100%"
                          placeholder="履历开始时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="履历结束时间" clearable prop="learnEnd">
          <el-date-picker v-model="resumeForm.learnEnd" type="date" style="width: 100%"
                          placeholder="履历结束时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="备注" clearable prop="remark">
          <el-input type="textarea" :rows="3" style="width: 100%" v-model="resumeForm.remark" clearable/>
        </el-form-item>
        <el-form-item>
          <div style="width: 300px; margin: auto">
            <el-button type="primary" @click="resumeAddButton">提交</el-button>
            <el-button @click="openTeacherResume = false">取消</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {getInform} from '@/api/smartSchool/teacherManage/teacherInformation'
import {listResume, getOneResume, addResume, editResume, deleteResume} from '@/api/smartSchool/teacherManage/resume'
// import message from "@/views/home/message/message";

export default {
  name: '',
  dicts: ['education', 'sys_user_sex', 'honor'],
  components: {},
  props: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      title: '',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 教师个人信息
      techerInfoForm: {},
      // 教师个人履历列表
      resumeList: [],
      // 新增或修改教师个人履历对话框
      openTeacherResume: false,
      // 新增或修改教师履历表单
      resumeForm: {},
      // 履历校验规则
      resumeRules: {
        learnSchool: [{required: true, message: '学校/工作单位不能为空', trigger: 'blur'}],
        learnStart: [{required: true, message: '履历开始时间不能为空', trigger: 'blur'}],
        learnEnd: [{required: true, message: '履历结束时间不能为空', trigger: 'blur'}]
      }
    }
  },
  watch: {
    //监听输入的履历开始、履历结束时间，对其就行判断，禁止其出现开始时间晚于结束时间的情况。
    'resumeForm.learnStart'() {
      if (this.resumeForm.learnStart != null) {
        if (this.resumeForm.learnEnd != null) {
          if (this.resumeForm.learnStart >= this.resumeForm.learnEnd) {
            this.resumeForm.learnStart = ''
            this.resumeRules.learnStart[0].message = '履历开始时间要早于履历结束时间！'
          }
        } else {
          this.resumeRules.learnEnd[0].message = '履历结束时间不能为空'
        }
      } else if (this.resumeForm.learnEnd != null) {
        this.resumeRules.learnStart[0].message = '履历开始时间不能为空'
      } else {
        this.resumeRules.learnStart[0].message = '履历开始时间不能为空'
        this.resumeRules.learnEnd[0].message = '履历结束时间不能为空'
      }
    },
    'resumeForm.learnEnd'() {
      if (this.resumeForm.learnEnd != null) {
        if (this.resumeForm.learnStart != null) {
          if (this.resumeForm.learnStart >= this.resumeForm.learnEnd) {
            this.resumeForm.learnEnd = ''
            this.resumeRules.learnEnd[0].message = '履历结束时间要晚于履历开始时间！'
          }
        } else {
          this.resumeRules.learnStart[0].message = '履历开始时间不能为空'

        }
      } else if (this.resumeForm.learnStart != null) {
        this.resumeRules.learnEnd[0].message = '履历结束时间不能为空'
      } else {
        this.resumeRules.learnStart[0].message = '履历开始时间不能为空'
        this.resumeRules.learnEnd[0].message = '履历结束时间不能为空'
      }
    }
  },
  computed: {},
  methods: {
    // 获取教师个人信息
    getTeacherInfo() {
      const id = this.$route.query.teacherId
      getInform(id).then(res => {
        this.techerInfoForm = res.data
      })
    },
    // 获取老师履历信息列表
    getList() {
      this.loading = true
      this.queryParams.teacherId = this.$route.query.teacherId
      listResume(this.queryParams).then(res => {
        if (res.code == 200) {
          this.resumeList = res.rows
          this.total = res.total
          this.loading = false
        } else {
          this.$message.error('数据请求失败')
          this.loading = false
        }
      })
    },
    // 清空表单
    reset() {
      this.resumeForm = {
        teacherId: this.teacherId,
        learnSchool: null,
        learnStart: null,
        learnEnd: null,
        remark: null,
      }
      this.resetForm('refresumeForm')
    },
    //新增操作
    resumeAdd() {
      this.reset()
      this.title = '履历新增'
      this.openTeacherResume = true
    },
    //删除
    resumeDelete() {
      const ids = this.ids
      this.$modal
        .confirm('是否确认删除所选履历信息数据项？')
        .then(function () {
          return deleteResume(ids)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getList()
        })
        .catch(() => {
        })
    },

    //修改
    resumeEdit() {
      const id = this.ids
      getOneResume(id).then(res => {
        this.resumeForm = res.data
        this.title = '履历修改'
        this.openTeacherResume = true
      })
    },
    /** 履历提交按钮 */
    resumeAddButton() {
      this.resumeForm.teacherId = this.$route.query.teacherId
      this.$refs['refresumeForm'].validate(valid => {
        if (valid) {
          if (this.resumeForm.id != null) {
            editResume(this.resumeForm).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.openTeacherResume = false
              this.getList()
            })
          } else {
            addResume(this.resumeForm).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.openTeacherResume = false
              this.getList()
            })
          }
        }
      })
    },
    dateFormat(row, column, cellValue, index) {
      const daterc = row[column.property]
      if (daterc) {
        if (daterc.indexOf('T') === -1) return daterc
        const arr = daterc.split('T')
        const d = arr[0]
        const darr = d.split('-')
        const t = arr[1]
        const tarr = t.split('.000')
        const marr = tarr[0].split(':')
        const dd = darr[0] + '-' + darr[1] + '-' + darr[2]
        return dd
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 关闭页面
    closePage() {
      this.$tab.closeOpenPage({
        path: '/teacherManage/teacherInforManage'
      })
    }
  },
  created() {
    this.getList()
    this.getTeacherInfo()
  },
  mounted() {
  }
}
</script>
<style scoped></style>
