from django.shortcuts import render, redirect
from .forms import EmployeeForm, ContractForm, ChargeNumberForm
from .models import Contract
# Create your views here.

# PAGES TO CREATE
#  dashboard
#  log in
#  log out
#  password reset
#  employee timecard
#  time card approval
#  contract
#  charge number
#  user management (?)

def index(request):    
    permissions = []
    context = {
        'is_procurment_officer':True,
        'is_worker':True,
        'is_manager':True,
        'is_finance': True

    }
    return render(request, "financial/index.html", context)

def contracts(request):
    contracts = Contract.objects.all()
    return render(request, 'financial/contracts.html', {'contracts':contracts})

def contract_detail(request, contract_slug):
    contract = Contract.objects.filter(slug=contract_slug)    
    if request.method == 'POST':
        form = ContractForm(request.POST, instance=contract[0])
        if form.is_valid():
            form.save()
            return redirect('contracts')
    else:
        form = ContractForm(instance = contract[0])    
        charge_number_form = ChargeNumberForm()
        # TODO: handle multiple better but we can assume there is only one
        
        return render(request, 'financial/contract_detail.html', {'is_new': False, 'form': form, 'charge_number_form':charge_number_form, 'contract': contract[0]})

def new_contract(request):
    if request.method == "POST":
        form = ContractForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('contracts')
        else:
            return render(request, 'financial/contract_detail.html', {"form": ContractForm(), 'is_new': True})
        
    else:
        return render(request, 'financial/contract_detail.html', {"form": ContractForm(), 'is_new': True})

def update_charge_number(request, charge_number_slug):
    return render(request, 'financial/not_created.html', {})

def new_charge_number(request, charge_number_slug):
    return render(request, 'financial/not_created.html', {})

def timekeeping(request):
    return render(request, 'financial/not_created.html', {})

def timeapproval(request):
    return render(request, 'financial/not_created.html', {})

def chargenumber(request):
    return render(request, 'financial/not_created.html', {})

def procurement(request):
    return render(request, 'financial/procurement.html', {})

def profile(request):
    form = EmployeeForm()
    return render(request, 'financial/profile.html', {'form': form})
# def contract_detail(request, contract_id): 