#!/usr/bin/env python
#-*- coding: utf-8 -*-

from django import forms
from django.contrib import auth
from django.contrib.auth import get_user_model
from UserManage.models import User,RoleList,PermissionList,GroupList,GroupListList
import datetime

class LoginUserForm(forms.Form):
    username = forms.CharField(label=u'账 号',error_messages={'required':u'账号不能为空'},
        widget=forms.TextInput(attrs={'class':'form-control',"placeholder":"请输入账号"}))
    password = forms.CharField(label=u'密 码',error_messages={'required':u'密码不能为空'},
        widget=forms.PasswordInput(attrs={'class':'form-control',"placeholder":"请输入密码"}))

    def __init__(self, request=None, *args, **kwargs):
        self.request = request
        self.user_cache = None

        super(LoginUserForm, self).__init__(*args, **kwargs)

    def clean_password(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')

        if username and password:
            self.user_cache = auth.authenticate(username=username,password=password)
            if self.user_cache is None:
                raise forms.ValidationError(u'账号密码不匹配')
            elif not self.user_cache.is_active:
                raise forms.ValidationError(u'此账号已被禁用')
        return self.cleaned_data

    def get_user(self):
        return self.user_cache

class ChangePasswordForm(forms.Form):
    old_password = forms.CharField(label=u'原始密码',error_messages={'required':'请输入原始密码'},
        widget=forms.PasswordInput(attrs={'class':'form-control'}))
    new_password1 = forms.CharField(label=u'新密码',error_messages={'required':'请输入新密码'},
        widget=forms.PasswordInput(attrs={'class':'form-control'}))
    new_password2 = forms.CharField(label=u'重复输入',error_messages={'required':'请重复新输入密码'},
        widget=forms.PasswordInput(attrs={'class':'form-control'}))

    def __init__(self, user, *args, **kwargs):
        self.user = user
        super(ChangePasswordForm, self).__init__(*args, **kwargs)

    def clean_old_password(self):
        old_password = self.cleaned_data["old_password"]
        if not self.user.check_password(old_password):
            raise forms.ValidationError(u'原密码错误')
        return old_password

    def clean_new_password2(self):
        password1 = self.cleaned_data.get('new_password1')
        password2 = self.cleaned_data.get('new_password2')
        if len(password1)<6:
            raise forms.ValidationError(u'密码必须大于6位')

        if password1 and password2:
            if password1 != password2:
                raise forms.ValidationError(u'两次密码输入不一致')
        return password2

    def save(self, commit=True):
        self.user.set_password(self.cleaned_data['new_password1'])
        if commit:
            self.user.save()
        return self.user

class AddUserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('username','password','sid','classid','email','nickname','sex','birthday','role','is_active')
        widgets = {
            'username' : forms.TextInput(attrs={'class':'form-control'}),
            'password' : forms.PasswordInput(attrs={'class':'form-control'}),
            'sid' : forms.TextInput(attrs={'class':'form-control'}),
            'classid' : forms.Select(choices=[(x.name,x.name) for x in GroupList.objects.all()],attrs={'class':'form-control'}),
            'email' : forms.TextInput(attrs={'class':'form-control'}),
            'nickname' : forms.TextInput(attrs={'class':'form-control'}),
            'sex' : forms.RadioSelect(choices=((u'男', u'男'),(u'女', u'女'),(u'保密', u'保密')),attrs={'class':'list-inline'}),
            'birthday' : forms.TextInput(attrs={'class':'form-control'}),
            'role' : forms.Select(attrs={'class':'form-control'}),
            'is_active' : forms.Select(choices=((True, u'启用'),(False, u'禁用')),attrs={'class':'form-control'}),
        }

    def __init__(self,*args,**kwargs):
        super(AddUserForm,self).__init__(*args,**kwargs)
        self.fields['username'].label=u'账 号'
        self.fields['username'].error_messages={'required':u'请输入账号'}
        self.fields['password'].label=u'密 码'
        self.fields['password'].error_messages={'required':u'请输入密码'}
        self.fields['sid'].label=u'学 号'
        self.fields['sid'].error_messages={'required':u'请输入学号','invalid':u'请输入有效学号'}
        self.fields['classid'].label=u'分 组'
        self.fields['classid'].error_messages={'required':u'请输入分组号','invalid':u'请输入有效分组号'}
        self.fields['email'].label=u'邮 箱'
        self.fields['email'].error_messages={'invalid':u'请输入有效邮箱'}
        self.fields['nickname'].label=u'姓 名'
        self.fields['nickname'].error_messages={'required':u'请输入姓名'}
        self.fields['sex'].label=u'性 别'
        self.fields['sex'].error_messages={'required':u'请选择性别'}
        self.fields['birthday'].label=u'生 日'
        self.fields['birthday'].error_messages={'invalid':u'请输入有效的生日'}
        self.fields['role'].label=u'角 色'
        self.fields['is_active'].label=u'状 态'

    def clean_password(self):
        password = self.cleaned_data.get('password')
        if len(password) < 6:
            raise forms.ValidationError(u'密码必须大于6位')
        return password

    def clean_sid(self):
        sid = self.cleaned_data.get('sid')
        if not str(sid).isdigit():
            raise forms.ValidationError(u'学号必须为纯数字')
        return sid



class EditUserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('username','sid','classid','email','nickname','sex','birthday','role','is_active')
        widgets = {
            'username' : forms.TextInput(attrs={'class':'form-control'}),
            #'password': forms.HiddenInput,
            'sid' : forms.TextInput(attrs={'class':'form-control'}),
            'classid' : forms.Select(choices=[(x.name,x.name) for x in GroupList.objects.all()],attrs={'class':'form-control'}),
            'email' : forms.TextInput(attrs={'class':'form-control'}),
            'nickname' : forms.TextInput(attrs={'class':'form-control'}),
            'sex' : forms.RadioSelect(choices=((u'男', u'男'),(u'女', u'女'),(u'保密', u'保密')),attrs={'class':'list-inline'}),
            'birthday' : forms.TextInput(attrs={'class':'form-control'}),
            'role' : forms.Select(choices=[(x.name,x.name) for x in RoleList.objects.all()],attrs={'class':'form-control'}),
            'is_active' : forms.Select(choices=((True, u'启用'),(False, u'禁用')),attrs={'class':'form-control'}),
        }

    def __init__(self,*args,**kwargs):
        super(EditUserForm,self).__init__(*args,**kwargs)
        self.fields['username'].label=u'账 号'
        self.fields['username'].error_messages={'required':u'请输入账号'}
        self.fields['sid'].label=u'学 号'
        self.fields['sid'].error_messages={'required':u'请输入学号','invalid':u'请输入有效学号'}
        self.fields['classid'].label=u'分 组'
        self.fields['classid'].error_messages={'required':u'请输入分组号','invalid':u'请输入有效分组号'}
        self.fields['email'].label=u'邮 箱'
        self.fields['email'].error_messages={'invalid':u'请输入有效邮箱'}
        self.fields['nickname'].label=u'姓 名'
        self.fields['nickname'].error_messages={'required':u'请输入姓名'}
        self.fields['sex'].label=u'性 别'
        self.fields['sex'].error_messages={'required':u'请选择性别'}
        self.fields['birthday'].label=u'生 日'
        self.fields['birthday'].error_messages={'invalid':u'请输入有效的生日'}
        self.fields['role'].label=u'角 色'
        self.fields['is_active'].label=u'状 态'

    def clean_password(self):
        password = self.cleaned_data.get('password')
        if len(password) < 6:
            raise forms.ValidationError(u'密码必须大于6位')
        return password

    def clean_sid(self):
        sid = self.cleaned_data.get('sid')
        if not str(sid).isdigit():
            raise forms.ValidationError(u'学号必须为纯数字')
        return sid



class PermissionListForm(forms.ModelForm):
    class Meta:
        model = PermissionList
        widgets = {
            'name' : forms.TextInput(attrs={'class':'form-control'}),
            'url' : forms.TextInput(attrs={'class':'form-control'}),
        }

    def __init__(self,*args,**kwargs):
        super(PermissionListForm,self).__init__(*args,**kwargs)
        self.fields['name'].label=u'名 称'
        self.fields['name'].error_messages={'required':u'请输入名称'}
        self.fields['url'].label=u'URL'
        self.fields['url'].error_messages={'required':u'请输入URL'}

class RoleListForm(forms.ModelForm):
    class Meta:
        model = RoleList
        widgets = {
            'name' : forms.TextInput(attrs={'class':'form-control'}),
            'permission' : forms.SelectMultiple(attrs={'class':'form-control','size':'10','multiple':'multiple'}),
            #'permission' : forms.CheckboxSelectMultiple(choices=[(x.id,x.name) for x in PermissionList.objects.all()]),
        }

    def __init__(self,*args,**kwargs):
        super(RoleListForm,self).__init__(*args,**kwargs)
        self.fields['name'].label=u'名 称'
        self.fields['name'].error_messages={'required':u'请输入名称'}
        self.fields['permission'].label=u'URL'
        self.fields['permission'].required=False


class GroupListForm(forms.ModelForm):
    class Meta:
        model = GroupList
        widgets = {
            'name' : forms.TextInput(attrs={'class':'form-control'}),
            'classid' : forms.TextInput(attrs={'class':'form-control'}),
            #'users' : forms.SelectMultiple(attrs={'class':'form-control','size':'10','multiple':'multiple'}),
            #'permission' : forms.CheckboxSelectMultiple(choices=[(x.id,x.name) for x in PermissionList.objects.all()]),
        }

    def __init__(self,*args,**kwargs):
        super(GroupListForm,self).__init__(*args,**kwargs)
        self.fields['name'].label=u'名 称'
        self.fields['name'].error_messages={'required':u'请输入名称'}
        self.fields['classid'].label=u'分组号'
        self.fields['classid'].error_messages={'required':u'请输入分组号','invalid':u'请输入有效分组号'}
        #self.fields['users'].label=u'姓 名'
        #self.fields['users'].required=False

class XLSFileForm(forms.Form):
    xls = forms.FileField()

    def __init__(self,*args,**kwargs):
        super(XLSFileForm,self).__init__(*args,**kwargs)
        self.fields['xls'].label=u'文 件'
        self.fields['xls'].error_messages={'required':u'请输入名称'}

class RemoveUsersForm(forms.ModelForm):
    class Meta:
        model = GroupListList
        widgets = {
            'classid' : forms.SelectMultiple(attrs={'class':'form-control','size':'10','multiple':'multiple'}),
            #'permission' : forms.CheckboxSelectMultiple(choices=[(x.id,x.name) for x in PermissionList.objects.all()]),
        }

    def __init__(self,*args,**kwargs):
        super(RemoveUsersForm,self).__init__(*args,**kwargs)
        self.fields['classid'].label=u'要删除的用户分组'
        self.fields['classid'].required=True



class ChangeUserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('email','nickname','sex','birthday')
        widgets = {
            #'password': forms.HiddenInput,
            'email' : forms.TextInput(attrs={'class':'form-control'}),
            'nickname' : forms.TextInput(attrs={'class':'form-control'}),
            'sex' : forms.RadioSelect(choices=((u'男', u'男'),(u'女', u'女'),(u'保密', u'保密')),attrs={'class':'list-inline'}),
            'birthday' : forms.TextInput(attrs={'class':'form-control'}),
        }

    def __init__(self,*args,**kwargs):
        super(ChangeUserForm,self).__init__(*args,**kwargs)
        self.fields['email'].label=u'邮 箱'
        self.fields['email'].error_messages={'invalid':u'请输入有效邮箱'}
        self.fields['nickname'].label=u'姓 名'
        self.fields['nickname'].error_messages={'required':u'请输入姓名'}
        self.fields['sex'].label=u'性 别'
        self.fields['sex'].error_messages={'required':u'请选择性别'}
        self.fields['birthday'].label=u'生 日'
        self.fields['birthday'].error_messages={'invalid':u'请输入有效的生日'}


class AddAdminForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('username','password','sid','email','nickname','sex','birthday')
        widgets = {
            'username' : forms.TextInput(attrs={'class':'form-control'}),
            'password' : forms.PasswordInput(attrs={'class':'form-control'}),
            'sid' : forms.TextInput(attrs={'class':'form-control'}),
            'email' : forms.TextInput(attrs={'class':'form-control'}),
            'nickname' : forms.TextInput(attrs={'class':'form-control'}),
            'sex' : forms.RadioSelect(choices=((u'男', u'男'),(u'女', u'女'),(u'保密', u'保密')),attrs={'class':'list-inline'}),
        }

    def __init__(self,*args,**kwargs):
        super(AddAdminForm,self).__init__(*args,**kwargs)
        self.fields['username'].label=u'账 号'
        self.fields['username'].error_messages={'required':u'请输入账号'}
        self.fields['password'].label=u'密 码'
        self.fields['password'].error_messages={'required':u'请输入密码'}
        self.fields['sid'].label=u'学 号'
        self.fields['sid'].error_messages={'required':u'请输入学号','invalid':u'请输入有效学号'}
        self.fields['email'].label=u'邮 箱'
        self.fields['email'].error_messages={'invalid':u'请输入有效邮箱'}
        self.fields['nickname'].label=u'姓 名'
        self.fields['nickname'].error_messages={'required':u'请输入姓名'}
        self.fields['sex'].label=u'性 别'
        self.fields['sex'].error_messages={'required':u'请选择性别'}
        self.fields['birthday'].label=u'生 日'
        self.fields['birthday'].error_messages={'invalid':u'请输入有效的生日'}

    def clean_password(self):
        password = self.cleaned_data.get('password')
        if len(password) < 6:
            raise forms.ValidationError(u'密码必须大于6位')
        return password


class SignInUserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('username','password','sid','classid','email','nickname','sex','birthday')
        widgets = {
            'username' : forms.TextInput(attrs={'class':'form-control'}),
            'password' : forms.PasswordInput(attrs={'class':'form-control'}),
            'sid' : forms.TextInput(attrs={'class':'form-control'}),
            'classid' : forms.Select(choices=[(x.name,x.name) for x in GroupList.objects.all()],attrs={'class':'form-control'}),
            'email' : forms.TextInput(attrs={'class':'form-control'}),
            'nickname' : forms.TextInput(attrs={'class':'form-control'}),
            'sex' : forms.RadioSelect(choices=((u'男', u'男'),(u'女', u'女'),(u'保密', u'保密')),attrs={'class':'list-inline'}),
            'birthday' : forms.TextInput(attrs={'class':'form-control'}),
        }

    def __init__(self,*args,**kwargs):
        super(SignInUserForm,self).__init__(*args,**kwargs)
        self.fields['username'].label=u'账 号'
        self.fields['username'].error_messages={'required':u'请输入账号'}
        self.fields['password'].label=u'密 码'
        self.fields['password'].error_messages={'required':u'请输入密码'}
        self.fields['sid'].label=u'学 号'
        self.fields['sid'].error_messages={'required':u'请输入学号','invalid':u'请输入有效学号'}
        self.fields['classid'].label=u'分 组'
        self.fields['classid'].error_messages={'required':u'请输入分组号','invalid':u'请输入有效分组号'}
        self.fields['email'].label=u'邮 箱'
        self.fields['email'].error_messages={'invalid':u'请输入有效邮箱'}
        self.fields['nickname'].label=u'姓 名'
        self.fields['nickname'].error_messages={'required':u'请输入姓名'}
        self.fields['sex'].label=u'性 别'
        self.fields['sex'].error_messages={'required':u'请选择性别'}
        self.fields['birthday'].label=u'生 日'
        self.fields['birthday'].error_messages={'invalid':u'请输入有效的生日'}


    def clean_password(self):
        password = self.cleaned_data.get('password')
        if len(password) < 6:
            raise forms.ValidationError(u'密码必须大于6位')
        return password

    def clean_sid(self):
        sid = self.cleaned_data.get('sid')
        if not str(sid).isdigit():
            raise forms.ValidationError(u'学号必须为纯数字')
        return sid
