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

from django.shortcuts import render
from django.views.generic import ListView
from apps.AppInfo.models import MyUser, Equip, Group
from django import forms
from django.db.models import Q
import re
import datetime


# 分配设备
class EquipAllot(ListView):
    def get(self, request):
        # 接收当前用户
        # current_user = request.session.get('username')
        current_user = MyUser.objects.get(username="admin")
        # print("current_user:", current_user)
        # current_user = MyUser.objects.get(username=current_user)
        page = request.GET.get("page")
        uname = request.GET.get("uname")
        user_list = MyUser.objects.all()
        nums = user_list.count()
        return render(request, 'backstageNet/equipallot/equipallot.html', context={"nums": nums,"page":page,"uname":uname})

    def post(self, request):
        pass


def allot_user_list(request):
    if request.method == 'GET':
        page = int(request.GET.get('page'))
        print("page=", page)
        uname = request.GET.get("uname")
        # 接收当前用户
        # current_user = request.session.get('username')
        current_user = MyUser.objects.get(username="admin")
        # print("current_user:", current_user)
        # current_user = MyUser.objects.get(username=current_user)
        # if current_user.is_superuser == True:
        user_list = MyUser.objects.all().order_by(
            "-date_joined")[(10*(page-1)):(page*10)]
        if uname:
            user_list = MyUser.objects.filter(username__contains=uname).order_by(
            "-date_joined")[(10*(page-1)):(page*10)]
        return render(request, 'backstageNet/equipallot/allot_user_list.html', context={'user_list': user_list})

    elif request.method == 'POST':
        # current_user = request.session.get('username')
        # print("current_user:", current_user)
        current_user = MyUser.objects.get(username="admin")
        print("筛选：")
        f_name = request.POST.get('uname')
        page = int(request.POST.get('page'))
        print("page:", page)
        user_list = MyUser.objects.filter(username__contains=f_name)
        nums = user_list.count()
        user_list = user_list[(10*(page-1)):(page*10)]
        return render(request, 'backstageNet/equipallot/allot_user_list.html', context={'user_list': user_list, "nums": nums})


from django.views.decorators.csrf import csrf_exempt


@csrf_exempt
def allot_equip_list(request):

    # 当前用户已拥有的设备的列表：
    equip_list1 = []
    # 未被分配的设备的列表：
    equip_list2 = []
    # 当前页的设备id列表:
    equip_ids = []

    if request.method == 'GET':
        # 选中需要进行分配设备的用户后：显示的设备列表；
        uname = request.GET.get('uname')
        print("uname=", uname)
        uname_obj = MyUser.objects.get(username=uname)
        page = int(request.GET.get('page'))
        print("page=", page)
        # 接收当前用户
        # current_user = request.session.get('username')
        # print("current_user:", current_user)
        current_user = MyUser.objects.get(username="admin")
        if current_user.is_superuser == True:
            equip_list = Equip.objects.filter(
                Q(equip_user=None) | Q(equip_user=uname_obj))
        equip_list = list(equip_list)
        print('type equip_list:', type(equip_list))
        # print('equip_list:', equip_list)

        for i in equip_list:
            if i.equip_user != None:
                equip_list1.append(i)
            else:
                equip_list2.append(i)
        # print('equip_list1:', equip_list1)
        # print('equip_list2:', equip_list2)

        equip_list = equip_list1+equip_list2
        equip_list = equip_list[(10*(page-1)):(page*10)]
        for i in equip_list:
            equip_ids.append(i.equip_id)
        equip_ids = ','.join(equip_ids)

        return render(request, 'backstageNet/equipallot/allot_equip_list.html', context={'equip_list': equip_list, 'equip_ids': equip_ids, "uname": uname})

    elif request.method == 'POST':
        # current_user = request.session.get('username')
        # print("current_user:", current_user)
        current_user = MyUser.objects.get(username="admin")

        print("筛选：")
        uname = request.POST.get('uname')
        print("uname=", uname)
        uname_obj = MyUser.objects.get(username=uname)
        f_type = request.POST.get('type')
        f_id = request.POST.get('f_id')
        print("f_type:", f_type)
        print("f_id:", f_id)
        page = int(request.POST.get('page'))
        print("page:", page)
        # 000
        if f_id == None and f_type == '0':
            equip_list = Equip.objects.filter(
                Q(equip_user=None) | Q(equip_user=uname_obj))
        # 010
        elif f_id == None and f_type != '0':
            equip_list = Equip.objects.filter(Q(equip_user=None, equip_type=f_type) | Q(
                equip_user=uname_obj, equip_type=f_type))
        # 100
        elif f_id != None and f_type == '0':
            equip_list = Equip.objects.filter(
                Q(equip_user=None, equip_id__contains=f_id) | Q(equip_user=uname_obj, equip_id__contains=f_id))
        # 110
        elif f_id != None and f_type != '0':
            equip_list = Equip.objects.filter(Q(equip_user=None, equip_id__contains=f_id, equip_type=f_type) | Q(
                equip_user=uname_obj, equip_id__contains=f_id, equip_type=f_type))
        nums = equip_list.count()
        equip_list = equip_list[(10*(page-1)):(page*10)]
        for i in equip_list:
            equip_ids.append(i.equip_id)
            
        equip_ids = ','.join(equip_ids)
        return render(request, 'backstageNet/equipallot/allot_equip_list.html', context={'equip_list': equip_list, 'equip_ids': equip_ids, "nums": nums})


# 进入对应的用户分配设备页面
def allot(request):
    if request.method == 'GET':
        # current_user = request.session.get('username')
        current_user = MyUser.objects.get(username="admin")
        print("current_user:", current_user)
        # current_user = MyUser.objects.get(username=current_user)
        uname = request.GET.get('uname')
        uname_obj = MyUser.objects.get(username=uname)
        print("uname=", uname)
        page = request.GET.get("currpage")
        f_name = request.GET.get("f_name")
        nums = Equip.objects.filter(
            Q(equip_user=None) | Q(equip_user=uname_obj)).count()
        print('nums:', nums)
        return render(request, 'backstageNet/equipallot/allot.html', context={"nums": nums, "uname": uname,"currpage":page,"f_name":f_name})

    if request.method == 'POST':
        # current_user = request.session.get('username')
        # print("current_user:", current_user)
        current_user = MyUser.objects.get(username="admin")
        uname = request.POST.get('uname')
        uname_obj = MyUser.objects.get(username=uname)
        print("uname=", uname)
        # 获得当前页的全部id：equip_ids
        equip_ids = request.POST.get('equip_ids')
        # print("equip_ids=", equip_ids)
        equip_ids = equip_ids.split(',')
        button = request.POST.get('submit')
        if button == "fenpei":
            # 获得选中的复选框:
            check_box_list = request.POST.getlist('check_box_list')
            print('check_box_list:', check_box_list)
            no_check_box_list = []

            for i in equip_ids:
                if i in check_box_list:
                    pass
                else:
                    no_check_box_list.append(i)

            # 分配复选框选中的设备：
            for checked_id in check_box_list:
                choose_equip = Equip.objects.get(equip_id=checked_id)
                # 如果选中的设备没有用户：即选中的设备 设备用户为None：
                if choose_equip.equip_user == None:  # 只有当选中的设备id对应的设备  没有用户时才分配
                    choose_equip.equip_user = uname_obj
                    choose_equip.save()  # 保存到数据库

            # 取消分配:给当前用户取消已分配的设备；

            for a in no_check_box_list:
                choose_equip = Equip.objects.get(equip_id=a)
                choose_equip.equip_user = None  # 把设备的设备用户改为None，表示取消分配
                choose_equip.save()
            equip_list = Equip.objects.filter(
                Q(equip_user=None) | Q(equip_user=uname_obj))
            user_equip_list = Equip.objects.filter(equip_user=uname_obj)
            equip_type_list = []
            for i in user_equip_list:
                equip_type_list.append(i.equip_type.type_id)
            print('abcdef',equip_type_list)
            groups = Group.objects.filter(user=uname_obj)
            nums = equip_list.count()
            return render(request, 'backstageNet/equipallot/allot.html', context={'equip_list': equip_list, 'nums': nums, "uname": uname, 'allot_note': '设备分配成功！'})
