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

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django_models_ext import BaseModelMixin, upload_file_path, upload_file_url, upload_path
from TimeConvert import TimeConvert as tc

from mch.models import DistributorInfo, ModelInfo
from sales.models import SalesResponsibilityInfo
from utils.qiniucdn import qiniu_file_url


class SaleclerkIntegralIncomeExpensesInfo(BaseModelMixin):
    INCOME = 0
    EXPENSE = 1
    UNFREEZE = 2

    TYPE = (
        (INCOME, u'收入'),
        (EXPENSE, u'支出'),
        (UNFREEZE, u'解冻'),
    )

    clerk_id = models.CharField(_(u'clerk_id'), max_length=32, blank=True, null=True, help_text=u'店员唯一标识', db_index=True)

    type = models.IntegerField(_(u'type'), choices=TYPE, default=INCOME, help_text=u'收支类别', db_index=True)

    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'品牌名称')

    model_id = models.CharField(_(u'model_id'), max_length=32, blank=True, null=True, help_text=u'型号唯一标识', db_index=True)
    model_name = models.CharField(_(u'model_name'), max_length=255, blank=True, null=True, help_text=u'型号名称')

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

    code = models.CharField(_(u'code'), max_length=32, blank=True, null=True, help_text=u'机身码', db_index=True)

    consumer_name = models.CharField(_(u'consumer_name'), max_length=32, blank=True, null=True, help_text=u'消费者姓名')
    consumer_phone = models.CharField(_(u'consumer_phone'), max_length=32, blank=True, null=True, help_text=u'消费者手机号')

    lat = models.FloatField(_(u'lat'), default=1.0, help_text=u'纬度')
    lon = models.FloatField(_(u'lon'), default=1.0, help_text=u'经度')

    image = models.FileField(_(u'image'), upload_to=upload_path, blank=True, null=True, help_text=u'图片')

    integral = models.IntegerField(_(u'integral'), default=0, help_text=u'增减积分')
    left_integral = models.IntegerField(_(u'left_integral'), default=0, help_text=u'积分增减后数量(分)')

    remark = models.CharField(_(u'remark'), max_length=255, blank=True, null=True, help_text=u'备注')

    test_user = models.BooleanField(_(u'test_user'), default=False, help_text=u'是否为测试用户')

    class Meta:
        verbose_name = _(u'saleclerkintegralincomeexpensesinfo')
        verbose_name_plural = _(u'saleclerkintegralincomeexpensesinfo')

    def __unicode__(self):
        return '%d' % self.pk

    @property
    def model_info(self):
        try:
            info = ModelInfo.objects.get(model_id=self.model_id).fulldata
        except ModelInfo.DoesNotExist:
            info = {}
        return info

    @property
    def data(self):
        return {
            'type': self.type,
            'integral': self.integral,
            'model_info': self.model_info,
            'modelName': self.model_name,
            'serialNo': self.code,
            'created_at': self.created_at,
        }


class SaleclerkSubmitLogInfo(BaseModelMixin):
    brand_pk = models.IntegerField(_(u'brand_pk'), default=0, help_text=u'品牌PK', db_index=True)
    brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')

    model_pk = models.IntegerField(_(u'model_pk'), default=0, help_text=u'型号PK', db_index=True)
    model_name = models.CharField(_(u'model_name'), max_length=255, blank=True, null=True, help_text=u'型号名称')
    model_uni_name = models.CharField(_(u'model_uni_name'), max_length=255, blank=True, null=True, help_text=u'型号统称')

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

    clerk_id = models.CharField(_(u'clerk_id'), max_length=32, blank=True, null=True, help_text=u'店员唯一标识', db_index=True)
    clerk_name = models.CharField(_(u'clerk_name'), max_length=32, blank=True, null=True, help_text=u'店员名称')

    code = models.CharField(_(u'code'), max_length=32, blank=True, null=True, help_text=u'机身码', db_index=True)

    consumer_name = models.CharField(_(u'consumer_name'), max_length=32, blank=True, null=True, help_text=u'消费者姓名')
    consumer_phone = models.CharField(_(u'consumer_phone'), max_length=32, blank=True, null=True, help_text=u'消费者手机号')

    lat = models.FloatField(_(u'lat'), default=1.0, help_text=u'纬度')
    lon = models.FloatField(_(u'lon'), default=1.0, help_text=u'经度')

    image = models.FileField(_(u'image'), upload_to=upload_path, max_length=255, blank=True, null=True, help_text=u'图片')
    code_image = models.FileField(_(u'code_image'), upload_to=upload_path, max_length=255, blank=True, null=True, help_text=u'序列号图片')

    has_scan = models.BooleanField(_(u'has_scan'), default=False, help_text=u'是否被消费者扫过')

    remark = models.CharField(_(u'remark'), max_length=255, blank=True, null=True, help_text=u'备注')

    dupload = models.BooleanField(_(u'dupload'), default=False, help_text=u'是否为重复提交')

    test_user = models.BooleanField(_(u'test_user'), default=False, help_text=u'是否为测试用户')
    test_sn = models.BooleanField(_(u'test_sn'), default=False, help_text=u'是否为测试序列号')

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

    trackingNo = models.CharField(_(u'trackingNo'), max_length=32, blank=True, null=True, help_text=u'快递单号')

    is_wxwork = models.BooleanField(_(u'is_wxwork'), default=False, help_text=u'是否为企业微信端')

    is_staff_delete = models.BooleanField(_(u'is_staff_delete'), default=False, help_text=u'是否管理员删除')
    code_version = models.IntegerField(_(u'code_version'), default=1, help_text=u'统览码版本', db_index=True)

    is_upload_qiniu = models.BooleanField(_(u'is_upload_qiniu'), default=False, help_text=u'是否已上传七牛')

    class Meta:
        verbose_name = _(u'销售员扫码出库记录')
        verbose_name_plural = _(u'销售员扫码出库记录')

    def __unicode__(self):
        return '%d' % self.pk

    @property
    def image_path(self):
        return upload_file_path(self.image)

    @property
    def image_url(self):
        return qiniu_file_url(self.image.name, bucket='tamron') if self.is_upload_qiniu else upload_file_url(self.image)

    @property
    def code_image_path(self):
        return upload_file_path(self.code_image)

    @property
    def code_image_url(self):
        return qiniu_file_url(self.code_image.name, bucket='tamron') if self.is_upload_qiniu else upload_file_url(self.code_image)

    @property
    def admindata(self):
        try:
            distributor = DistributorInfo.objects.get(distributor_id=self.distributor_id)
        except DistributorInfo.DoesNotExist:
            distributor = None
        if distributor:
            try:
                sr = SalesResponsibilityInfo.objects.get(sr_id=distributor.sr_id)
            except SalesResponsibilityInfo.DoesNotExist:
                sr = None
        else:
            sr = None
        if sr:
            office = distributor.office
            sr_name = sr.name
        else:
            office = ''
            sr_name = ''

        return {
            'pk': self.pk,
            'model_name': self.model_name,
            'model_uni_name': self.model_uni_name,
            'code': self.code,
            'trackingNo': self.trackingNo,
            'is_staff_delete': self.is_staff_delete,
            'code_version': self.code_version,
            'clerk_name': self.clerk_name,
            'distributor_id': self.distributor_id,
            'distributor_name': self.distributor_name,
            'office': office,
            'sr_name': sr_name,
            'remark': self.remark,
            'dupload': self.dupload,
            'has_scan': self.has_scan,
            'image': self.image_url,
            'code_image': self.code_image_url,
            'created_at': tc.local_string(utc_dt=self.created_at),
        }