from django.shortcuts import render,  redirect
from django.http import HttpResponse,HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate
from django.contrib.auth import logout as auth_logout
from django.contrib.auth import login as auth_login
from django.core.urlresolvers import reverse
from django.conf import settings
import datetime
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
# from django.contrib.auth.hashers import make_password

from django.contrib.auth.models import User
from staff.models import *
from farmer.models import *

# Create your views here.
def index(request):
	return render(request,'staff/index.html', {'nbar':'home'})

def createStaff(request):
	if request.method == 'POST':
		user = User(
				username = request.POST['username'],				
			)
		user.set_password(request.POST['password'],)
		user.save()
		staff = Staff(
				username = request.POST['username'],
				full_name = request.POST['full_name'],
				role = request.POST['role'],				
				photo = request.FILES['photo'],				
			) 
		staff.save()
		account = Akun_Staff(
				akun = User.objects.get(username=request.POST['username']),
				staff = Staff.objects.get(username=request.POST['username']),
				akun_id = User.objects.get(username=request.POST['username']).id,
				staff_id = Staff.objects.get(username=request.POST['username']).id,
				jenis_akun = request.POST['role'],				
			)
		account.save()
		return redirect('/staff/daftar-staff')
	else:		
		return render(request, 'staff/data_staff/create.html', {'nbar':'activeStaff', 'li':'createStaff'})

def daftarStaff(request):
	# account = Pengguna.objects.get(username=request.session['username'])
	daftar_staff = Staff.objects.all().order_by('-role')
	paginator = Paginator(daftar_staff, 5)
	page = request.GET.get('page')
	try:
		daftar_staff = paginator.page(page)	
	except PageNotAnInteger:
		daftar_staff = paginator.page(1)
	except EmptyPage:
		daftar_staff = paginator.page(paginator.num_pages)
	return render(request, 'staff/data_staff/daftar.html', {'nbar':'activeStaff', 'li':'daftarStaff', 'daftar_staff':daftar_staff})

def pendingPengajuanLahan(request):
	# account = Pengguna.objects.get(username=request.session['username'])
	pengajuan_pending = Data_Pengajuan_Lahan.objects.filter(status='pending').order_by('-tanggal_pengajuan')
	paginator = Paginator(pengajuan_pending, 10)
	page = request.GET.get('page')
	try:
		pengajuan_pending = paginator.page(page)	
	except PageNotAnInteger:
		pengajuan_pending = paginator.page(1)
	except EmptyPage:
		pengajuan_pending = paginator.page(paginator.num_pages)
	return render(request, 'staff/pengajuan_lahan/pending.html', {'nbar':'activePengajuan', 'li':'pendingPengajuan', 'pengajuan_pending':pengajuan_pending})


def rejectPengajuanLahan(request):
	# account = Pengguna.objects.get(username=request.session['username'])
	pengajuan_reject = Data_Pengajuan_Lahan.objects.filter(status='reject').order_by('-tanggal_pengajuan')
	paginator = Paginator(pengajuan_reject, 5)
	page = request.GET.get('page')
	try:
		pengajuan_reject = paginator.page(page)	
	except PageNotAnInteger:
		pengajuan_reject = paginator.page(1)
	except EmptyPage:
		pengajuan_reject = paginator.page(paginator.num_pages)
	return render(request, 'staff/pengajuan_lahan/reject.html', {'nbar':'activePengajuan', 'li':'rejectPengajuan', 'pengajuan_reject':pengajuan_reject})

def pendingPeminjamanUang(request):
	# account = Pengguna.objects.get(username=request.session['username'])
	peminjaman_uang = Data_Pengajuan_Peminjaman.objects.filter(status='pending').order_by('-tanggal_peminjaman')
	paginator = Paginator(peminjaman_uang, 5)
	page = request.GET.get('page')
	try:
		peminjaman_uang = paginator.page(page)	
	except PageNotAnInteger:
		peminjaman_uang = paginator.page(1)
	except EmptyPage:
		peminjaman_uang = paginator.page(paginator.num_pages)
	return render(request, 'staff/peminjaman_uang/pending.html', {'nbar':'activePeminjaman', 'li':'pendingPeminjaman', 'peminjaman_uang':peminjaman_uang})

def acceptPeminjamanUang(request):
	return render(request, 'staff/peminjaman_uang/accept.html', {'nbar':'activePeminjaman', 'li':'acceptPeminjaman'})

def rejectPeminjamanUang(request):
	# account = Pengguna.objects.get(username=request.session['username'])
	peminjaman_uang = Data_Pengajuan_Peminjaman.objects.filter(status='tolak').order_by('-tanggal_peminjaman')
	paginator = Paginator(peminjaman_uang, 5)
	page = request.GET.get('page')
	try:
		peminjaman_uang = paginator.page(page)	
	except PageNotAnInteger:
		peminjaman_uang = paginator.page(1)
	except EmptyPage:
		peminjaman_uang = paginator.page(paginator.num_pages)
	return render(request, 'staff/peminjaman_uang/reject.html', {'nbar':'activePeminjaman', 'li':'rejectPeminjaman', 'peminjaman_uang':peminjaman_uang})

def indexDataLahan(request):
	return render(request,'staff/data_lahan/index.html', {'nbar':'lahan'})