# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from cmdb.models import Service
from cmdb.models import HostPhysical
from cmdb.models import HostVirtual
import json
import urllib
import cmdb_log

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt  
def service_get(request):
    if not request.user.is_authenticated():
        json_r = json.dumps({"result":"no login"})
        return HttpResponse(json_r)
    key = request.POST.get('key','all')
    if key == 'all':
        pageIndex = request.POST.get('pageIndex',0)
        pageSize = request.POST.get('pageSize',100)
        start = int(pageIndex)*int(pageSize)
        stop = int(pageIndex)*int(pageSize) + int(pageSize)
        service_r = list(Service.objects.all().values())
        data = {"total":len(service_r),"data":service_r[start:stop]}
        json_r = json.dumps(data)
    elif key == 'id':
        id = request.POST.get('id')
        service_r = list(Service.objects.filter(id=id).values())[0]
        json_r = json.dumps(service_r)
    else:
        service_r = list(Service.objects.filter(Service_Name__contains=key).values())
        json_r = json.dumps(service_r)
    return HttpResponse(json_r)

@csrf_exempt
def service_search(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect("/ops/cmdb/html/login.html")
    json_str =request.body
    data = json.loads(json_str)
    key = data['key']
    if key == 'id':
        service_r = list(Service.objects.filter(id=data['id']).values())[0]
        json_r = json.dumps(service_r)
    if key == 'Service_Name':
        service_r = list(Service.objects.filter(Service_Name__contains=data['Service_Name']).values())[0]
        json_r = json.dumps(service_r)
    return HttpResponse(json_r)

@csrf_exempt  
def service_save(request):
    if not request.user.is_authenticated():
        json_r = json.dumps({"result":"no login"})
        return HttpResponse(json_r)
    elif not request.user.has_perm('cmdb.change_service'):
        json_r = json.dumps({"result":"no permission"})
        return HttpResponse(json_r)
    json_str = request.body
    data = json.loads(json_str)
    if  data['id']:
        i = Service.objects.filter(id=data['id'])
        message = cmdb_log.cmp(list(i.values())[0],data)
        i.update(Service_Name = data['Service_Name'])
        cmdb_log.log_change(request,i[0],data['Service_Name'],message)
    else:
        i = Service(Service_Name = data['Service_Name'])
        i.save()
        cmdb_log.log_addition(request,i,data['Service_Name'],data)
    json_r = json.dumps({"result":"save sucess"})
    return HttpResponse(json_r)
@csrf_exempt
def service_del(request):
    if not request.user.is_authenticated():
        json_r = json.dumps({"result":"no login"})
        return HttpResponse(json_r)
    elif not request.user.has_perm('cmdb.change_service'):
        json_r = json.dumps({"result":"no permission"})
        return HttpResponse(json_r)
    json_str =request.body
    data = json.loads(json_str)
    ids = data['id'].split(',')
    for del_id in ids:
        i = Service.objects.filter(id=del_id)
        h = HostPhysical.objects.filter(service=del_id)
        v = HostVirtual.objects.filter(service=del_id)
        n = len(h)+len(v)
        if n:
            json_r = json.dumps({"result":"include hosts"})
            return  HttpResponse(json_r)
        cmdb_log.log_deletion(request,i[0],i[0].Service_Name,data)
        i.delete()
    json_r = json.dumps({"result":"delete sucess"})
    return HttpResponse(json_r)
