<?php include("cn.php");?>
<?php $title = "Brands";
?>
<!DOCTYPE html>
<html lang="en">
<?php include("head-tag.php");
$where = $_REQUEST["brand"];
$where_sql = ($where=="")?"":"where brands.Id = '$where'";

$sql = mysqli_query($connect, "select count(*) as total, brands.* from brands $where_sql order by Title ASC") or die(mysqli_error($connect));
$data_count = 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">Brands</li>
                                            </ol>
                                            <h4 class="text-dark">Brands</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 align-center">
                        <h4 class="card-title  mb-0 mt-2"><?php echo $data_count["total"]?> Brands
	<a href="javascript:void(0)" class="btn btn-theme ctm-border-radius text-white btn-sm float-right" data-toggle="modal" data-target="#add-brand"><i class="fa fa-plus" aria-hidden="true"></i> Add Brand</a>
                        </h4>
                        
                    </div>  
                </div>
                <div class="row">
                  <?php $sql = mysqli_query($connect, "select brands.*, count(cars.Id) as total from brands 
                  left join cars on brands.Id = cars.Brand
                  $where_sql
                  group by brands.Id
                  order by brands.Title ASC");
						while($data = mysqli_fetch_assoc($sql)){?>
                            <div class="col-md-6">
                                <div class="ctm-border-radius shadow-sm card">
                                    <div class="card-header">
                                        <h4 class="page-sub-title d-inline-block mb-0 mt-2"><?php echo $data['Title']?></h4>
                                        
                                    </div>
                                    <div class="card-body">
                                            <span class="avatar" data-toggle="tooltip" data-placement="top" title="<?php echo $data['Title']?>" data-original-title="<?php echo $min_data['Title']?>">
                                                <img src="cars/car.jpg" alt="<?php echo $min_data['Title']?>" class="img-fluid"> </span>
                                                 <span class="ctm-text-sm ml-4"><?php echo $data['total']?> Cars</span>
                                        <a href="cars.php?brand=<?php echo $data['Id']?>" class="btn btn-theme button-1 btn-sm float-right ctm-border-radius text-white ml-2"><i class="fa fa-eye" aria-hidden="true"></i></a>
                                        <a href="javascript:void(0)" class="btn btn-theme button-1 ctm-border-radius text-white btn-sm float-right ml-2" data-toggle="modal" data-target="#edit-brand<?php echo $data['Id']?>"><i class="fa fa-pencil" aria-hidden="true"></i></a>
                                            <div class="modal fade" id="edit-brand<?php echo $data['Id']?>">
<div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-body">
                <form method = "post" action="ajax/edit-brand.php" >
				<input type="hidden" name="brand" value="<?php echo $data['Id']?>">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title mb-3">Edit Brand</h4>
                <div class="row">
                    <div class="col-md-12 col-sm-12 col-xs-12 form-group">
                        <input class="form-control" name="brandname1" type="text" placeholder="Brand Name" value="<?php echo $data['Title']?>" />
                    </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>
                                </div>
                            </div>
                        <?php }?>  
                </div>
                
            </div>
        </div>
    </div>
</div>


</div>
<div class="modal fade" id="add-brand">
<div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-body">
                <form method = "post" action="ajax/save-brand.php" enctype="multipart/form-data">

                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title mb-3">New Brand</h4>
                <div class="row">
                    <div class="col-md-12 col-sm-12 col-xs-12 form-group">
                        <input class="form-control" type="text" name="brandname1" placeholder="Brand Name" required/>
                    </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>

</html>