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

from __future__ import division

from django.db import transaction
from django_logit import logit
from django_response import response
from paginator import pagination

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


@logit
def clerk_add(request):
    distributor_id = request.POST.get('distributor_id', '')
    clerk_name = request.POST.get('clerk_name', '')
    clerk_phone = request.POST.get('clerk_phone', '')

    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(distributor_id=distributor_id, status=True)
    except DistributorInfo.DoesNotExist:
        return response(ProductDistributorStatusCode.DISTRIBUTOR_NOT_FOUND)

    SaleclerkInfo.objects.create(
        brand_id=administrator.brand_id,
        brand_name=administrator.brand_name,
        distributor_id=distributor_id,
        distributor_name=distributor.distributor_name,
        clerk_name=clerk_name,
        clerk_phone=clerk_phone,
    )

    return response(200, 'Clerk Add Success', u'销售员添加成功')


@logit
@transaction.atomic
def clerk_delete(request):
    distributor_id = request.POST.get('distributor_id', '')
    clerk_id = request.POST.get('clerk_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:
        clerk = SaleclerkInfo.objects.select_for_update().get(brand_id=administrator.brand_id, clerk_id=clerk_id, status=True)
    except SaleclerkInfo.DoesNotExist:
        return response(SaleclerkStatusCode.CLERK_NOT_FOUND)

    clerk.user_status = SaleclerkInfo.UNVERIFIED
    clerk.status = False
    clerk.save()

    return response(200, 'Clerk Delete Success', u'销售员删除成功')


@logit
@transaction.atomic
def clerk_update(request):
    distributor_id = request.POST.get('distributor_id', '')
    clerk_id = request.POST.get('clerk_id', '')
    clerk_name = request.POST.get('clerk_name', '')
    clerk_phone = request.POST.get('clerk_phone', '')
    is_auth = request.POST.get('is_auth', '0')  # 1 为已授权，０ 为未授权

    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:
        clerk = SaleclerkInfo.objects.select_for_update().get(brand_id=administrator.brand_id, clerk_id=clerk_id, status=True)
    except SaleclerkInfo.DoesNotExist:
        return response(SaleclerkStatusCode.CLERK_NOT_FOUND)

    if clerk_name:
        clerk.clerk_name = clerk_name
    if clerk_phone:
        clerk.clerk_phone = clerk_phone
    if is_auth:
        clerk.user_status = clerk.is_auth = bool(int(is_auth))

    clerk.save()

    return response(200, 'Clerk Update Success', u'销售员更新成功')


@logit
def clerk_list(request):
    distributor_id = request.POST.get('distributor_id', '')
    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)

    clerks = SaleclerkInfo.objects.filter(brand_id=administrator.brand_id, status=True).order_by('-pk')
    if distributor_id:
        clerks = clerks.filter(distributor_id=distributor_id)
    count = clerks.count()
    clerks, left = pagination(clerks, page, num)
    clerks = [clerk.admindata for clerk in clerks]

    return response(200, 'Get Clerk List Success', u'获取销售员列表成功', {
        'clerks': clerks,
        'count': count,
        'left': left,
    })
