# -*- coding: utf-8 -*-
import json

from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
import requests
from django.http import HttpResponse
from django.shortcuts import render, redirect

# Create your views here.
from django.views.decorators.csrf import csrf_exempt

from account.forms import UserForm, LoginForm
from account.models import User
from machine.models import Warp_Machine, Knit_Machine
from order.models import Order, Order_DesignData

def aaaa(request):
    return render(request, 'schedule20191101.html')

@csrf_exempt
def pjs1234(request):
    username=request.POST['username']
    return HttpResponse(json.dumps({'username': username}))

def signUo(request):
    if request.user.is_anonymous:
        if request.method == "POST":
            form = UserForm(request.POST)
            if form.is_valid():
                user = form.save(commit=False)
                User.objects.create_user(username=user.username, email=user.email, phone=user.phone, password=user.password, name=user.name, is_active=False)
                print(user.password)
                return redirect('/')
        else:
            form = UserForm()
        return render(request, 'signup.html', {'form': form})
    else:
        return redirect('/')

def login_site(request):
    if request.user.is_anonymous:
        if request.method == "POST":
            username = request.POST['username']
            password = request.POST['password']

            user_active = User.objects.get(username=username)
            if user_active.is_active:
                user = authenticate(username = username, password = password)
                if user is not None:
                    login(request, user)
                    return redirect('/server')
                else:
                    msg = '아이디를 확인해주세요'
                    return render(request, 'msg/errorPage.html', {'msg': msg})
            else:
                msg = '승인대기중입니다.'
                return render(request, 'msg/errorPage.html', {'msg': msg})
        else:
            form = LoginForm()
            return render(request, 'login.html', {'form': form})
    else:
        return render(request, 'index.html', {})

@login_required(login_url='/')
def dashboard(request):
    totalOrder = Order.objects.all().count()
    pendingOrder = Order.objects.filter(state=0).count()
    progressOrder = Order.objects.filter(state=1).count()
    compOrder = Order.objects.filter(state=2).count()
    #warp_Machine = Warp_Machine.objects.all().count()
    #on_warp_Machine = Warp_Machine.objects.filter(tws_onoff='on').count()

    #knit_Machine = Knit_Machine.objects.all().count()
    #on_knit_Machine = Knit_Machine.objects.filter(trs_onoff='on').count()

    #all_machine_count = warp_Machine + knit_Machine
    #on_machine_count = on_warp_Machine + on_knit_Machine

    designOrderList = Order_DesignData.objects.all().order_by('-id')[:13]

    try:
        knitJsonData = requests.get("http://tnswebserver.iptime.org:1978/values/trs").json()
        warpJsonData = requests.get("http://tnswebserver.iptime.org:1978/values/tws").json()

        all_knit_machine = len(knitJsonData["trs"])
        all_warp_machine = len(warpJsonData["tws"])
        on_knit_machine = 0
        on_warp_machine = 0

        # 경편기 On 카운트
        for knitOn in knitJsonData["trs"]:
            if knitOn["trs_onoff"] == "ON":
                on_knit_machine = on_knit_machine + 1

        # 정경기 On 카운트
        for warpOn in warpJsonData["tws"]:
            if warpOn["tws_onoff"] == "ON":
                on_warp_machine = on_warp_machine + 1

        all_machine_count = all_knit_machine + all_warp_machine
        on_machine_count = on_knit_machine + on_warp_machine

    except:
        all_machine_count = 0
        on_machine_count = 0


    title = 'Dashboard'

    variables = {'title':title, 'totalOrder':totalOrder, 'pendingOrder':pendingOrder, 'progressOrder':progressOrder, 'compOrder':compOrder, 'designOrderList': designOrderList, 'all_machine_count':all_machine_count, 'on_machine_count':on_machine_count}

    return render(request, 'index.html', variables)

def logout_site(request):
    logout(request)
    return redirect('/')

@login_required(login_url='/')
def server(request):

    return render(request, 'server.html', {})

