<?php include("cn.php");?>
<?php $title = "Cars";?>
<!DOCTYPE html>
<html lang="en">
<?php include("head-tag.php");
$where = $_REQUEST["brand"];
$where_sql = ($where=="")?"":"where cars.Brand = '$where'";
$sql = mysqli_query($connect, "select count(*) as total from cars $where_sql");
$data = mysqli_fetch_assoc($sql);

?>
<body>

<div class="inner-wrapper">
<?php include("pre-loader.php");?>

<?php include("header.php");?>
<div class="page-wrapper">
    <div class="container-fluid">
        <div class="row">
            <div class="col-xl-3 col-lg-4 col-md-12 theiaStickySidebar">
                <aside class="sidebar sidebar-user">
                    <div class="card ctm-border-radius shadow-sm">
                        <div class="card-body py-4">
                            <div class="row">
                                <div class="col-md-12 mr-auto text-left">
                                        <div class="custom-search input-group">
                                            <div class="custom-breadcrumb">
                                                <ol class="breadcrumb no-bg-color d-inline-block p-0 m-0 mb-2">
                                                    <li class="breadcrumb-item d-inline-block"><a href="index.php" class="text-dark">Home</a></li>
                                                    <li class="breadcrumb-item d-inline-block active"><?php echo $title?></li>
                                                </ol>
                                                <h4 class="text-dark">Cars</h4>
                                            </div>
                                        </div>
                                    </div>
                            </div>
                        </div>
                    </div>
                    <div class="quicklink-sidebar-menu ctm-border-radius shadow-sm bg-white card">
                        <div class="card-body">
                            <form action="" method="get">
                                <div class="form-group">
                                    <select class="form-control select" name="brand">
                                        <option value="">-- All Brands --</option>
                                        <?php $sql = mysqli_query($connect, "select count(cars.Id) as total, brands.* 
										from brands 
										inner join cars ON brands.Id = cars.Brand
                                        group by brands.Id");
										while($data_m = mysqli_fetch_assoc($sql)){?>
											<option value="<?php echo $data_m["Id"]?>"><?php echo $data_m['Title']." (".$data_m['total'].")";?></option>
										<?php }?>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <button type="submit" class="btn btn-theme button-1 ctm-border-radius text-white btn-block"><span><i class="fa fa-filter"></i></span> Filter</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </aside>
            </div>
            <div class="col-xl-9 col-lg-8 col-md-12">
                <div class="card shadow-sm ctm-border-radius">
                    <div class="card-body">
                        <h4 class="card-title mb-0 mt-2"><?php echo $data["total"]?> Cars
                        	<a href="javascript:void(0)" class="btn btn-theme ctm-border-radius text-white btn-sm float-right" data-toggle="modal" data-target="#add-car"><i class="fa fa-plus" aria-hidden="true"></i> Add Car</a>
                        </h4>
                    </div>
                </div>
                
                <div class="ctm-border-radius shadow-sm card">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table id="cars" class="table table-striped table-bordered ">
						<thead>
						<tr>
						  <th class="text-center">Car</th>
						  <th class="text-center">Name</th>
						  <th class="text-center">Brand</th>
						  <th class="text-center">Model</th>
						  <th class="text-center">Daily Rate</th>
                          <th class="text-center">Action</th>
						</tr>
						</thead>
						<tbody>
										<?php
										$sql = mysqli_query($connect, "select cars.*, brands.Title as brand_title
										from cars
    										left join brands ON brands.Id = cars.Brand
                                            $where_sql");
                                           
										while($data = mysqli_fetch_assoc($sql)){?>
											<tr>
											    
                                            <td>
                                                <div style="width:50px;height:50px;">
                                                    <img class="img-responsive img-thumbnail" src="<?php echo  $data["Image"]?>">
                                                </div>
                                                
                                                
                                            </td>
                                            <td>
                                                <h2 class="ml-2"><a href="details.php?id=<?php echo $data["Id"]?>"><?php echo  $data["Title"]?></a></h2>
                                            </td>
                                            <td class="text-center"> <?php echo $data["brand_title"]?> </td>
                                            <td class="text-center">
                                                <?php echo $data["Model"]?></a>
                                            </td>
                                            <td class="text-center"> <?php echo $data["Rate"]?> AED</td>
                                            
                                            <td class="text-center">
                                            <a href="javascript:void(0)" class="btn btn-theme button-1 ctm-border-radius text-white btn-sm float-center" data-toggle="modal" data-target="#edit-car<?php echo $data['Id']?>"><i class="fa fa-pencil" aria-hidden="true"></i></a>
                                            <a href="details.php?id=<?php echo $data["Id"]?>" class="btn btn-theme button-1 ctm-border-radius text-white btn-sm float-center" ><i class="fa fa-car" aria-hidden="true"></i></a>

                                            <div class="modal fade" id="edit-car<?php echo $data['Id']?>">
                                                <div class="modal-dialog modal-dialog-centered">
                                                    <div class="modal-content">
                                                        <div class="modal-body float-left text-left">
                                                            <form method = "post" action="ajax/edit-car.php" enctype='multipart/form-data'>
                                            				<input type="hidden" name="id" value="<?php echo $data['Id']?>">
                                                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                                                            <h4 class="modal-title mb-3">Edit Car</h4>
                                                            <div class="row">
                                                                <div class="col-md-6 col-sm-12 col-xs-12">
                        											<div class="form-group">
                        												<input type="file" name="photo" id="photo" class="dropify" data-height="220"
                        												data-default-file='<?php echo $data['Image']?>'
                        												data-allowed-file-extensions='["jpg", "png", "jpeg"]'/>
                        											</div>
                                                                </div>
                                                                <div class="col-md-6 col-sm-12 col-xs-12">
                                                                    <div class="form-group">
                                                                     <input type="text" class="form-control " placeholder="Name of Car"  name="title" value="<?php echo $data['Title']?>" required/>
                                                                    </div>
                                                                    <div class="form-group">
                                                                        <select class="form-control select" name="brand">
                                                                            <option value="">-- Select Brand --</option>
                                                                            <?php $sql_m = mysqli_query($connect, "select brands.* 
                                    										from brands");
                                    										while($data_m = mysqli_fetch_assoc($sql_m)){?>
                                    											<option value="<?php echo $data_m["Id"]?>"
                                    											<?php echo ($data['Brand']==$data_m["Id"])?"selected":"";?>><?php echo $data_m['Title'];?></option>
                                    										<?php }?>
                                                                        </select>
                                                                    </div>
                                                                    
                                                                    <div class="form-group">
                                                                        <input type="text" class="form-control" placeholder="Model" name="model" value="<?php echo $data['Model']?>" />
                                                                    </div>
                                                                    <div class="form-group">
                                                                        <input class="form-control" name="rate" type="text" placeholder="Rate Per Day" value="<?php echo $data['Rate']?>" />
                                                                    </div>
                                                                </div>
                                                                
                                                            </div>
                                                            <button type="button" class="btn btn-danger float-right ml-3 ctm-border-radius" data-dismiss="modal">Cancel</button>
                                                            <button type="submit" class="btn btn-theme  button-1 text-white ctm-border-radius float-right">Save</button>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            </td>
                                        </tr>
										<?php }?>
										
                                    </tbody>
						</table>
                        </div>
                    </div>
                </div>
            </div>
            
        </div>
    </div>
</div>


</div>
<div class="modal fade" id="add-car">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-body">
                <form method = "post" action="ajax/save-car.php" enctype='multipart/form-data'>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title mb-3">Add Car</h4>
                <div class="row">
                    <div class="col-md-6 col-sm-12 col-xs-12">
						<div class="form-group">
							<input type="file" name="photo" id="photo" class="dropify" data-height="220"
							data-allowed-file-extensions='["jpg", "png", "jpeg"]'/>
						</div>
                    </div>
                    <div class="col-md-6 col-sm-12 col-xs-12">
                        <div class="form-group">
                         <input type="text" class="form-control " placeholder="Name of Car"  name="title" value="<?php echo $data['Title']?>" required/>
                        </div>
                        <div class="form-group">
                            <select class="form-control select" name="brand">
                                <option value="">-- Select Brand --</option>
                                <?php $sql = mysqli_query($connect, "select brands.* 
								from brands");
								while($data_m = mysqli_fetch_assoc($sql)){?>
									<option value="<?php echo $data_m["Id"]?>"><?php echo $data_m['Title'];?></option>
								<?php }?>
                            </select>
                        </div>
                        
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="Model" name="model" value="<?php echo $data['Model']?>" />
                        </div>
                        <div class="form-group">
                            <input class="form-control" name="rate" type="text" placeholder="Rate Per Day" value="<?php echo $data['Rate']?>" />
                        </div>
                    </div>
                    
                </div>
                <button type="button" class="btn btn-danger float-right ml-3 ctm-border-radius" data-dismiss="modal">Cancel</button>
                <button type="submit" class="btn btn-theme  button-1 text-white ctm-border-radius float-right">Save</button>
                </form>
            </div>
        </div>
    </div>
</div>
<div class="sidebar-overlay" id="sidebar_overlay"></div>

<?php include("scripts.php");?>
</body>
<script>
    $(document).ready(function() {
        
    });
</script>
</html>