<?php require_once('header.php'); ?>
<?php if($allPermissions['transport']['create']=="1"){ ?>

<?php
if(isset($_GET['key']) && $_GET['key']!=''){
    $location_id = id_decrypt($_GET['key']);
    $statement = $pdo->prepare("SELECT * FROM tbl_material_type where id=?");
	$statement->execute(array($location_id));
	$partner_result = $statement->fetchAll(PDO::FETCH_ASSOC);
	$partner_count = $statement->rowCount();
}else{
    $partner_count=0;
}
    
?>

<section class="content-header">
	<div class="row">
		<div class="col-md-6" style="text-align: left;">
			<h4> <?php if(isset($_GET['key']) && $_GET['key']!=''){ echo 'Update';}else { echo 'Add';} ?> Material Type Details</h4>
		</div>
		<div class="col-md-6" style="text-align: right;">
		    <a href="material-type.php<?php if(isset($_GET['key']) && $_GET['key']!=''){ echo '?key='.$_GET['key'];} ?>" class="btn btn-success btn-sm"><i class="fa fa-refresh"></i> Reset Form</a>&nbsp;
			<a href="material-type-list.php" class="btn btn-info btn-sm"><i class="fa fa-list"></i> Back to Material Type List</a>
		</div>
	</div>
</section>


<section class="content">

	<div class="row">
		<div class="col-md-12">

			<?php if($error_message): ?>
			<div class="callout callout-danger">
				<p>
					<?php echo $error_message; ?>
				</p>
			</div>
			<?php endif; ?>

			<?php if($success_message): ?>
			<div class="callout callout-success">
				<p><?php echo $success_message; ?></p>
			</div>
			<?php endif; ?>
			<div id="content-div">
			   <form id="form_details" class="form-horizontal" action="" method="post" enctype="multipart/form-data" autocomplete="new-password">
				<div class="box box-info">
					<div class="box-body">
					  
					    
					    <div class="row">
					        <div class="col-12 col-sm-12 col-md-3 col-lg-3">
					             <label for="required" class="control-label">Name <span class="required"></span></label>
					             <input type="text" autocomplete="off" placeholder="Name" class="form-control" name="name" value="<?php if($partner_count){ echo $partner_result[0]['name'];} ?>" required >
					           <input type="hidden"  class="form-control" name="id" value="<?php if($partner_count){ echo $partner_result[0]['id'];} ?>" >
					        </div>
					        <div class="col-12 col-sm-12 col-md-3 col-lg-3">
					             <label for="required" class="control-label">Status <span class="required"></span></label>
					             <select class="form-control select2" name="status" id="status">
							        <option value="1" <?php if($partner_count && $partner_result[0]['status']==1){ echo "selected";} ?>>Active</option>
							        <option value="0" <?php if($partner_count && $partner_result[0]['status']==0){ echo "selected";} ?>>In-Active</option>
							    </select>
					        </div>
					    </div>
					     <br>
    					  <div class="row">
    					      <div class="col-12 col-sm-12 col-md-4 col-lg-4">
    					          <center><button type="submit" id="submit" class="btn btn-success btn-sm" name="form1"><i class="fa fa-save"></i>  Save Material Type Details</button></center>
    					      </div>
    					  </div>
						
					</div>
				</div>
			</form>
			</div>
			<div class="container" id="loading-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">
                    <center> <img src="img/loading.gif" style="width: 250px;"><br><b class="blinking">Processing Please Wait...</b></center>
            </div>
            
            <div class="container" id="success-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">
                        <center><b class="blinking" style="font-size:16px;">Material Type Details <?php if(isset($_GET['key']) && $_GET['key']!=''){ echo 'Updated';}else { echo 'Added';} ?> Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <a href="material-type.php<?php if(isset($_GET['key']) && $_GET['key']!=''){ echo '?key='.$_GET['key'];}?>"><button class="btn btn-info btn-sm"><i class="fa fa-refresh"></i> View Updated Details</button></a>
                            &nbsp;&nbsp;
                            <a href="material-type-list.php"><button class="btn btn-warning btn-sm"><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </center>
                    </div>
		</div>
	</div>

</section>
<?php }else{ ?>
<center><h4 style="color:red;">You don't have permission to add transport </h4></center>
<?php } ?>
<?php require_once('footer.php'); ?>



<script>
$(document).ready(function() {
   $('#form_details').on('submit', function(e){
                e.preventDefault();
                $('#loading-div').show();
                $('#content-div').hide();
                $("#submit").prop('disabled', true);
                var formData = new FormData(this);
               <?php if(isset($_GET['key']) && $_GET['key']!=''){ ?>  formData.append('type', 'update_location'); <?php } ?>
               <?php if(!isset($_GET['key'])){ ?>  formData.append('type', 'create_location'); <?php } ?>
                    $.ajax({
                            url: 'apis/material-type.php',
                            type: 'post',
                            dataType: 'json',
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            enctype: 'multipart/form-data',
                            success: function (response) {
                                console.log(response);
                                if(response['status']==200){
                                     $('#form_details').trigger("reset");
                                     $("#submit").prop('disabled', false);
                                     $('#success-div').show();
                                     $('#loading-div').hide();
                                }else{
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
   });
});
</script>

