# coding=utf-8
from django.db import models
from django.conf import settings
from django.utils import timezone
from utils.exceptions import CustomError
from apps.option.models import Option
from apps.agent.models import Store

class ReportCustomer(models.Model):
    NOT_CHECKED = 0
    CHECKED = 1
    REPEAT_REPORT = 2
    STATUS_CHOICES = (
        (NOT_CHECKED, '待审核'),
        (CHECKED, '已分配'),
        (REPEAT_REPORT, '重复报备')
    )

    name = models.CharField(max_length=20, verbose_name=u"姓名")
    tel = models.CharField(max_length=15, verbose_name=u'电话')
    gender = models.PositiveSmallIntegerField(choices=settings.GENDER_CHOICES, verbose_name=u'性别',
                                              default=settings.MALE)
    village = models.CharField(max_length=200, verbose_name=u'小区')
    address = models.CharField(max_length=200, verbose_name=u'地址')
    source = models.ForeignKey(Option, verbose_name=u'来源', related_name='report_customer_source', on_delete=models.PROTECT)
    project = models.ManyToManyField(Option, verbose_name=u'项目', related_name='report_customer_category', editable=False,
                                     null=True,blank=True)
    notes = models.CharField(max_length=500, verbose_name=u"备注", null=True)
    report_status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, verbose_name=u'报备状态', default=NOT_CHECKED)
    check_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u'审核人', related_name='report_customer_check_user',
                                   on_delete=models.PROTECT, null=True)
    check_time = models.DateTimeField(verbose_name=u'审核时间', null=True)
    create_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u'添加人', related_name='report_customer_create_user',
                                    editable=False, on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name=u'添加时间', auto_now_add=True, editable=False)
    store = models.ForeignKey(Store, verbose_name=u'门店', related_name='report_customer_store',
                                    editable=False, on_delete=models.PROTECT)

    class Meta:
        db_table = 'report_customer'
        verbose_name = u'客户报备'
        ordering = ['-id']
        default_permissions = ()
        permissions = [
            ('view_report_customer', u'查看'),
            ('add_report_customer', u'添加'),
            ('delete_report_customer', u'删除'),
            ('check_report_customer', u'审核'),
        ]


class NewCustomer(models.Model):
    NORMAL = 1
    ABANDONED = 2
    STATUS_CHOICES = (
        (NORMAL, u'正常'),
        (ABANDONED, u'放弃'),
    )

    report_customer = models.ForeignKey(ReportCustomer, verbose_name=u'报备客户信息', related_name='customer_source',
                                        on_delete=models.PROTECT)
    potential_level = models.ForeignKey(Option, verbose_name=u'潜客级别', related_name='potential_level', null=True,
                                        on_delete=models.PROTECT)
    stage_progress = models.ForeignKey(Option, verbose_name=u'阶段进度', related_name='stage_progress',
                                       on_delete=models.PROTECT)
    track_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u'跟踪人', related_name='track_user',
                                   editable=False, on_delete=models.PROTECT)
    end_time = models.DateTimeField(verbose_name=u'最后跟踪时间', null=True)
    next_time = models.DateField(verbose_name=u'下次跟踪时间', null=True)
    create_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u'建档人', related_name='newcustomer_create_user',
                                    editable=False, on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name=u'建档时间', auto_now_add=True, editable=False)
    status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, verbose_name=u"状态", default=NORMAL)

    class Meta:
        db_table = 'new_customer'
        verbose_name = u'潜客跟踪'
        ordering = ['-id']
        default_permissions = ()
        permissions = [
            ('view_new_customer', u'查看'),
            ('add_review', u'跟踪'),
        ]

class Review(models.Model):
    DEFAULT = 0
    KEEPUP = 1
    ABANDON = 2
    CHECK_STATUS_CHOICES = (
        (DEFAULT, u'未审核'),
        (KEEPUP, u'继续跟踪'),
        (ABANDON, u'放弃'),
    )

    customer = models.ForeignKey(NewCustomer, related_name='review_ref_customer', verbose_name=u"客户",
                                  on_delete=models.PROTECT)
    description = models.CharField(max_length=5000, verbose_name=u"回访情况")
    instruction = models.CharField(max_length=200, verbose_name=u"申请支援或放弃", blank=True, null=True)
    is_giveup = models.BooleanField(verbose_name=u"请求放弃", default=False)
    is_entry = models.BooleanField(verbose_name=u"是否进店", default=False)
    store = models.ForeignKey(Store, verbose_name=u'门店', on_delete=models.PROTECT)
    create_user = models.ForeignKey(settings.AUTH_USER_MODEL,related_name='review_ref_user', verbose_name=u"创建人",
                                    on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name=u"添加时间", default=timezone.now, editable=False)
    check_status = models.PositiveSmallIntegerField(choices=CHECK_STATUS_CHOICES, verbose_name=u"审核状态", default=DEFAULT)
    check_comment = models.CharField(max_length=200, verbose_name=u"审核批示", blank=True, null=True)
    check_user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='review_ref_checkuser', verbose_name=u"审核人",
                                   editable=False, blank=True, null=True, on_delete=models.PROTECT)
    check_time = models.DateTimeField(verbose_name=u"审核时间", editable=False, blank=True, null=True)
    next_time = models.DateTimeField(verbose_name=u"下次回访时间", editable=False, blank=True, null=True)


    class Meta:
        db_table = "customer_review"
        verbose_name = u"客户跟踪记录"
        ordering = ('-id',)
        permissions = (
        )
