from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms import ValidationError
from wtforms.validators import DataRequired, Length,  EqualTo, Regexp

from cpblog.models import Admin


class LoginForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Length(1, 254), Regexp('.*@.*\.com',message='qq邮箱')])
    password = PasswordField('Password', validators=[DataRequired()])
    remember_me = BooleanField('Remember me')
    submit = SubmitField('登陆')


class RegisterForm(FlaskForm):
    name = StringField('Name', validators=[DataRequired(), Length(1, 30)])
    email = StringField('Email', validators=[DataRequired(), Length(1, 254), Regexp('.*@.*\.com',message='qq邮箱')])
    username = StringField('Username', validators=[DataRequired(), Length(1, 20),Regexp('^[a-zA-Z0-9]*$',message='只能字母数字.')])
    password = PasswordField('Password', validators=[DataRequired(), Length(8, 128), EqualTo('password2')])
    password2 = PasswordField('Confirm password', validators=[DataRequired()])
    InviteCode = StringField('InviteCode', validators=[DataRequired()])
    submit = SubmitField()

    def validate_email(self, field):
        if Admin.query.filter_by(email=field.data.lower()).first():
            raise ValidationError('邮箱已注册.')

    def validate_username(self, field):
        if Admin.query.filter_by(username=field.data).first():
            raise ValidationError('用户名已存在.')


class ForgetPasswordForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Length(1, 254), Regexp('.*@.*\.com',message='qq邮箱')])
    submit = SubmitField()


class ResetPasswordForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Length(1, 254), Regexp('.*@.*\.com',message='qq邮箱')])
    password = PasswordField('Password', validators=[
        DataRequired(), Length(8, 128), EqualTo('password2')])
    password2 = PasswordField('Confirm password', validators=[DataRequired()])
    submit = SubmitField()