<template>
  <Form ref="loginForm" :model="form" :rules="rules" @keydown.enter.native="handleSubmit">
    <FormItem prop="mobile">
      <Input v-model="form.mobile" placeholder="请输入手机号">
        <span slot="prepend">
          <Icon :size="16" type="ios-person"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="vcode">
      <Input  v-model="form.vcode" placeholder="请输入收到的验证码">
        <span slot="prepend">
          <Icon :size="14" type="md-lock"></Icon>
        </span>
        <span slot="append">
          <Button type="primary">发送验证码</Button>
        </span>
      </Input>
    </FormItem>
    <FormItem>
      <Button @click="handleSubmit" type="primary" long>登录</Button>
    </FormItem>
  </Form>
</template>
<script>
export default {
  name: 'PhoneForm',
  props: {
    mobileRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '电话号码不能为空', trigger: 'blur' }
        ]
      }
    },
    vcodeRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  data () {
    return {
      form: {
        mobile: '',
        vcode: ''
      }
    }
  },
  computed: {
    rules () {
      return {
        userName: this.mobileRules,
        password: this.vcodeRules
      }
    }
  },
  methods: {
    handleSubmit () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$emit('on-success-valid', {
            userName: this.form.mobile,
            password: this.form.vcode
          })
        }
      })
    }
  }
}
</script>
