<?php
session_start();
include_once "../config.php";
require_once '../database1.php';
require_once "../check_admin.php";

$id='';
if(isset($_GET['id']))
{
	$id=$_GET['id'];
}

class userOrder extends database
{
    public function index($id)
    { 
    	$where[]=$id;
    	$sql = ' SELECT ci.*, p.cl_name, p.cl_supplier, p.cl_product_id FROM tb_checkout_item ci INNER JOIN tb_product p ON ci.cl_product_id=p.cl_id WHERE ci.cl_deleted_at IS NULL AND ci.cl_checkout_id = ? ';
    	return $this->callQuery($sql, $where);
		//$where['cl_checkout_id']=$id;
		//return $this->fetchAll('tb_checkout_item',$where);
	}
	 public function getCheckout($id)
    { 
		$where['cl_id']=$id;
		return $this->fetchRow('tb_checkout',$where);
	}
	 public function getProduct($id)
    { 
    	$sql = " SELECT * FROM tb_product WHERE cl_id=? ";

		$where[]=$id;
		return $this->callQuery($sql,$where);
	}
}


$obj = new userOrder();	
$userOrders = $obj->index($id);

$us = $obj->getCheckout($id);
												
include '../header.php';
?>
            <!-- MAIN CONTENT-->
            <div class="main-content">
                <div class="section__content section__content--p30">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="overview-wrap">
                                    <h2 class="title-1">All Order items</h2>
                                    
                                </div>
								
                            </div>
                        </div>
                        
                        <div class="row m-t-25 m-row">
							<div class="col-lg-12">
								<?php
										if($us['cl_invoice'] == 1)
										{
									?>
									<div class="alert alert-success">This order is already converted to Invoice</div>
									<div class="alert alert-success">Invoice No: <?php echo $us['cl_invoice_no']; ?></div>
									<?php
								}
									?>
								 <div class="well-white col-sm-12">
	                                <div class="col-md-6 pull-left">
										<h5 class="small-heading py-4">Pasūtītāja dati</h5>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Vārds</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_fname']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Uzvārds</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_surname']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Kontakttālrunis</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_mobile']);?></p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>E-pasts</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_email']);?></p>
											</div>
										</div>
									</div>
									<div class="col-md-6 pull-left">
										<h5 class="small-heading py-4">Piegādes adrese</h5>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Pilsēta</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_city']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Novads</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_county']);?></p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Adrese</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_address']);?><br/>
												<?php echo htmlspecialchars($us['cl_pin']);?>
												</p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Pasta indeks</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_pasta_indeks']);?> </p>
											</div>
										</div>
									</div>  

									<div class="col-md-6 pull-left">
										<div class="col-sm-12 pull-left">
											<div class="row">
											<div class="col-md-4">
													<p><strong>Delivery Instructions</strong></p>
												</div>
												<div class="col-md-8">
													<p><?php echo htmlspecialchars($us['cl_instruction']);?> </p>
												</div>
											</div>
										</div>
										<div class="col-sm-12 pull-left">
											<div class="row">
											<div class="col-md-4">
													<p><strong>Delivery Type</strong></p>
												</div>
												<div class="col-md-8">
													<p><?php echo htmlspecialchars($us['cl_delivery_type']);?> </p>
												</div>
											</div>
										</div>
										<div class="col-sm-12 pull-left">
											<div class="row">
											<div class="col-md-4">
													<p><strong>Payment Type</strong></p>
												</div>
												<div class="col-md-8">
													<p><?php echo htmlspecialchars($us['cl_payment_type']);?> </p>
												</div>
											</div>
										</div>
										<div class="col-sm-12 pull-left">
											<div class="row">
											<div class="col-md-4">
													<p><strong>Bank Name</strong></p>
												</div>
												<div class="col-md-8">
													<p><?php echo htmlspecialchars($us['cl_bank_name']);?> </p>
												</div>
											</div>
										</div>
										<div class="col-sm-12 pull-left">
											<div class="row">
											<div class="col-md-4">

													<p><strong>Account</strong></p>
												</div>
												<div class="col-md-8">
													<p><?php echo htmlspecialchars($us['cl_account']);?> </p>
												</div>
											</div>
										</div>
										<div class="col-sm-12 pull-left">
											<div class="row">
											<div class="col-md-4">
													<p><strong>Note</strong></p>
												</div>
												<div class="col-md-8">
													<p><?php echo htmlspecialchars($us['cl_note']);?> </p>
												</div>
											</div>
										</div>
										<div class="col-sm-12 pull-left">
											<div class="row">
											<div class="col-md-4">
													<p><strong>Maksātājs sakrīt ar piegādes informācijā norādīto vārdu un uzvārdu</strong></p>
												</div>
												<div class="col-md-8">
													<p><?php if($us['cl_match'] == 1){echo 'Yes';}else{echo 'No';}?> </p>
												</div>
											</div>
										</div>

										<div class="col-sm-12 pull-left">
											<div class="row">
												<div class="col-md-4">
													<p><strong>Created On</strong></p>
												</div>
												<div class="col-md-8">
													<p><?php echo date("d M Y H:i:s", strtotime($us['cl_created_at']));?> </p>
												</div>
											</div>
										</div>	
									</div>
									<div class="col-md-6 pull-left">
										<div class="row">
											
											<div class="col-md-12" style="margin-top:15px">
												<textarea style="min-height: 200px;border:1px solid gray;width: 100%;padding:5px" placeholder="Notes" value="" id="notes"><?php echo $us['cl_notes'] ?></textarea>
												<div class="notes_error"></div>
											</div>
										</div>
									</div>
								</div>		
								<?php 
								if($us['cl_payment_type'] == 'Loan')
								{ ?>
								<div class="well-white col-sm-12">
									<h5 class="small-heading col-sm-12 py-4">Līzings</h5>
                                	<b class="col-sm-12 pull-left"><u>Dati Par Aizņēmēju un darba vietu</u></b>
	                                <div class="col-md-6 pull-left">
	                                	<div class="row">
											<div class="col-md-4">
												<p><strong>Vārds</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_fname2']);?> </p>
											</div>
										</div>

										<div class="row">
											<div class="col-md-4">
												<p><strong>Uzvārds</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_surname2']);?> </p>
											</div>
										</div>

										<div class="row">
											<div class="col-md-4">
												<p><strong>Persons kods</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_kods']);?> </p>
											</div>
										</div>

										<div class="row">
											<div class="col-md-4">
												<p><strong>Pases numurs</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_mobile2']);?> </p>
											</div>
										</div>

										<div class="row">
											<div class="col-md-4">
												<p><strong>Dzimums</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_dzimums']);?> </p>
											</div>
										</div>

										<div class="row">
											<div class="col-md-4">
												<p><strong>Gimenes Stavoklis</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_Gimenes']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Skaits</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_skaits']);?> </p>
											</div>
										</div>

	                                </div>
	                                <div class="col-md-6 pull-left">

										<div class="row">
											<div class="col-md-4">
												<p><strong>Ikmenesa</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_ikmenesa']);?> </p>
											</div>
										</div>

										<div class="row">
											<div class="col-md-4">
												<p><strong>Darba vieta</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_darba_vieta']);?> </p>
											</div>
										</div>

										<div class="row">
											<div class="col-md-4">
												<p><strong>Amats</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_amats']);?> </p>
											</div>
										</div>

										<div class="row">
											<div class="col-md-4">
												<p><strong>Darba vieta adrese</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_darba_vieta_adrese']);?> </p>
											</div>
										</div>

										<div class="row">
											<div class="col-md-4">
												<p><strong>Darba vieta telefone</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_darba_vieta_telefone']);?> </p>
											</div>
										</div>
	                                	<div class="row">
											<div class="col-md-4">
												<p><strong>Datums</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_datums']);?> </p>
											</div>
										</div>

										<div class="row">
											<div class="col-md-4">
												<p><strong>Kopijie</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_kopijie']);?> </p>
											</div>
										</div>

										
	                                </div>
	                                <b class="col-sm-12 pull-left" style="margin-top: 30px"><u>Aiznemeju Deklareta Adrese</u></b>
	                                <div class="col-md-6 pull-left">
	                                	<div class="row">
											<div class="col-md-4">
												<p><strong>Meklet Addrese</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_address3']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Novads</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_novads3']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Pilseta</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_pilseta3']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Pagastss</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_pagastss3']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Ciems</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_ciems3']);?> </p>
											</div>
										</div>
										
									</div>
									<div class="col-md-6 pull-left">
										<div class="row">
											<div class="col-md-4">
												<p><strong>Lela</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_lela3']);?> </p>
											</div>
										</div>
	                                	<div class="row">
											<div class="col-md-4">
												<p><strong>Ekas Nr. / Nosaukums, Korpuss</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_ekas3']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Dzivokla Numurs</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_dzivokla3']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Pasta Indekss</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_pasta_index3']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Talrunis</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_talrunis3']);?> </p>
											</div>
										</div>
										
									</div>
									<b class="col-sm-12 pull-left" style="margin-top: 30px"><u>Aiznemeju Faktiska Dzive Svietas Adrese</u></b>
	                                <div class="col-md-6 pull-left">
	                                	<div class="row">
											<div class="col-md-4">
												<p><strong>Meklet Addrese</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_address4']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Faktiska dzivesvietas adrese sakrit ar deklerato adresi</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php if($us['cl_checkbox_address4'] == 1){echo 'Yes';}else{echo 'No';} ?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Novads</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_novads4']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Pilseta</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_pilseta4']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Pagastss</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_pagastss4']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Ciems</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_ciems4']);?> </p>
											</div>
										</div>
									</div>
									<div class="col-md-6 pull-left">
										<div class="row">
											<div class="col-md-4">
												<p><strong>Lela</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_lela4']);?> </p>
											</div>
										</div>
										
										<div class="row">
											<div class="col-md-4">
												<p><strong>Ekas Nr. / Nosaukums, Korpuss</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_ekas4']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Dzivokla Numurs</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_dzivokla4']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Pasta Indekss</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_pasta_indekss4']);?> </p>
											</div>
										</div>
										<div class="row">
											<div class="col-md-4">
												<p><strong>Talrunis</strong></p>
											</div>
											<div class="col-md-8">
												<p><?php echo htmlspecialchars($us['cl_talrunis4']);?> </p>
											</div>
										</div>
									</div>
	                            </div>
	                            <?php
	                        	}
	                        	?>
                                <div class="table-responsive table--no-card m-b-30 top-margin pull-left">
									
                                    <table class="table table-borderless table-data3" style="width:100%">
                                        <thead>
                                            <tr>
												<th>Product Name</th>
                                               
												<th>Supplier</th>
												<th>Product ID</th>

												<th>Sale Price</th>
												<th>Quantity</th>
												<th>Total</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                           <?php
                                           $total = 0;

											foreach($userOrders AS $orders)
											{
												
												//$product = $obj->getProduct($orders['cl_product_id']);
											
												//$product = $product[0];
												//if($product)
												//{
											?>
												<tr class="action-container">
													<td><?php echo htmlspecialchars($orders['cl_name']);?></td>
													<td><?php echo htmlspecialchars($orders['cl_supplier']);?></td>
													<td><?php echo htmlspecialchars($orders['cl_product_id']);?></td>
													
													
													<td>$ <?php echo htmlspecialchars($orders['cl_amount']) / htmlspecialchars($orders['cl_quantity']);?></td>
													<td> <?php echo htmlspecialchars($orders['cl_quantity']);?></td>
													<td>$ <?php echo htmlspecialchars($orders['cl_amount']);?></td>
													<?php $total+= $orders['cl_amount'] ?>
													
												</tr>
											<?php
										//}
											}
											?>
                                           
                                        </tbody>
                                        <tfoot>
                                        	<tr>
                                        		<td colspan="6" style="text-align: right">
                                        			Total : $ <?php echo $total ?>
                                        		</td>
                                        	</tr>
                                        	<tr>
                                        		<td colspan="6" style="text-align: right">
                                        			PVN : $ <?php echo $us['cl_pvn'] ?>
                                        		</td>
                                        	</tr>
                                        	<tr>
                                        		<td colspan="6" style="text-align: right">
                                        			$ <?php echo $us['cl_total'] ?>
                                        		</td>
                                        	</tr>
                                        </tfoot>
                                    </table>
									
                                </div>
                                <div class="btn-container">
								<?php
										if($us['cl_invoice'] == 0)
										{
									?>
                                        <button type="button" cluster-url="../ajax/convert_to_invoice_without_email?id=<?php echo $id;?>" class="btn btn-success pull-right btn-link-new" cluster-redirect="view?id=<?php echo $id;?>" cluster-message="Converting order to Invoice">Convert to Invoice (Without email)</button>
									   <button type="button" cluster-url="../ajax/convert_to_invoice?id=<?php echo $id;?>" class="btn btn-primary pull-right btn-link-new" cluster-redirect="view?id=<?php echo $id;?>" cluster-message="Converting order to Invoice" style="margin-right:5px">Convert to Invoice (With Email)</button>
									   <div class="error-area col-sm-12 pull-left"></div>
									   <?php
										}
										else 
										{
									   ?>
									   <button class="btn btn-primary pull-right btn-link-new" cluster-url="../ajax/send_invoice_again?id=<?php echo $id;?>" cluster-message="Sending Email"> Sent Inoice again to customer</button>
									    <button class="btn btn-success pull-right "  disabled="disabled" style="margin-right : 5px"><i class="fa fa-check"></i>  Conveted to Invoice</button>

									    <div class="error-area col-sm-12 pull-left"></div>
									     <?php
										}
										?>
									</div>
                            </div>
                            
                          
                        </div>
                        
                       <script>
						$(document).ready(function(){
							$('.active').removeClass('active');
							$('#user-order-menu').addClass('active');

							$('#notes').blur(function(){
								$('.notes_error').html('<div class="alert alert-warning">Updating Notes</div>');
								var notes = $(this).val();
								$.post('../ajax/save_notes?id=<?php echo htmlspecialchars($id) ?>', {notes:notes}, function(res){
									$('.notes_error').html('<div class="alert alert-success">' + res.message + '</div>');
								}, 'json')
								.fail(function(xhr) {

							          var html = '';
							          var res = xhr.responseJSON;
							          console.log(xhr);
							          if (typeof res === 'undefined') {
							          	$('.notes_error').html('<div class="alert alert-danger">Something went wrong</div>').show();
							          }
							          if (typeof res.error_description === 'undefined') {
							          	if(typeof res.error_text === 'undefined')
							          	{
							            	$('.notes_error').html('<div class="alert alert-danger">Something went wrong</div>').show();
							            	window.location.href = '#body_part';
							            	return;
							            }
							            else
							            {
							            	$('.notes_error').html('<div class="alert alert-danger">' + res.error_text + '</div>').show();
							            	window.location.href = '#body_part';
							            	return;
							            }
							          }
							        
							          
							          window.location.href = '#body_part';
							      },'json');;
							})
						}); 
						</script>               

  <?php
  include '../footer.php';
  ?>