<?php


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

class edit extends database
{
    
    public function index($data,$id)
    {
        $where['cl_id']= $id;
        return $this->update('tb_product',$data, $where);
    }
    public function Getdata($id)
    {
        $where['cl_id']= $id;
        return $this->fetchRow('tb_product', $where);
    }

    public function getAllImages($id)
    {
        $where['cl_product_id'] = $id;
        return $this->fetchAll('tb_files', $where);
    }
	public function GetCategory()
    {
		return $this->fetchAll('tb_product_category');
	}
	public function GetBrand()
    {
		return $this->fetchAll('tb_brands');
	}

    public function getCurrentProductCategory($catID)
    {
        $where['cl_id'] = $catID;
        return $this->fetchRow('tb_product_category', $where);
    }

    public function getAllSubCategories($catID)
    {
        $where['cl_cat_id'] = $catID;
        return $this->fetchAll('tb_product_subcategory', $where);
    }

    public function getAllProductsEAN($ean)
    {
        $where['cl_ean'] = $ean;
        return $this->fetchAll('tb_product', $where);
    }

    public function getAllFiltersofSubCategory($subCategoryID)
    {
        $sql = ' SELECT f.* FROM tb_filter f INNER JOIN tb_product_subcategory_filter psf ON f.cl_id=psf.cl_filter_id WHERE f.cl_deleted_at IS NULL AND psf.cl_sub_category_id=? ORDER BY f.cl_id ';
        $where[] = $subCategoryID;

        return $this->callQuery($sql, $where);
    }
}

$obj = new edit();

$data = $obj->Getdata($id);
if(!$data)
{
    header('Location: list.php');
    exit;
}

$productCategory = $obj->getCurrentProductCategory($data['cl_cat_id']);
$subCategories = $obj->getAllSubCategories($data['cl_cat_id']);


$images = $obj->getAllImages($id);
$productCategories=$obj->GetCategory();
$brands=$obj->GetBrand();

$filters = $obj->getAllFiltersofSubCategory($data['cl_sub_cat_id']);

$otherProducts = [];
$ean = $data['cl_ean'];
if($ean != null && trim($ean) != '')
{
    $otherProducts = $obj->getAllProductsEAN($ean);
}
$templateType = 'edit';
include '../header.php';
?>

<script src="<?php echo MAIN_DOMAIN ?>/ckeditor/ckeditor.js"></script>
 <link href="<?php echo MAIN_DOMAIN ?>/dropzone/dist/dropzone.css" rel="stylesheet" />
 <script src="<?php echo MAIN_DOMAIN ?>//dropzone/dist/dropzone.js"></script>
   <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">Add Filter to Product - <?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">
                                    <div class="card-body card-block">
                                          <div class="table-responsive" style="width:100%">
                                              <table class="table table-bordered table-striped" id="filter_table" style="width:100%">
                                                  <thead>
                                                    <tr>
                                                        <th>
                                                            Filter
                                                        </th>
                                                        <th>
                                                            Value
                                                        </th>
                                                        <th>

                                                        </th>
                                                    </tr>
                                                  </thead>
                                                  <tbody>
                                                    <?php
                                                    $filterName = '';

                                                    if(!empty($filters))
                                                    {
                                                        foreach ($filters as $filter) {

                                                            
                                                            ?>
                                                            <tr cluster-id="<?php echo $filter['cl_id'] ?>">
                                                                <td>
                                                                    <?php echo $filter['cl_name'] ?>
                                                                </td>
                                                                <td class="select_value">
                                                                    <select class="form-control select_value_value">

                                                                    </select>
                                                                </td>
                                                                <td>
                                                                    <button type="button" class="btn btn-success btn-update" cluster-id="<?php echo $filter['cl_id'] ?>">Edit</button>
                                                                    <div class="error-main"></div>
                                                                </td>
                                                            </tr>
                                                            <?php
                                                        }
                                                    }
                                                    ?>
                                                  </tbody>
                                              </table>
                                          </div>
                                
                                        <form method="post" class="form" cluster-url="../ajax/product/edit_ajax?id=<?php echo htmlspecialchars($_GET['id']);?>" cluster-redirect="list.php" >
                                           
                                        
                                        </form>
                                    </div>
                                </div>
                            </div>
                          
                        </div>

                        <script>
                                  CKEDITOR.replace( 'small_specification');
                                  CKEDITOR.replace( 'big_specification');
                                  CKEDITOR.replace( 'big_specification_english');
                                  CKEDITOR.replace( 'description');
                        </script>  


                        <script>

                            $(document).ready(function(){
                                $('#filter_table tbody tr').each(function(i, value){
                                    var html = '<option value="">---Select Value----</option>';
                                    var id = $(value).attr('cluster-id');
                                    var valueID = '0';

                                    $.get('../ajax/product/get_filters?id=' + id + '&product_id=<?php echo $id ?>', function(res){
                                        $(res).each(function(i1, value1){

                                            var selected = '';
                                            if(value1.cl_value_id == value1.cl_id)
                                            {
                                                selected = ' selected="selected" ';
                                            }
                                            html+= '<option value="' + value1.cl_id + '" ' + selected + '>' + value1.cl_value + '</option>';
                                            
                                        });
                                        $(value).find('.select_value select').html(html);
                                    }, 'json');
                                    
                                    
                                });
                            });


                             $(document).on('click', '.btn-update', function(){
                                 var current = $(this);
                                 $(current).attr('disabled', 'disabled');
                                var parent = $(this).parents('tr').first();
                                var id = $(this).attr('cluster-id');

                                var data = new Object();
                                data['filter_id'] = id;
                                data['value_id'] = $(parent).find('.select_value_value').val();
                                data['product_id'] = <?php echo $id ?>;

                                   $(parent).find('.error-main').html('<div class="alert alert-warning">Updating....</div>').show();
                                $.post('../ajax/product/update_filter?id=' + id, {data:data}, function(){
                                    $(current).removeAttr('disabled');
                                     $(parent).find('.error-main').html('<div class="alert alert-success">Updated Successfully</div>').show();
                                }) .fail(function(xhr) {

                                    $(current).removeAttr('disabled');
                                   
                                      var html = '';
                                      var res = xhr.responseJSON;
                                      console.log(xhr);
                                      if (typeof res === 'undefined') {
                                        $(parent).find('.error-main').html('<div class="alert alert-danger">Something went wrong</div>').show();
                                      }
                                      if (typeof res.error_description === 'undefined') {
                                        if(typeof res.error_text === 'undefined')
                                        {
                                            $(parent).find('.error-main').html('<div class="alert alert-danger">Something went wrong</div>').show();
                                            window.location.href = '#body_part';
                                            return;
                                        }
                                        else
                                        {
                                            $(parent).find('.error-main').html('<div class="alert alert-danger">' + res.error_text + '</div>').show();
                                            window.location.href = '#body_part';
                                            return;
                                        }
                                      }
                                  
                                      
                                      
                                      window.location.href = '#body_part';
                                  },'json');;
                            });
                        </script>

<?php
include '../footer.php';
?>