<?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 = "select count(*) as total from cars ".$where_sql;
$sql = mysqli_query($connect, $sql);
$data = mysqli_fetch_assoc($sql);

$sql = "select count(*) as total from cars ";
$sql = mysqli_query($connect, $sql);
$data1 = 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">
            <!-- Brands Filter Start -->
            <div class="col-xl-3 col-lg-3 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="cars.php" method="post">
                      <div class="form-group">
                        <select class="form-control select" name="brand">
                          <option value="">-- All Brands --</option> <?php $sql = mysqli_query($connect, "SELECT count(cars.car_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)){
                                            $brandname = $data_m['Title'];
                                            $brandid = $data_m["Id"];
                                            $total = $data_m['total'];
                                            ?> <option value="
														<?php echo $brandid;?>"> <?php echo $brandname." (".$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>
            <!-- Brands Filter End -->
            <div class="col-xl-9 col-lg-9 col-md-12">
              <!-- Add car Button  -->
              <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 (Out of <?php echo $data1["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>
              <!-- Add car Button End-->
              <!-- Cars List/ Table  -->
              <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">Name Plate</th>
                          <th class="text-center">Daily Rate</th>
                          <th class="text-center">Rate per Min</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");
                                            $url="../booking/";
										while($data = mysqli_fetch_assoc($sql)){
                                            $carname = $data["car_name"];
                                            ?> <tr>
                          <td>
                            <div style="width:50px;height:50px;">
                              <img class="img-responsive img-thumbnail" src="<?php echo  $data["car_img"]?>">
                            </div>
                          </td>
                          <td>
                            <h2 class="ml-2">
                              <a href="details.php?id=<?php echo $data["car_id"]?>"> <?php echo  $carname; ?> </a>
                            </h2>
                          </td>
                          <td class="text-center"> <?php echo $data["brand_title"]; ?> </td>
                          <td class="text-center"> <?php echo $data["model"]?> </td>
                          <td class="text-center"> <?php echo $data["car_nameplate"]?> </td>
                          <td class="text-center"> <?php echo $data["ac_price_per_day"]; ?> AED </td>
                          <td class="text-center"> <?php echo $data["ac_price"]; ?> 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["car_id"]?>"><i class="fa fa-pencil" aria-hidden="true"></i></a>
    <a href="details.php?id=<?php echo $data["car_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>

                                            <!-- Edit Car in Cars List/ Table  -->
        
        <div class="modal fade" id="edit-car<?php echo $data["car_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="carid" id="carid" value ="<?php echo $data["car_id"]?>">
                  <button type="button" class="close" data-dismiss="modal">&times;</button>
                  
                  <h4 class="modal-title mb-3">Edit Car (Car Id = <?php echo $data["car_id"]; ?>) </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="285"
    						data-default-file='<?php echo $data['car_img']?>'
    						data-allowed-file-extensions='["jpg", "png", "jpeg"]'/>
    				</div>
                    </div>
                    <div class="col-md-6 col-sm-12 col-xs-12">
                      <div class="form-group">
                        <label style="color:blue;">Car Name</label>
                        <input type="text" class="form-control " placeholder="Name of Car" name="title" value="<?php echo $data['car_name']?>" required />
                      </div>
                      <div class="form-group">
                        <label style="color:blue;">Car Brand</label>
                        <!--<input type="text" class="form-control " placeholder="Name of Car" name="Brand" value="<?php echo $data['Brand']?>"/>-->
                        <select class="form-control" name="brand" required>
                            
                            <option value="">-- All Brands --</option>
                            <?php
                            $what = $data['Brand'];
                            $query = "SELECT brands.* FROM brands";
                            $sql_b = mysqli_query($connect,$query);
                            while($data_b = mysqli_fetch_assoc($sql_b))
                            {
                                $brandid = $data_b['Id'];
                                $brandname = $data_b['Title'];
                                ?> <option value="<?php echo $brandid; ?>" <?php if ($what == $brandid) echo 'selected';?>> <?php
                                echo $brandname."</option>";
                            }
                            ?>
                          </select>
                      </div>

                      
                      <div class="form-group">
                        <label style="color:blue;">Model</label>
                        <input type="text" class="form-control" placeholder="Model" name="model" value="<?php echo $data['model']?>" />
                      </div>
                      <div class="form-group">
                        <label style="color:blue;">Car Number Plate</label>
                        <input type="text" class="form-control" placeholder="Number Plate" name="plate" value="<?php echo $data['car_nameplate']?>" />
                      </div>
                    </div>
                    <div class="col-md-6 col-sm-12 col-xs-12">
                      <div class="form-group">
                        <label style="color:blue;">Rate Per Day.</label>
                        <input class="form-control" name="rate" type="text" placeholder="Rate Per Day" value="<?php echo $data['ac_price_per_day']?>" />
                      </div>
                    </div>
                    <div class="col-md-6 col-sm-12 col-xs-12">
                      <div class="form-group">
                        <label style="color:blue;">Rate Per Minute.</label>
                        <input class="form-control" name="ratePerMin" type="text" placeholder="Rate Per Min" value="<?php echo $data['ac_price']?>" />
                      </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>
                  <!--Edit Car in Cars List/ Table End -->
                  </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">
                    <label style="color:blue;">Car Name</label>
                    <input type="text" class="form-control " placeholder="Name of Car" name="title" value="<?php echo $data['Title']?>" required />
                  </div>
                  <div class="form-group">
                    <label style="color:blue;">Car Brand</label>
                    <select class="form-control select" name="brand" required/>
                      <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["Id"] == $where )? "selected" : "" ;?>><?php echo $data_m['Title'];?></option>
				<?php }?>
                    </select>
                  </div>
                  </div>
                  <div class="col-md-6 col-sm-12 col-xs-12">
                    <div class="form-group">
                      <label style="color:blue;">Model</label>
                      <input type="text" class="form-control" placeholder="Model" name="model" value="<?php echo $data['Model']?>" required/>
                    </div>
                  </div>
                  <div class="col-md-6 col-sm-12 col-xs-12">
                    <div class="form-group">
                      <label style="color:blue;">Number Plate</label>
                      <input type="text" class="form-control" placeholder="Number Plate" name="plate" value="<?php echo $data['Plate']?>" required/>
                    </div>
                  </div>
                  <div class="col-md-6 col-sm-12 col-xs-12">
                    <div class="form-group">
                      <label style="color:blue;">Rate Per Day (AED)</label>
                      <input class="form-control" name="dailyrate" type="text" placeholder="Rate Per Day" value="<?php echo $data['ac_price_per_day']?>" required/>
                    </div>
                  </div>
                  <div class="col-md-6 col-sm-12 col-xs-12">
                    <div class="form-group">
                      <label style="color:blue;">Rate Per Minute (AED)</label>
                      <input class="form-control" name="ratepermin" type="text" placeholder="Rate Per Min" value="<?php echo $data['ac_price']?>" required/>
                    </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() {});
    
    $("#form").on("submit", function(e) {
        
        
        if(name!="" && email!="" && phone!="" && city!=""){
			$.ajax({
				url: "save.php",
				type: "POST",
				data: {
					name: title,
					brand: brand,
					model: model,
					plate: plate,
					dailyrate: dailyrate,
					reatepermin: ratepermin
				},
				cache: false,
				success: function(dataResult){
					var dataResult = JSON.parse(dataResult);
					if(dataResult.statusCode==200){
						$("#butsave").removeAttr("disabled");
						$('#fupForm').find('input:text').val('');
						$("#success").show();
						$('#success').html('Data added successfully !'); 						
					}
					else if(dataResult.statusCode==202){
						$("#error").show();
						$('#error').html('Car with same NamePlate already exists !');
					}
					
				}
			});
		}
		else{
			alert('Please fill all the field !');
		}
	});
});
</script>
</html>