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

class Order(models.Model):
    NORMAL = 1
    ABANDONED = 2
    WAIT_DISPATCH = 3
    STATUS_CHOICES = (
        (NORMAL, u'正常'),
        (ABANDONED, u'放弃'),
        (WAIT_DISPATCH, u'进度待分配'),
    )
    no = models.CharField(max_length=50, verbose_name='单号', blank=True)
    project = models.ManyToManyField(Option, verbose_name=u'项目', related_name='order_category', editable=False)
    stage_progress = models.ForeignKey(Option, verbose_name=u'阶段进度', related_name='order_stage_progress',
                                       on_delete=models.PROTECT)
    customer = models.ForeignKey(NewCustomer, related_name='order_customer', verbose_name=u"订单客户",
                                 on_delete=models.PROTECT)
    service_user = models.ForeignKey(settings.AUTH_USER_MODEL,related_name='order_service_user', verbose_name=u"服务人员",
                                     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)
    notes = models.CharField(max_length=500, verbose_name=u"备注", null=True)
    store = models.ForeignKey(Store, verbose_name=u'门店', related_name='order_store',
                              editable=False, on_delete=models.PROTECT)

    class Meta:
        db_table = "order"
        verbose_name = u"订单管理"
        ordering = ('-id',)
        default_permissions = ()
        permissions = [
            ('view_order', u'查看'),
            ('update_order_process', u'更新进度'),
            ('order_process_dispatch', u'进度分配'),
        ]

    def get_no(self):
        now = timezone.now()
        no = '%s%d-%s' % ('DD', self.store.id, now.strftime('%Y%m%d%H%M%S'))
        return no

    @staticmethod
    def get_instance_by_id(id):
        try:
            id = int(id)
        except:
            raise CustomError('无效的报修单ID')
        instance = Order.objects.filter(pk=id).first()
        if not instance:
            raise CustomError('未找到对应的报修工单')
        return instance


class ProgressDetails(models.Model):

    order = models.ForeignKey(Order, verbose_name=u'订单', on_delete=models.PROTECT)
    operation = models.CharField(max_length=500, verbose_name='操作')
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u"操作人", on_delete=models.PROTECT,
                             editable=False)
    notes = models.CharField(max_length=500, verbose_name='备注', null=True)
    operation_time = models.DateTimeField(verbose_name='操作时间', auto_now_add=True)

    class Meta:
        db_table = 'progress_details'
        verbose_name = '进度明细'
        ordering = ('-id', )
        default_permissions = ()
