<template>
  <div id="root">
    <el-form :model="formInline" :rules="rules" label-width="80px" class="form-class">
      <el-form-item label="名称" prop="name">
        <el-input v-model="formInline.name"/>
      </el-form-item>
      <el-form-item label="号码" prop="tel">
        <el-input v-model="formInline.tel" oninput="value=value.replace(/[^\d]/g,'')"/>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="formInline.remark"/>
      </el-form-item>
      <div class="button-class">
        <el-button type="info" @click="exit">取消</el-button>
        <el-button type="primary" @click="updateTels">保存</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Index',
  data() {
    return {
      formInline: {
        id: '',
        name: '',
        tel: '',
        remark: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, max: 11, required: true, message: '请输入11位手机号', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.formInline.id = this.$route.query.id
    this.getTelsById()
  },
  methods: {
    updateTels() {
      this.$store.dispatch('TelUpdate', this.formInline).then(res => {
        const resCode = res.code
        if (resCode === '1') {
          this.$message.error(res.message)
          return
        }
        this.$message.success(res.message)
        this.$router.go(-1)
      }).catch(() => {
        console.log('error TelUpdate')
      })
    },
    exit() {
      this.$router.go(-1)
    },
    getTelsById() { // 根据详情查询物业号码
      this.$store.dispatch('GetTelById', this.formInline.id).then(res => {
        const resCode = res.code
        if (resCode === '1') {
          this.$message.error(res.message)
          return
        }
        const resData = res.data
        this.formInline.name = resData.name
        this.formInline.tel = resData.tel
        this.formInline.remark = resData.remark
      }).catch(() => {
        console.log('error ')
      })
    }
  }
}
</script>

<style scoped>
.form-class {
  width: 400px;
  margin-right: auto;
  margin-left: auto;
  margin-top: 20px;
}
.button-class {
  display: flex;
  justify-content: center;
}
</style>
