<template>
  <Form :model="form" :rules="rules" @keydown.enter.native="handleSubmit" ref="loginForm">
    <FormItem prop="username">
      <Input autofocus :maxlength="36" placeholder="用户ID" v-model="form.username">
        <span slot="prepend">
          <Icon :size="16" type="ios-person"></Icon>
        </span>
      </Input>
      <!-- <Select prefix="ios-person" v-model="form.username">
        <Option :key="item" :value="item" v-for="item in userList">{{ item }}</Option>
      </Select>-->
    </FormItem>
    <FormItem prop="password">
      <Input :maxlength="36" placeholder="密码" type="password" v-model="form.password">
        <span slot="prepend">
          <Icon :size="14" type="md-lock"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem>
      <Button :loading="logining" @click="handleSubmit" long type="primary">登录</Button>
    </FormItem>
  </Form>
</template>
<script>
export default {
  name: 'LoginForm',
  props: {
    usernameRules: {
      type: Array,
      default: () => {
        return [{ required: true, message: '用户ID不能为空', trigger: 'blur' }]
      }
    },
    passwordRules: {
      type: Array,
      default: () => {
        return [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    },
    logining: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      userList: [
        'boc_201900001',
        'sip7030',
        'sip7033',
        'sip7032',
        'boc_20181128',
        'boc_123456',
        'boc_20180824',
        'boc_201900',
        'boc_1231213',
        'egoo_201810241',
        '10006',
        '237165',
        'HF008'
      ]
    }
  },
  computed: {
    rules () {
      return {
        username: this.usernameRules,
        password: this.passwordRules
      }
    }
  },
  methods: {
    handleSubmit () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.$emit('on-success-valid', {
            username: this.form.username,
            password: this.form.password
          })
        }
      })
    }
  }
}
</script>
