<template>
  <div class="letter">
    <header class="header">
      <div class="inner">
        <div class="logo">
          <router-link to="/"
            ><img src="https://webapp-pub.ezijing.com/project/application/images/ezijing-logo.png"
          /></router-link>
        </div>
      </div>
    </header>
    <nav class="nav">
      <h1 class="title">万达酒店及度假村中/高端酒店总经理培养项目推荐信<br />CAAS MBA Recommendation Letter</h1>
    </nav>
    <section class="content">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="form"
        label-width="130px"
        label-position="top"
        @submit.native.prevent
      >
        <el-form-item label="推荐人姓名/Name of Referrer" prop="provider_name">
          <el-input v-model="ruleForm.provider_name" @blur="onBlur('provider_name')"></el-input>
        </el-form-item>
        <el-form-item label="推荐信内容/Content of Recommendation Letter" prop="letter_content">
          <el-input
            type="textarea"
            :autosize="{ minRows: 6 }"
            v-model="ruleForm.letter_content"
            @blur="onBlur('letter_content')"
          ></el-input>
        </el-form-item>
        <el-form-item label="推荐人手机号/Contact Number of Referrer" prop="provider_phone_number">
          <el-input v-model="ruleForm.provider_phone_number"></el-input>
        </el-form-item>
        <el-form-item label="推荐人工作单位/Work Place of Referrer" prop="provider_company_name">
          <el-input v-model="ruleForm.provider_company_name" @blur="onBlur('provider_company_name')"></el-input>
        </el-form-item>
        <el-form-item label="推荐人职务/Position/Title of Referrer" prop="provider_job_title">
          <el-input v-model="ruleForm.provider_job_title" @blur="onBlur('provider_job_title')"></el-input>
        </el-form-item>
      </el-form>
      <div class="buttons">
        <el-button type="primary" @click="handleSubmit">确认提交/Submit</el-button>
      </div>
    </section>

    <el-dialog title="推荐信提交成功" :visible.sync="dialogVisible" :center="true" width="348px">
      <div style="text-align: center">
        <img src="https://webapp-pub.ezijing.com/project/application/images/icon_success.png" />
      </div>
      <template #footer>
        <el-button type="primary" @click="toHome">了解项目</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import * as api from '@/api/my'

export default {
  name: 'LetterIndex',
  data() {
    return {
      ruleForm: {
        provider_name: '',
        letter_content: '',
        provider_phone_number: '',
        provider_company_name: '',
        provider_job_title: '',
      },
      rules: {
        provider_name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        letter_content: [{ required: true, message: '请输入推荐信内容', trigger: 'blur' }],
        provider_phone_number: [{ required: true, pattern: /^1[3-9]\d{9}$/, message: '请输入手机号', trigger: 'blur' }],
        provider_company_name: [{ required: true, message: '请输入工作单位', trigger: 'blur' }],
        provider_job_title: [{ required: true, message: '请输入职务', trigger: 'blur' }],
      },
      dialogVisible: false,
      clientWidth: 0,
    }
  },
  computed: {
    userId() {
      return this.$route.params.userId
    },
    letterId() {
      return this.$route.params.letterId
    },
    labelPosition() {
      return this.clientWidth < 768 ? 'top' : 'left'
    },
  },
  methods: {
    // 提交
    handleSubmit() {
      this.$refs.form.validate().then(() => {
        this.handleAddRequest()
      })
    },
    // 提交推荐信
    handleAddRequest() {
      api.addLetter(this.userId, this.letterId, this.ruleForm).then((response) => {
        const { error, message } = response
        if (error.toString() === '0') {
          this.dialogVisible = true
        } else {
          this.$message.error(message)
        }
      })
    },
    toHome() {
      this.$router.push('/')
    },
    getClientWidth() {
      this.clientWidth = document.body.clientWidth
    },
    onBlur(key) {
      if (this.ruleForm[key]) {
        this.ruleForm[key] = this.ruleForm[key].trim()
      }
    },
  },
  mounted() {
    this.getClientWidth()
    document.body.style = 'background-color:#eee;'
    window.addEventListener('resize', this.getClientWidth)
  },
  destroyed() {
    document.body.style = ''
    window.removeEventListener('resize', this.getClientWidth)
  },
}
</script>

<style lang="scss" scoped>
.letter {
  .header {
    background-color: #fff;
    .inner {
      max-width: 1000px;
      margin: 0 auto;
    }
    .logo {
      padding: 14px 0;
      img {
        height: 50px;
      }
    }
  }
  .nav {
    background-color: var(--main-color);
    .title {
      padding: 15px 0;
      font-size: 24px;
      font-weight: 600;
      color: #fff;
      line-height: 30px;
      text-align: center;
    }
  }
  .content {
    max-width: 1000px;
    margin: 20px auto;
    padding: 20px;
    background-color: #fff;
    box-sizing: border-box;
  }
  ::v-deep .el-input {
    max-width: 270px;
  }
  ::v-deep .el-form--label-top {
    .el-form-item__label {
      line-height: 20px;
    }
    .el-input {
      max-width: 100%;
    }
  }
  ::v-deep .el-input__inner,
  ::v-deep .el-textarea__inner {
    border-radius: 0;
  }
  .buttons {
    border-top: 1px solid #f1f1f1;
    padding: 20px 0;
  }
  @media (max-width: 767px) {
    .logo {
      text-align: center;
    }
    .buttons {
      padding: 20px 0;
      .el-button {
        width: 100%;
        display: block;
      }
    }
  }
}
</style>
