
from django.db import models
from apps.hrm.models import Employee
from apps.utils.models import CommonAModel, CommonBDModel, CommonBModel, BaseModel
from apps.system.models import User
from apps.wf.models import Ticket


# Create your models here.
class Visit(CommonBDModel):
    """TN: 来访项目"""
    V_PURPOSE_CHOICES = (
        (2, '售后服务'),
        (4, '业务洽谈'),
        (10, '参观'),
        (20, '拜访'),
        (30, '面试'),
        (40, '开会'),
        (50, '拉送货'),
        (60, '其他')
    )
    V_CREATE = 10
    V_AUDIT = 20
    V_ENTER = 30
    V_WORKING = 40
    V_DONE = 50
    V_CLOSE = 60
    V_STATE_CHOICES = (
        (10, '创建中'),
        (20, '审批中'),
        (30, '待入厂'),
        (40, '进行中'),
        (50, '已完成'),
        (60, '已关闭')
    )
    V_LEVEL_CHOICES = (
        (10, '一般'),
        (20, '重要')
    )
    purpose = models.PositiveSmallIntegerField('来访事由', choices=V_PURPOSE_CHOICES)
    level = models.PositiveSmallIntegerField('访问级别', choices=V_LEVEL_CHOICES)
    company = models.CharField('来访单位', max_length=100, null=True, blank=True)
    state = models.PositiveSmallIntegerField(choices=V_STATE_CHOICES, default=10)
    name = models.CharField('来访概述', max_length=50)
    description = models.TextField('来访详述', null=True, blank=True)
    visit_time = models.DateTimeField('来访时间')
    leave_time = models.DateTimeField('离开时间')
    count_people = models.PositiveSmallIntegerField('来访人数', null=True, blank=True)
    vehicle_photos = models.ManyToManyField('system.file', verbose_name='车辆照片', related_name='v_v_photos', blank=True)
    receptionist = models.ForeignKey(User, verbose_name='接待人', on_delete=models.CASCADE, null=True, blank=True)
    visitors = models.ManyToManyField('vm.visitor', through='vm.vpeople', related_name='v_visitors')
    ticket = models.ForeignKey(Ticket, verbose_name='关联工单',
                               on_delete=models.SET_NULL, related_name='visit_ticket', null=True, blank=True)
    # create_by 创建人


class Visitor(CommonAModel):
    """TN: 访客信息"""
    employee = models.ForeignKey(Employee, verbose_name='成员信息', on_delete=models.SET_NULL, null=True, blank=True)
    name = models.CharField('姓名', max_length=20)
    phone = models.CharField('手机号', max_length=11, null=True, blank=True)
    photo = models.CharField('证件照', max_length=1000, null=True, blank=True)
    id_number = models.CharField('身份证号', max_length=100, null=True, blank=True)
    visit = models.ForeignKey(Visit, verbose_name='最近所属访问项目', on_delete=models.SET_NULL,
                              null=True, blank=True)


class Vpeople(BaseModel):
    """TN: 来访项目与访客关系表"""
    visit = models.ForeignKey(Visit, verbose_name='关联访问项目', on_delete=models.CASCADE)
    visitor = models.ForeignKey(Visitor, verbose_name='访客', on_delete=models.CASCADE)
    is_main = models.BooleanField('是否主访人', default=False)

    return_date = models.DateField('返乡时间', null=True, blank=True)
    come_place = models.CharField('来源地', max_length=100, null=True, blank=True)
    trip_desc = models.CharField('行程轨迹', max_length=200, null=True, blank=True)
    measure = models.CharField('管控措施', max_length=20, null=True, blank=True)
    test_date = models.DateField('核酸检测日期', null=True, blank=True)
    is_reported = models.BooleanField('是否报备', default=True)
    health_code = models.CharField('健康码', max_length=1000, null=True, blank=True)
    travel_code = models.CharField('行程码', max_length=1000, null=True, blank=True)
    test_report = models.CharField('核酸报告', max_length=1000, null=True, blank=True)


