<template>
  <el-form v-loading="loading" ref="ruleForm" :model="user" :rules="rules" label-width="80px">
    <el-row>
      <el-col :span="12">
        <el-form-item label="老师姓名" prop="teacherName">
          <el-input v-model="user.teacherName" placeholder="请输入老师姓名" maxlength="30"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="user.idCard" placeholder="请输入身份证号" maxlength="18"/>
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-select v-model="user.nation" style="width: 100%" placeholder="请选择民族">
            <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="birthday">
          <el-date-picker
            v-model="user.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生日日期"
            style="width:100%">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="user.sex">
            <el-radio label="0">男</el-radio>
            <el-radio label="1">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="手机号码" prop="teacherTel">
          <el-input v-model="user.teacherTel" placeholder="请输入手机号码" maxlength="11"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="user.email" placeholder="请输入邮箱" maxlength="50"/>
        </el-form-item>
        <el-form-item label="工作编号" prop="teacherCode">
          <el-input v-model="user.teacherCode" placeholder="请输入工作编号" maxlength="11"/>
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-select v-model="user.education" style="width: 100%" placeholder="请选择学历">
            <el-option
              v-for="dict in dict.type.education"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="家庭住址" prop="homeAddress">
          <el-input v-model="user.homeAddress" placeholder="请输入家庭住址" maxlength="30"/>
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item>
      <el-button icon="el-icon-check" type="primary" size="mini" @click="submit">保 存</el-button>
      <el-button icon="el-icon-refresh" type="info" size="mini" @click="resetForm">重 置</el-button>
      <el-button icon="el-icon-close " type="danger" size="mini" @click="close">关 闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {editInfo} from "@/api/system/basicInfo";
import {checkIdcard} from '@/utils/utilLibrary/validate'
import {Nations} from "@/enums/common";

export default {
  dicts: ['education'],
  props: {
    user: {
      teacherName: undefined,
      idCard: undefined,
      nation: undefined,
      birthday: undefined,
      sex: undefined,
      teacherTel: undefined,
      email: undefined,
      teacherCode: undefined,
      education: undefined,
      homeAddress: undefined,
    }
  },
  data() {
    return {
      loading: false,
      Nations,
      // 表单校验
      rules: {
        teacherName: [
          {required: true, message: "老师名字不能为空", trigger: "blur"}
        ],
        email: [
          {required: true, message: "邮箱地址不能为空", trigger: "blur"},
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        teacherTel: [
          {required: true, message: "手机号码不能为空", trigger: "blur"},
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        sex: [
          {required: true, message: "请选择老师性别", trigger: "blur"},
        ],
        idCard: [{required: true, validator: checkIdcard, trigger: 'blur'}],
      }
    };
  },
  methods: {
    submit() {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          this.loading = true;
          console.log(this.user, 128)
          delete this.user.avatar
          delete this.user.sign
          console.log(this.user, 130)
          editInfo(this.user).then(response => {
            if (response.code == 200) {
              this.loading = false;
              this.$modal.msgSuccess("修改成功");
            }
          }).catch(() => {
              this.loading = false;
              this.$modal.msgError("修改失败");
            }
          )

        }
      });
    },
    close() {
      this.$tab.closePage();
    },
    /** 重置按钮操作 */
    resetForm() {
      this.$refs["ruleForm"].resetFields();
    },


  }
};
</script>
<style scoped>
/* 样式穿透 */
.el-form-item::v-deep {
  margin-right: 40px;
}
</style>
