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

from __future__ import division

from django_logit import logit
from django_response import response
from paginator import pagination

from mch.models import AdministratorInfo, DistributorInfo
from utils.error.errno_utils import AdministratorStatusCode, ProductDistributorStatusCode


@logit
def distributor_add(request):
    distributor_name = request.POST.get('distributor_name', '')
    province_code = request.POST.get('province_code', '')
    province_name = request.POST.get('province_name', '')

    admin_id = request.session.get('admin_id')

    try:
        administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
    except AdministratorInfo.DoesNotExist:
        return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)

    DistributorInfo.objects.create(
        brand_id=administrator.brand_id,
        distributor_name=distributor_name,
        distributor_province_code=province_code,
        distributor_province_name=province_name,
    )

    return response(200, 'Distributor Add Success', u'经销商添加成功')


@logit
def distributor_delete(request):
    distributor_id = request.POST.get('distributor_id', '')

    admin_id = request.session.get('admin_id')

    try:
        administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
    except AdministratorInfo.DoesNotExist:
        return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)

    try:
        distributor = DistributorInfo.objects.get(brand_id=administrator.brand_id, distributor_id=distributor_id, status=True)
    except DistributorInfo.DoesNotExist:
        return response(ProductDistributorStatusCode.DISTRIBUTOR_NOT_FOUND)

    distributor.status = False
    distributor.save()

    return response(200, 'Distributor Delete Success', u'经销商删除成功')


@logit
def distributor_update(request):
    distributor_id = request.POST.get('distributor_id', '')
    distributor_name = request.POST.get('distributor_name', '')
    province_code = request.POST.get('province_code', '')
    province_name = request.POST.get('province_name', '')

    admin_id = request.session.get('admin_id')

    try:
        administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
    except AdministratorInfo.DoesNotExist:
        return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)

    try:
        distributor = DistributorInfo.objects.get(brand_id=administrator.brand_id, distributor_id=distributor_id, status=True)
    except DistributorInfo.DoesNotExist:
        return response(ProductDistributorStatusCode.DISTRIBUTOR_NOT_FOUND)

    if distributor_name:
        distributor.distributor_name = distributor_name
    if province_code:
        distributor.distributor_province_code = province_code
    if province_name:
        distributor.distributor_province_name = province_name

    distributor.save()

    return response(200, 'Distributor Update Success', u'经销商更新成功')


@logit
def distributor_list(request):
    page = request.POST.get('page', 1)
    num = request.POST.get('num', 20)

    admin_id = request.session.get('admin_id')

    try:
        administrator = AdministratorInfo.objects.get(admin_id=admin_id, user_status=AdministratorInfo.ACTIVATED, status=True)
    except AdministratorInfo.DoesNotExist:
        return response(AdministratorStatusCode.ADMINISTRATOR_NOT_FOUND)

    distributors = DistributorInfo.objects.filter(brand_id=administrator.brand_id, status=True).order_by('-pk')
    count = distributors.count()
    distributors, left = pagination(distributors, page, num)
    distributors = [distributor.admindata for distributor in distributors]

    return response(200, 'Get Distributor List Success', u'获取经销商列表成功', {
        'distributors': distributors,
        'count': count,
        'left': left,
    })
