<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" label-width="100px" size="small" label-position="top" class="hall-form-body">
      <el-form-item label="姓名" prop="personal_name" >
        <el-input v-model="ruleForm.personal_name" :disabled="Number(ruleForm.submit) === 1"></el-input>
      </el-form-item>
      <el-form-item label="班级" prop="class_name">
        <el-input v-model="ruleForm.class_name" :disabled="Number(ruleForm.submit) === 1"></el-input>
      </el-form-item>
      <el-form-item label="Sofia ID" prop="sofia_id" key="sofia_id">
        <el-input v-model="ruleForm.sofia_id" type="number" :disabled="Number(ruleForm.submit) === 1"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="telephone" key="telephone" v-if="schoolReport === 'pareport'">
        <el-input v-model="ruleForm.telephone" :disabled="Number(ruleForm.submit) === 1"></el-input>
      </el-form-item>
      <el-form-item label="邮寄地址" prop="email">
        <el-input v-model="ruleForm.email" :disabled="Number(ruleForm.submit) === 1"></el-input>
      </el-form-item>
      <el-form-item label="需打印的份数" prop="number" key="number" v-if="schoolReport === 'pareport'">
        <el-input v-model.number="ruleForm.number" type="number" :disabled="Number(ruleForm.submit) === 1"></el-input>
      </el-form-item>
      <el-form-item class="ts-width">
        <el-button type="primary" :disabled="Number(ruleForm.submit) === 1" @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() {
    return {
      ruleForm: {
        personal_name: '',
        class_name: '',
        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, message: '请输入Sofia ID', trigger: 'blur' }
        ],
        telephone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式错误' }
        ],
        email: [
          { required: true, message: '请输入email', trigger: 'blur' }
        ],
        number: [
          { required: true, message: '请输入打印份数', 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)
    }
  }
}
</script>

<style lang="scss" scoped>
.hall-form {
  width: 100%;
  .hall-form-body {
    padding: 15px 10px 20px 10px;
  }
}
::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>
