from django.db import models
from django.template.defaultfilters import default
from rbac.models import SoftCommonModel, CommonModel
from django.contrib.postgres.fields import JSONField, ArrayField
from question.models import Questioncat, Question
from vod.models import Course



# Create your models here.
class TestRule(SoftCommonModel):
    name = models.CharField(max_length=200, unique=True, verbose_name='名称')
    danxuan_count = models.IntegerField(default=0, verbose_name='单选数量')
    danxuan_score = models.FloatField(default=0, verbose_name='单选分数')
    duoxuan_count = models.IntegerField(default=0, verbose_name='多选数量')
    duoxuan_score = models.FloatField(default=0, verbose_name='多选分数')
    panduan_count = models.IntegerField(default=0, verbose_name='判断数量')
    panduan_score = models.FloatField(default=0, verbose_name='判断分数')
    limit = models.IntegerField(default=0, verbose_name='限时(分钟)')
    total_score = models.FloatField(default=0, verbose_name='满分')
    pass_score = models.FloatField(default=0, verbose_name='及格分数')
    

    class Meta:
        verbose_name = '出题规则'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class WorkScope(CommonModel):
    name = models.CharField(max_length=200, unique=True, verbose_name='名称')
    subject = models.ForeignKey(Questioncat, verbose_name='所属学科', on_delete=models.CASCADE , related_name='workscope_subject')
    questioncat = models.ManyToManyField(Questioncat, verbose_name='所选科目')
    rule = models.ForeignKey(TestRule, on_delete=models.CASCADE, verbose_name='试卷结构')
    can_exam = models.BooleanField('是否可组织考试', default=False)
    sortnum = models.IntegerField('排序码', default=1)
    is_public = models.BooleanField('是否公开', default=True)
    # can_choose = models.BooleanField('客户是否可选择', default=False)
    courses = models.ManyToManyField(Course, verbose_name='所选课程')
    issue_year = models.PositiveSmallIntegerField('证书有效期', default=5)
    # can_examself = models.BooleanField('是否可自主考核', default=False)

    class Meta:
        verbose_name = '工作类别'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Paper(SoftCommonModel):
    name = models.CharField(max_length=200, verbose_name='名称')
    workscope = models.ForeignKey(WorkScope, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='名称')
    questions = models.ManyToManyField(Question, through='PaperQuestions')
    limit = models.IntegerField(default=0, verbose_name='限时(分钟)')
    total_score = models.FloatField(default=0, verbose_name='满分')
    pass_score = models.FloatField(default=0, verbose_name='及格分数')
    danxuan_count = models.IntegerField(default=0, verbose_name='单选数量')
    danxuan_score = models.FloatField(default=2, verbose_name='单选分数')
    duoxuan_count = models.IntegerField(default=0, verbose_name='多选数量')
    duoxuan_score = models.FloatField(default=4, verbose_name='多选分数')
    panduan_count = models.IntegerField(default=0, verbose_name='判断数量')
    panduan_score = models.FloatField(default=2, verbose_name='判断分数')
    class Meta:
        verbose_name = '押题卷'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
class PaperQuestions(CommonModel):
    paper = models.ForeignKey(Paper, on_delete=models.CASCADE, verbose_name='试卷')
    question = models.ForeignKey(Question, on_delete=models.CASCADE, verbose_name='试题')
    total_score = models.FloatField(default=0, verbose_name='满分')