
# -*- coding: utf-8 -*-
from django.shortcuts import render, redirect
from django.views.generic import View
from django.http import HttpResponse
from reportlab.pdfgen import canvas

from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.platypus import (Flowable, Paragraph,
                                SimpleDocTemplate, Spacer)

from .models import TablaSolicitud
from .models import Bitacora

from .models import TablaAlumno


# Create your views here.
class ConsultarDocumento(View):
    template_name = "consultarDocumento.html"
    def get(self, request):
        return render(
            request,
            self.template_name,
        )

class VerDocumento(View):
    template_name = "verDocumento.html"
    model = TablaAlumno
    model2 = TablaSolicitud

    def 
(self, request, folio):
        self.request.session['errorConsulta'] = None
        print(folio)
        context = dict()
        try:
            alumn=self.model.objects.get(codigo = folio)
        except:
            self.request.session['errorConsulta'] = "Es incorrecto el código insertado"
            return redirect('consultar')
        context['solicitudes'] = self.model2.objects.filter(alumno_id=alumn.id)
        return render(
            request,
            self.template_name,
            context
        )


class VerPdf(View):
    template_name = "verPdf.html"
    model = TablaSolicitud
    model2 = TablaAlumno

    def get(self, request, id, solicitudId):
        response = HttpResponse(content_type='application/pdf')
        response['Content-Disposition'] = 'attachment; filename="Documento.pdf"'
        p = canvas.Canvas(response)
        alumno = self.model2.objects.get(codigo=id)
        bitacora = self.model.objects.get(id = solicitudId)
        #  x,y
        p.setFont("Helvetica", 16)
        p.drawCentredString(260,800,"INSTITUTO POLITECNICO NACIONAL")
        p.drawCentredString(260,770,"ESCUELA SUPERIOR DE COMPUTO")
        p.drawCentredString(280,740,"SUBDIRECCION DE SERVIVIOS EDUCATIVOS E INTEGRACION SOCIAL")
        p.line(120,700,580,700)
        p.setFont("Helvetica", 12)
        p.drawCentredString(260,715,"DEPARTAMENTO DE GESTION ESCOLAR")
        p.drawCentredString(260,700,str(bitacora.documento))
        p.drawCentredString(100,695,"A QUIEN CORRESPONDA:")
        p.drawCentredString(100,670,"HACE CONSTAR QUE EL ALUMNO")
        p.drawCentredString(260,650,str(bitacora.alumno))
        p.drawCentredString(100,630,"CON NUMERO DE BOLETA")
        p.drawCentredString(230,630,str(bitacora.alumno.boleta))
        p.drawCentredString(380,630,"ESTA INSCRITO EN ESTE PLANTEL");



        p.drawCentredString(200, 600, str(bitacora.fecha))
        p.drawCentredString(200, 610, str(bitacora.estado))
        p.drawCentredString(200, 620, str(bitacora.folio))

        p.showPage()
        p.save()
        return response

get