<template>
  <Form ref="loginForm" :model="form" :rules="rules" @keydown.enter.native="handleSubmit">
    <FormItem prop="userName">
      <Input size="large"  v-model="form.userName" placeholder="请输入账号" prefix="ios-contact-outline"></Input>
    </FormItem>
    <FormItem prop="password">
      <Input size="large" type="password" v-model="form.password" placeholder="请输入密码" prefix="ios-lock-outline"></Input>
    </FormItem>
    <FormItem>
      <Checkbox v-model="remember">记住用户名</Checkbox>
    </FormItem>
    <FormItem>
      <Button size="large" @click="handleSubmit" type="primary" long>登录</Button>
    </FormItem>
  </Form>
</template>
<script>
import axios from 'axios'
export default {
  name: 'LoginForm',
  props: {
    userNameRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ]
      }
    },
    passwordRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  data () {
    return {
      remember: true,
      form: {
        userName: '',
        password: ''
      }
    }
  },
  computed: {
    rules () {
      return {
        userName: this.userNameRules,
        password: this.passwordRules
      }
    }
  },
  methods: {
    handleSubmit () {
      if(this.form.password == '123456'){
        this.$Message.info({
          content:'密码错误',
          duration:3,
        });
        return
      }
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          if (this.remember) {
            sessionStorage.setItem('userName', this.form.userName)
          } else {
            sessionStorage.setItem('userName', '')
          }
          this.$emit('on-success-valid', {
            userName: this.form.userName,
            password: this.form.password
          })
        }
      })
    }
  },
  mounted () {
    let userName = sessionStorage.getItem('userName')
    userName && (this.form.userName = userName)
  }
}
</script>
