<?php 
session_start();
require '../config/database.php';
require 'model/model.php';
global $conn;

$sales_users = sales_users();

if (isset($_SESSION['admin_id'])) {
    $name = $_SESSION['name'];
} else {
    echo "<script>window.location='index.php'</script>";
    exit;
}
$result = '';
if(isset($_REQUEST['submit'])){
	$result = search_company();
}

include('layout/header.php');
include('layout/left_sidebar.php');

?>

<style type="text/css">
	./*trashcolor{
		background-color: pink !important;
	}
	.call_backcolor{
		background-color: blue !important;
	}
	.leadcolor{
		background-color: aqua !important;
	}*/
	.trcolor{
		background-color: green !important;
	}
</style>
<!-- Title -->
<div class="row heading-bg">
	<div class="col-lg-3 col-md-4 col-sm-4 col-xs-12">
		<h5 class="txt-dark">Search</h5>
	</div>
</div>
<!-- /Title -->

<!-- Row -->
<div class="row">
	<div class="col-md-12">
		<div class="panel panel-default card-view">
			<!-- <div class="panel-heading">
				<div class="pull-left">
					<h6 class="panel-title txt-dark">Search</h6>
				</div>
				<div class="clearfix"></div>
			</div> -->
			<div class="panel-wrapper collapse in">
				<div class="panel-body">
					<div class="form-wrap">
						<form method="post" action="">
							<div class="row">
								<div class="col-sm-4">
									<div class="form-group">
										<select class="selectpicker" data-style="btn-primary btn-outline" name="fleet_size" required id="fleet_size">
											<option value="">Fleet Size</option>
											<option value="1-9" <?= (isset($_REQUEST['fleet_size'])) && $_REQUEST['fleet_size'] == "1-9" ? "selected" : ""?>>1 – 9 vehicles</option>
											<option value="10-19" <?= (isset($_REQUEST['fleet_size'])) && $_REQUEST['fleet_size'] == "10-19" ? "selected" : ""?>>10 – 19 vehicles</option>
											<option value="20-39" <?= (isset($_REQUEST['fleet_size'])) && $_REQUEST['fleet_size'] == "20-39" ? "selected" : ""?>>20 – 39 vehicles</option>
											<option value="40-99" <?= (isset($_REQUEST['fleet_size'])) && $_REQUEST['fleet_size'] == "40-99" ? "selected" : ""?>>40 – 99 vehicles</option>
											<option value="100-1000" <?= (isset($_REQUEST['fleet_size'])) && $_REQUEST['fleet_size'] == "100-1000" ? "selected" : ""?>>100+ vehicles</option>
										</select>
									</div>
								</div>
								<div class="col-sm-4">
									<div class="form-group">
										<input type="text" name="postcode" id="postcode" placeholder="Post Code" class="form-control" value="<?= isset($_REQUEST['postcode'])?$_REQUEST['postcode']:'' ?>" required minlength="2">
									</div>
								</div>
								<div class="col-sm-4">
									<div class="form-group">
										<button type="submit" class="btn btn-primary" name="submit">Submit</button>

									<?php if(isset($_REQUEST['submit'])){ ?>
										<button type="button" class="btn btn-primary btn-outline" name="" data-toggle="modal" data-target=".save-search">Save Search</button>
									<?php } ?>

									</div>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<!-- /Row -->

<!-- Row -->
<div class="row">
	<div class="col-sm-12">
		<div class="panel panel-default card-view">
			<!-- <div class="panel-heading">
				<div class="pull-left">
					<h6 class="panel-title txt-dark">data Table</h6>
				</div>
				<div class="clearfix"></div>
			</div> -->
			<div class="panel-wrapper collapse in">
				<div class="panel-body">
					<div class="table-wrap">
						<?php if ($result != '' && mysqli_num_rows($result) > 0) {
							echo "<p>".mysqli_num_rows($result)." Results from the current search</p>";
						} ?>
						<div class="table-responsive">
							<table id="companyTable" class="table table-hover display pb-30">
								<thead>
									<tr>
										<th class="text-center">Operators Licence No.</th>
										<th class="text-center">Operator Name</th>
										<th class="text-center">Postcode</th>
										<th class="text-center">Fleet Size</th>
										<th class="text-center">Status</th>
										<th class="text-center">Action</th>
									</tr>
								</thead>
								<tbody>
									<?php if ($result != '' && mysqli_num_rows($result) > 0) {

									while ($row = mysqli_fetch_assoc($result)) {
											// if(isset($row['status']) && $row['status'] == 'trash'){
											// 	$trcolor = 'trashcolor';
											// }
											// if(isset($row['status']) && $row['status'] == 'call_back'){
											// 	$trcolor = 'call_backcolor';	
											// }
											// if(isset($row['status']) && $row['status'] == 'lead'){
											// 	$trcolor = 'leadcolor';
											// }
										$trcolor = '';
										if(isset($row['status']) && $row['status'] != ''){
											$trcolor = 'background-color: #8BC34A !important;';
										}
										?>
									 	<tr id="<?=$row['company_id']?>_tr" style="<?=$trcolor?>">
											<td class="text-center"><?=$row['licence_number']?></td>
											<td class="text-left"><?=$row['company_name']?></td>
											<td class="text-center"><?=$row['postcode']?></td>
											<td class="text-center"><?=$row['total_fleet_size']?></td>
											<td>
												<a id="<?=$row['company_id']?>_trash" href="javascript:void(0);" class="btn btn-info <?=($row['status'] == 'trash') ? "" : "btn-outline";?> btn-xs change_status" data-status="trash" data-id="<?=$row['company_id']?>">Trash</a>
												<a id="<?=$row['company_id']?>_call_back" href="javascript:void(0);" class="btn btn-info <?=($row['status'] == 'call_back') ? "" : "btn-outline";?> btn-xs change_status" data-status="call_back" data-id="<?=$row['company_id']?>">Call back</a>
												<a id="<?=$row['company_id']?>_lead" href="javascript:void(0);" class="btn btn-info <?=($row['status'] == 'lead') ? "" : "btn-outline";?> btn-xs change_status" data-status="lead" data-id="<?=$row['company_id']?>">Lead</a>

												<a href="javascript:void(0);" class="btn btn-info btn-xs change_status btn-outline" data-status="no_status" data-id="<?=$row['company_id']?>">No Status</a>

											</td>
											<td class="text-center"><a target="_blank" href="details.php?id=<?=$row['company_id']?>" class="btn btn-info btn-outline btn-xs" style="background-color: green !important; color: white;">Details</a></td>
										</tr>
									<?php } ?>
								<?php } ?>
								</tbody>
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>	
	</div>
</div>
<!-- /Row -->

<div class="modal fade save-search" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-sm">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h5 class="modal-title" id="mySmallModalLabel">Choose Sales User</h5>
			</div>
			<div class="modal-body">
				<div class="form-group">
					<select class="selectpicker" data-style="btn-primary btn-outline" name="sales_id" id="sales_id">
						<option value="" disabled="">Select User</option>
						<?php if (mysqli_num_rows($sales_users) > 0) {
                         while ($row = mysqli_fetch_assoc($sales_users)) {?>
						   <option value="<?=$row['sales_id']?>"><?=$row['first_name']?> <?=$row['last_name']?></option>
						<?php } ?>
					<?php } ?>
					</select>
				</div>
				<div class="form-group text-center">
					<button type="button" class="btn btn-primary save-search-btn">Save</button>
				</div>
			</div>
		</div>
		<!-- /.modal-content -->
	</div>
	<!-- /.modal-dialog -->
</div>
<button id="myBtn"><a href="javascript:void(0);" style="color: white">Back To Top</a></button>
<style>
	/*Floating Back-To-Top Button*/
    #myBtn {
        position: fixed;
        bottom: 10px;
        float: right;
        left: 90%;
        max-width: 100px;
        width: 100%;
        font-size: 14px;
        border-color: rgba(85, 85, 85, 0.2);
        background-color: rgb(100,100,100);
        padding: .5px;
        border-radius: 4px;

    }
/*On Hover Color Change*/
    #myBtn:hover {
        background-color: #7dbbf1;
    }
</style>
<?php include('layout/footer.php');?>
<script type="text/javascript">
	$(document).ready(function() {
		$('#companyTable').DataTable({
			"searching": false,
			"ordering": false,
			"bLengthChange": false,
			'iDisplayLength': 100
		});
		$(document).on("click", "#companyTable tr", function() {
            $("#companyTable tr").removeClass("highlight");
            $(this).toggleClass("highlight");
        });
		$(document).on("click", ".paginate_button", function() {
			$("html, body").animate({ scrollTop: 0 }, "slow");
		});
		$(document).on("click", "#myBtn", function() {
			$("html, body").animate({ scrollTop: 0 }, "slow");
		});
        

		$(document).on("click", ".change_status", function() {
			var status = $(this).data('status');
			var company_id = $(this).data('id');
            $.ajax({
		        url : "ajax/update_status.php",
		        data : { status: status, company_id: company_id},
		        dataType: 'json',
		    }).done(function (data) { 
		    	if(status == 'no_status'){
		    		$("#"+company_id+"_"+"trash").addClass('btn-outline');
			    	$("#"+company_id+"_"+"call_back").addClass('btn-outline');
			    	$("#"+company_id+"_"+"lead").addClass('btn-outline');
			    	setTimeout(function(){
			    		$("#"+company_id+"_"+"tr").removeClass('highlight');
				        $("#"+company_id+"_"+"tr").css('background-color','#FFFFFF');
				   }, 500);
		    	}else{
		    		$("#"+company_id+"_"+"trash").addClass('btn-outline');
			    	$("#"+company_id+"_"+"call_back").addClass('btn-outline');
			    	$("#"+company_id+"_"+"lead").addClass('btn-outline');
			        $("#"+company_id+"_"+status).removeClass('btn-outline');

			        $("#"+company_id+"_"+"tr").css('background-color','#8BC34A');
		    	}
		    });
        });

		$(document).on("click", ".save-search-btn", function() {
			var fleet_size = $("#fleet_size").val();
			var postcode = $("#postcode").val();
			var sales_id = $("#sales_id").val();
            $.ajax({
		        url : "ajax/save_searching.php",
		        data : { fleet_size: fleet_size, postcode: postcode, sales_id:sales_id},
		        dataType: 'json',
		    }).done(function (data) { 
		    	$(".save-search").modal("hide");
		    	alert('Searching saved successfully.');
		    });
        });
        
	});
	</script>