<?php
	
	require_once __DIR__ . '/../magic/auth.php';

$auth = bdr_require_privilege([3, 20, 39], '../logout.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];

$contract_id = $_REQUEST['dd'];
$carcheck ='';
	$ch = $db->prepare('SELECT carcheck from contracts WHERE contract_id = ?');
	$ch->bindParam(1, $contract_id);
	$ch->execute();
if ($ch->rowCount() > 0) {
		$row = $ch->fetch(PDO::FETCH_ASSOC);
		$carcheck = $row["carcheck"];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $contract_id ;?></title>
    <link rel="stylesheet" href="../dist/css/bootstrap.css" />
	<link rel="stylesheet" href="../css/all.min.css">
	<link rel="stylesheet" href="../css/myCar.css" />
	<script src="../js/jquery.js"></script>
	<script src="../dist/js/bootstrap.js"></script>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <br />
            <br />
        </div>
<div class="row">
	<div class="col-2 text-center">
	<a href="print/check_print.php?J=<?php echo $contract_id; ?>" target="_blank"><button type="button" style="font-weight:bold;font-size:17px;padding:5px;" class="btn btn-outline-success form-control"> طباعة التشييك <i class="fa fa-id-card" aria-hidden="true"></i> </button></a>
	</div>
    <div class="col-8 text-center">
        <label style="font-size: 21px;">شاشة تحميل و طباعة تشييك</label>
    </div>
</div>
<br />
<div class="row">
        <div class="col text-center">
<form method="POST" id="form_photo" class="form-horizontal" enctype="multipart/form-data">
					<br /><br />
					<div class="row">
					<input type="hidden" id="contract_id" name="contract_id" value="<?php echo $contract_id ;?>" />
					</div>
					<div class="form-row align-items-center">
						<img id="car_preview" src="<?php echo($carcheck); ?>" class="img-fluid rounded" width="100%" height="auto" alt="صورة التشييك غير متاحة" />
					</div>

					<div class="form-row align-items-center">
						<div class="col-6">
							<input style="font-weight:bold;" type="file" accept="image/*" class="btn btn-primary" name="car_check" id="car_check" />
						</div>
						<div class="col-2">
							<input style="float:right ; font-weight:bold;" type="submit" class="btn btn-primary" id="photo_paly" value="تحميل الصورة" />
						</div>	
					</div>
					<div class="form-row align-items-center">
					<div class="col-8" id="preview"></div>
					</div>
				</form>
				<div id="err"></div>
                </div>
                </div>
                </div>
                <script>
$(document).ready(function() {
$('#form_photo').on('submit', function(event) {
				event.preventDefault();
				var extension = $('#car_check').val().split('.').pop().toLowerCase();
				if (extension != '') {
					if (jQuery.inArray(extension, ['jpeg', 'jpg', 'png', 'gif', 'bmp' , 'pdf']) == -1) {
						alert("صور jpg فقط");
						$('#car_check').val('');
						return false;
					}
					var oFReader = new FileReader();
					oFReader.readAsDataURL(document.getElementById("car_check").files[0]);
					var f = document.getElementById("car_check").files[0];
					var fsize = f.size || f.fileSize;
					if (fsize > 1900000) {
						alert("حجم الصورة كبير جدا ...");
						return false;
					}
					// alert('ajax');
					$.ajax({
						url: "../actions/check_upload.php",
						method: "POST",
						data: new FormData(this),
						contentType: false,
						processData: false,
					beforeSend : function()
   							{
   						 $("#err").fadeOut();
  								 },
					success: function(data) {	
   				 			if(data=='invalid')
    						{
     // invalid file format.
     				$("#err").html("فضلا اختار صور فقط .. !").fadeIn();
    						}
    						else
    					{
     // view uploaded file.
     					$("#preview").html(data).fadeIn();
     					$("#form")[0].reset(); 
    				}
      				},
     			error: function(e) 
      				{
    				$("#err").html(e).fadeIn();
      					}   
					});
				}
			});
        });//end of script
                </script>
</body>
</html>