from django.db import models
from utils.model import BaseModel
from apps.system.models import CommonAModel, CommonBModel, Organization, User, Dict, File
# Create your models here.
class InspectTemplate(CommonAModel):
    """
    检查模板
    """
    name = models.CharField('模板名称', max_length=200)

class InspectItem(CommonAModel):
    """
    检查条款
    """
    type_choices = (
        ('合规', '合规'),
        ('体系', '体系'),
    )
    name = models.TextField('名称')
    cate = models.ForeignKey(Dict, on_delete=models.SET_NULL, null=True, blank=True)
    type = models.CharField('类型', max_length=50, choices=type_choices, default='合规')
    require = models.TextField('取证要求', null=True, blank=True)
    sortnum = models.IntegerField('序号', default=1)
    template = models.ForeignKey(InspectTemplate, on_delete=models.CASCADE, verbose_name='所属模板')
    class Meta:
        verbose_name = '检查清单'
        verbose_name_plural = verbose_name
    
class InspectTask(CommonAModel):
    """
    巡查任务
    """
    state_choices = (
        ('待发布', '待发布'),
        ('执行中', '执行中'),
        ('已完成', '已完成'),
    )

    name = models.CharField('任务名', max_length=100)
    start_date = models.DateField('巡查开始日期')
    end_date = models.DateField('巡查结束日期')
    state = models.CharField('任务状态', max_length=50, choices=state_choices, default='待发布')
    template = models.ForeignKey(InspectTemplate, on_delete=models.CASCADE, verbose_name='使用模板', null=True, blank=True)

    class Meta:
        verbose_name = '巡查任务'
        verbose_name_plural = verbose_name

class SubInspectTask(CommonAModel):
    """
    巡查子任务
    """
    state_choices = (
        ('待发布', '待发布'),
        ('执行中', '执行中'),
        ('已完成', '已完成'),
    )
    name = models.CharField('子任务名', max_length=100)
    inspecttask = models.ForeignKey(InspectTask, on_delete=models.CASCADE, verbose_name='所属任务')
    state = models.CharField('任务状态', max_length=50, choices=state_choices, default='待发布')

    class Meta:
        verbose_name = '巡查子任务'
        verbose_name_plural = verbose_name

class InspectTeam(BaseModel):
    """
    巡查组构成
    """
    type_choices = (
        ('组长', '组长'),
        ('组员', '组员'),
    )
    member = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='成员')
    type = models.CharField('成员类型', max_length=50, choices=type_choices, default='组员')
    subtask = models.ForeignKey(SubInspectTask, on_delete=models.CASCADE, verbose_name='关联子任务', related_name='team_subtask')

    class Meta:
        verbose_name = '巡查组'
        verbose_name_plural = verbose_name

class InspectDept(BaseModel):
    """
    受巡查组织
    """
    state_choices = (
        ('待检查', '待检查'),
        ('检查中', '检查中'),
        ('已提交', '已提交'),
    )
    dept = models.ForeignKey(Organization, on_delete=models.CASCADE, verbose_name='受巡查组织')
    subtask = models.ForeignKey(SubInspectTask, on_delete=models.CASCADE, verbose_name='关联子任务', related_name='dept_subtask')
    start_date = models.DateField('开始检查日期', null=True, blank=True)
    note = models.TextField('检查说明', null=True, blank=True)
    state = models.CharField('任务状态', max_length=50, choices=state_choices, default='待检查')

    class Meta:
        verbose_name = '受巡查组织'
        verbose_name_plural = verbose_name

class InspectRecord(CommonAModel):
    """
    巡查记录
    """
    result_choices = (
        ('发现', '发现'),
        ('未发现', '未发现'),
        ('不适用', '不适用')
    )
    item = models.ForeignKey(InspectItem, on_delete=models.CASCADE, verbose_name='检查项')
    dept = models.ForeignKey(Organization, on_delete=models.CASCADE, verbose_name='受检查单位')
    result = models.CharField('检查结果', max_length=50, choices=result_choices, null=True, blank=True)
    note = models.TextField('检查说明', null=True, blank=True)
    imgs = models.ManyToManyField(File, verbose_name="检查图片", blank=True)
    subtask = models.ForeignKey(SubInspectTask, on_delete=models.CASCADE, verbose_name='关联子任务')
    checker = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='检查人', null=True, blank=True)
    checked = models.BooleanField('是否检查', default=False)
    check_time = models.DateTimeField('检查时间', null=True, blank=True)

    class Meta:
        verbose_name = '巡查记录'
        verbose_name_plural = verbose_name

class Document(CommonAModel):
    
    name = models.CharField('名称', max_length=100)
    description = models.TextField('描述', default="", blank=True)
    file = models.ForeignKey(File, on_delete=models.CASCADE)

    class Meta:
        verbose_name = '质量文件库'
        verbose_name_plural = verbose_name