<?php
ob_start();
$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );
$pdfgen_settings = get_option('_custom_pdfgen_all_settings');
$newDate =  date("d/m/Y");
if(isset($pdfgen_settings) && !empty($pdfgen_settings)){
   extract($pdfgen_settings);

   if(isset($fdate) && !empty($fdate)){
      $originalDate = $fdate;
      $newDate = date("d/m/Y", strtotime($originalDate));
   }else{
      $newDate ='30/10/2021';
   } 
   
   if(isset($_POST['fdate']) && !empty($_POST['fdate'])){
      $originalDate = $_POST['fdate'];
      $newDate = date("d/m/Y", strtotime($originalDate));
   }else{
      $newDate ='30/10/2021';
   }
}
 
$image_url    = '';
$haddress    = '';
$hurl    = '';
$hphone    = '';
$maintitle    = '';
$column1title = '';
$column2title = '';
$column3title = '';
$column1value = '';
$column2value = '';
$column3value = '';
$fdate        = '';
$fcode        = '';
$fsku         = '';
$fpdfname     = '';
$hcompany     = '';
$s_design     = '';
$pageborder     = '';
$bordercolor     = '';

if(isset($_POST) && !empty($_POST)){
   if(isset($_POST['image_url']) && !empty($_POST['image_url'])){$image_url = $_POST['image_url'];}else{$image_url = WCGEN_IMG . 'logo1.png';}
   $maintitle    = $_POST['maintitle'];
   $haddress    = $_POST['haddress'];
   $hurl    = $_POST['hurl'];
   $hphone    = $_POST['hphone'];
   $column1title = $_POST['column1title'];
   $column2title = $_POST['column2title'];
   $column3title = $_POST['column3title'];
   $column1value = $_POST['column1value'];
   $column2value = $_POST['column2value'];
   $column3value = $_POST['column3value'];
   $fdate        = $newDate;
   $fcode        = $_POST['fcode'];
   $fsku         = $_POST['fsku'];
   $fpdfname     = $_POST['fpdfname'];
   $hcompany     = $_POST['hcompany'];
   $s_design     = $_POST['s_design'];
   $pageborder   = $_POST['pageborder'];
   $bordercolor  = $_POST['bordercolor'];
}else{
   if(isset($image_url) && !empty($image_url)){$image_url = $image_url;}else{$image_url = WCGEN_IMG . 'logo1.png';}
   if(isset($hcompany) && !empty($hcompany)){$hcompany = $hcompany;}else{$hcompany ='Hirewebxperts';}
   if(isset($haddress) && !empty($haddress)){$haddress = $haddress;}else{$haddress ='36/2 Opp. Chadha Palace';}
   if(isset($hphone) && !empty($hphone)){$hphone = $hphone;}else{$hphone = '+919034190323';}
   if(isset($hurl) && !empty($hurl)){$hurl = $hurl;}else{$hurl = 'www.plugins.hirewebxperts.com';}
   if(isset($maintitle) && !empty($maintitle)){$maintitle = $maintitle;}else{$maintitle = 'WARRANTY CARD';}
   if(isset($column1title) && !empty($column1title)){$column1title = $column1title;}else{$column1title = 'PRODUCT DETAILS';}
   if(isset($column2title) && !empty($column2title)){$column2title = $column2title;}else{$column2title = 'SN';}
   if(isset($column3title) && !empty($column3title)){$column3title = $column3title;}else{$column3title = 'WARRANTY';}
   if(isset($column1value) && !empty($column1value)){$column1value = $column1value;}else{$column1value = ' Wordpress Plugin
      Custom add to cart button and link
      Custom add to cart button and link PRO
      Text Case Convertor
      Password Manager
      Awesome Checkout For WooCommerce';}
   if(isset($column2value) && !empty($column2value)){$column2value = $column2value;}else{$column2value = 'A15520';}
   if(isset($column3value) && !empty($column3value)){$column3value = $column3value;}else{$column3value = '24 MONTHS RTB';}
   if(isset($fdate) && !empty($fdate)){$fdate = $newDate;}else{$fdate = date("d/m/Y");}
   if(isset($fcode) && !empty($fcode)){$fcode = $fcode;}else{$fcode = 'O-1686/T-8605';}
   if(isset($fsku) && !empty($fsku)){$fsku = $fsku;}else{$fsku = '220921JA0592.978';}
   if(isset($fpdfname) && !empty($fpdfname)){$fpdfname = $fpdfname;}else{$fpdfname = 'dummy';}
   if(isset($s_design) && !empty($s_design)){$s_design = $s_design;}else{$s_design = 'desingL1';}
   if(isset($pageborder) && !empty($pageborder)){$pageborder = $pageborder;}else{$pageborder = '10';}
   if(isset($bordercolor) && !empty($bordercolor)){$bordercolor = $bordercolor;}else{$bordercolor = 'rgb(0 ,255 ,0,1)';}
}

$bordercolor = str_replace("rgb(","",$bordercolor);
$bordercolor = str_replace(")","",$bordercolor);
$bordercolor = explode(",",$bordercolor); 

require('tcpdf.php');

$pdf =new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->setHeaderTemplateAutoreset(true);
// set margins
$pdf->SetMargins(20, 20, 20);


// 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);
}
$content = '<table cellpadding="0" style="text-align:center">
<tr>
   <td cellpadding="10px" style="text-align:left"><h4>'.esc_html($hcompany).'</h4><br>'.esc_textarea($haddress).' <br>Tel: '.esc_html($hphone).'<br>'.esc_html($hurl).'</td>
   <td colspan="3" style="text-align:right"><img src="'.esc_url($image_url).'" height="90"></td>
</tr>
<tr>
   <td colspan="6"></td>
</tr>
<tr>
   <td colspan="6" style="border-top:2px solid #000"></td>
</tr>
<tr>
   <td colspan="6"></td>
</tr>
<tr>
   <td colspan="6" style="text-align:center;">
      <table>
         <tr>
            <td style="font-size:20px;color:#000000;"><h3>'.esc_html($maintitle).'</h3></td>
         </tr>
         <tr>
            <td colspan="6"></td>
         </tr>
      </table>
   </td>
</tr>
<tr>
   <td colspan="6">     
      <table border="1" style="text-align:center" cellpadding="5">
         <thead>
            <tr>
               <th style="width:690px"><h4>'.esc_html($column1title).'</h4></th>
               <th style="width:100px"><h4>'.esc_html($column2title).'</h4></th>
               <th style="width:100px"><h4>'.esc_html($column3title).'</h4></th>
            </tr>
         </thead>
         <tbody>
            <tr>
               <td style="text-align:left">'.nl2br($column1value) .'</td>
               <td>'.esc_html($column2value).'</td>
               <td>'.esc_html($column3value).'</td>
            </tr>
         </tbody>
      </table>
   </td>
</tr>
<tr>
   <td colspan="6">
      <table width="500px" cellpadding="10px" style="text-align:left">
         <tr>
            <td style="font-size:12px;color:#000000;">'.esc_html($fdate).' – '.esc_html($fcode).' SKU: '.esc_html($fsku).'</td>
         </tr>
      </table>
   </td>
</tr>
</table>';

$pdf->SetTitle('Digital Warranty Card Generator');
// $pdf->AddPage('P',"A4");
// $width = $pdf->pixelsToUnits(750); 
// $height = $pdf->pixelsToUnits(750);

// $resolution= array($width, $height);
$pdf->AddPage('L', "A4");
$pdf->writeHTMLCell(0, 0, '', '', $content, 0, 1, 0, true, '', true);
$pdf->SetLineStyle(array('width' => $pageborder, 'color' => $bordercolor));
$pdf->Line(0, 0, $pdf->getPageWidth(), 0);
$pdf->Line($pdf->getPageWidth(), 0, $pdf->getPageWidth(), $pdf->getPageHeight());
$pdf->Line(0, $pdf->getPageHeight(), $pdf->getPageWidth(), $pdf->getPageHeight());
$pdf->Line(0, 0, 0, $pdf->getPageHeight());

if(isset($_POST['submit']) && !empty($_POST['submit']) && $_POST['submit'] == 'Preview'){
   $outputs = "I";
}else{
   $outputs = "d";
}

$pdf->Output($fpdfname.'.pdf', $outputs);
ob_end_flush();
?>