# -*- coding: utf-8 -*-

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django_models_ext import BaseModelMixin, upload_file_url, upload_path
from shortuuidfield import ShortUUIDField


class SalesResponsibilityInfo(BaseModelMixin):
    REFUSED = -1
    UNVERIFIED = 0
    ACTIVATED = 1
    DISABLED = 2
    DELETED = 3
    ASSIGN = 10

    USER_STATUS_TUPLE = (
        (REFUSED, u'已拒绝'),
        (UNVERIFIED, u'未验证'),
        (ACTIVATED, u'已激活'),
        (DISABLED, u'已禁用'),
        (DELETED, u'已删除'),
        (ASSIGN, u'已分配'),
    )

    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')

    sr_id = ShortUUIDField(_(u'sr_id'), max_length=32, blank=True, null=True, help_text=u'销售担当唯一标识', db_index=True, unique=True)

    user_id = models.CharField(_(u'user_id'), max_length=32, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
    unionid = models.CharField(_(u'unionid'), max_length=32, blank=True, null=True, help_text=u'微信 UnionID', db_index=True)
    openid = models.CharField(_(u'openid'), max_length=32, blank=True, null=True, help_text=u'微信 OpenID', db_index=True)

    name = models.CharField(_(u'name'), max_length=255, blank=True, null=True, help_text=u'销售担当姓名')
    phone = models.CharField(_(u'phone'), max_length=11, blank=True, null=True, help_text=u'销售担当联系电话')
    avatar = models.CharField(_(u'avatar'), max_length=255, blank=True, null=True, help_text=u'销售担当头像')

    user_status = models.IntegerField(_(u'user_status'), choices=USER_STATUS_TUPLE, default=UNVERIFIED, help_text=u'用户状态', db_index=True)
    refused_reason = models.TextField(_(u'refused_reason'), blank=True, null=True, help_text=u'审核拒绝原因')

    is_auth = models.BooleanField(_(u'is_auth'), default=False, help_text=_(u'是否已授权'), db_index=True)

    is_super = models.BooleanField(_(u'is_super'), default=False, help_text=_(u'是否超级销售担当'), db_index=True)

    class Meta:
        verbose_name = _(u'销售担当信息')
        verbose_name_plural = _(u'销售担当信息')

        unique_together = (
            ('unionid', 'brand_id'),
        )

    def __unicode__(self):
        return u'{}-{}'.format(self.name, self.phone)

    @property
    def base_data(self):
        return {
            'sr_id': self.sr_id,
            'is_sr': True,
            'is_super': self.is_super,
        }

    @property
    def admindata(self):
        return {
            'brand_id': self.brand_id,
            'brand_name': self.brand_name,
            'sr_id': self.sr_id,
            'name': self.name,
            'phone': self.phone,
            'status': self.user_status,
            'refused_reason': self.refused_reason,
            'is_auth': self.is_auth,
        }

    data = admindata


class SalesResponsibilityInfoModelsSaleStatisticInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)

    # 销售担当，供 SuperSalesResponsibilityInfoModelsSaleStatisticInfo 统计使用
    sr_id = models.CharField(_(u'sr_id'), max_length=32, blank=True, null=True, help_text=u'销售担当唯一标识', db_index=True)

    # 经销商
    distributor_id = models.CharField(_(u'distributor_id'), max_length=32, help_text=u'经销商唯一标识', db_index=True)
    distributor_name = models.CharField(_(u'distributor_name'), max_length=255, blank=True, null=True, help_text=u'经销商名称')

    # 型号
    model_id = models.CharField(_(u'model_id'), max_length=32, help_text=u'型号唯一标识', db_index=True)
    model_name = models.CharField(_(u'model_name'), max_length=255, blank=True, null=True, help_text=u'型号名称', db_index=True)
    is_important = models.BooleanField(_(u'is_important'), default=True, help_text=_(u'是否重要型号'), db_index=True)

    ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True)  # 例：20171208, tc.local_string(format='%Y%m%d'), 0 为全部

    today_num = models.IntegerField(_(u'today_num'), default=0, help_text=u'支数(今日)')
    yesterday_num = models.IntegerField(_(u'yesterday_num'), default=0, help_text=u'支数(昨日)')
    current_month = models.IntegerField(_(u'current_month'), default=0, help_text=u'支数(本月)')
    last_month = models.IntegerField(_(u'last_month'), default=0, help_text=u'支数(上月)')

    position = models.IntegerField(_(u'position'), default=1, help_text=u'排序')

    class Meta:
        verbose_name = _(u'[销售担当]型号销量统计')
        verbose_name_plural = _(u'[销售担当]型号销量统计')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def data(self):
        return {
            'model_id': self.model_id,
            'model_name': self.model_name,
            'is_important': self.is_important,
            'today_num': self.today_num,
            'yesterday_num': self.yesterday_num,
            'current_month': self.current_month,
            'last_month': self.last_month,
        }


class SuperSalesResponsibilityInfoModelsSaleStatisticInfo(BaseModelMixin):
    brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)

    # 销售担当
    sr_id = models.CharField(_(u'sr_id'), max_length=32, blank=True, null=True, help_text=u'销售担当唯一标识', db_index=True)
    sr_name = models.CharField(_(u'sr_name'), max_length=255, blank=True, null=True, help_text=u'销售担当名称')
    sr_avatar = models.CharField(_(u'sr_avatar'), max_length=255, blank=True, null=True, help_text=u'销售担当头像')

    ymd = models.IntegerField(_(u'ymd'), default=0, help_text=u'年月日', db_index=True)  # 例：20171208, tc.local_string(format='%Y%m%d'), 0 为全部

    today_num = models.IntegerField(_(u'today_num'), default=0, help_text=u'支数(今日)')
    yesterday_num = models.IntegerField(_(u'yesterday_num'), default=0, help_text=u'支数(昨日)')
    current_month = models.IntegerField(_(u'current_month'), default=0, help_text=u'支数(本月)')
    last_month = models.IntegerField(_(u'last_month'), default=0, help_text=u'支数(上月)')

    position = models.IntegerField(_(u'position'), default=1, help_text=u'排序')

    class Meta:
        verbose_name = _(u'[超级销售担当]销售担当销量统计')
        verbose_name_plural = _(u'[超级销售担当]销售担当销量统计')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def data(self):
        return {
            'sr_id': self.sr_id,
            'sr_name': self.sr_name,
            'sr_avatar': self.sr_avatar,
            'today_num': self.today_num,
            'yesterday_num': self.yesterday_num,
            'current_month': self.current_month,
            'last_month': self.last_month,
        }
