<template>
  <div class="hall-form">
    <el-radio
      v-model="schoolReport"
      @change="changeReport"
      label="elreport"
      border
      size="small"
    >电子成绩单</el-radio>
    <el-radio
      v-model="schoolReport"
      @change="changeReport"
      label="pareport"
      border
      size="small"
    >纸质成绩单</el-radio>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      :disabled="Number(ruleForm.submit) === 1"
      label-width="100px"
      size="small"
      label-position="top"
      class="hall-form-body"
    >
      <!-- <el-form-item label="姓名" prop="personal_name">
        <el-input v-model.trim="ruleForm.personal_name"></el-input>
      </el-form-item>
      <el-form-item label="班级" prop="class_name">
        <el-input v-model.trim="ruleForm.class_name"></el-input>
      </el-form-item>-->
      <el-form-item label="Sofia ID" prop="sofia_id" key="sofia_id">
        <el-input v-model.trim="ruleForm.sofia_id"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="telephone" key="telephone" v-if="schoolReport === 'pareport'">
        <el-input v-model.trim="ruleForm.telephone"></el-input>
      </el-form-item>
      <el-form-item :label="schoolReport === 'elreport' ? '邮箱':'邮寄地址'" prop="email">
        <el-input v-model.trim="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="需打印的份数" prop="number" key="number" v-if="schoolReport === 'pareport'">
        <el-input v-model.trim="ruleForm.number"></el-input>
      </el-form-item>
      <el-form-item class="ts-width">
        <span class="red" v-if="schoolReport === 'elreport'">
          毕业生可申请成绩单。
          <br />请仔细核查您填写的信息后提交，提交后不可修改。电子成绩单将发送至您填写的邮箱。
        </span>
        <span class="red" v-else>
          毕业生可申请成绩单。
          <br />请仔细核查您填写的信息后提交，提交后不可修改。纸质成绩单将按您填写的地址邮寄给您。
        </span>
      </el-form-item>
      <el-form-item class="ts-width">
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
        >{{Number(ruleForm.submit) === 1 ? '已提交' : '提交'}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'hall-form',
  props: {
    formData: {
      type: Object
    }
  },
  watch: {
    formData: {
      immediate: true,
      handler(value) {
        this.ruleForm = Object.assign({}, value)
        this.$nextTick(() => {
          this.$refs.ruleForm.clearValidate()
        })
      }
    }
  },
  data() {
    var checkPhone = (rule, value, callback) => {
      var reg = /^1[345789]\d{9}$/
      if (!reg.test(value)) {
        callback(new Error('请输入11位手机号！'))
      } else {
        callback()
      }
    }
    var blurText = async (rule, value, callback) => {
      var boolean = new RegExp('^[1-9][0-9]*$').test(value)
      if (!boolean) {
        callback(new Error('请输入正整数！'))
      } else {
        callback()
      }
    }
    var checkEmail = async (rule, value, callback) => {
      if (!value) {
        if (this.schoolReport === 'pareport') {
          callback(new Error('请输入邮寄地址！'))
        } else {
          callback(new Error('请输入邮箱！'))
        }
      } else {
        callback()
      }
    }
    var checksofiaId = (rule, value, callback) => {
      var boolean = new RegExp('^[1-9][0-9]*$').test(value)
      if (!value) {
        callback(new Error('请输入Sofia ID！'))
      } else if (!boolean) {
        callback(new Error('请输入正整数！'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        sofia_id: '',
        telephone: '',
        email: '',
        number: null,
        submit: 0
      },
      rules: {
        // personal_name: [
        //   { required: true, message: '请输入姓名', trigger: 'blur' }
        // ],
        // class_name: [
        //   { required: true, message: '请输入班级', trigger: 'blur' }
        // ],
        sofia_id: [{ required: true, validator: checksofiaId, trigger: 'blur' }],
        telephone: [{ required: true, validator: checkPhone, trigger: 'blur' }],
        email: [{ required: true, validator: checkEmail, trigger: 'blur' }],
        number: [{ required: true, validator: blurText, trigger: 'blur' }]
      },
      schoolReport: 'elreport'
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$emit('submit', this.ruleForm, this.schoolReport)
        } else {
          return false
        }
      })
    },
    changeReport() {
      this.$emit('submitType', this.schoolReport)
    }
  },
  mounted() {
    this.changeReport()
  }
}
</script>

<style lang="scss" scoped>
.hall-form {
  width: 100%;
  .hall-form-body {
    padding: 15px 10px 20px 10px;
    .red {
      color: red;
    }
  }
}
.hall-form .red:before {
  content: '*';
  color: #f56c6c;
  margin-right: 4px;
}
::v-deep .el-form-item__label {
  padding: 0 0 5px 0;
}
::v-deep .el-form-item {
  margin-bottom: 12px;
}
::v-deep .el-input {
  width: 25%;
}
::v-deep .el-button {
  width: 25%;
  margin-top: 20px;
}
</style>
