from django.db import models
from rbac.models import SoftCommonModel, CommonModel, UserProfile
from django.contrib.postgres.fields import JSONField, ArrayField
from question.models import Questioncat, Question
from crm.models import Consumer
from .models_paper import WorkScope, Paper


class Exam(CommonModel):
    """
    组织的正式考试
    """
    code = models.CharField('考试编号', max_length=100, null=True, blank=True)
    name = models.CharField('名称', max_length=100)
    place = models.CharField('考试地点', max_length=100, null=True, blank=True)
    workscope = models.ForeignKey(WorkScope, verbose_name='工作类别', on_delete= models.DO_NOTHING)
    opentime = models.DateTimeField('开启时间', null=True, blank=True)
    closetime = models.DateTimeField('关闭时间', null=True, blank=True)
    proctor_name = models.CharField('监考人姓名', max_length=100)
    proctor_phone = models.CharField('监考人联系方式', max_length=100)
    create_admin = models.ForeignKey(UserProfile, on_delete=models.SET_NULL, null=True, blank=True, related_name='exam_create_admin')
    chance = models.IntegerField('考试机会', default=3)
    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)
    paper = models.ForeignKey(Paper, verbose_name='选定的试卷', on_delete=models.CASCADE, null=True, blank=True)
    qdimgs = JSONField('签到图片', default=list)
    xcimgs = JSONField('现场图片', default=list)
    
    def __str__(self):
        return self.name

class ExamTest(CommonModel):
    '''
    考试记录表
    '''
    type_choices = (
        ('自助模考', '自助模考'),
        ('押卷模考', '押卷模考'),
        ('正式考试', '正式考试')
    )
    name = models.CharField(max_length=200, verbose_name='名称')
    type = models.CharField(max_length=50, default='自助模考',choices = type_choices, verbose_name='考试类型')
    limit = models.IntegerField(default=0, verbose_name='限时(分钟)')
    workscope = models.ForeignKey(WorkScope, on_delete=models.SET_NULL, verbose_name='工作类别', null=True, blank=True)
    paper = models.ForeignKey(Paper, on_delete=models.SET_NULL, verbose_name='所用试卷', null=True, blank=True)
    consumer = models.ForeignKey(Consumer, on_delete=models.CASCADE, verbose_name='模考人')
    consumer_detail = JSONField('用户详情', null=True, blank=True)
    total_score = models.FloatField(default=0, verbose_name='总分')
    score = models.FloatField(default=0, verbose_name='得分')
    took = models.IntegerField(default=0, verbose_name='耗时(秒)')
    start_time = models.DateTimeField(verbose_name='开始答题时间')
    end_time = models.DateTimeField(verbose_name='结束答题时间')
    detail = models.ManyToManyField(Question, verbose_name='答题记录', through='AnswerDetail')
    is_pass = models.BooleanField(default=True, verbose_name='是否通过')

    exam = models.ForeignKey(Exam, verbose_name='关联的正式考试', null=True, blank=True, related_name='examtest_exam', on_delete= models.SET_NULL)

    class Meta:
        verbose_name = '模拟考试'
        verbose_name_plural = verbose_name
    
    def __str__(self):
        return self.name



class AnswerDetail(SoftCommonModel):
    examtest = models.ForeignKey(ExamTest, on_delete=models.CASCADE, related_name='answerdetail_examtest')
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    user_answer = JSONField(null=True,blank=True)
    score = models.FloatField(default=0, verbose_name='本题得分')
    is_right = models.BooleanField(default=False, verbose_name='是否正确')
    class Meta:
        verbose_name = '答题记录'
        verbose_name_plural = verbose_name

    
class Banner(CommonModel):
    name = models.CharField(max_length=200, verbose_name='名称')
    path = models.CharField(max_length=1000, null=True, blank=True, verbose_name='图片地址')
    url = models.CharField(max_length=1000, null=True, blank=True, verbose_name='链接地址')
    sort = models.IntegerField(default=1, verbose_name='排序数字')