#!/usr/bin/env Python
# coding=utf-8

from django import forms
from django.contrib.auth.models import User
from .models import UserProfile
import datetime


# 用户登录表单
class User_Login_Form(forms.Form):
    username = forms.CharField(max_length=20, label='用户名', widget=forms.TextInput(attrs={
        'class': 'input',
        'placeholder': '用户名/邮箱在此输入...'
    }))
    password = forms.CharField(min_length=6, max_length=18, label='密码', widget=forms.PasswordInput(attrs={
        'class': 'input',
        'placeholder': '密码在此输入...'
    }))

    def clean_password(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')
        if username == password:
            raise forms.ValidationError('用户名和密码不能一致!!!')
        return password


# 用户注册表单
class User_Register_Form(forms.ModelForm):
    class Meta:
        model = User
        fields = ('email', 'password_first', 'password_second')

    email = forms.EmailField(label='邮箱', max_length=32, widget=forms.EmailInput(attrs={
        'class': 'input',
        'placeholder': '请输入用户名...'
    }))
    password_first = forms.CharField(label='首次密码', min_length=6, widget=forms.PasswordInput(attrs={
        'class': 'input',
        'placeholder': '请输入密码...'
    }))
    password_second = forms.CharField(label='确认密码', min_length=6, widget=forms.PasswordInput(attrs={
        'class': 'input',
        'placeholder': '再次输入密码...'
    }))

    def clean_email(self):
        email = self.cleaned_data.get('email')
        exists = User.objects.filter(email=email).exists()
        if exists:
            raise forms.ValidationError('该邮箱已被占用...')
        return email

    def clean_password_check(self):
        if self.cleaned_data['password_first'] != self.cleaned_data['password_second']:
            raise forms.ValidationError('两次密码输入不一致...')
        return self.cleaned_data['password_first']


# 用户忘记密码
class Forget_Password_Form(forms.Form):
    email = forms.EmailField(label='请输入注册邮箱地址', min_length=4, widget=forms.EmailInput(attrs={
        'class': 'input',
        'placeholder': '用户名/邮箱'
    }))


# 修改用户密码
class Modify_Password_Form(forms.Form):
    New_password = forms.CharField(label='新密码', min_length=6, widget=forms.PasswordInput(attrs={
        'class': 'input',
        'placeholder': '在此输入新密码'
    }))
    Confirm_password = forms.CharField(label='确认密码', min_length=6, widget=forms.PasswordInput(attrs={
        'class': 'input',
        'placeholder': '再次输入新密码'
    }))


class Editor_UserForm(forms.ModelForm):

    email = forms.EmailField(widget=forms.EmailInput(attrs={
        'class': 'input',
        'readonly': 'true'  # 邮箱禁止用户修改
    }))

    class Meta:
        model = User
        fields = ('email', )


class Editor_UserProfileForm(forms.ModelForm):
    # 获得当前年份
    Now_year = datetime.date.today().year

    name = forms.CharField(widget=forms.TextInput(attrs={
        'class': 'input'
    }))

    info = forms.CharField(widget=forms.TextInput(attrs={
        'class': 'textarea'
    }))

    gexing = forms.CharField(widget=forms.TextInput(attrs={
        'class': 'input'
    }))

    birthday = forms.DateField(widget=forms.SelectDateWidget(attrs={
        'class': 'select'
    }, years=range(Now_year - 30, Now_year)))

    # password = forms.CharField(widget=forms.PasswordInput(attrs={
    #     'class': 'input'
    # }))  # 这里密码没有修改成功

    class Meta:
        model = UserProfile
        fields = ('name', 'info', 'gexing', 'birthday', )
