<?php

if(isset($_GET['id']) && $_GET['id'] > 0){
    $qry = $conn->query("SELECT * from `application_list` where id = '{$_GET['id']}' ");
    if($qry->num_rows > 0){
        foreach($qry->fetch_assoc() as $k => $v){
            $$k=$v;
        }
    }
}
?>
<div class="mx-0 py-5 px-3 mx-ns-4 bg-gradient-primary">
	<h3><b><?= isset($id) ? "Update Application Details" : "Create New Application" ?></b></h3>
</div>
<style>
	img#cimg{
      max-height: 15em;
      width: 100%;
      object-fit: scale-down;
    }
</style>
<div class="row justify-content-center" style="margin-top:-2em;">
	<div class="col-lg-10 col-md-11 col-sm-11 col-xs-11">
		<div class="card rounded-0 shadow">
			<div class="card-body">
				<div class="container-fluid">
					<div class="container-fluid">
						<form action="" id="application-form">
							<input type="hidden" name ="id" value="<?php echo isset($id) ? $id : '' ?>">
							<div class="form-group mb-3">
								<label for="location" class="control-label">Location</label>
								<textarea rows="3" class="form-control form-control-sm rounded-0" id="location" name="location" required="required"><?= isset($location) ? $location : '' ?></textarea>
							</div>
							<div class="form-group mb-3">
								<label for="destination" class="control-label">Destination</label>
								<textarea rows="3" class="form-control form-control-sm rounded-0" id="destination" name="destination" required="required"><?= isset($destination) ? $destination : '' ?></textarea>
							</div>
							<div class="form-group p-0 col-lg-6 col-md-6 col-sm-12 col-xs-12 mb-3">
								<label for="date_from" class="control-label">Date From</label>
								<input type="date" class="form-control form-control-sm rounded-0" id="date_from" name="date_from" value="<?= isset($date_from) ? $date_from : '' ?>" min = "<?= date("Y-m-d") ?>" required="required">
							</div>
							<div class="form-group p-0 col-lg-6 col-md-6 col-sm-12 col-xs-12 mb-3">
								<label for="date_to" class="control-label">Date To</label>
								<input type="date" class="form-control form-control-sm rounded-0" id="date_to" name="date_to" value="<?= isset($date_to) ? $date_to : '' ?>" min = "<?= date("Y-m-d") ?>" required="required">
							</div>
							<div class="form-group mb-3">
								<label for="reason" class="control-label">Reason of Travelling</label>
								<textarea rows="3" class="form-control form-control-sm rounded-0" id="reason" name="reason" required="required"><?= isset($reason) ? $reason : '' ?></textarea>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="card-footer py-1 text-center">
				<button class="btn btn-primary btn-sm bg-gradient-primary rounded-0" form="application-form"><i class="fa fa-save"></i> Save</button>
				<a class="btn btn-light btn-sm bg-gradient-light border rounded-0" href="./?page=applications"><i class="fa fa-angle-left"></i> Cancel</a>
			</div>
		</div>
	</div>
</div>
<script>
	$(document).ready(function(){
		$('#application-form').submit(function(e){
			e.preventDefault();
            var _this = $(this)
			 $('.err-msg').remove();
			start_loader();
			$.ajax({
				url:_base_url_+"classes/Master.php?f=save_application",
				data: new FormData($(this)[0]),
                cache: false,
                contentType: false,
                processData: false,
                method: 'POST',
                type: 'POST',
                dataType: 'json',
				error:err=>{
					console.log(err)
					alert_toast("An error occured",'error');
					end_loader();
				},
				success:function(resp){
					if(typeof resp =='object' && resp.status == 'success'){
						location.href = "./?page=applications/view_application&id="+resp.aid
					}else if(resp.status == 'failed' && !!resp.msg){
                        var el = $('<div>')
                            el.addClass("alert alert-danger err-msg").text(resp.msg)
                            _this.prepend(el)
                            el.show('slow')
                            $("html, body, .modal").scrollTop(0)
                            end_loader()
                    }else{
						alert_toast("An error occured",'error');
						end_loader();
                        console.log(resp)
					}
				}
			})
		})

	})
</script>