<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.csc.library.utility.DHMConvert"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Vector"%>
<%@page import="java.util.*"%>
<%@page import="com.csc.library.report.ReportUDF"%>
<%@page import="javax.servlet.jsp.jstl.sql.Result"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="com.csc.library.utilities.MyLog"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.text.DecimalFormat"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.text.SimpleDateFormat" %>
<%@page import="java.util.Locale" %>
<%@page import="java.util.Date" %>
<%@page import="java.util.GregorianCalendar" %>
<%@page import="com.csc.library.database.DataSet"%>
<%@page import="com.csc.library.session.InitialInquiry"%>
<%@page import="com.csc.library.session.InitialRecord"%>
<%@page import="com.csc.library.utilities.UProfile"%>
<%@page import="com.csc.library.utilities.CscCalendar"%>
<%@page import="com.csc.library.session.DbInquiry"%>
<%@page import="com.csc.library.session.DbRecord"%>
<%@page import="com.csc.library.utilities.CheckNull"%>
<%@page import="com.csc.library.entry.HelpEntry"%>
<%@page import="com.itextpdf.text.*"%>
<%@page import="com.itextpdf.text.pdf.*"%>
<%@page import="java.io.ByteArrayOutputStream" %>
<%@page import="com.csc.library.session.InitialEnvironment" %>
<%@page import="java.io.File" %>
<jsp:useBean id="TRRE043_EXP" class="com.csc.library.system.Task" scope="page" />
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8"/>
    <title>Report</title>
  </head>
  <body>
    Not found data
    <%
    TRRE043_EXP.setChannel(request, response);
    HelpEntry screen = (HelpEntry) TRRE043_EXP.process("HelpEntry", "TRRE043_EXP");
    CheckNull chk = new CheckNull();

    String filter = chk.chkNullString(request.getParameter("__filter"));

    screen.setMaxLine(1);
    screen.inq.setFilter(filter);
    screen.process();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();//72
    Document document = new Document(PageSize.A4.rotate(), 50, 50, 30, 36);
    float[] sizeColumn = new float[] {};

    PdfWriter.getInstance(document, baos);
    document.open();
    document.addTitle("Document Title");

    BaseFont bf = BaseFont.createFont(application.getRealPath("FONTS/Calibri.ttf"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    BaseFont bfHead = BaseFont.createFont(application.getRealPath("FONTS/Calibri.ttf"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true);
    Font font = new Font(bf, 11);
    Font fontTopic = new Font(bfHead, 20, Font.BOLD);
    Font fontHead = new Font(bfHead, 13, Font.BOLD);
    Font fontHeadWhiteColor = new Font(bfHead, 11, Font.BOLD, new BaseColor(255, 255, 255));
    //fontHead.setStyle(Font.BOLD);
    Font fontContent = new Font(bfHead, 11);
    Font fontBoldContent = new Font(bfHead, 11, Font.BOLD);
    Font fontTitle = new Font(bfHead, 20, Font.BOLD);
    fontTitle.setStyle(Font.BOLD);

    // set vlign middle
    Phrase content = new Phrase("Blah blah blah", font);
    Float fontSize = content.getFont().getSize();
    Float capHeight = content.getFont().getBaseFont().getFontDescriptor(BaseFont.CAPHEIGHT, fontSize);

    boolean hasData = false;
    if(screen.getInquiry().recCount() > 0) {
      hasData = true;
      screen.nextRec();
      screen.referLangOn();
      DbInquiry dbinq = null;
      int i = 0;

      String totaltraineeDesc = "";

      DbInquiry inq_totaltrainee = new InitialInquiry(screen.getUProfile()).getDbInquiry("ttraintrnee");
      inq_totaltrainee.setFilter("trainingid='"+screen.getString("TTRAINING","TRAININGID")+"'");
      totaltraineeDesc = String.valueOf(inq_totaltrainee.recCount());

      try{
        CscCalendar calendar = new CscCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
        GregorianCalendar gc= new GregorianCalendar();
        boolean langthai = screen.getUProfile().get("lang").equalsIgnoreCase("THA");
        String reportname = "", printdate = "", printby = "", refer = "RTA2007";
        String reportname_label = "", printdate_label = "", printby_label = "", refer_label = "";
        if(langthai){
          reportname_label = "รายงาน :";
          reportname = "รายงานข้อมูลพนักงาน";
          printdate_label = "วันที่พิมพ์ :";
          printdate = "วัน"+calendar.getLongThiDOW()+"ที่ "+calendar.getDate()+ " " + calendar.getThaiLongMonth() + " พ.ศ. "+ calendar.getThaiYYYY() + " " + calendar.getHHMM() + " น.";
          printby_label = "พิมพ์โดย :";
          printby = screen.getUProfile().get("tfullname");
          refer_label = "อ้างอิง #";
        } else {
          reportname_label = "Report Name :";
          reportname = "Personal Report";
          printdate_label = "Print Date :";
          printdate = calendar.getEngFull()+ " " + sdf.format((Date) gc.getTime());
          printby_label = "Print By :";
          printby = screen.getUProfile().get("efullname");
          refer_label = "Ref #";
        }


        // --------------- Report Header -----------------
        sizeColumn = new float[] {50.0f,50.0f};
        PdfPTable detail = new PdfPTable(2);
        detail.setWidthPercentage(100);
        detail.setWidths(sizeColumn);
        PdfPCell cellDetail;
        Image img = null;
        setPaddingTop(0.5f);
        setPaddingLeft(0.0f);
        setPaddingRight(0.0f);
        detail.addCell( getCellData("Lesson Plan", fontTitle, true, "LEFT", "MIDDLE", 1, 0) );

        String defaultFile = "default.png";
        String filename = screen.getString("MCOMPANY","logo");
        InitialEnvironment inv = new InitialEnvironment("GLOBAL");
        String path = inv.getValue("COMPANY_UPLOAD-dir")+"LOGO/";
        if (!new File(path+filename).exists() || filename.equals("")){
          filename = defaultFile;
        }

        img = Image.getInstance(path+filename);
        cellDetail = new PdfPCell(img, true);
        cellDetail.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cellDetail.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cellDetail.setFixedHeight(40);
        cellDetail.setBorder(Rectangle.NO_BORDER);
        detail.addCell(cellDetail);
        // document.add(detail);
        // --------------- new line -----------------

      /*  sizeColumn = new float[] {100.0f};
        detail = new PdfPTable(1);
        detail.setWidthPercentage(100);
        detail.setWidths(sizeColumn);*/
        // --------------- new line -----------------

        cellDetail = new PdfPCell(new Phrase(" ", font));
				cellDetail.setPadding(padding);
        cellDetail.setPaddingTop(paddingtop);
        cellDetail.setHorizontalAlignment(Element.ALIGN_LEFT);
        cellDetail.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cellDetail.setBorder(Rectangle.BOTTOM);
        detail.addCell(cellDetail);
        detail.addCell( getCellData(" ", font, true, "LEFT", "MIDDLE", 1, 0) );
        document.add(detail);
        // --------------- new line -----------------

        sizeColumn = new float[] {18.0f,32.0f,18.0f,32.0f};
        detail = new PdfPTable(4);
        detail.setWidthPercentage(100);
        detail.setWidths(sizeColumn);
        setPaddingTop(1.0f);
        setPaddingLeft(3.5f);
        setPaddingRight(3.5f);
        setPaddingBottom(4.5f);
        setBaseBackgroundColor(0, 93, 147);
        detail.addCell( getCellData("Training ID", fontHeadWhiteColor, false, "LEFT", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData(screen.getString("TTRAINING","TRAININGID"), font, false, "LEFT", "MIDDLE", 1, 0) );
        detail.addCell( getCellData("Training Title", fontHeadWhiteColor, false, "LEFT", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData(screen.getString("TTRAINING","TRTITLE"), font, false, "LEFT", "MIDDLE", 1, 0) );

        detail.addCell( getCellData("Course ID", fontHeadWhiteColor, false, "LEFT", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData(screen.getString("TTRAINING","CRSID"), font, false, "LEFT", "MIDDLE", 1, 0) );
        detail.addCell( getCellData("Training Type", fontHeadWhiteColor, false, "LEFT", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData(screen.getSystemCode("MTRAINTYPE","TRAIN_TYPE"), font, false, "LEFT", "MIDDLE", 1, 0) );

        detail.addCell( getCellData("Instructor Responsible", fontHeadWhiteColor, false, "LEFT", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData(screen.getString("TTRAINING","instructor_name"), font, false, "LEFT", "MIDDLE", 1, 0) );
        detail.addCell( getCellData("Instructor Validation", fontHeadWhiteColor, false, "LEFT", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData(screen.getString("TTRAINING","instructor_validation"), font, false, "LEFT", "MIDDLE", 1, 0) );

        detail.addCell( getCellData("Training Dates From", fontHeadWhiteColor, false, "LEFT", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData(screen.getString("TTRAINING","CLASSDATEFRM"), font, false, "LEFT", "MIDDLE", 1, 0) );
        detail.addCell( getCellData("Training Dates To", fontHeadWhiteColor, false, "LEFT", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData(screen.getString("TTRAINING","CLASSDATETO"), font, false, "LEFT", "MIDDLE", 1, 0) );

        detail.addCell( getCellData("Number of Trainees", fontHeadWhiteColor, false, "LEFT", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData(totaltraineeDesc, font, false, "LEFT", "MIDDLE", 1, 0) );
        detail.addCell( getCellData("Training Location", fontHeadWhiteColor, false, "LEFT", "MIDDLE", 1, 2, true) );
        detail.addCell( getCellData(screen.getString("TTRAINING","training_location"), font, false, "LEFT", "MIDDLE", 1, 2) );

        detail.addCell( getCellData("Responsible Person", fontHeadWhiteColor, false, "LEFT", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData(screen.getString("TTRAINING","responsible_person_name"), font, false, "LEFT", "MIDDLE", 1, 0) );
        document.add(detail);
        // --------------- new line -----------------

        sizeColumn = new float[] {100.0f};
        detail = new PdfPTable(1);
        detail.setWidthPercentage(100);
        detail.setWidths(sizeColumn);
        setPaddingTop(15.0f);
        setPaddingLeft(0.0f);
        setPaddingRight(0.0f);
        setPaddingBottom(0.0f);
        detail.addCell( getCellData(screen.getLabel("MCOURSE","EDITORIAL")+" :", fontHead, true, "LEFT", "BOTTOM", 1, 0) );
        setPaddingTop(0.5f);
        setPaddingLeft(15.0f);
        detail.addCell( getCellData(screen.getString("MCOURSE","EDITORIAL"), font, true, "LEFT", "TOP", 1, 0) );
        setPaddingTop(15.0f);
        setPaddingLeft(0.0f);
        detail.addCell( getCellData("Training Objective :", fontHead, true, "LEFT", "BOTTOM", 1, 0) );
        setPaddingTop(0.5f);
        setPaddingLeft(15.0f);
        detail.addCell( getCellData(screen.getString("MCOURSE","OBJECTIVE"), font, true, "LEFT", "TOP", 1, 0) );
        setPaddingTop(15.0f);
        setPaddingLeft(0.0f);
        detail.addCell( getCellData("Training Content :", fontHead, true, "LEFT", "BOTTOM", 1, 0) );
        setPaddingTop(0.5f);
        setPaddingLeft(15.0f);
        detail.addCell( getCellData(screen.getString("MCOURSE","BENEFIT"), font, true, "LEFT", "TOP", 1, 0) );
        setPaddingTop(15.0f);
        setPaddingLeft(0.0f);
        detail.addCell( getCellData(screen.getLabel("MACADEMY","PRIVILEGE")+" :", fontHead, true, "LEFT", "BOTTOM", 1, 0) );
        setPaddingTop(0.5f);
        setPaddingLeft(15.0f);
        detail.addCell( getCellData(screen.getString("MACADEMY","PRIVILEGE"), font, true, "LEFT", "TOP", 1, 0) );
        setPaddingTop(15.0f);
        setPaddingLeft(0.0f);
        detail.addCell( getCellData("Participant Criteria :", fontHead, true, "LEFT", "BOTTOM", 1, 0) );
        setPaddingTop(0.5f);
        setPaddingLeft(15.0f);
        detail.addCell( getCellData(screen.getString("MCOURSE","CONDITION_GEN"), font, true, "LEFT", "TOP", 1, 0) );
        setPaddingTop(15.0f);
        setPaddingLeft(0.0f);
        detail.addCell( getCellData("General Condition :", fontHead, true, "LEFT", "BOTTOM", 1, 0) );
        setPaddingTop(0.5f);
        setPaddingLeft(15.0f);
        detail.addCell( getCellData(screen.getString("MCOURSE","COMMENT_GEN"), font, true, "LEFT", "TOP", 1, 0) );
        setPaddingTop(15.0f);
        setPaddingLeft(0.0f);
        detail.addCell( getCellData("Assessment Criteria :", fontHead, true, "LEFT", "BOTTOM", 1, 0) );
        setPaddingTop(0.5f);
        setPaddingLeft(15.0f);
        detail.addCell( getCellData(screen.getString("MCOURSE","PROPERTY"), font, true, "LEFT", "TOP", 1, 0) );

        setPaddingTop(15.0f);
        setPaddingLeft(0.0f);
        detail.addCell( getCellData("Topic and Subject :", fontHead, true, "LEFT", "BOTTOM", 1, 0) );
        setPaddingTop(0.5f);
        setPaddingBottom(6.5f);
        detail.addCell( getCellData("(Please note when people break into groups, especially the sizes of groups)", font, true, "LEFT", "TOP", 1, 0) );
        document.add(detail);

        sizeColumn = new float[] {10.0f,10.0f,25.0f,45.0f,10.0f};
        detail = new PdfPTable(5);
        detail.setWidthPercentage(100);
        detail.setWidths(sizeColumn);
        setPaddingTop(1.0f);
        setPaddingLeft(3.5f);
        setPaddingRight(3.5f);
        setPaddingBottom(4.5f);
        setBaseBackgroundColor(0, 93, 147);
        detail.addCell( getCellData("Module", fontHeadWhiteColor, false, "CENTER", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData("(Hour)", fontHeadWhiteColor, false, "CENTER", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData("Subject Description", fontHeadWhiteColor, false, "CENTER", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData("Detail", fontHeadWhiteColor, false, "CENTER", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData("Timing", fontHeadWhiteColor, false, "CENTER", "MIDDLE", 1, 0, true) );

        int ts_round = 1;
        setBaseBackgroundColor(211, 223, 238);
        boolean showbg = true;
        DHMConvert dhm = new DHMConvert();
        int total_minute = 0;
        DbInquiry inq_mcoursedetail = new InitialInquiry(screen.getUProfile()).getDbInquiry("mcoursedetail");
        inq_mcoursedetail.setColumn("*");
        inq_mcoursedetail.setFilter("crsid = '"+screen.getString("TTRAINING","CRSID")+"'");
        inq_mcoursedetail.refresh();
        while(inq_mcoursedetail.next()){
          total_minute += dhm.timeToMin(chk.chkNullStringToDouble(inq_mcoursedetail.getString("hour")));
          detail.addCell( getCellData(ts_round+"", fontHead, false, "CENTER", "MIDDLE", 1, 0, showbg) );
          detail.addCell( getCellData(chk.chkNullStringToDouble(inq_mcoursedetail.getString("hour"))+"", font, false, "CENTER", "MIDDLE", 1, 0, showbg) );
          detail.addCell( getCellData(inq_mcoursedetail.getString("edesc"), font, false, "LEFT", "MIDDLE", 1, 0, showbg) );
          detail.addCell( getCellData(inq_mcoursedetail.getString("edetail"), font, false, "LEFT", "MIDDLE", 1, 0, showbg) );
          detail.addCell( getCellData(" ", font, false, "LEFT", "MIDDLE", 1, 0, showbg) );
          ts_round++;
          showbg = ts_round % 2 == 0 ? false : true;
        }

        detail.addCell( getCellData("Total", fontBoldContent, false, "CENTER", "MIDDLE", 1, 0, showbg) );
        detail.addCell( getCellData(dhm.MinToTime(total_minute), fontBoldContent, false, "CENTER", "MIDDLE", 1, 0, showbg) );
        detail.addCell( getCellData(" ", fontBoldContent, false, "LEFT", "MIDDLE", 1, 0, showbg) );
        detail.addCell( getCellData(" ", fontBoldContent, false, "LEFT", "MIDDLE", 1, 0, showbg) );
        detail.addCell( getCellData(" ", fontBoldContent, false, "LEFT", "MIDDLE", 1, 0, showbg) );

        document.add(detail);

        sizeColumn = new float[] {100.0f};
        detail = new PdfPTable(1);
        detail.setWidthPercentage(100);
        detail.setWidths(sizeColumn);
        setPaddingTop(15.0f);
        setPaddingLeft(0.0f);
        setPaddingBottom(0.0f);
        detail.addCell( getCellData("Additional Information", fontHead, true, "LEFT", "BOTTOM", 1, 0) );
        setPaddingTop(0.5f);
        setPaddingBottom(5.0f);
        detail.addCell( getCellData("Is there a need for handouts or additional materials to be prepared?", font, true, "LEFT", "BOTTOM", 1, 0) );
        detail.addCell( getCellData("\n\n\n\n\n\n\n\n", fontTitle, false, "LEFT", "MIDDLE", 1, 0, true) );
        detail.addCell( getCellData(" ", fontHead, true, "LEFT", "BOTTOM", 1, 0) );
        document.add(detail);

        sizeColumn = new float[] {60.0f,40.0f};
        detail = new PdfPTable(2);
        detail.setWidthPercentage(100);
        detail.setWidths(sizeColumn);
        setPaddingTop(3.5f);
        setPaddingLeft(6.5f);
        setPaddingRight(6.5f);
        setPaddingBottom(10.5f);
        detail.addCell( getCellData(" ", fontHead, true, "LEFT", "BOTTOM", 1, 0) );
        detail.addCell( getCellData("Prepared by :", fontHead, false, "LEFT", "BOTTOM", 1, 0, true) );
        detail.addCell( getCellData(" ", fontHead, true, "LEFT", "BOTTOM", 1, 0) );
        detail.addCell( getCellData("Authorized by :", fontHead, false, "LEFT", "BOTTOM", 1, 0, true) );
        document.add(detail);

      } catch(Exception e) {
        e.printStackTrace();
      } finally {
        document.close();
      }
    }

    if(hasData){
      OutputStream os = response.getOutputStream();
      // Create a reader
      PdfReader reader = new PdfReader(baos.toByteArray());
      // Create a stamper
      PdfStamper stamper = new PdfStamper(reader,baos);

      // Close the stamper
      stamper.close();
      reader.close();

      response.setHeader("Expires", "0");
      response.setHeader("Cache-Control","must-revalidate, post-check=0 , pre-check=0");
      response.setHeader("Pragma", "public");
      // setting the content type
      response.setContentType("application/pdf");
      // the contentlength
      response.setContentLength(baos.size());
      // write ByteArrayOutputStream to the ServletOutputStream

      baos.writeTo(os);

      os.flush();
      os.close();
    }

    %>

    <%!
    Float padding = 0.0f;
    Float paddingtop = 0.0f;
    Float paddingbottom = 0.0f;
    Float paddingleft = 0.0f;
    Float paddingright = 0.0f;
    BaseColor basecolor = new BaseColor(0,0,0);

    private PdfPCell getCellData(String text, Font font, boolean noborder, String horalign, String veralign, int colspan, int rowspan) {
      return getCellData(text, font, noborder, horalign, veralign, colspan, rowspan, false);
    }

    private PdfPCell getCellData(String text, Font font, boolean noborder, String horalign, String veralign, int colspan, int rowspan, boolean bgcolor) {
      PdfPCell cellDetail = new PdfPCell(new Phrase(text,font));

      if(horalign.equalsIgnoreCase("LEFT")){
        cellDetail.setHorizontalAlignment(Element.ALIGN_LEFT);
      } else if(horalign.equalsIgnoreCase("CENTER")){
        cellDetail.setHorizontalAlignment(Element.ALIGN_CENTER);
      } else if(horalign.equalsIgnoreCase("RIGHT")){
        cellDetail.setHorizontalAlignment(Element.ALIGN_RIGHT);
      }

      if(veralign.equalsIgnoreCase("TOP")){
        cellDetail.setVerticalAlignment(Element.ALIGN_TOP);
      } else if(veralign.equalsIgnoreCase("MIDDLE")){
        cellDetail.setVerticalAlignment(Element.ALIGN_MIDDLE);
      } else if(veralign.equalsIgnoreCase("BOTTOM")){
        cellDetail.setVerticalAlignment(Element.ALIGN_BOTTOM);
      }

      if(padding > 0.0f){
        cellDetail.setPadding(padding);
      }
      if(paddingtop > 0.0f){
        cellDetail.setPaddingTop(paddingtop);
      }
      if(paddingbottom > 0.0f){
        cellDetail.setPaddingBottom(paddingbottom);
      }
      if(paddingleft > 0.0f){
        cellDetail.setPaddingLeft(paddingleft);
      }
      if(paddingright > 0.0f){
        cellDetail.setPaddingRight(paddingright);
      }
      if(colspan > 0){
        cellDetail.setColspan(colspan);
      }
      if(rowspan > 0){
        cellDetail.setRowspan(rowspan);
      }
      if(noborder){
        cellDetail.setBorder(Rectangle.NO_BORDER);
      }
      if(bgcolor){
        cellDetail.setBackgroundColor(basecolor);
      }

      return cellDetail;
    }

    private void setPadding(Float padding) {
      this.padding = padding;
    }

    private void setPaddingTop(Float padding) {
      this.paddingtop = padding;
    }

    private void setPaddingBottom(Float padding) {
      this.paddingbottom = padding;
    }

    private void setPaddingLeft(Float padding) {
      this.paddingleft = padding;
    }

    private void setPaddingRight(Float padding) {
      this.paddingright = padding;
    }

    private void setBaseBackgroundColor(int r, int g, int b) {
      this.basecolor = new BaseColor(r,g,b);
    }

    %>

  </body>
</html>