<?php
if(isset($_GET['id']) && $_GET['id'] > 0){
    $qry = $conn->query("SELECT * from `instructor_list` where id = '{$_GET['id']}' ");
    if($qry->num_rows > 0){
        foreach($qry->fetch_assoc() as $k => $v){
            $$k=$v;
        }
    }
}
?>
<style>
	#cimg{
		width:15em;
		height:15em;
		object-fit:scale-down;
		object-position:center center;
	}
</style>
<div class="content py-5 px-3 bg-gradient-primary">
	<h2><b><?= isset($id) ? "Update instructor Details" : "New instructor Entry" ?></b></h2>
</div>
<div class="row mt-lg-n4 mt-md-n4 justify-content-center">
	<div class="col-lg-10 col-md-10 col-sm-12 col-xs-12">
		<div class="card rounded-0">
			<div class="card-body">

				<div class="container-fluid">
					<form action="" id="instructor-form">
						<input type="hidden" name ="id" value="<?php echo isset($id) ? $id : '' ?>">
						<div class="form-group col-lg-12 col-md-12 col-sm-12 col-xs-12">
							<label for="name" class="control-label">Instructor Name</label>
							<div class="input-group input-group-sm">
								<input type="text" class="form-control form-control-sm rounded-0" id="firstname" name="firstname" placeholder="First Name" required="required" value="<?= isset($firstname) ? $firstname : '' ?>">
								<input type="text" class="form-control form-control-sm rounded-0" id="middlename" name="middlename" placeholder="Middle Name (optional)" value="<?= isset($middlename) ? $middlename : '' ?>">
								<input type="text" class="form-control form-control-sm rounded-0" id="lastname" name="lastname" placeholder="Last Name" required="required" value="<?= isset($lastname) ? $lastname : '' ?>">
							</div>
						</div>
						<div class="form-group col-lg-12 col-md-12 col-sm-12 col-xs-12">
							<label for="instructor_for" class="control-label">Instructor for</label>
							<textarea rows="4" name="instructor_for" id="instructor_for" class="form-control form-control-sm rounded-0" required><?php echo isset($instructor_for) ? $instructor_for : ''; ?></textarea>
						</div>
						<div class="form-group col-lg-12 col-md-12 col-sm-12 col-xs-12">
							<label for="short_description" class="control-label">Short Description</label>
							<textarea rows="4" name="short_description" id="short_description" class="form-control form-control-sm rounded-0" required><?php echo isset($short_description) ? $short_description : ''; ?></textarea>
						</div>
						<div class="form-group col-lg-6 col-md-6 col-sm-12 col-xs-12">
							<label for="status" class="control-label">Status</label>
							<select name="status" id="status" class="form-control form-control-sm rounded-0" required="required">
								<option value="1" <?= isset($status) && $status == 1 ? 'selected' : '' ?>>Active</option>
								<option value="0" <?= isset($status) && $status == 0 ? 'selected' : '' ?>>Inactive</option>
							</select>
						</div>
						<div class="form-group col-lg-6 col-md-6 col-sm-12 col-xs-12">
							<label for="status" class="control-label">Avatar</label>
							<div class="custom-file custom-file-sm">
								<input type="file" class="custom-file-input rounded-0" id="customFile1" name="img" onchange="displayImg(this)">
								<label class="custom-file-label" for="customFile1">Choose file</label>
							</div>
						</div>
						<div class="form-group col-lg-6 col-md-6 col-sm-12 col-xs-12">
							<img src="<?php echo validate_image(isset($image_path) ? $image_path : '') ?>" alt="" id="cimg" class="img-fluid img-thumbnail rounded-circle">
						</div>
					</form>
				</div>
			</div>
			<div class="card-footer py-1 text-center">
				<button class="btn btn-primary btn-sm bg-gradient-primary btn-flat" form="instructor-form"><i class="fa fa-save"></i> Save</button>
				<a class="btn btn-light btn-sm bg-gradient-light border btn-flat text-dark" href="./?page=instructors"><i class="fa fa-times"></i> Cancel</a>
			</div>
		</div>
	</div>
</div>
<script>
	function displayImg(input) {
	    if (input.files && input.files[0]) {
	        var reader = new FileReader();
	        reader.onload = function (e) {
	        	$('#cimg').attr('src', e.target.result);
	        	$(input).siblings('.custom-file-label').html(input.files[0].name)
	        }

	        reader.readAsDataURL(input.files[0]);
	    }else{
			$('#cimg').attr('src', "<?php echo validate_image(isset($image_path) ? $image_path : '') ?>");
			$(input).siblings('.custom-file-label').html('Choose file')
		}
	}
	$(document).ready(function(){
		$('#instructor-form').submit(function(e){
			e.preventDefault();
            var _this = $(this)
			 $('.err-msg').remove();
			start_loader();
			$.ajax({
				url:_base_url_+"classes/Master.php?f=save_instructor",
				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.replace('./?page=instructors/view_instructor&id='+resp.iid)
					}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").scrollTop(0);
                            end_loader()
                    }else{
						alert_toast("An error occured",'error');
						end_loader();
                        console.log(resp)
					}
				}
			})
		})

	})
</script>