# -*- 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
from sales.models import SalesResponsibilityInfo
from utils.error.errno_utils import AdministratorStatusCode


@logit
def sr_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)

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

    return response(200, 'Get SR List Success', u'获取销售担当列表成功', {
        'srs': srs,
        'count': count,
        'left': left,
    })
