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

from __future__ import division

from django.conf import settings
from django.db import transaction
from django.shortcuts import render
from django_response import response
from logit import logit

from account.models import SaleclerkInfo
from integral.models import SaleclerkIntegralIncomeExpensesInfo
from product.models import ProductCodeSubmitLogInfo, ProductInfo, ProductModelInfo
from utils.error.errno_utils import ProductModelStatusCode, ProductStatusCode, SaleclerkStatusCode


def clerk_sale_oauth(request):
    unionid = request.GET.get('unionid', '')

    models = ProductModelInfo.objects.filter(status=True)
    models = [model.data for model in models]

    try:
        clerk = SaleclerkInfo.objects.get(unionid=unionid)
    except SaleclerkInfo.DoesNotExist:
        clerk = None

    return render(request, 'page/clerk_sale.html', {
        'domain': settings.DOMAIN,
        'models': models,
        'clerk_info': clerk and clerk.data,
    })


@logit
@transaction.atomic
def clerk_sale_submit_api(request):
    """ 店员信息提交 """
    step = int(request.POST.get('step', 1))

    clerk_id = request.POST.get('clerk_id', '')
    model_id = request.POST.get('model_id', '')
    mount = request.POST.get('mount', 'NO')
    code = request.POST.get('code', '')
    name = request.POST.get('name', '')
    sex = int(request.POST.get('sex', 1))
    age = int(request.POST.get('age', 1))
    phone = request.POST.get('phone', '')

    # 店员是否存在
    try:
        clerk = SaleclerkInfo.objects.select_for_update().get(clerk_id=clerk_id)
    except SaleclerkInfo.DoesNotExist:
        return response(SaleclerkStatusCode.CLERK_NOT_FOUND)

    # 店员是否激活
    if clerk.user_status != SaleclerkInfo.ACTIVATED:
        return response(SaleclerkStatusCode.CLERK_NOT_ACTIVATED)

    # 型号是否存在
    try:
        model = ProductModelInfo.objects.get(model_id=model_id)
    except ProductModelInfo.DoesNotExist:
        return response(ProductModelStatusCode.MODEL_NOT_FOUND)

    # 记录销售提交记录
    ProductCodeSubmitLogInfo.objects.create(
        model_id=model.model_id,
        model_name=model.model_name,
        mount=mount,
        code=code,
        franchiser_id=clerk.franchiser_id,
        clerk_id=clerk.clerk_id,
        consumer_name=name,
        consumer_sex=sex,
        consumer_age=age,
        consumer_phone=phone,
    )

    # 产品是否存在
    try:
        product = ProductInfo.objects.select_for_update().get(model_id=model_id, mount=mount, code=code)
    except ProductInfo.DoesNotExist:
        return response(ProductStatusCode.PRODUCT_NOT_FOUND)

    # 产品是否使用
    if step == 1 and product.code_status:
        return response(ProductStatusCode.PRODUCT_HAS_USED)
    if step == 2 and not product.code_status:
        return response(ProductStatusCode.PRODUCT_NOT_USED)

    # 产品使用
    product.code_status = True
    product.integral_status = True
    product.franchiser_id = clerk.franchiser_id
    product.clerk_id = clerk.clerk_id
    if 'name' in request.POST:
        product.consumer_name = name
    if 'sex' in request.POST:
        product.consumer_sex = sex
    if 'age' in request.POST:
        product.consumer_age = age
    if 'phone' in request.POST:
        product.consumer_phone = phone
    product.save()

    # 店员积分
    clerk.integral += product.integral
    clerk.save()

    # 店员积分记录
    SaleclerkIntegralIncomeExpensesInfo.objects.create(
        franchiser_id=clerk.franchiser_id,
        clerk_id=clerk.clerk_id,
        type=SaleclerkIntegralIncomeExpensesInfo.INCOME,
        code=code,
        integral=product.integral,
        left_integral=clerk.integral,
    )

    return response(200, 'Submit Success', u'提交成功', {})
