<?php
include_once(dirname(dirname(dirname(__FILE__))) . '/classes/check.class.php');
include_once(dirname(dirname(dirname(__FILE__))) . '/admin/classes/functions.php');

if ( empty($_GET['token']) ) {
	die('No Invoice');
}

if ( $_GET['token'] == 'creedDefaultToken' ) {
	$invoice_id = @$_GET['invoice_id'];
} else {
    $token = base64_decode($_GET['token']);
    $invoice_id = str_replace('creedToken::', '', $token);
    $invoice_id = str_replace('::frToken', '', $invoice_id);
    $invoice_id = $invoice_id / 333;
}

//$invoice_id = @$_GET['invoice_id'];

if ( !$invoice_id ) {
	die('No Invoice');
}

$db = DB::getInstance();

$invoice = $db->query('SELECT * FROM invoice WHERE id = ?', [$invoice_id])->toArray();

if ( empty($invoice) )
	die('No Invoice');

$invoice = $invoice[0];

$clientInfo = null;
if ( !empty($invoice['client_id']) ) {
	$clientInfo = getClient($invoice['client_id']);
}

if ( isset($_GET['email']) && $_GET['email'] == 'y' )
{
    if ( filter_var($invoice['email'], FILTER_VALIDATE_EMAIL) )
    {
//	    $to = 'faisal.islam70@gmail.com';
	    $to = $invoice['email'];
        $subject = 'facture# ' . $invoice_id . ' from ' . $_SERVER['HTTP_HOST'];
	    $from = 'no-reply@bossesg.com';

// To send HTML mail, the Content-type header must be set
	    $headers  = 'MIME-Version: 1.0' . "\r\n";
	    $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

	    //populate cc
        $ccArray = array();
        if ( !empty($clientInfo['additional_email']) ) {
            $add_emails = explode(',', $clientInfo['additional_email']);
            foreach ( $add_emails as $ae ) {
                if ( filter_var($ae, FILTER_VALIDATE_EMAIL) ) {
	                $ccArray[] = $ae;
                }
            }
        }

// Create email headers
        $cc = !empty($ccArray) ? 'Cc: ' . implode(',', $ccArray) . "\r\n" : '';
	    $headers .= 'From: '.$from."\r\n".
	                'Reply-To: '.$from."\r\n" .
                    $cc .
	                'X-Mailer: PHP/' . phpversion();
	    $message = file_get_contents( 'http://' . $_SERVER['HTTP_HOST'] . '/admin/invoice/index-email.php?bps=nscreed&token=creedDefaultToken&invoice_id=' . $invoice_id);

	    $invoicePublicURL = 'http://' . $_SERVER['HTTP_HOST'] . '/admin/invoice/index-email.php?token=' . base64_encode('creedToken::' . $invoice_id * 333 .'::frToken');

	    $message = str_ireplace('{{+PUBLIC_URL+}}', $invoicePublicURL, $message);

// Sending email
	    if( mail($to, $subject, $message, $headers) )
	    {
            echo 'Votre courriel a été envoyé à ' . $invoice['email'];
	    } else {
            echo 'Impossible d\'envoyer le courriel. SVP essayer à nouveau.';
	    }
    } else {
        echo '<h2>Aucune adresse courriel disponible <b>'. $invoice['email'] .'</b>.</h2>';
    }

    //die;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
	
	<title>Facture</title>

    <style>
        /*
	 CSS-Tricks Example
	 by Chris Coyier
	 http://css-tricks.com
*/
        @import url('https://fonts.googleapis.com/css?family=Cousine');
        * { margin: 0; padding: 0; }
        body {
            font-family: 'Cousine', monospace;
            font-size: 14px;
        }
        #page-wrap { width: 800px; margin: 0 auto; }

        textarea { border: 0; font: 14px Georgia, Serif; overflow: hidden; resize: none; }
        table { border-collapse: collapse; }
        table td, table th { border: 1px solid black; padding: 2px; }

        #header { height: 15px; width: 100%; margin: 10px 0; background: #222; text-align: center; color: white; font: bold 15px Helvetica, Sans-Serif; text-decoration: uppercase; letter-spacing: 20px; padding: 8px 0px; }

        #address { width: 420px; float: right; text-align: right; }
        #customer { overflow: hidden; }

        #logo { text-align: left; float: left; position: relative; margin-top: 5px; overflow: hidden; }
        /*#logo:hover, #logo.edit { border: 1px solid #000; margin-top: 0px; max-height: 125px; }*/
        #logoctr { display: none; }
        /*#logo:hover #logoctr, #logo.edit #logoctr { display: block; text-align: right; line-height: 25px; background: #eee; padding: 0 5px; }*/
        #logohelp { text-align: left; display: none; font-style: italic; padding: 10px 5px;}
        #logohelp input { margin-bottom: 5px; }
        .edit #logohelp { display: block; }
        .edit #save-logo, .edit #cancel-logo { display: inline; }
        .edit #image, #save-logo, #cancel-logo, .edit #change-logo, .edit #delete-logo { display: none; }
        #customer-title { font-size: 14px; font-weight: 500; float: left; margin-top: 5px; }

        #meta { margin-top: 1px; width: 300px; float: right; }
        #meta td { text-align: right;  }
        #meta td.meta-head { text-align: left; background: #eee; }
        #meta td textarea { width: 100%; height: 20px; text-align: right; }

        #items { clear: both; width: 100%; margin: 10px 0 0 0; border: 1px solid black; }
        #items th { background: #eee; }
        #items textarea { width: 80px; height: 50px; }
        #items tr.item-row td { border: 0; vertical-align: top; }
        #items td.description { width: 300px; }
        #items td.item-name { width: 450px; }
        #items td.description textarea, #items td.item-name textarea { width: 100%; }
        #items td.total-line { border-right: 1px solid #444444; text-align: right; background: #e4e4e4 !important; }
        #items td.total-value { border-left: 0; padding: 2px; }
        #items td.total-value textarea { height: 20px; background: none; }
        #items td.balance { background: #eee; }
        #items td.blank { border: 0; }

        #items .item-row td:last-child { text-align: right; width: 60px; }

        #terms { text-align: center; margin: 20px 0 0 0; }
        #terms h5 { text-transform: uppercase; font: 13px Helvetica, Sans-Serif; letter-spacing: 10px; border-bottom: 1px solid black; padding: 0 0 8px 0; margin: 0 0 8px 0; }
        #terms textarea { width: 100%; text-align: center;}

        textarea:hover, textarea:focus, #items td.total-value textarea:hover, #items td.total-value textarea:focus, .delete:hover { background-color:#EEFF88; }

        .delete-wpr { position: relative; }
        .delete { display: block; color: #000; text-decoration: none; position: absolute; background: #EEEEEE; font-weight: bold; padding: 0px 3px; border: 1px solid; top: -6px; left: -22px; font-family: Verdana; font-size: 12px; }

        #address { width: 550px; }
        #address .address-details {  margin-right: 10px; display: inline-block; width: 500px !important; }

        .address-signature {
            /*width: 450px;*/
            margin-top: 20px;
            /*float: left;*/
            font-size: 11px;
        }
        .address-signature img {
            height: 50px;
        }
        .address-signature h3 {
            font-size: 10px;
            border-top: 1px dashed #666;
            padding-top: 5px;
        }
        .half-w { width: 45%; float: left; }
        .half-w+.half-w { float: right; }
        .car-info {
            font-size: 13px;
            padding: 7px 0;
margin-top:5px;
        }
    </style>

</head>

<body>

<?php if ( isset($_GET['bps']) && $_GET['bps'] == 'nscreed' ): ?>
    <h4 style="text-align: center; margin-top: 20px;">
        See your invoice in the <b><a href="{{+PUBLIC_URL+}}"><?php echo $_SERVER['HTTP_HOST']; ?></a></b> website.
    </h4>
<?php endif; ?>

	<div id="page-wrap">

		<div id="header">FACTURE</div>

        <div id="identity">

            <div id="logo">
                <img id="image" style="height: 95px;" src="<?php echo 'http://' . $_SERVER['HTTP_HOST']; ?>/admin/invoice/images/email-logo.png" alt="logo" />
            </div>

            <div style="margin-top: 10px; padding-right:0;" id="address">
                <!--				<div class="address-details">-->
                <!--                    9059-1652 Québec Inc.<br>-->
                <!--                    7 des Tournois, Blainville,<br>-->
                <!--                    Québec<br>-->
                <!--                    514-916-7514<br>-->
                <!--                    katyadufour@bosse.ca-->
                <!--                </div>-->
                <span style="color: #ff8f73; font-weight: 600; text-transform: uppercase; line-height: 2;"><?php echo !empty($invoice['company']) ? $invoice['company'] : ( $clientInfo ? $clientInfo['company']: '' ); ?></span>
                <br>
                <span style="font-size: 15px; color: #2f3640;">
                    <?php echo $clientInfo['address']; ?> <br>
                    <?php echo $clientInfo['tel']; ?> <br>
                    <?php echo $clientInfo['email']; ?> <br>
                </span>
            </div>

        </div>

        <div style="clear:both"></div>

        <div id="customer">

            <div id="customer-title">

                <!--				<span style="color: #ff8f73; font-weight: 600; text-transform: uppercase; line-height: 2;">--><?php //echo !empty($invoice['company']) ? $invoice['company'] : ( $clientInfo ? $clientInfo['company']: '' ); ?><!--</span>-->
                <!--				<br>-->
                <!--                <span style="font-size: 15px; color: #2f3640;">-->
                <!--                    --><?php //echo $clientInfo['address']; ?><!-- <br>-->
                <!--                    --><?php //echo $clientInfo['tel']; ?><!-- <br>-->
                <!--                    --><?php //echo $clientInfo['email']; ?>
                <!--                </span>-->

                <strong>9059-1652 Québec Inc.</strong><br>
                7 des Tournois, Blainville,<br>
                Québec<br>
                514-916-7514<br>
                katyadufour@bosse.ca

            </div>

            <table id="meta">
                <tr>
                    <td class="meta-head"># de facture</td>
                    <td><textarea><?php echo $invoice['reclamation']; ?></textarea></td>
                </tr>
                <tr>
                    <td class="meta-head">Date</td>
                    <td><textarea id="date"><?php echo date('F j, Y', strtotime($invoice['created_at'])); ?></textarea></td>
                </tr>
                <tr>
                    <td class="meta-head">Status</td>
                    <td style="color: <?php echo $invoice['payment_status'] == 1 ? 'green' : 'red'; ?>;"><?php echo $invoice['payment_status'] == 1 ? 'payé' : 'non-payé'; ?></td>
                </tr>
                <tr>
                    <td class="meta-head">Paiement</td>
                    <td><?php echo $invoice['payment_method']; ?></td>
                </tr>
            </table>

        </div>
		

        <div class="car-info">
            <strong>VIN:</strong> <?php echo $invoice['vin']; ?>
            <strong>Marque:</strong> <?php echo $invoice['brand']; ?>
            <strong>Modèle:</strong> <?php echo $invoice['model']; ?>
            <strong>Année:</strong> <?php echo $invoice['year']; ?>
            <strong>P.A:</strong> <?php echo $invoice['particular_area']; ?>
            <strong>B.T:</strong> <?php echo $invoice['brake_type']; ?>
        </div>

		<table id="items">
		
		  <tr>
		      <th>Items</th>
		      <th>Description</th>
		      <th>Prix</th>
		  </tr>

		  <?php
		  	$items = array(
				1 => 'CAPOT',
				2 => 'PAVILLON',
				3 => 'DESSUS HAYON',
				4 => 'VALISE HAYON',
				5 => 'PAN. LATERAL G',
				6 => 'PORTE ARR. G',
				7 => 'PORTE AV. G',
				8 => 'LONGERON G',
				9 => 'AILE G',
				10 => 'AILE D',
				11 => 'PORTE Av. D',
				12 => 'PORTE ARR. D',
				13 => 'PAN LATERAL D',
				14 => 'LONGERON D',
				15 => 'OTHER',
				'stripping' => 'Dégarnissage',
				'other_fees' => 'Autres',
				'glazier' => 'Vitrier',
				'work_force' => 'Main d`oeuvre',
				'parts' => 'Pièces'
			);
		  ?>

		  <?php foreach ( $items as $key => $item ):

			  //if ( $invoice['inv_' . $key . '_price'] <= 0 ) continue;

			  ?>
			  <tr class="item-row">
				  <td>
					  <?php echo $item; ?>
				  </td>
				  <td class="item-name">
					  <div class="delete-wpr"><?php echo $invoice['inv_' . $key . '_note']; ?></div>
				  </td>
				  <td>
					  <span class="price">$<?php echo $invoice['inv_' . $key . '_price']; ?></span>
				  </td>
			  </tr>
		  <?php endforeach; ?>

<tr class="item-row">
    <td colspan="3"></td>
</tr>

		  <tr>
		      <td colspan="2" class="total-line">Sous-total</td>
		      <td class="total-value">
				  <div id="subtotal">$<?php echo $invoice['sub_total']; ?></div>
			  </td>
		  </tr>
		  <tr>
<!--		      <td class="blank"> </td>-->
		      <td  colspan="2" class="total-line">142389147 TPS(5%)</td>
		      <td class="total-value">
				  <div id="subtotal">$<?php echo $invoice['tps']; ?></div>
			  </td>
		  </tr>
		  <tr>
<!--		      <td class="blank"> </td>-->
		      <td colspan="2"  class="total-line">1021225025 TVQ(9.975%)</td>
		      <td class="total-value">
				  <div id="subtotal">$<?php echo $invoice['tvq']; ?></div>
			  </td>
		  </tr>
		  <tr>
<!--		      <td class="blank"> </td>-->
		      <td  colspan="2" class="total-line">Franchise</td>
		      <td class="total-value">
				  <div id="subtotal">$<?php echo $invoice['franchise']; ?></div>
			  </td>
		  </tr>

		  <tr>
<!--		      <td class="blank"> </td>-->
		      <td  colspan="2" class="total-line">Total</td>
		      <td class="total-value">
				  <div id="subtotal">$<?php echo $invoice['total']; ?></div>
			  </td>
		  </tr>

		  <tr>
<!--		      <td class="blank"> </td>-->
		      <td colspan="2"  class="total-line">Dépôt</td>
		      <td class="total-value">
				  <div id="subtotal">$<?php echo $invoice['deposit']; ?></div>
			  </td>
		  </tr>

		  <tr>
<!--		      <td class="blank"> </td>-->
		      <td  colspan="2" class="total-line">Balance</td>
		      <td class="total-value">
				  <div id="subtotal">$<?php echo $invoice['balance']; ?></div>
			  </td>
		  </tr>

		</table>
		
<!--		<div id="terms">-->
<!--		  <h5>Terms</h5>-->
<!--		  <p>A Custom Note</p>-->
<!--		</div>-->


        <div class="address-signature">
            <div class="half-w">
            <p>Je soussigné(e), assuré(e) ou représentant de l’assuré(e), déclare avoir pris connaissance des réparations de débosselage sans peinture exécuter sur mon véhicule par Carrossier Express, et je m’en déclare entièrement satisfait(e).</p>
            </div>
            <div class="half-w">
                <img src="<?php echo $invoice['signature_img']; ?>" alt="Signature of <?php echo $invoice['f_name'] . ' ' . $invoice['l_name']; ?>">
                <h3>Signature du client</h3>
            </div>
        </div>

	</div>

</body>

</html>