<?php
session_start();
include_once "../config.php";
require_once '../database1.php';
require_once "../check_admin.php";

require_once '../check_edit_page.php';


class edit extends database
{
    
    
    public function Getdata($id)
    {
        $where['cl_id']= $id;
        return $this->fetchRow('tb_product_category', $where);
    }
    
    public function getAllBrands()
    {
        return $this->fetchAll('tb_brands');
    }
    
    public function getAllCategoryBrands($id)
    {
        $sql = ' SELECT b.cl_name,b.cl_image, '
                . 'pcb.cl_id,pcb.cl_url'
                . ' FROM tb_product_category_brands pcb INNER JOIN tb_brands b ON pcb.cl_brand_id=b.cl_id '
                . 'WHERE pcb.cl_deleted_at IS NULL '
                . ' AND b.cl_deleted_at IS NULL'
                . ' AND pcb.cl_category_id=?';
        return $this->callQuery($sql, [$id]);
    }
   
}

$obj = new edit();

$data = $obj->Getdata($id);
$brands = $obj->getAllBrands();

$categoryBrands = $obj->getAllCategoryBrands($id);

$templateType = 'edit';
include '../header.php';
?>

<style>
    .select2-container
    {
        width : 100% !important;
    }
    </style>
 <link href="<?php echo MAIN_DOMAIN ?>/dropzone/dist/dropzone.css" rel="stylesheet" />
 <script src="<?php echo MAIN_DOMAIN ?>//dropzone/dist/dropzone.js"></script>
               <!-- MAIN CONTENT-->
            <div class="main-content">
                <div class="section__content section__content--p30">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="overview-wrap">
                                    <h2 class="title-1">Edit Product Category  - <?php echo $data['cl_name'] ?></h2>
                                    
                                </div>
                                
                              
                            </div>
                        </div>
                        
                        <div class="row m-t-25 m-row">
                    
                            <div class="col-lg-10">
                                <div class="card">
                                    <ul class="nav nav-tabs">
                                        <li class="nav-item">
                                          <a class="nav-link" href="edit.php?id=<?php echo $id ?>">Main</a>
                                        </li>
                                        <li class="nav-item">
                                          <a class="nav-link" href="banners.php?id=<?php echo $id ?>">Banners</a>
                                        </li>
                                        <li class="nav-item">
                                            <a class="nav-link" href="top_rated.php?id=<?php echo $id ?>">Top Rated</a>
                                        </li>
                                        <li class="nav-item">
                                          <a class="nav-link" href="banners2.php?id=<?php echo $id ?>">Banners 2</a>
                                        </li>
                                        <li class="nav-item">
                                            <a class="nav-link active" href="brands.php?id=<?php echo $id ?>">Brands</a>
                                          </li>
                                          <li class="nav-item">
    <a class="nav-link" href="custom_text.php?id=<?php echo $id ?>">Custom Texts</a>
  </li>

                                      </ul>
                                   
                                    <div class="card-body card-block">
                                        <form method="post"  enctype="multipart/form-data" class="form" cluster-url="../ajax/product_category/add_brand?id=<?php echo $id;?>" cluster-redirect="/product_category/brands?id=<?php echo $id ?>">
                                            <div class="col-12">
                                                <div class="row">
                                                    <div class="col-12 new_design_container">
                                                        <div class="row">
                                                            <div class="col-12">
                                                                <label for="Name" class=" form-control-label">Brands</label>
                                                                <select class="form-control select2" name="brand_id" id="brand_id">
                                                                    <option value="">---Select---</option>
                                                                    <?php
                                                                    if(!empty($brands))
                                                                    {
                                                                        foreach($brands AS $brand)
                                                                        {
                                                                            ?>
                                                                            <option value="<?php echo $brand['cl_id'] ?>"><?php echo $brand['cl_name'] ?></option>
                                                                            <?php
                                                                        }
                                                                    }
                                                                    ?>
                                                                </select>
                                                                <div class="error-input"></div>

                                                                

                                                            </div>
                                                            <div class="col-12 mt-2">
                                                                <button type="submit" class="track-btn pull-right btn-submit">Submit</button>

                                                            </div>
                                                            
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                       </form>
                                   </div>
                                </div>
                                
                                <div class="card mt-2">
                                    <table class="table table-borderless table-data3 ACP_table" style="width: 100%">
                                        <thead>
                                            <tr>
                                                <th>
                                                    Image
                                                </th>
                                                <th>Brand</th>
                                                <th>
                                                    URL
                                                </th>
                                                
                                                <th data-priority="1">Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                           <?php
                                           if(!empty($categoryBrands))
                                           {
                                               foreach($categoryBrands AS $categoryBrand)
                                               {
                                                   ?>
                                            <tr>
                                                <td>
                                                    <img style="max-width: 150px;max-height : 150px;width:auto;height : auto;" src="<?php echo MAIN_DOMAIN ?>/uploads/brands/<?php echo $categoryBrand['cl_image'] ?>" />
                                                </td>
                                                <td>
                                                    <?php echo htmlspecialchars($categoryBrand['cl_name']) ?>
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control url" value="<?php echo htmlspecialchars($categoryBrand['cl_url']) ?>" />
                                                </td>
                                                <td class="action-container">
                                                    <button type="button" class="btn btn-success btn-update" cluster-id="<?php echo $categoryBrand['cl_id'] ?>"><i class="fa fa-edit"></i></button>   
                                                    <button type="button" cluster-href="../ajax/product_category/delete_brands?id=<?php  echo htmlspecialchars($categoryBrand['cl_id']); ?>" class="remove btn btn-danger btn-delete"><i class="fa fa-trash"></i></button>
                                                    <div class="error-input"></div>

                                                </td>
                                            </tr>
                                                   <?php
                                               }
                                           }
                                                   
                                           
                                           ?>
                                           
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                          
                        </div>
                        
                       <script>
    
     
     
     $(document).ready(function() {
    $('.select2').select2();
    
    
});


$(document).on('click', '.btn-update', function(){
    var id = $(this).attr('cluster-id');
    var data = new Object();
    var parent = $(this).parents('tr').first();
    var current = $(this);
    $(current).attr('disabled', 'disabled');
    $(parent).find('.error-input').html('<div class="alert alert-warning">Updating....</div>');
    data['url'] = $(parent).find('.url').val();
    $.post('../ajax/product_category/update_brand?id=' + id, {data:data}, function(){
        $(current).removeAttr('disabled');
        $(parent).find('.error-input').html('<div class="alert alert-success">Updated Successfully</div>');
    }, 'json').fail(function(){
        $(current).removeAttr('disabled');
        $(parent).find('.error-input').html('<div class="alert alert-danger">Something Went Wrong</div>');
    });
});
 </script>       
                        
                   <?php
				   include '../footer.php';
				  