<?php
	$this->load->view('master/common/header.php', $data);
	$base_name = $data['base_name'];
	?>
<?php
	$error_message = '';
	if(isset($_POST['form1'])) {
	    $valid = 1;
	    if(empty($_POST['subject_text'])) {
	        $valid = 0;
	        $error_message .= 'Subject can not be empty\n';
	    }
	    if(empty($_POST['message_text'])) {
	        $valid = 0;
	        $error_message .= 'Subject can not be empty\n';
	    }
	    if($valid == 1) {
	
	        $subject_text = strip_tags($_POST['subject_text']);
	        $message_text = strip_tags($_POST['message_text']);
	
	        // Getting Customer Email Address
	        $statement = $this->db->query("SELECT * FROM tbl_customer WHERE cust_id=?",array($_POST['cust_id']));
	        $result = $statement->result_array();                            
	        foreach ($result as $row) {
	            $cust_email = $row['cust_email'];
	            $cust_name = $row['cust_name'];
	        }
	
	        // Getting Admin Email Address
	        $statement = $this->db->query("SELECT * FROM tbl_settings WHERE id=1");
	        $result = $statement->result_array();                            
	        foreach ($result as $row) {
	            $admin_email = $row['contact_email'];
	        }
	
	        $order_detail = '';
	        $statement = $this->db->query("SELECT * FROM tbl_payment WHERE payment_id=?",array($_POST['payment_id']));
	        $result = $statement->result_array();                            
	        foreach ($result as $row) {
	        	
	        	if($row['payment_method'] == 'PayPal'):
	        		$payment_details = '
	Transaction Id: '.$row['txnid'].'<br>
	        		';
	        	elseif($row['payment_method'] == 'Stripe'):
					$payment_details = '
	Transaction Id: '.$row['txnid'].'<br>
	Card number: '.$row['card_number'].'<br>
	Card CVV: '.$row['card_cvv'].'<br>
	Card Month: '.$row['card_month'].'<br>
	Card Year: '.$row['card_year'].'<br>
	        		';
	        	elseif($row['payment_method'] == 'Bank Deposit'):
					$payment_details = '
	Transaction Details: <br>'.$row['bank_transaction_info'];
	        	endif;
	
	            $order_detail .= '
	Customer Name: '.$row['customer_name'].'<br>
	Customer Email: '.$row['customer_email'].'<br>
	Payment Method: '.$row['payment_method'].'<br>
	Payment Date: '.$row['payment_date'].'<br>
	Payment Details: <br>'.$payment_details.'<br>
	Paid Amount: '.$row['paid_amount'].'<br>
	Payment Status: '.$row['payment_status'].'<br>
	Shipping Status: '.$row['shipping_status'].'<br>
	Payment Id: '.$row['payment_id'].'<br>
	            ';
	        }
	
	        $i=0;
	        $statement = $this->db->query("SELECT * FROM tbl_order WHERE payment_id=?",array($_POST['payment_id']));
	        $result = $statement->result_array();                            
	        foreach ($result as $row) {
	            $i++;
	            $order_detail .= '
	<br><b><u>Product Item '.$i.'</u></b><br>
	Product Name: '.$row['product_name'].'<br>
	Size: '.$row['size'].'<br>
	Color: '.$row['color'].'<br>
	Quantity: '.$row['quantity'].'<br>
	Unit Price: '.$row['unit_price'].'<br>
	            ';
	        }
	
	        $statement = $this->db->query("INSERT INTO tbl_customer_message (subject,message,order_detail,cust_id) VALUES (?,?,?,?)",array($subject_text,$message_text,$order_detail,$_POST['cust_id']));
	
	        // sending email
	        $to_customer = $cust_email;
	        $message = '
	<html><body>
	<h3>Message: </h3>
	'.$message_text.'
	<h3>Order Details: </h3>
	'.$order_detail.'
	</body></html>
	';
	        
	
	        try {
	            
	            $mail->setFrom($admin_email, 'Admin');
	            $mail->addAddress($to_customer, $cust_name);
	            $mail->addReplyTo($admin_email, 'Admin');
	            
	            $mail->isHTML(true);
	            $mail->Subject = $subject;
	
	            $mail->Body = $message;
	            $mail->send();
	
	            $success_message = 'Your email to customer is sent successfully.';   
	        } catch (Exception $e) {
	            echo 'Message could not be sent.';
	            echo 'Mailer Error: ' . $mail->ErrorInfo;
	        }
	        
	        
	
	    }
	}
	?>
<div class="content">
<!-- Start Content-->
<div class="container-fluid">
<!-- start page title -->
<div class="row">
	<div class="col-12">
		<div class="page-title-box">
			<div class="page-title-right">
				<ol class="breadcrumb m-0">
					<li class="breadcrumb-item"><a href="javascript: void(0);"><?= $base_name; ?></a></li>
					<li class="breadcrumb-item active text-capitalize"><?= $page; ?></li>
				</ol>
			</div>
			<h4 class="page-title text-uppercase"><?= $page; ?></h4>
		</div>
	</div>
</div>
<!-- end page title --> 
<div class="row" style="font-size:16px;">
	<?php for($x=0;$x<2;$x++){
		if($x==1){
		    echo '<div class="col-md-12 col-sm-12 col-xs-12"><br>
		<hr>
		<br></div>';
		}
		?>
	<div class="col-md-12" style="<?php if($x==1){echo "display:none;";}?>">
		<div style="width:100%;" style="border:1px solid black;" id="printableArea">
			<div style="width:100%;float:none">
				<div class="row pad-top-botm ">
					<div style="float:left;padding-left:15px;">
						<img src="<?=base_url('uploads/website/favicon/'.$data['favicon']);?>" style="padding-bottom:20px;width:110px;" /> 
					</div>
					<div style="float:left;width:500px;padding-left:15px">
						<strong><?php echo $data['base_name'];?></strong>
						<br />Address : <?php echo $data['contact_address'];?>
						<br>Mob. No.: <?php echo $data['contact_phone'];?>
						<br>Email: <?php echo $data['contact_email'];?>
					</div>
				</div>
			</div>
			<div style="width:100%;float:none">
				<div  class=" pad-top-botm client-info">
					<div style="float:left;width:240px;padding-left:20px;">
						<h5>  <strong>Customer Information</strong></h5>
						<?php
							$i=0;
							$statement1 = $this->db->query("SELECT * FROM tbl_payment where payment_id='$_GET[id]'");
							$result1 = $statement1->result_array();							
							foreach ($result1 as $row1) {
							
							  // select Delivery Address for this order
							           $statement2222 = $this->db->query("SELECT * FROM applied_delivery_address WHERE payment_id = ?",array($_GET['id']));
							           $result222 = $statement2222->result_array();
							           foreach ($result222 as $row222) {
							               $SH_name = $row222['name'];
							               $SH_phone = $row222['phone'];
							               $SH_state = $row222['state'];
							               $SH_district = $row222['district'];
							               $SH_pin_code = $row222['pin_code'];
							               $SH_address = $row222['address'];
							           }
								?>
						<strong> <input type="text"  title="Customer Name" class="border" value="<?php echo $SH_name; ?>"></strong>
						<br />
						<b>Address :</b> <br> 
						<textarea  type="text" title="Address" class="border" ><?php echo $SH_address; ?>,<?php echo $SH_district; ?>, <?php echo $SH_state.' '.$SH_pin_code; ?></textarea>
						<br>
						<b>Call :</b> <input type="text" value="<?php echo $SH_phone; ?>" title="Mobile Number" class="border">
						<br />
						<b>Pin Code :</b> <input type="text" value="<?php echo $SH_pin_code; ?>"  title="Email" class="border">
					</div>
					<div style="float:left;padding-left:5px;width:240px;">
						<h5>  <strong>Bill Details </strong></h5>
						Bill Date : <?php echo date('d-m-Y H:i:s', strtotime($row1['payment_date']));
							?>
						<br />
						Delivery Date : <input type="text" class="border"><br>
						Gst no :  <input type="text" class="border"><br>
						Vehicle Name : <input type="text" class="border"><br>
						Vihicle No : <input type="text"  class="border"><br>
						Reg. State Code : 10<br>
					</div>
					<div style="float:left;width:230px;">
						<h5>  <strong>Payment Details </strong></h5>
						<b>Booking By :<?php echo $row1['payment_method']; ?> </b>
						<br />
						<b>Payment Status :  <?php 
							if($row1['payment_method']=='COD')
							{
							 echo 'N/A (COD)';   
							}
							else{
							echo $row1['payment_status']; 
							
							}
							?> 
						</b>
						<br />
						Purchase Date : 
						<?php echo date('d-m-Y h:i:s A',strtotime($row1['payment_date']));?>
					</div>
					<?php } ?>
				</div>
				<br>
			</div>
		</div>
		<br>
		<div style="width:100%;float:none">
			<div style="width:100%;">
				<div class="col-lg-12 col-md-12 col-sm-12 col-12 col-xs-12">
					<div class="">
						<table class="table table-striped table-bordered table-hover">
							<thead>
								<tr>
									<th>S. No.</th>
									<th>Perticulars</th>
									<th>Quantity.</th>
									<th>MRP Price</th>
									<th>Discounted Price</th>
									<th>Discount</th>
									<th>PV</th>
									<th>Sub Total</th>
								</tr>
							</thead>
							<tbody>
								<?php
									$i=1;
									 $statement1 = $this->db->query("SELECT * FROM tbl_order WHERE payment_id=?",array($row1['payment_id']));
									 $result1 = $statement1->result_array();
									 foreach ($result1 as $row1) { ?>
								<tr>
									<td><?php echo $i++ ?></td>
									<td><?php echo $row1['product_name'];?></td>
									<td>Qty.:<?php echo $row1['quantity'];?><br>
										<?php
											if(!empty($row1['color']))
											{
											?>
										Clour:<?php echo $row1['color'];?><br>
										<?php
											}
											
											if(!empty($row1['size']))
											{
											?>
										Size:<?php echo $row1['size'];?><br>
										<?php
											}
											?>
									</td>
									<td> 
										<i class="fa fa-inr" aria-hidden="true"></i>&nbsp;
										<?php echo $row1['mrp_price'];?>
									</td>
									<td> 
										<i class="fa fa-inr" aria-hidden="true"></i>&nbsp;
										<?php echo $row1['unit_price'];?>
									</td>
									<td> 
										<i class="fa fa-inr" aria-hidden="true"></i>&nbsp;
										<?php echo $row1['mrp_price']-$row1['unit_price'];?>
									</td>
									<td> 
										<i class="fa fa-inr" aria-hidden="true"></i>&nbsp;
										<?php echo $row1['pv'];$pvtotal+=($row1['pv'])*($row1['quantity']);?>
									</td>
									<td>
										<i class="fa fa-inr" aria-hidden="true"></i>&nbsp;
										<?php  $total=($row1['unit_price'])*($row1['quantity']);
											echo $total;
											
											
											?>.00
									</td>
								</tr>
								<?php 
									error_reporting(0);
									         
									     }
									     ?>      
							</tbody>
						</table>
					</div>
					<hr />
					<div style="width:60%;float:left;">
						<div style="float:left;width:60%;">
							<b>
								<!--Digital Signature Verified : -->
							</b>
							<br>
							<!--<img src="img/signature.png" style="width:120px;height:60px;filter: grayscale(100%);">-->
							<br>Proprietor Name : <b>(Admin)</b>
						</div>
						<div style="float:left;width:40%;">
							<br>
							<br>
							<b>Customer Signature </b><br>
						</div>
					</div>
					<div style="width:40%;float:left;">
						<?php
							// select aplied Coupon for this order
							   $statement2222 = $this->db->query("SELECT * FROM applied_coupon WHERE payment_id = ?",array($_GET['id']));
							   $result222 = $statement2222->result_array();
							   foreach ($result222 as $row222) {
							       $coupon_id = $row222['coupon_id'];
							   }
							   
							   // select coupon details for this order
							   $statement2222 = $this->db->query("SELECT * FROM tbl_coupon WHERE coupon_id = ?",array($coupon_id));
							   $result222 = $statement2222->result_array();
							   foreach ($result222 as $row222) {
							       $percentage = $row222['percentage'];
							       $type = $row222['type'];
							   }
							   
							   
							   
							   $statement1 = $this->db->query("SELECT * FROM tbl_order WHERE payment_id=?",array($_GET['id']));
							   $result1 = $statement1->result_array();
							   foreach ($result1 as $row1) {
							       $total=($row1['unit_price'])*($row1['quantity']);
							       $total1+=$total;
							       $total2=($row1['mrp_price'])*($row1['quantity']);
							       $total21+=$total2;
							   }
							   if($percentage){
							       
							  if($type==1)  
							  {
							      $cp = $percentage; 
							  }
							  else
							  {
							   $cp = ($total1/100)*$percentage;
							  }
							   
							   }
							   else
							   {
							       $cp=0.00;
							   }
							   
							   
							   
							   $statement1 = $this->db->query("SELECT * FROM applied_shipping_cost WHERE payment_id=?",array($_GET['id']));
							   $result1 = $statement1->result_array();
							   foreach ($result1 as $row1) {
							       $shipping_cost='&#8377; '.number_format($row1['shipping_cost'],2);
							       $shipping_cost2=$row1['shipping_cost'];
							   }
							   if(empty($shipping_cost)){
							   $shipping_cost="FREE";
							   }
							?>
						<div style="width:300px;float: right;">
							<h5>MRP Total: <strong class="pull-right"><?php echo ' &nbsp;&#8377; '.number_format($total21,2);?></strong></h5>
							<h5>Discount: <strong class="pull-right"><?php echo ' &nbsp;&#8377; '.number_format($total21-$total1,2);?></strong></h5>
							<h5>Sub Total: <strong class="pull-right"><?php echo ' &nbsp;&#8377; '.number_format($total1,2);?></strong></h5>
							<h5>Shipping Cost: <strong class="pull-right"><?php echo $shipping_cost;?></strong></h5>
							<h5>Coupon Discount: <strong class="pull-right"><?php echo ' &nbsp;&#8377; '.number_format($cp,2);?></strong></h5>
							<h5>Self Benefit: <strong class="pull-right"><?php echo ' &nbsp;&#8377; '.number_format($pvtotal,2);?></strong></h5>
							<h4>Bill Amount : <strong class="pull-right"><?php echo ' &nbsp;&#8377; '.number_format($total1-$cp+$shipping_cost2-$pvtotal,2);?></strong></h4>
							<h6> <strong class="text-success pull-right" style="color:green;">You Save <?php echo ' &nbsp;&#8377; '.number_format($total21-$total1+$pvtotal,2);?></strong></h6>
						</div>
						<br>
					</div>
					<div style="width:100%;float:bottom; " class="col-md-12 col-xs-12 col-sm-12 col-12">
						<div style="width:100%;">
							<br>
							<div class="col-lg-12 col-md-12 col-sm-12">
								<strong> Important: </strong>
								<ol>
									<li>
										Please read all terms and polices on  <b><?=$data['web_url'];?></b> for returns, replacement and other issues.
									</li>
								</ol>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<br>      
		<div class="row pad-top-botm print-hide">
			<div class="col-lg-12 col-md-12 col-sm-12">
				<hr />
				<input class="btn btn-primary btn-lg" type="button" onclick="printDiv('printableArea')" value="Print Invoice" />
			</div>
		</div>
		<script>
			function printDiv(divName) {
			   
			     window.print();
			
			}
		</script>
	</div>
	<?php } ?>
</div>
</section>
<style>
	.pad-top-botm {
	padding-bottom:10px;
	padding-top:20px;
	}
	h4 {
	text-transform:uppercase;
	}
	/* =============================================================
	PAGE STYLES
	============================================================ */
	.contact-info span {
	font-size:14px;
	padding:0px 50px 0px 50px;
	}
	.contact-info hr {
	margin-top: 0px;
	margin-bottom: 0px;
	}
	b{
	font-size:12px;
	}
	.client-info {
	font-size:14px;
	}
	.ttl-amts {
	text-align:right;
	padding-right:50px;
	}
	@media print{
	html{
	background-color: #f7f9fa;
	}
	.navbar-custom{
	display: none;
	}
	.left-side-menu{
	display: none;
	}
	.page-title-box{
	display: none;
	}
	}
	@media print{
	    .border{
	        border: 0px solid #fff;
	        border-color: transparent;
            width: 100%;
            background: #f7f9fa;
	    }
	.print-hide{
	display:none;
	}
	pre, blockquote { page-break-inside: avoid;}
	
	.cds{
	display:block !important;
	}
	.border{
	border:0px solid #f7f9fa;
	border:0px solid #f7f9fa;
	}
	}
</style>
<?php
	$this->load->view('master/common/footer.php');
	?>