from django.db import models
import django.utils.timezone as timezone
from rbac.models import SoftCommonModel, CommonModel, UserProfile
from question.models import Questioncat, Question
from examtest.models_paper import WorkScope
from django.contrib.postgres.fields import JSONField
# Create your models here.

class Company(CommonModel):
    '''
    客户企业
    '''
    name = models.CharField(max_length=60, verbose_name='名称')
    pid = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL, verbose_name='父')
    create_admin = models.ForeignKey(UserProfile, default=1, on_delete=models.DO_NOTHING)
    geo = JSONField('地理位置', null=True, blank=True)
    class Meta:
        verbose_name = '客户企业'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class ConsumerPerm(CommonModel):
    """
    权限表
    """
    name = models.CharField(max_length=30, unique=True, verbose_name="名称")
    code = models.CharField(max_length=50, verbose_name="方法/代号", unique=True)
    pid = models.ForeignKey("self", null=True, blank=True, on_delete=models.SET_NULL, verbose_name="父")

    class Meta:
        verbose_name = '客户权限表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.code
    
class ConsumerRole(CommonModel):
    """
    客户角色表
    """
    name = models.CharField(max_length=30, unique=True, verbose_name="名称")
    perms = models.ManyToManyField(ConsumerPerm, blank=True, verbose_name="功能权限")
    description = models.CharField(max_length=50, blank=True, null=True, verbose_name="描述")

    class Meta:
        verbose_name = '客户角色表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Consumer(CommonModel):
    '''
    学员
    '''
    name = models.CharField(max_length=200, verbose_name='真实姓名')
    username = models.CharField(max_length=11, verbose_name='手机号码', unique=True, null=True, blank=True)
    company = models.ForeignKey('Company', null=True, blank=True, on_delete=models.SET_NULL, verbose_name='所属单位')
    openid = models.CharField(max_length=200, verbose_name='openid', null=True, blank=True, unique=True)
    avatar = models.CharField(default='/media/default/avatar.png',max_length=1000, null=True, blank=True, verbose_name='头像')
    nickname = models.CharField(max_length=200, verbose_name='昵称', null=True, blank=True)
    subjects = models.ManyToManyField(Questioncat, verbose_name='付费学科', through='PaySubject')
    workscope = models.ForeignKey(WorkScope, verbose_name='默认工作类别', on_delete=models.SET_NULL, null=True, blank=True, related_name='consumer_w')
    remain_count = models.IntegerField('体验次数', default=3)
    role = models.ForeignKey(ConsumerRole, verbose_name='用户角色', default=1, on_delete=models.SET_NULL, null=True, blank=True)

    collects = models.ManyToManyField(Question, verbose_name='收藏试题')
    process = JSONField('练习进度', blank=True, default=dict)

    ID_number = models.CharField('查询身份证号', max_length=100, null=True, blank=True)
    realname = models.CharField('查询真实姓名', max_length=100, null=True, blank=True)

    ID_number1 = models.CharField('身份证号', max_length=100, null=True, blank=True)
    create_admin = models.ForeignKey(UserProfile, on_delete=models.SET_NULL, null=True, blank=True, related_name='consumer_create_admin')
    deptname = models.CharField('所在部门', max_length=100, null=True, blank=True)
    exceed_date = models.DateField('账号过期', null=True, blank=True)

    workscopes = models.ManyToManyField(WorkScope, verbose_name='可选工作类别', through='crm.candidate')
    class Meta:
        verbose_name = '客户'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username

class PaySubject(CommonModel):
    '''
    付费学科关联表
    '''
    consumer = models.ForeignKey(Consumer, on_delete=models.CASCADE)
    subject = models.ForeignKey(Questioncat, on_delete=models.CASCADE)
    
    class Meta:
        unique_together=(('consumer','subject'),)



class SendCode(CommonModel):
    '''
    验证码发送记录
    '''
    phone = models.CharField(max_length=11, verbose_name='发送号码')
    code = models.CharField(max_length=6, verbose_name= '验证码')

class Candidate(CommonModel):
    consumer = models.ForeignKey(Consumer, on_delete=models.SET_NULL, related_name='candidate_consumer', null=True, blank=True)
    workscope = models.ForeignKey(WorkScope, on_delete=models.SET_NULL, related_name='candidate_workscope', null=True, blank=True)
    examtest = models.OneToOneField(to='examtest.examtest', verbose_name='关联考试', null=True, blank=True, on_delete=models.SET_NULL)
    is_manual = models.BooleanField('是否手动创建', default=False)

    # 下面是证书信息
    number = models.CharField('报告单号', max_length=60, null=True, blank=True)
    path = models.TextField('证书地址', null=True, blank=True) # 可以是pdf证书
    workscope_name = models.CharField('证书工作类别', max_length=60, null=True, blank=True)
    consumer_name = models.CharField('姓名', max_length=60, null=True, blank=True)
    ID_number = models.CharField('身份证号', max_length=60, null=True, blank=True)
    company_name = models.CharField('单位', max_length=60, null=True, blank=True)
    deptname = models.CharField('部门', max_length=60, null=True, blank=True)
    issue_date = models.DateField('发证日期', null=True, blank=True)
    start_date = models.DateField('有效期始', null=True, blank=True)
    end_date = models.DateField('有效期止', null=True, blank=True)
    examtest_date = models.DateField('考试时间', null=True, blank=True)
    photo = models.CharField('照片', max_length=200, null=True, blank=True)
    train_name = models.CharField('培训名称', null=True, blank=True, max_length=50)
    train_start_date = models.DateField('培训开始日期', null=True)
    train_end_date = models.DateField('培训结束日期', null=True)
    create_admin = models.ForeignKey(UserProfile, verbose_name="创建管理员", null=True, blank=True, on_delete=models.SET_NULL)

    class Meta:
        verbose_name = '证书'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.consumer.username
