<?php $this->include('rs/utils.php')?>
<?= $this->extend('rs/template-popup'); ?>
<?= $this->section('content'); ?>
<?php //var_dump(session()->get('seller'));?>

<style>
.bordertable{width:100%;border-bottom:1px solid #ddd;}
.bordertable td {width: auto !important;padding:5px;border-left:1px solid #ccc; border-collapse:separate;min-height:60px}
.buybtn {color:#fff;background:#00A54F;text-align:center}
.intable td{ border:none;padding:0 5px 0 0}
label{width:50%;padding:0 0 0 15px!important;margin-bottom:3px!important}
detail-body{max-width:500px!important}
.favicon{font-size:40px;color:#999}
</style>  


<div class="card shadow detail-body" id="maincard" style="border:none!important">
	<div class="card-body mh-25">
		<div class="row">
			<div class="card" style="min-height:100px;margin:10px;width:25rem;">
				<table class="bordertable w-100">
				 <tr>
					<td>Ad code:  <?=$car['code']?><a class="noprint" style="float:right" href="javascript:navigator.share({title: '<?=$car['model_dec']?>',	text: '<?=$car['model_dec']?>',url: window.location.href})"><i class="fa fa-share-alt" aria-hidden="true"></i> Share</a></td>
					</td>
				  </tr>
				</table>
				
				<table class="bordertable w-100">
					<tr>
						<td ><?=$car['brand']?> - <?=$car['model_dec']?></td>
					</tr>
				</table>
				
				<table class="bordertable w-100">
					
					<tr>
												
						<td>
							<b>Seller price</b></br>
							<table class="intable"><tr><td>BGN </td><td align="right"><?=seller_bgnBrut($car['price_bgn'])?></br></td></tr><tr><td>BGN+VAT </td><td align="right"><?=seller_bgnBrut($car['price_bgn'])?></td></tr></table>
						</td>
						
						<td>
							<b>My price for Client</b> </br>
							<table class="intable"><tr><td>EUR </td><td align="right"><?=round(buyer_eurNet($car['price_bgn']))?></td></tr><tr><td>EUR+VAT </td><td align="right"><?=round(buyer_eurBrut($car['price_bgn']))?></td></tr></table>
						</td>
							
			    	</tr>
				</table>
					
				<br>
				
				<!-- Carousel wrapper -->
				<div id="carouselCtl" class="carousel slide" data-ride="carousel" data-interval="false">
				  <div class="carousel-inner">
						<?php 
						$first=true;
						$cnt=0;
						foreach ($imgs as  $img) {?>
						  <div class="carousel-item <?=($first?'active':'')?>">
						   <a data-toggle="modal" data-target="#modalGallery"><img  class="d-block w-100<?=(!$first?' noprint':'')?>" src="<?=front_url().'uploads/cars_img/'.$img["url"]?>" alt=""></a>
						 </div>
						<?php 
							$first=false;
							$cnt++;
						} 
						?> 
				  </div>
				  <a id="prev" class="carousel-control-prev noprint" href="#carouselCtl" role="button" data-slide="prev">
					<span class="carousel-control-prev-icon" aria-hidden="true"></span>
					<span class="sr-only">Previous</span>
				  </a>
				  <a id="nxt" class="carousel-control-next noprint" href="#carouselCtl" role="button" data-slide="next">
					<span class="carousel-control-next-icon" aria-hidden="true"></span>
					<span class="sr-only">Next</span>
				  </a>
				</div>
				<div class="row my-1 noprint" style="margin-left:0px">
				<?php 
					$crt=0;
					
					foreach ($imgs as  $img) {		
				?>
				<div class="car-pic-sm d-inline noprint" onclick="showPic(<?=$crt?>)"
					style="background-image:url('<?php echo front_url().'uploads/cars_img/'.$img["url"] ?>')"></div> 
					<?php
						$crt++;
					}?>
				</div>
				<!-- Carousel wrapper -->
				
				<hr/>	
				<form class="" id="frmdata" class="form-inline" action="/profile" method="post" >
					<label>Model class</label><label> <?=$car['model']?></label><br>
					<label>Body type</label><label> <?=$car['body_type']?></label><br>
					<label>Steering wheel on</label><label> <?=$car['steering_whell']?></label><br>
					<label>Engine capacity (CC)</label><label> <?=$car['cubic_capacity']?></label><br>
					<label>Engine power (HP)</label><label> <?=$car['engine_power_hp']?></label><br>
					<label>Fuel</label><label> <?=$car['fuel']?></label><br>
					<label>Emission standard</label><label> <?=$car['emission_standard']?></label><br>
					<label>Manufacture date</label><label> <?=$car['date_manufacture']?></label><br>
					<label>Gearbox</label><label> <?=$car['gear_box']?></label><br>
					<label>Drivetrain</label><label> <?=$car['drivetrain']?></label><br>
					<label>Km</label><label> <?=$car['km']?></label><br>
					<label>VIN</label><label> <?=$car['vin']?></label><br>
					<label>Technical status</label><label> <?=$car['technical_status']?></label><br>
					<label>Country of documents</label><label> <?=$car['country_of_origin']?></label><br>
					<label>Notes</label><label> <?=$car['notes']?></label><br>
				</form>	
						
				<form id="frmAdditionals">
				<?php
					$formdata = json_decode( $car["additionals"],true);
					if( count($formdata))
					{
						echo '<hr/>';
						foreach( $formdata as $field )
						{
							echo '<label>'.$field['name'].'</label><label> '.$field['value'].'</label><br>';
						}
					}
				?>
				</form>
				<hr/>
				<form class="px-3" style="font-size:15px!important" >
					<a href="https://cars.zadio.bg/<?=$car['code']?>/<?=$car['brand']?>-<?=$car['model_dec']?>">https://cars.zadio.bg/<?=$car['code']?>/<?=$car['brand']?>-<?=$car['model_dec']?></a><br />
					<a class="noprint" href="javascript:copyLink()">Copy link</a><br class="noprint" />
					<a class="noprint" href="javascript:print('maincard')">Print</a><br class="noprint" />
					<a class="noprint" href="javascript:pdf('maincard')">Export in PDF</a><br  class="noprint"/>
					<a class="noprint" href="javascript:print()">Send by email</a><br class="noprint" />
				</form>
				<?php
					if(!session()->get('id') || session()->get('seller')==1 || session()->get('buyer')==1)
					{
						echo '<hr class="noprint">
						<form style="font-size:15px!important" class="px-3">
						Call ZadioCars<br/>
						+359 879 655 188</form><br>';
					}
					else
						echo '<br>';
						
				?>
				
			</div>	
	
		</div>
			
	</div>
</div>




<div class="modal fade" id="modalGallery" tabindex="-1" role="dialog" aria-labelledby="modalTitle" aria-hidden="true">
  <div class="modal-dialog modal-xl" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="modalTitle"><?=$car['code']?></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
       <div id="carouselCtlModal" class="carousel slide" data-ride="carousel" data-interval="false">
				  <div class="carousel-inner">
						<?php 
						$first=true;
						$cnt=0;
						foreach ($imgs as  $img) {?>
						  <div class="carousel-item <?=($first?'active':'')?>">
						   <img  class="d-block w-100" src="<?=front_url().'uploads/cars_img/'.$img["url"]?>" alt="">
						 </div>
						<?php 
							$first=false;
							$cnt++;
						} 
						?> 
				  </div>
				  <a class="carousel-control-prev" href="#carouselCtlModal" role="button" data-slide="prev">
					<span class="carousel-control-prev-icon" aria-hidden="true"></span>
					<span class="sr-only">Previous</span>
				  </a>
				  <a class="carousel-control-next" href="#carouselCtlModal" role="button" data-slide="next">
					<span class="carousel-control-next-icon" aria-hidden="true"></span>
					<span class="sr-only">Next</span>
				  </a>
				</div>
      </div>
     
    </div>
  </div>
</div>


<form action="/export_pdf.php" method="post" id="contentForm" target="_blank"><input type="hidden" name="contx" id="contx"/><input type="hidden" name="docname" value="zadiocars_<?=$car['code']?>.pdf"/></form>

	
<script>

var crtbrand="";
var crtmodel="<?php if (isset($filter['model'])){echo $filter['model'];} else {echo '';}?>";
$( document ).ready(function() {
	crtbrand=$('#brand').val();
	//alert(crtbrand)
    showModels()
});

$('#brand').change(function(){
	
	crtbrand=$('#brand').val();
	//alert(crtbrand)
	showModels();
});

function showPic(crt){
$('#carouselCtl').carousel(crt) 
}
function showModels()
{
	$.ajax({
        type: "POST",
        url: "<?php echo site_url('/brandmodels'); ?>", 
        data:{brand: crtbrand},
        dataType:"json",//return type expected as json
        success: function(models){
			   //console.log(models)
               
			   $('#model1').find('option').remove().end();
			   var opt = $('<option selected disabled/>');
			   opt.val('');
               opt.text('Model class');
			   $('#model1').append(opt);			   
			   $.each(models,function(key,value){
				    console.log(value.model_name)
                    var opt = $('<option />'); 
                    opt.val(value.model_name);
                    opt.text(value.model_name);
					if(crtmodel==value.model_name)
						 opt.attr('selected','selected');
					$('#model1').append(opt);
               });
			   //$("#model1").selectpicker("refresh");
			   
        },
    });
}


function copyLink() {
  copyText="https://cars.zadio.bg/<?=$car['code']?>/<?=$car['brand']?>-<?=$car['model_dec']?>";
  navigator.clipboard.writeText(copyText);
  alert("The link was copied.");
}


function print(elem) {
  
  var divToPrint=document.getElementById(elem);
   newWin= window.open("");
   var head="<head>\n"+
		"<style>\n"+
		"label {display:inline-block!important;width:200px!important;}\n"+
		".car-pic-sm{display:none}\n"+
		"@media print {\n"+
		"  .noprint {display:none!important;}\n"+
		"}\n"+
		"img{width:400px}\n"+
		"</style>\n"+
		"</head>";
		
   var body=divToPrint.outerHTML
   newWin.document.write(head+body);
   newWin.print();
   newWin.close();
  return false;
}

function pdf(elem)
{
	var divToPrint=document.getElementById(elem);
	var header_str = '<html><body>';
    var footer_str = '</body></html>';
    var new_str =divToPrint.innerHTML;
	
	
	//new_str=new_str.replace(/<a id=.*a>/g, '');
	
	
	$('#contx').val(new_str);
	$('#contentForm').submit();
}

function encodeHTML(str){
    return str.replace(/([\u00A0-\u9999<>&])(.|$)/g, function(full, char, next) {
      if(char !== '&' || next !== '#'){
        if(/[\u00A0-\u9999<>&]/.test(next))
          next = '&#' + next.charCodeAt(0) + ';';

        return '&#' + char.charCodeAt(0) + ';' + next;
      }

      return full;
    });
}
</script>

<?= $this->endsection(); ?>