<?php 
if(isset($_GET['id'])){
    $user = $conn->query("SELECT * FROM users where id ='{$_GET['id']}' ");
    foreach($user->fetch_array() as $k =>$v){
        $meta[$k] = $v;
    }
}
?>
<?php if($_settings->chk_flashdata('success')): ?>
<script>
	alert_toast("<?php echo $_settings->flashdata('success') ?>",'success')
</script>
<?php endif;?>
<div class="card card-outline rounded-0 card-navy">
	<div class="card-body">
		<div class="container-fluid">
			<div id="msg"></div>
			<form action="" id="manage-user">	
				<input type="hidden" name="id" value="<?= isset($meta['id']) ? $meta['id'] : '' ?>">
				<div class="form-group">
					<label for="name">First Name</label>
					<input type="text" name="firstname" id="firstname" class="form-control" value="<?php echo isset($meta['firstname']) ? $meta['firstname']: '' ?>" required>
				</div>
				<div class="form-group">
					<label for="name">Middle Name</label>
					<input type="text" name="middlename" id="middlename" class="form-control" value="<?php echo isset($meta['middlename']) ? $meta['middlename']: '' ?>">
				</div>
				<div class="form-group">
					<label for="name">Last Name</label>
					<input type="text" name="lastname" id="lastname" class="form-control" value="<?php echo isset($meta['lastname']) ? $meta['lastname']: '' ?>" required>
				</div>
				<div class="form-group">
					<label for="username">Username</label>
					<input type="text" name="username" id="username" class="form-control" value="<?php echo isset($meta['username']) ? $meta['username']: '' ?>" required  autocomplete="off">
				</div>
				<div class="form-group">
					<label for="password"><?= isset($meta['id']) ? "New" : "" ?> Password</label>
					<input type="password" name="password" id="password" class="form-control" value="" autocomplete="off">
                    <?php if(isset($meta['id'])): ?>
					<small><i>Leave this blank if you dont want to change the password.</i></small>
                    <?php endif; ?>
				</div>
                <div class="form-group px-0 col-lg-6 col-md-6 col-sm-12 col-xs-12">
                    <label for="type" class="control-label">Type</label>
                    <select name="type" id="type" class="form-control form-control-sm rounded-0" required>
                    <option value="1" <?php echo isset($meta['type']) && $meta['type'] == 1 ? 'selected' : '' ?>>Administrator</option>
                    <option value="2" <?php echo isset($meta['type']) && $meta['type'] == 2 ? 'selected' : '' ?>>Toll Collecter</option>
                    </select>
                </div>
				<div class="form-group px-0 col-lg-6 col-md-6 col-sm-12 col-xs-12 d-none">
                    <label for="toll_id" class="control-label">Toll Gate</label>
                    <select name="toll_id" id="toll_id" class="form-control form-control-sm rounded-0">
                    	<option value="" <?php echo !isset($meta['toll_id']) || (isset($meta['toll_id']) && is_null($meta['toll_id'])) ? 'selected' : '' ?>></option>
						<?php 
						$toll_qry = $conn->query("SELECT * FROM `toll_list` where delete_flag = 0 and `status` = 1 order by abs(`name`) asc, `name` asc ");
						while($row = $toll_qry->fetch_assoc()):
						?>
						<option value="<?= $row['id'] ?>" <?= isset($meta['toll_id']) && $meta['toll_id'] == $row['id'] ? 'selected' : '' ?>><?= $row['name'] ?></option>
						<?php endwhile; ?>
                    </select>
                </div>
				<div class="form-group">
					<label for="" class="control-label">Avatar</label>
					<div class="custom-file">
		              <input type="file" class="custom-file-input rounded-circle" id="customFile" name="img" onchange="displayImg(this,$(this))" accept="image/png, image/jpeg">
		              <label class="custom-file-label" for="customFile">Choose file</label>
		            </div>
				</div>
				<div class="form-group d-flex justify-content-center">
					<img src="<?php echo validate_image(isset($meta['avatar']) ? $meta['avatar'] :'') ?>" alt="" id="cimg" class="img-fluid img-thumbnail">
				</div>
			</form>
		</div>
	</div>
	<div class="card-footer">
			<div class="col-md-12">
				<div class="row">
					<button class="btn btn-sm btn-primary rounded-0 mr-3" form="manage-user">Save User Details</button>
					<a href="./?page=user/list" class="btn btn-sm btn-default border rounded-0" form="manage-user"><i class="fa fa-angle-left"></i> Cancel</a>
				</div>
			</div>
		</div>
</div>
<style>
	img#cimg{
		height: 15vh;
		width: 15vh;
		object-fit: cover;
		border-radius: 100% 100%;
	}
</style>
<script>
	function displayImg(input,_this) {
	    if (input.files && input.files[0]) {
	        var reader = new FileReader();
	        reader.onload = function (e) {
	        	$('#cimg').attr('src', e.target.result);
	        }

	        reader.readAsDataURL(input.files[0]);
	    }else{
			$('#cimg').attr('src', "<?php echo validate_image(isset($meta['avatar']) ? $meta['avatar'] :'') ?>");
		}
	}
	$(function(){
		$('#toll_id').select2({
			placeholder:'Select Toll Gate Here',
			containerCssClass:'form-control form-control-sm rounded-0'
		})
		$('#type').change(function(){
			var type = $(this).val()
			if(type == 2){
				$('#toll_id').val('<?= isset($meta['toll_id']) ? $meta['toll_id'] : ''  ?>').trigger('change').attr('required',true)
				$('#toll_id').parent().removeClass('d-none')
			}else{
				$('#toll_id').val('').trigger('change').attr('required',false)
				$('#toll_id').parent().addClass('d-none')
			}
		})
		if('<?= isset($meta['id']) ?>' == 1){
			$('#type').trigger('change')
		}
		$('#manage-user').submit(function(e){
			e.preventDefault();
			start_loader()
			$.ajax({
				url:_base_url_+'classes/Users.php?f=save',
				data: new FormData($(this)[0]),
				cache: false,
				contentType: false,
				processData: false,
				method: 'POST',
				type: 'POST',
				success:function(resp){
					if(resp ==1){
						location.href='./?page=user/list'
					}else{
						$('#msg').html('<div class="alert alert-danger">Username already exist</div>')
						end_loader()
					}
				}
			})
		})
	})


</script>