from utils import model
from django.contrib.postgres.fields import JSONField
from django.db import models
from rest_framework.exceptions import ParseError
from simple_history.models import HistoricalRecords

from apps.certset.models import ImplementRule, UnitType, EvaluationItem
from apps.crm.models import Enterprise
from apps.system.models import CommonAModel, CommonBModel, Dict, User

# Create your models here.

class Project(CommonBModel):
    """
    认证项目
    """
    pass
    
class Application(CommonBModel):
    """
    主申请
    """
    status_choices = (
        ('草稿', '草稿'),
        ('已申请', '已申请'),
        ('已受理', '已受理'),
        ('进行中', '进行中'),
        ('已中止', '已中止'),
        ('已完成', '已完成')
    )
    result_choices = (
        ('合格', '合格'),
        ('不合格', '不合格'),
        ('有条件放行', '有条件放行')
    )
    number = models.CharField('申请编号', max_length = 100)
    apply_date = models.DateField('申请日期', null=True, blank=True)
    accept_date = models.DateField('受理日期', null=True, blank=True)

    applicant_v = JSONField(verbose_name='申请方')
    applicant = models.ForeignKey(Enterprise, related_name='application_applicant', on_delete=models.DO_NOTHING)
    manufacturer_v = JSONField(verbose_name='制造商', blank=True)
    manufacturer = models.ForeignKey(Enterprise, related_name='application_manufacturer', on_delete=models.SET_NULL, null=True, blank=True)
    factory_v = JSONField(verbose_name='生产厂', blank=True)
    factory = models.ForeignKey(Enterprise, related_name='application_factory', on_delete=models.SET_NULL, null=True, blank=True)
    cert_field = models.ForeignKey(Dict, verbose_name='认证领域', related_name='application_cert_field', on_delete=models.SET_NULL, null=True)
    status = models.CharField('申请状态', choices=status_choices, default='草稿', max_length=50)
    evdetail = models.ManyToManyField(EvaluationItem, verbose_name='评审详细记录', through='EvaluationDetail')
    evresult = models.CharField('评审结论', choices = result_choices, default='合格', max_length=50)
    evremark = models.TextField('备注', blank=True)

    class Meta:
        verbose_name = '认证主申请'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.number

class SubApplication(CommonBModel):
    """
    子申请
    """
    number = models.CharField('子申请编号', max_length = 100)
    application = models.ForeignKey(Application, on_delete=models.CASCADE, related_name='subapplication_application')
    cert_field = models.ForeignKey(Dict, verbose_name='认证领域', related_name='subapplication_cert_field', on_delete=models.DO_NOTHING)
    cccpv_class = models.ForeignKey(Dict, verbose_name='涉及CCC/PV分类', related_name='subapplication_cccpv_class', on_delete=models.DO_NOTHING)
    cnas_scopes = models.ManyToManyField(Dict, verbose_name='涉及cnas分类', related_name='subapplication_cnas_sopes')
    project = models.ForeignKey(Project, related_name='subapplication_project', on_delete=models.SET_NULL, null=True)

    class Meta:
        verbose_name = '认证子申请'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.number

class EvaluationDetail(CommonBModel):
    result_choices = (
        ('符合', '符合'),
        ('不符合', '不符合'),
        ('不适用', '不适用'),
        ('需说明', '需说明')
    )
    application = models.ForeignKey(Application, on_delete=models.CASCADE)
    item = models.ForeignKey(EvaluationItem, verbose_name='项目内容', on_delete=models.DO_NOTHING)
    result = models.CharField('评审结果',choices = result_choices, default='符合', max_length=50)
    remark = models.TextField('备注', blank=True)

    class Meta:
        verbose_name = '评审记录详情'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.application.number

class Unit(CommonBModel):
    """
    认证单元,一个单元一张证书
    """
    factory = models.ForeignKey(Enterprise, on_delete=models.DO_NOTHING)
    factory_v = JSONField(verbose_name='生产厂', blank=True)
    name = models.CharField('单元名称', max_length=200)
    description = models.TextField('单元描述', blank=True)
    unittype = models.ForeignKey(UnitType, verbose_name='单元类型', on_delete = models.SET_NULL, null=True, blank=True)
    subapplication = models.ForeignKey(Application, verbose_name='所属子申请', on_delete = models.CASCADE)

    class Meta:
        verbose_name = '认证单元'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

