<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Exporter extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library("Pdf");
        $this->load->model('epm_listings/pdfs_model');
        
        if (!$this->session->userdata('logged_in')) {
            $this->session->set_flashdata('noaccess', 'Sorry, you are not logged in');
            redirect('auth/login');
        }
    }
    
        /**
	 * Export to Pdf engine list
	 */


    public function create_pdf_engines() {
        
        
        $idz = $this->ion_auth->get_user_id();
        $user = $this->ion_auth->user($idz)->row();
            
            if($user->listing == 'Y') 
            {
                

        //arguments can be modified in file tcpdf_config.php inside libraries/tcpdf/config
        // create new PDF document
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor(PDF_AUTHOR);
        $pdf->SetTitle(PDF_HEADER_TITLE);
        $pdf->SetSubject('Engines');
        $pdf->SetKeywords('engines, pdf, weekly, listings');


        // set default header data
        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

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

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

        // set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        // 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);
        }

        // ---------------------------------------------------------     
        // set default font subsetting mode
        $pdf->setFontSubsetting(true);

        // Set font
        // dejavusans is a UTF-8 Unicode font, if you only need to
        // print standard ASCII chars, you can use core fonts like
        // helvetica or times to reduce file size.
        $pdf->SetFont('Helvetica', '', 14, '', true);

        // Add a page
        // This method has several options, check the source code documentation for more information.
        $pdf->AddPage();

        // set text shadow effect
        $pdf->setTextShadow(array('enabled' => true, 'depth_w' => 0.2, 'depth_h' => 0.2, 'color' => array(196, 196, 196), 'opacity' => 1, 'blend_mode' => 'Normal'));

        // Set some content to print


        $this->data['listings_exp'] = $this->pdfs_model->get_listings_export();
        $this->data['count'] = $this->pdfs_model->get_totalcount_exp();

        $html = '';
        $html .= "<style type=text/css>";
        $html .= "th{color: #fff; font-weight: bold; background-color: #222; border-collapse:collapse; font-size:10px}";
        $html .= "td{background-color: #ffffff; color: #000000; font-size:10px}";
        $html .= "</style>";
        $html .= "<h2>Total Engine Listings </h2><h4>Current Available: " . $this->data['count'] . ".</h4>";
        $html .= "<table width='100%'>";
        $html .= "<tr><th>Manufacturer</th><th>Engine Model</th><th>ESN</th><th>Lister</th><th>Transaction</th><th>Location</th>
            <th>Asking Price</th><th>Monthly Rent</th><th>Available Date</th></tr>";

        if ($this->data['listings_exp'] != NULL) {

            foreach ($this->data['listings_exp'] as $row) {
                $manufacturer = $row->manufacturer;
                $engine_model = $row->engine_model;
                $esn = $row->esn;
                $lister = $row->lister;
                $transaction = $row->transaction;
                $location = $row->location;
                $asking_price = $row->asking_price;
                $monthly_rent = $row->monthly_rent;
                $available_date = $row->available_date;

                $html .= "<tr><td class='manufacturer'>" . $manufacturer . "</td><td class='engine_model'>" . $engine_model . "</td><td class='esn'>" . $esn . "</td><td class='lister'>" . $lister . "</td>
                <td class='selection'>" . $transaction . "</td><td class='location'>" . $location . "</td><td class='asking_price'>" . $asking_price . "</td><td class='monthly_rent'>" . $monthly_rent . "</td>
                    <td class='available_date'>" . $available_date . "</td></tr>";
            }
        }

        $html .= "</table>";

        //    // Print text using writeHTMLCell()
        $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

        // reset pointer to the last page
        $pdf->lastPage();

        // ---------------------------------------------------------    
        // Close and output PDF document
        // This method has several options, check the source code documentation for more information.
        $pdf->Output('My_Engines.pdf', 'I');
      
        //============================================================+
        // END OF FILE
        //============================================================+ 
            }
            else
            {
                $this->data['subview'] = 'listings/listing_disabled';
                $this->load->view("layouts/main_logged_in", $this->data);
            }
        
    }
    

        /**
	 * Export to Pdf engine parts list
	 */


    public function create_pdf_parts() {

        //arguments can be modified in file tcpdf_config.php inside libraries/tcpdf/config
        // create new PDF document
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor(PDF_AUTHOR);
        $pdf->SetTitle(PDF_HEADER_TITLE);
        $pdf->SetSubject('Parts');
        $pdf->SetKeywords('parts, pdf, weekly, listings');


        // set default header data
        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

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

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

        // set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        // 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);
        }

        // ---------------------------------------------------------     
        // set default font subsetting mode
        $pdf->setFontSubsetting(true);

        // Set font
        // dejavusans is a UTF-8 Unicode font, if you only need to
        // print standard ASCII chars, you can use core fonts like
        // helvetica or times to reduce file size.
        $pdf->SetFont('Helvetica', '', 14, '', true);

        // Add a page
        // This method has several options, check the source code documentation for more information.
        $pdf->AddPage();

        // set text shadow effect
        $pdf->setTextShadow(array('enabled' => true, 'depth_w' => 0.2, 'depth_h' => 0.2, 'color' => array(196, 196, 196), 'opacity' => 1, 'blend_mode' => 'Normal'));

        // Set some content to print


        $this->data['plistings_exp'] = $this->pdfs_model->get_plistings_export();
        $this->data['pcount'] = $this->pdfs_model->get_totalpcount_exp();

        $html = '';
        $html .= "<style type=text/css>";
        $html .= "th{color: #fff; font-weight: bold; background-color: #222; border-collapse:collapse; font-size:10px}";
        $html .= "td{background-color: #ffffff; color: #000000; font-size:10px}";
        $html .= "</style>";
        $html .= "<h2>Total Parts Listings </h2><h4>Current Available: " . $this->data['pcount'] . ".</h4>";
        $html .= "<table width='100%'>";
        $html .= "<tr><th>Part Number</th><th>Part Condition</th><th>Tag</th><th>Quantity</th><th>Date Listed</th></tr>";

        if ($this->data['plistings_exp'] != NULL) {

            foreach ($this->data['plistings_exp'] as $row) {
                $part_number = $row->part_number;
                $part_condition = $row->part_condition;
                $tag = $row->tag;
                $quantity = $row->quantity;
                $date_listed = $row->date_listed;

                $html .= "<tr><td class='part_number'>" . $part_number . "</td><td class='part_condition'>" . $part_condition . "</td><td class='tag'>" . $tag . "</td><td class='quantity'>" . $quantity . "</td><td class='date_listed'>" . $date_listed . "</td></tr>";
            }
        }

        $html .= "</table>";

        //    // Print text using writeHTMLCell()
        $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

        // reset pointer to the last page
        $pdf->lastPage();

        // ---------------------------------------------------------    
        // Close and output PDF document
        // This method has several options, check the source code documentation for more information.
        $pdf->Output('My_Parts.pdf', 'I');

        //============================================================+
        // END OF FILE
        //============================================================+        
    }

}
