<?php

session_start();
    include_once '../../config/database.php';
    include_once '../security/session_config.php';
    include_once "../../resources/offering.php";
    include_once '../../config/properties.php';
    include_once "../../resources/company.php";
    require_once('tcpdf_include.php');
    // require_once('../EncryptDecrypt.php');
    // 
    // initialize objects
    $offering = new Offering();

    $id = $_SESSION['uid'];
    $offering->user_id = $id;

    // $encrypted_oid = $_GET['id'];
    // $mcrypt = new MCrypt();
    $oid = $_GET['id'];
    $offering->offering_id = $oid;

    $medal = FALSE;
    $course_name = "";
    $score = 0;
    $first_name = "";
    $last_name = "";
    $course_name = "";
    $attended_date = "";
    $template_id = "";
    $grade_name = "";

    $cname = "";
    $clogo = "";

    //User Company 
    $users = new Company();
    $cust_company_logo = "";
    $cust_company_name = "";
    $company_name_con = "";
    $stmt_company = $users->UserCompanyDetails($_SESSION['uid']);
    if ($row_company = $stmt_company->fetch(PDO::FETCH_ASSOC)) {
        extract($row_company);
        $cust_company_logo = $logo_path;
        $cust_company_name = $name;
    }
    //End

    $stmt = $offering->get_Company_profile();
    if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $cname = $row['name'];
        $clogo = $row['logo_path'];
    }

    $stmt_r = $offering->get_details_cert();
    if ($row_r = $stmt_r->fetch(PDO::FETCH_ASSOC)) {
        $first_name = $row_r['first_name'];
        $last_name = $row_r['last_name'];
        $course_name = $row_r['course_name'];
        $attended_date = $row_r['attended_date'];
        $score = $row_r['score'];
    } else {
        header("Location: 404.php");
    }

    $date = strtotime($attended_date);
    $date1 = date("F d, Y", $date);

    $stmt = $offering->get_grade_cert();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

        $template_id = $row['template_id'];

        if ($row['cut_off'] != null && $row['cut_off'] <= $score) {
            $medal = TRUE;
            $grade_name = $row['grade'];

            $offering->template_id = $template_id;
            $stmt2 = $offering->get_cert_template();
            if ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)) {
                $template_name = $row2['template_name'];
                // $new_template_name = str_replace(' ', '%20', $template_name);
                $new_template_name = $template_name;
                $template_file = $row2['template_file'];
                // $new_template_file = str_replace(' ', '%20', $template_file);
                $new_template_file = $template_file;
                $text1 = $row2['text1'];
                $text2 = $row2['text2'];
                $text3 = $row2['text3'];
                $text4 = $row2['text4'];
                $text5 = $row2['text5'];
                $text6 = $row2['text6'];
                $company_name_con = $row2['comp_name'];

                $text1_color = $row2['text1_color'];
                $record1 = explode(",", $text1_color);
                $text2_color = $row2['text2_color'];
                $record2 = explode(",", $text2_color);
                $text3_color = $row2['text3_color'];
                $record3 = explode(",", $text3_color);
                $text4_color = $row2['text4_color'];
                $record4 = explode(",", $text4_color);
                $text5_color = $row2['text5_color'];
                $record5 = explode(",", $text5_color);
                $text6_color = $row2['text6_color'];
                $record9 = explode(",", $text6_color);
                $comp_name_color = $row2['comp_name_color'];
                $record10 = explode(",", $comp_name_color);
                $name_color = $row2['name_color'];
                $record6 = explode(",", $name_color);
                $course_color = $row2['course_color'];
                $record7 = explode(",", $course_color);
                $grade_color = $row2['grade_color'];
                $record8 = explode(",", $grade_color);

                $text1_size = $row2['text1_size'];
                $text2_size = $row2['text2_size'];
                $text3_size = $row2['text3_size'];
                $text4_size = $row2['text4_size'];
                $text5_size = $row2['text5_size'];
                $text6_size = $row2['text6_size'];
                $comp_name_size = $row2['comp_name_size'];
                $name_size = $row2['name_size'];
                $course_size = $row2['course_size'];
                $grade_size = $row2['grade_size'];

                $comp_name_path = $row2['comp_name_path'];
                $sign_path = $row2['sign_path'];

                $offering->badge_id = $row['badge_id'];
                $stmt3 = $offering->getgrade_file_achv();
                if ($row3 = $stmt3->fetch(PDO::FETCH_ASSOC)) {
                    if ($row['grade_type'] == "Gold") {
                        $grade_image = $row3['gold_file'];
                    } else if ($row['grade_type'] == "Silver") {
                        $grade_image = $row3['silver_file'];
                    } else if ($row['grade_type'] == "Bronze") {
                        $grade_image = $row3['bronze_file'];
                    }
                }

                // $GLOBALS['s_path'] = $mobitute_template . $new_template_name . '/' . $new_template_file;
                define('TEMPLATE_PATH1', ADMIN_HTTP_PATH . $properties['jdbc.cert_template'] . "/" . $template_id . '/' . rawurlencode($new_template_file));

// Extend the TCPDF class to create custom Header and Footer
                class MYPDF extends TCPDF {

                    //Page header
                    public function Header() {
                        // get the current page break margin
                        $bMargin = $this->getBreakMargin();
                        // get current auto-page-break mode
                        $auto_page_break = $this->AutoPageBreak;
                        // disable auto-page-break
                        $this->SetAutoPageBreak(false, 0);
                        // set bacground image
                        //$img_file = $GLOBALS['s_path'];

                        $this->Image(TEMPLATE_PATH1, 0, 0, 297, 210, '', '', '', false, 300, '', false, false, 0);
                        // restore auto-page-break status
                        $this->SetAutoPageBreak($auto_page_break, $bMargin);
                        // set the starting point for the page content
                        $this->setPageMark();
                    }

                }

// create new PDF document
                $pdf = new MYPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
                $pdf->SetCreator(PDF_CREATOR);
                $pdf->SetTitle('Certificate');

// set header and footer fonts
                $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));

// set default monospaced font
                $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
                $pdf->SetMargins(0, 0, 0);
                $pdf->SetHeaderMargin(0);
                $pdf->SetFooterMargin(0);

// remove default footer
                $pdf->setPrintFooter(false);

// set auto page breaks
                $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
                $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
                if (@file_exists(dirname(__FILE__) . '/lang/eng.php')) {
                    require_once(dirname(__FILE__) . '/lang/eng.php');
                    $pdf->setLanguageArray($l);
                }

// add a page
                $pdf->AddPage();
                $style3 = array('width' => 0.2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0));
// Print a text
                $pdf->Ln(20);
                if ($comp_name_path != "") {
                    $pdf->Image(ADMIN_HTTP_PATH . $properties['jdbc.comp_name_image_folder'] . "/" . $comp_name_path, '', '', '', '', '', '', '', true, 300, 'C');
                } else if ($company_name_con != "") {
                    $pdf->SetFont('freesans', 'B', $comp_name_size);
                    $pdf->SetTextColor($record10[0], $record10[1], $record10[2]);
                    $pdf->MultiCell(0, 0, $cust_company_name, 0, 'C', 0, 0, '', '', true, 0);
                }
                $pdf->Ln(22);
                $pdf->SetFont('freesans', 'B', $text1_size);
                $pdf->SetTextColor($record1[0], $record1[1], $record1[2]);
                $pdf->MultiCell(0, 0, $text1, 0, 'C', 0, 0, '', '', true, 0);
                $pdf->Ln(22);
                $pdf->SetFont('freesans', 'B', $text2_size);
                $pdf->SetTextColor($record2[0], $record2[1], $record2[2]);
                $pdf->MultiCell(0, 0, $text2, 0, 'C', 0, 0, '', '', true, 0);
                $pdf->Ln(12);
                if ($grade_image != "") {
                    $grade_image = rawurlencode($grade_image);
                    $pdf->Image(ADMIN_HTTP_PATH . $properties['jdbc.badge_folder'] . "/" . $row['badge_id'] . '/' . $grade_image, 40, 76, '', '', '', '', '', true, 300, '');
                }
                $pdf->SetFont('freesans', 'B', $name_size);
                $pdf->SetTextColor($record6[0], $record6[1], $record6[2]);
                $pdf->MultiCell(0, 0, $first_name . ' ' . $last_name, 0, 'C', 0, 0, '', '', true, 0);
                $pdf->Ln(20);
                $pdf->SetFont('freesans', 'B', $text3_size);
                $pdf->SetTextColor($record3[0], $record3[1], $record3[2]);
                $pdf->MultiCell(0, 0, $text3, 0, 'C', 0, 0, '', '', true, 0);
                $pdf->Ln(11);
                $pdf->SetFont('freesans', 'B', 20);
                $pdf->SetTextColor($record7[0], $record7[1], $record7[2]);
                $pdf->MultiCell(0, 0, $course_name, 0, 'C', 0, 0, '', '', true, 0);
                $pdf->Ln(14);
                $pdf->SetFont('freesans', 'B', $text4_size);
                $pdf->SetTextColor($record4[0], $record4[1], $record4[2]);
                $pdf->MultiCell(0, 0, $text4, 0, 'C', 0, 0, '', '', true, 0);
                $pdf->Ln(10);
                $pdf->SetFont('freesans', 'BU', $grade_size);
                $pdf->SetTextColor($record8[0], $record8[1], $record8[2]);
                $pdf->MultiCell(0, 0, $grade_name, 0, 'C', 0, 0, '', '', true, 0);
                $pdf->Ln(15);
                /*                 * ************************************************* */
                $pdf->setCellPaddings($left = '45', $top = '', $right = '46', $bottom = '');
                if ($text5 != "" && $attended_date != NULL) {
                    $pdf->SetFont('freesans', 'B', $text5_size);
                    $pdf->SetTextColor($record5[0], $record5[1], $record5[2]);
                    $pdf->Cell(0, 0, $date1, 0, false, 'L', 0, '', 0, false, 'T', 0);
                }

                if ($cust_company_logo != "") {
                    $pdf->Image(ADMIN_HTTP_PATH . $properties['jdbc.comp_image_folder'] . "/" . $cust_company_logo, '', '', '', '', '', '', '', true, 300, 'C');
                }

                if ($sign_path != "") {
                    $pdf->Image(ADMIN_HTTP_PATH . $properties['jdbc.signatory_image_folder'] . "/" . $sign_path, 220, 142, '', '', '', '', '', true, 300, FALSE);
                }
                if ($text5 != "" && $attended_date != NULL) {
                    $pdf->Line(30, 156, 100, 156);
                }
                if ($text6 != "") {
                    $pdf->Line(200, 156, 270, 156);
                }
                $pdf->Ln(1);
                $pdf->setCellPaddings($left = '45', $top = '', $right = '30', $bottom = '');
                if ($text5 != "" && $attended_date != NULL) {
                    $pdf->SetFont('freesans', 'B', $text5_size);
                    $pdf->SetTextColor($record5[0], $record5[1], $record5[2]);
                    $pdf->Cell(0, 30, $text5, 0, false, 'L', 0, '', 0, false, 'T', 'M');
                }
                if ($text6 != "") {
                    $pdf->SetFont('freesans', 'B', $text6_size);
                    $pdf->SetTextColor($record6[0], $record6[1], $record6[2]);
                    $pdf->Cell(0, 30, $text6, 0, false, 'R', 0, '', 0, false, 'T', 'M');
                }
                /*                 * ************************************************* */

// --- example with background set on page ---
// remove default header
                $pdf->setPrintHeader(false);
                $pdf->setPrintFooter(false);

// ---------------------------------------------------------
//Close and output PDF document
                die($pdf->Output('certificate.pdf', 'I'));
            }
        }
    }
    if ($medal == FALSE) {

        $stmt = $offering->get_grade_cert();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            if ($row['cut_off'] == null) {

                $grade_name_1 = $row['grade'];

                $offering->template_id = $template_id;
                $stmt4 = $offering->get_cert_template();
                if ($row4 = $stmt4->fetch(PDO::FETCH_ASSOC)) {
                    $template_name_1 = $row4['template_name'];
                    $template_file_1 = $row4['template_file'];
                    $text1_1 = $row4['text1'];
                    $text2_1 = $row4['text2'];
                    $text3_1 = $row4['text3'];
                    $text4_1 = $row4['text4'];
                    $text5_1 = $row4['text5'];
                    $text6_1 = $row4['text6'];

                    $text1_color_1 = $row4['text1_color'];
                    $record1_1 = explode(",", $text1_color_1);
                    $text2_color_1 = $row4['text2_color'];
                    $record2_1 = explode(",", $text2_color_1);
                    $text3_color_1 = $row4['text3_color'];
                    $record3_1 = explode(",", $text3_color_1);
                    $text4_color_1 = $row4['text4_color'];
                    $record4_1 = explode(",", $text4_color_1);
                    $text5_color_1 = $row4['text5_color'];
                    $record5_1 = explode(",", $text5_color_1);
                    $text6_color_1 = $row4['text6_color'];
                    $record9_1 = explode(",", $text6_color_1);
                    $comp_name_color_1 = $row4['comp_name_color'];
                    $record10_1 = explode(",", $comp_name_color_1);
                    $name_color_1 = $row4['name_color'];
                    $record6_1 = explode(",", $name_color_1);
                    $course_color_1 = $row4['course_color'];
                    $record7_1 = explode(",", $course_color_1);
                    $grade_color_1 = $row4['grade_color'];
                    $record8_1 = explode(",", $grade_color_1);

                    $text1_size_1 = $row4['text1_size'];
                    $text2_size_1 = $row4['text2_size'];
                    $text3_size_1 = $row4['text3_size'];
                    $text4_size_1 = $row4['text4_size'];
                    $text5_size_1 = $row4['text5_size'];
                    $text6_size_1 = $row4['text6_size'];
                    $comp_name_size_1 = $row4['comp_name_size'];
                    $name_size_1 = $row4['name_size'];
                    $course_size_1 = $row4['course_size'];
                    $grade_size_1 = $row4['grade_size'];

                    $comp_name_path_1 = $row4['comp_name_path'];
                    $sign_path_1 = $row4['sign_path'];
                    $company_name_con = $row2['comp_name'];

                    //$GLOBALS['s_path_1'] = $mobitute_template . $template_name_1 . '/' . $template_file_1;
                    define('TEMPLATE_PATH2', ADMIN_HTTP_PATH . $properties['jdbc.cert_template'] . "/" . $template_id . '/' . rawurlencode($template_file_1));

// Extend the TCPDF class to create custom Header and Footer
                    class MYPDF extends TCPDF {

                        //Page header
                        public function Header() {

                            // get the current page break margin
                            $bMargin = $this->getBreakMargin();
                            // get current auto-page-break mode
                            $auto_page_break = $this->AutoPageBreak;
                            // disable auto-page-break
                            $this->SetAutoPageBreak(false, 0);
                            // set bacground image
                            //$img_file = $GLOBALS['s_path_1'];
                            $this->Image(TEMPLATE_PATH2, 0, 0, 297, 210, '', '', '', false, 300, '', false, false, 0);
                            // restore auto-page-break status
                            $this->SetAutoPageBreak($auto_page_break, $bMargin);
                            // set the starting point for the page content
                            $this->setPageMark();
                        }

                    }

// create new PDF document
                    $pdf = new MYPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
                    $pdf->SetCreator(PDF_CREATOR);
                    $pdf->SetTitle('Certificate');

// set header and footer fonts
                    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));

// set default monospaced font
                    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
                    $pdf->SetMargins(0, 0, 0);
                    $pdf->SetHeaderMargin(0);
                    $pdf->SetFooterMargin(0);

// remove default footer
                    $pdf->setPrintFooter(false);

// set auto page breaks
                    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
                    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
                    if (@file_exists(dirname(__FILE__) . '/lang/eng.php')) {
                        require_once(dirname(__FILE__) . '/lang/eng.php');
                        $pdf->setLanguageArray($l);
                    }

// add a page
                    $pdf->AddPage();

// Print a text
                    $pdf->Ln(20);
                    if ($comp_name_path_1 != "") {
                        $pdf->Image(ADMIN_HTTP_PATH . $properties['jdbc.comp_name_image_folder'] . "/" . $comp_name_path_1, '', '', '', '', '', '', '', true, 300, 'C');
                    } else if ($company_name_con != "") {
                        $pdf->SetFont('freesans', 'B', $comp_name_size_1);
                        $pdf->SetTextColor($record10_1[0], $record10_1[1], $record10_1[2]);
                        $pdf->MultiCell(0, 0, $cust_company_name, 0, 'C', 0, 0, '', '', true, 0);
                    }
                    $pdf->Ln(22);
                    $pdf->SetFont('freesans', 'B', $text1_size_1);
                    $pdf->SetTextColor($record1_1[0], $record1_1[1], $record1_1[2]);
                    $pdf->MultiCell(0, 0, $text1_1, 0, 'C', 0, 0, '', '', true, 0);
                    $pdf->Ln(22);
                    $pdf->SetFont('freesans', 'B', $text2_size_1);
                    $pdf->SetTextColor($record2_1[0], $record2_1[1], $record2_1[2]);
                    $pdf->MultiCell(0, 0, $text2_1, 0, 'C', 0, 0, '', '', true, 0);
                    $pdf->Ln(12);
                    $pdf->SetFont('freesans', 'B', $name_size_1);
                    $pdf->SetTextColor($record6_1[0], $record6_1[1], $record6_1[2]);
                    $pdf->MultiCell(0, 0, $first_name . ' ' . $last_name, 0, 'C', 0, 0, '', '', true, 0);
                    $pdf->Ln(20);
                    $pdf->SetFont('freesans', 'B', $text3_size_1);
                    $pdf->SetTextColor($record3_1[0], $record3_1[1], $record3_1[2]);
                    $pdf->MultiCell(0, 0, $text3_1, 0, 'C', 0, 0, '', '', true, 0);
                    $pdf->Ln(11);
                    $pdf->SetFont('freesans', 'B', 20);
                    $pdf->SetTextColor($record7_1[0], $record7_1[1], $record7_1[2]);
                    $pdf->MultiCell(0, 0, $course_name, 0, 'C', 0, 0, '', '', true, 0);
                    $pdf->Ln(14);
//                    if ($score != 0) {
//                        $pdf->SetFont('times', 'B', $text4_size_1);
//                        $pdf->SetTextColor($record4_1[0], $record4_1[1], $record4_1[2]);
//                        $pdf->MultiCell(0, 0, $text4_1, 0, 'C', 0, 0, '', '', true, 0);
//                        $pdf->Ln(10);
//                        $pdf->SetFont('times', 'BU', $grade_size_1);
//                        $pdf->SetTextColor($record8_1[0], $record8_1[1], $record8_1[2]);
//                        $pdf->MultiCell(0, 0, $grade_name_1, 0, 'C', 0, 0, '', '', true, 0);
//                        $pdf->Ln(15);
//                    } else {
                    $pdf->Ln(10);
                    $pdf->Ln(15);
//                    }
                    /*                     * ************************************************* */

                    $pdf->setCellPaddings($left = '45', $top = '', $right = '46', $bottom = '');
                    if ($text5_1 != "" && $attended_date != NULL) {
                        $pdf->SetFont('freesans', 'B', $text5_size_1);
                        $pdf->SetTextColor($record5_1[0], $record5_1[1], $record5_1[2]);
                        $pdf->Cell(0, 0, $date1, 0, false, 'L', 0, '', 0, false, 'T', 0);
                    }
                    if ($cust_company_logo != "") {
                        $pdf->Image(ADMIN_HTTP_PATH . $properties['jdbc.comp_image_folder'] . "/" . $cust_company_logo, '', '', '', '', '', '', '', true, 300, 'C');
                    }
                    if ($sign_path_1 != "") {
                        $pdf->Image(ADMIN_HTTP_PATH . $properties['jdbc.signatory_image_folder'] . "/" . $sign_path_1, 220, 142, '', '', '', '', '', true, 300, FALSE);
                    }
                    if ($text5_1 != "" && $attended_date != NULL) {
                        $pdf->Line(30, 156, 100, 156);
                    }
                    if ($text6_1 != "") {
                        $pdf->Line(200, 156, 270, 156);
                    }
                    $pdf->Ln(1);
                    $pdf->setCellPaddings($left = '45', $top = '', $right = '30', $bottom = '');
                    if ($text5_1 != "" && $attended_date != NULL) {
                        $pdf->SetFont('freesans', 'B', $text5_size_1);
                        $pdf->SetTextColor($record5_1[0], $record5_1[1], $record5_1[2]);
                        $pdf->Cell(0, 30, $text5_1, 0, false, 'L', 0, '', 0, false, 'T', 'M');
                    }
                    if ($text6_1 != "") {
                        $pdf->SetFont('freesans', 'B', $text6_size_1);
                        $pdf->SetTextColor($record6_1[0], $record6_1[1], $record6_1[2]);
                        $pdf->Cell(0, 30, $text6_1, 0, false, 'R', 0, '', 0, false, 'T', 'M');
                    }
                    /*                     * ************************************************* */

// --- example with background set on page ---
// remove default header
                    $pdf->setPrintHeader(false);
                    $pdf->setPrintFooter(false);

// ---------------------------------------------------------
//Close and output PDF document

                    die($pdf->Output('certificate.pdf', 'I'));
                }
            } else if($score < $row['cut_off']){
                echo "<html><center><br/><br/><h2>Congratulations on passing the course! However, as your score did not meet the required cut-off, the certificate has not been issued.</h2></center></html>";
		break;
            }
        }
    }