<?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 getCurrentSubCategory($subCatID)
    {
        $where['cl_id'] = $subCatID;
        return $this->fetchRow('tb_product_subcategory', $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);
    }
    
    
    function getAllSubCategoryFilters($subCategoryID)
    {
        return $this->fetchAll('tb_product_subcategory_filter', ['cl_sub_category_id' => $subCategoryID]);
    }
    
    function getAllSubCategoryFilterValues($subCategoryFilterID)
    {
        return $this->fetchAll('tb_product_subcategory_values_filter', ['cl_product_subcategory_filter_id' => $subCategoryFilterID]);
    }
}

$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']);

$currentSubCategory = $obj->getCurrentSubCategory($data['cl_sub_cat_id']);
$subCategoryFilters = $obj->getAllSubCategoryFilters($currentSubCategory['cl_id']);


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

$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">Edit Product</h2>
                                    
                                </div>
                           
                            </div>
                        </div>
                        
                        <div class="row m-t-25 m-row">
                    
                            <div class="col-lg-10">

                                <div class="card">
                                    <div class="card-header">
                                        Other Products with same EAN
                                    </div>
                                    <div class="card-body">
                                        <table class="table table-striped">
                                        <?php
                                            if(!empty($otherProducts))
                                            {
                                                foreach($otherProducts AS $otherProduct)
                                                {
                                                    if($otherProduct['cl_id'] != $data['cl_id'])
                                                    {
                                                    ?>
                                                    <tr>
                                                        <td>
                                                            <a href="/product/edit.php?id=<?php echo htmlspecialchars($otherProduct['cl_id']) ?>"><?php echo htmlspecialchars($otherProduct['cl_name']) ?></a>
                                                        </td>
                                                    </tr>
                                                    <?php
                                                    }   
                                                }
                                            }
                                        ?>
                                    </table>
                                    </div>
                                </div>

                                <div class="card">
                                    
                                    <div class="card-body card-block">
                                        <form action="../ajax/product/product_file_upload?id=<?php echo htmlspecialchars($_GET['id']) ?>"
                  class="dropzone"
                  id="flie_upload_dropzone">
                    

                  </form>


                  <div class="table-responsive" style="width:100%">
                  <table class="table table-bordered table-striped" style="width:100%">
                      <thead>
                        <tr>
                            <th>
                                Image
                            </th>
                            <th>
                                Extension
                            </th>
                            <th>

                            </th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php
                            if(!empty($images))
                            {
                                foreach($images AS $image)
                                {
                                    ?>
                                    <tr class="action-container">
                                        <td>
                                            <img src="<?php echo MAIN_DOMAIN ?>/uploads/products/<?php echo htmlspecialchars($image['cl_name']) ?>" style="max-width: 150px" />
                                        </td>
                                        <td>
                                            <?php echo htmlspecialchars($image['cl_extension']) ?>
                                        </td>
                                        <td>
                                             <button type="button" cluster-href="../ajax/product/delete_image_ajax?id=<?php  echo htmlspecialchars($image['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>

          <div class="row">
          <div class="col-xs-12 col-sm-6">
           <div class="card">
                <div class="card-body card-block">
                    <a class="btn btn-danger" href="#variable_products">Variable Products</a>
                    <form method="post" class="form" cluster-url="../ajax/product/edit_ajax?id=<?php echo htmlspecialchars($_GET['id']);?>" cluster-redirect="list.php" >
                        <?php include_once "template/product_form.php" ?>
                    </form>


                    <div class="row " style="float:left;width:100%;margin-top: 15px">
                        <div class="col-lg-12">
                            <div class="card">
                                
                                <div class="card-body card-block">
                                   <table class="table table-bordered table-striped" id="existing_table_filter" style="width:100%">
                                        <thead>
                                            <tr>
                                                <th colspan="2">
                                                    Existing Tags
                                                </th>
                                            </tr>
                                           
                                        </thead>
                                        <tbody>

                                        </tbody>
                                   </table>

                                    <table class="table table-bordered table-striped top-margin" id="new_filter_table" style="margin-top: 15px;width:100%">
                                        <thead>
                                            <tr>
                                                <th colspan="2">
                                                    New Tag
                                                    <button type="button" class="btn btn-danger" id="btn-add">Add</button>
                                                </th>
                                            </tr>
                                            
                                        </thead>
                                        <tbody>
                                            
                                        </tbody>
                                   </table>
                               </div>
                            </div>
                        </div>
                          
                    </div>

                      
                </div>
            </div>
        </div>
        <div class="col-xs-12 col-sm-6">
            <div class="card">
                <div class="col-sm-12 top-margin">

                                                <div class="row input-container">
                                                    <h5 class="col-12 mb-2">
                                                        Filters
                                                    </h5>
                                                    <table class="table table-bordered">
                                                        
                                                    <?php
                                                    if(!empty($subCategoryFilters))
                                                    {
                                                        foreach($subCategoryFilters AS $subCategoryFilter)
                                                        {
                                                            $subCategoryFilterID  = $subCategoryFilter['cl_id'];
                                                            $filterValues = $obj->getAllSubCategoryFilterValues($subCategoryFilterID);
                                                            ?>
                                                        <tr>
                                                            <td>
                                                                <?php echo htmlspecialchars($subCategoryFilter['cl_name']) ?>
                                                            </td>
                                                            <td>
                                                                <select class="form-control valueSelect" cluster-id="<?php echo htmlspecialchars($subCategoryFilter['cl_id']) ?>">
                                                                    <option value="0">---Select---</option>
                                                                    <?php
                                                                    if(!empty($filterValues))
                                                                    {
                                                                        foreach($filterValues AS $filterValue)
                                                                        {
                                                                            ?>
                                                                    <option value="<?php echo htmlspecialchars($filterValue['cl_id']) ?>"><?php echo htmlspecialchars($filterValue['cl_name']) ?></option>
                                                                            <?php
                                                                        }
                                                                    }
                                                                    ?>
                                                                </select>
                                                                <div class="error-input"></div>
                                                            </td>
                                                        </tr>
                                                            
                                                            <?php
                                                        }
                                                    }
                                                    ?>
                                                    </table>
                                                    <div class="col-sm-12 error-main">


                                                    </div>
                                                </div>
                                             </div>
            </div>
            <div class="">
                <div class="filter_main_container">

                </div>

            </div>
        </div>
</div>


<div class="row top-margin" id="variable_products">
    <div class="col-xs-12 col-sm-12">
       <div class="card">
            <div class="card-header">
                Variable Products
            </div>

            <div class="table-responsive filter_container" style="width:100%">
                                             <div class="col-sm-12" id="filter_select_container" style="padding: 15px;background-color:lightgray;margin-bottom:15px;margin-top:15px">
                                                  <div class="row well" >

                                                  </div>
                                                    <div style="margin-top:15px;margin-bottom: 15px">
                                                        <label >Default? </label> <input type="checkbox" id="input-checkbox"/>
                                                        <button class="btn btn-success" id="btn-add-variable-product">Add</button>
                                                        <div class="error-main"></div>
                                                    </div>
                                                 </div>
                                          </div>
             <div class="table-responsive filter_container" style="width:100%">
                                             <table class="table table-bordered table-striped" id="attribute_table">
                                                <thead>
                                                    <tr>
                                                        <th>
                                                            Filter & Value
                                                        </th>
                                                        
                                                        <th>
                                                            Supplier Price
                                                        </th>
                                                        <th>
                                                            Monthly Price
                                                        </th>
                                                        <th>
                                                            Weight
                                                        </th>
                                                        <th>
                                                            Show?
                                                        </th>
                                                        <th>
                                                            Defautl?
                                                        </th>
                                                        <th>

                                                        </th>
                                                    </tr>
                                                </thead>
                                                <tbody>

                                                </tbody>
                                             </table>
                                          </div>
        </div>
    </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(){
                                getAllTagsByProductID(<?php echo $id ?>);
                                $('#btn-add').click(function(){
                                    var html = '<tr><td class="input-container"><input type="text" class="form-control name" name="name" /><div class="error-input"></div></td><td><button type="button" class="btn btn-danger btn-add"><i class="fa fa-plus"></i></button></td></tr>';
                                    $('#new_filter_table tbody').html(html);
                                });
                            });

                            function getAllTagsByProductID(id)
                            {
                                $('#existing_table_filter tbody').html('');
                                $.get('../ajax/product/getAllTags?id=' + id, function(res){
                                    var html = '';
                                    $(res).each(function(i, value){

                                       

                                        html = '<tr class="existing_tr"><td class="input-container"><input type="text" class="form-control name" name="name" value="' + value.cl_name + '" /><div class="error-input"></div></td><td><button type="button" class="btn btn-success btn-update" cluster-id="' + value.cl_id + '"><i class="fa fa-edit"></i></button><div class="error-main"></div></td></tr>';

                                        $('#existing_table_filter tbody').append(html);
                                    });

                                    //setAllFilters();
                                },'json');
                            }


                            $(document).on('click', '.btn-add', function(){
                                var current = $(this);
                                var parent = $(this).parents('tr').first();
                                

                                $(current).attr('disabled', 'disabled');
                                var data = new Object();
                                 data['name'] = $(parent).find('.name').val();

                                $.post('../ajax/product/add_tag?id=<?php echo $id ?>', {data:data}, function(){
                                    $(parent).remove();
                                    getAllTagsByProductID(<?php echo $id ?>);
                                },'json') .fail(function(xhr) {

                                    $(current).removeAttr('disabled');
                                   
                                      var html = '';
                                      var res = xhr.responseJSON;
                                      console.log(xhr);
                                      if (typeof res === 'undefined') {
                                        $(current).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')
                                        {
                                            $(current).find('.error-main').html('<div class="alert alert-danger">Something went wrong</div>').show();
                                            
                                            return;
                                        }
                                        else
                                        {
                                            $(current).find('.error-main').html('<div class="alert alert-danger">' + res.error_text + '</div>').show();
                                            
                                            return;
                                        }
                                      }
                                    
                                    // var res1 = $.parseJSON('"' + res + '"');
                                     $.each(res.error_description, function(i, value) {
                                           var parentTD = $(parent).find("[name='" + i + "']").parents('.input-container').first();
                                            $(parentTD).find('.error-input').append('<div class="alert alert-danger alert-flat">' + value + '</div>');
                                          
                                      });
                                      
                                      
                                     
                                  },'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['name'] = $(parent).find('.name').val();

                                   $(parent).find('.error-main').html('<div class="alert alert-warning">Updating....</div>').show();
                                $.post('../ajax/product/update_tag?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>



                        <!-- Attribute Script -->
                        <script>

                            function setfiltersOnContainer(html, html1)
                            {
                                $('.filter_main_container').html(html);
                                $('#filter_select_container .row').html(html1);
                                getEachFilterAttributes();

                            }

                            function getAllVariableProductFilters()
                            {
                                var html = '';
                                var html1 = '';
                                $.get('../ajax/product/getAllVariableProductFIlters?id=<?php echo $id ?>', function(res){
                                    $(res).each(function(i, value){
                                        html+= '<div class="col-xs-12 block-container top-margin" cluster-id="' + value.cl_id + '">';
                                        html+= '<div class="col-xs-12 block-header">Filter : ' + value.cl_filter + '</div>';
                                        html+= '<div class="col-xs-12 block-body">'; 
                                        html+= '<div class="col-xs-12"><input type="text" class="form-control filter_name_each" value="' + value.cl_filter + '" /><div class="filter_name_error_input"></div></div>';
                                        html+= '<div class="col-xs-12"><label class="col-xs-12">Show?</label><select class="form-control filter_show"><option value="1"';

                                        if(value.cl_show == 1)
                                        {
                                            html+= ' selected="selected" ';
                                        }
                                        html+= ' >Yes</option><option value="0" ';
                                        if(value.cl_show == 0)
                                        {
                                            html+= ' selected="selected" ';
                                        }

                                        html+= '>No</option></select><div class="select_show_error_input"></div></div>';
                                        html+= '<div class="top-margin col-sm-12 existing_attribute"><table class="table table-bordered" style="width:100%">';
                                        html+= '<thead><tr><th colspan="3">Existing Attributes </th></tr><tr><th>Attribute</th><th>Show?</th><th></th></tr></thead>';
                                        html+= '<tbody></tbody>';
                                        html+= '</table></div>';
                                        html+= '<div class="top-margin col-sm-12 new_attribute">';
                                        html+= '<table class="table table-bordered" style="width:100%">';
                                        html+= '<thead><tr><th colspan="3">New Attributes <button type="button" class="btn btn-danger pull-right btn-attribute-add">Add +</button></th></tr><tr><th>Attribute</th><th>Show?</th><th></th></tr></thead>';
                                        html+= '<tbody><tr class="input-container"><td><input type="text" class="form-control name" name="name" /><div class="error-input"></div></td><td><select class="form-control show" name="show"><option value="1">Yes</option><option value="0">No</option></select><div class="error-input"></div></td><td><button type="button" class="btn btn-success btn-sm single_attribute_add"><i class="fa fa-plus"></i></button><div class="error-input"></div></td></tr></tbody>';
                                        html+= '</table>';
                                        html+= '<button type="button" class="btn btn-danger col-xs-12 col-sm-12 top-margin">Delete This Filter</button>';
                                        html+= '</div>';
                                        html+= '</div></div>'; 
                                        if(value.cl_show == 1)
                                        {
                                            html1+= '<div class="col-sm-6 col-md-3 col-xs-12"><select class="form-control col-sm-12 pull-left select_filter" cluster-id="' + value.cl_id + '"><option value="">---Select ' + value.cl_filter + ' ---</option></select></div>';
                                        }

                                    });
                                    setfiltersOnContainer(html, html1);
                                },'json');

                                
                            }

                            function getAllVAriableProductFilterAttributes(id)
                            {
                                html = '';
                                html1 = '';
                                $('.block-container[cluster-id="' + id + '"]').find('.existing_attribute table tbody').html('');
                                $.get('../ajax/product/getAllFilterAttributes?id=' + id, function(res){

                                    $(res).each(function(i, value){
                                        html = '<tr class="input-container"><td><input type="text" class="form-control name" name="name" value="' + value.cl_name + '" /><div class="error-input"></div></td><td><select class="form-control show" name="show"><option value="1"';

                                        if(value.cl_show == 1)
                                        {
                                            html+= ' selected="selected" ';
                                        }

                                        html+= '>Yes</option><option value="0"';
                                        if(value.cl_show == 0)
                                        {
                                            html+= ' selected="selected" ';
                                        }
                                        html+= '>No</option></select><div class="error-input"></div></td><td><button type="button" class="btn btn-primary btn-sm single_attribute_update" cluster-id="' + id + '"><i class="fa fa-edit"></i></button><div class="error-input"></div></td></tr>';
                                        
                                        $('.block-container[cluster-id="' + id + '"]').find('.existing_attribute table tbody').append(html);

                                        if(value.cl_show == 1)
                                        {
                                            html1 = '<option value="' + value.cl_id + '">' + value.cl_name + '</option>';
                                            $('.select_filter[cluster-id="' + id + '"]').append(html1);
                                        }
                                    })
                                    
                                },'json');
                            }

                            function getEachFilterAttributes()
                            {
                                $('.block-container').each(function(i, value){
                                    var filterID = $(this).attr('cluster-id');
                                    getAllVAriableProductFilterAttributes(filterID);
                                })
                            }


                            function getAllVariableProducts()
                            {
                                $('#attribute_table tbody').html('<tr><td colspan="4">Loading...</td></tr>');
                                var attributeTableHtml = '';
                                $.get('../ajax/product/getAllVariableProducts?id=<?php echo $id ?>', function(res){
                                    $('#attribute_table tbody').html('');
                                    $(res).each(function(i, value){
                                        var showCheckBox = '';
                                        if(value.cl_show == 1)
                                        {
                                            showCheckBox+= ' checked="checked" ';
                                        }

                                        var showDefault = '';
                                        if(value.cl_default == 1)
                                        {
                                            showDefault+= ' checked="checked" ';
                                        }

                                        var randomNumber = (Math.random() * 10000000000000000000);
                                        attributeTableHtml = '<tr style="border-top: 2px solid black"><td colspan="7"><button type="button" class="btn btn-danger">Delete This Variable Product</button></td></tr><tr><td class="filter_value_span" >' + value.filter_value + '</td><td><input type="text" class="form-control supplier_price" value="' + value.cl_supplier_price + '"" /></td><td><input type="text" class="form-control monthly_price" value="' + value.cl_monthly_price + '"" /></td><td><input type="text" class="form-control weight" value="' + value.cl_weight + '"" /></td><td><input type="checkbox" class="show" ' + showCheckBox + '/></td><td><input type="checkbox" class="default" ' + showDefault + ' /></td><td><button type="button" cluster-id="' + value.cl_id + '" class="btn btn-success btn-update-variable-product">Update</button><div class="error-input"></div></td></tr><tr><td colspan="7" > <form action="../ajax/product/variable_product_file_upload?id=' + value.cl_id + '" class="dropzone randomNumber_' + randomNumber + '"  id="flie_upload_dropzone"></form></td></tr><tr><td colspan="7"><table class="table table-bordered table-images" cluster-id="' + value.cl_id + '" style="width:100%"></table></td></tr>';
                                        $('#attribute_table tbody').append(attributeTableHtml);
                                        var myDropzone = new Dropzone(".randomNumber_" + randomNumber);

                                        var variableProductID = value.cl_id;
                                        $.get('../ajax/product/getAllVariableProductImages?id=' + variableProductID, function(res1){
                                            var html = '';
                                           
                                            $(res1).each(function(i2, value2){
                                              
                                                html = '<tr>';
                                                html+= '<td><img src="<?php echo MAIN_DOMAIN ?>/uploads/products/' + value2.cl_name + '" style="max-width:200px"/></td><td><button type="button" class="btn btn-danger"><i class="fa fa-trash"></i></button></td>';
                                                html+= '</tr>';
                                                console.log(variableProductID);
                                                $('.table-images[cluster-id="' + variableProductID + '"]').append(html);
                                            });
                                        },'json');
                                    });
                                }, 'json');
                               // getAllVariableProductImages();
                            }

                            $(document).ready(function(){
                                getAllVariableProductFilters();
                                getAllVariableProducts();
                            });

                            $(document).on('click', '#btn-add', function(){
                                $('#btn-add').attr('disabled', 'disabled');
                                var parent = $(this).parents('.card').first();
                                var data = new Object();
                                data['filter'] = $('.filter_name').val();
                                $(parent).find('.error-main').html('<div class="alert alert-warning">Adding Filter.....</div>');
                                
                                var current = $(this);
                                $('.error-input').html('').show();

                                $.post('../ajax/product/addVariableProductFilter?id=<?php echo $id ?>', {data:data}, function(res){
                                    $('#btn-add').removeAttr('disabled');
                                    $('.filter_name').val('');
                                    $(parent).find('.error-main').html('<div class="alert alert-success">' + res.message + '</div>');
                                    getAllVariableProductFilters();
                                },'json').fail(function(xhr) {

                                    $('#btn-add').removeAttr('disabled');
                                   $('.error-main').html('').show();
                                      var html = '';
                                      var res = xhr.responseJSON;
                                      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();
                                           
                                            return;
                                        }
                                        else
                                        {
                                            $(parent).find('.error-main').html('<div class="alert alert-danger">' + res.error_text + '</div>').show();
                                            return;
                                        }
                                      }

                                       $.each(res.error_description, function(i, value) {
                                            var parent = $(current).parents('.input-container').first();
                                            $(parent).find('.error-input').html('<div class="alert alert-danger alert-flat">' + value + '</div>');
                                          
                                      });
                                      
                                  
                                  },'json');;
                            });


                            $(document).on('change', '.filter_show', function(){
                                var current = $(this);
                                $(current).attr('disabled', 'disabled');
                                 var parentBlock = $(this).parents('.block-container').first();
                                 $(parentBlock).find('.select_show_error_input').html('<div class="alert alert-warning">Updating....</div>');
                                var id = $(parentBlock).attr('cluster-id');
                                var data = new Object();
                                 data['show'] = $(this).val();
                                $.post('../ajax/product/save_show_filter?id=' + id, {data:data}, function(res){
                                    $(current).removeAttr('disabled');
                                     $(parentBlock).find('.select_show_error_input').html('<div class="alert alert-success">' + res.message + '</div>');
                                     getAllVariableProductFilters();
                                },'json').fail(function(xhr) {

                                   $(current).removeAttr('disabled');
                                   $('.error-input').html('').show();
                                      var html = '';
                                      var res = xhr.responseJSON;
                                      if (typeof res === 'undefined') {
                                        $(parentBlock).find('.select_show_error_input').html('<div class="alert alert-danger">Something went wrong</div>').show();
                                        return;
                                      }
                                      if (typeof res.error_description === 'undefined') {
                                        if(typeof res.error_text === 'undefined')
                                        {
                                            $(parentBlock).find('.select_show_error_input').html('<div class="alert alert-danger">Something went wrong</div>').show();
                                            return;
                                        }
                                        else
                                        {
                                            $(parentBlock).find('.select_show_error_input').html('<div class="alert alert-danger">' + res.error_text + '</div>').show();
                                            return;
                                        }
                                      }

                                       $.each(res.error_description, function(i, value) {
                                            
                                            $(parentBlock).find('.select_show_error_input').html('<div class="alert alert-danger alert-flat">' + value + '</div>');
                                          
                                      });
                                      
                                  
                                },'json');;
                           });


                            $(document).on('change', '.filter_name_each', function(){
                                var current = $(this);
                                $(current).attr('disabled', 'disabled');
                                 var parentBlock = $(this).parents('.block-container').first();
                                 $(parentBlock).find('.filter_name_error_input').html('<div class="alert alert-warning">Updating....</div>');
                                var id = $(parentBlock).attr('cluster-id');
                                var data = new Object();
                                 data['filter'] = $(this).val();
                                $.post('../ajax/product/update_filter_name?id=' + id, {data:data}, function(res){
                                    $(current).removeAttr('disabled');
                                     $(parentBlock).find('.filter_name_error_input').html('<div class="alert alert-success">' + res.message + '</div>');
                                     getAllVariableProductFilters();
                                },'json').fail(function(xhr) {

                                   $(current).removeAttr('disabled');
                                   $('.error-input').html('').show();
                                      var html = '';
                                      var res = xhr.responseJSON;
                                      if (typeof res === 'undefined') {
                                        $(parentBlock).find('.filter_name_error_input').html('<div class="alert alert-danger">Something went wrong</div>').show();
                                        return;
                                      }
                                      if (typeof res.error_description === 'undefined') {
                                        if(typeof res.error_text === 'undefined')
                                        {
                                            $(parentBlock).find('.filter_name_error_input').html('<div class="alert alert-danger">Something went wrong</div>').show();
                                            return;
                                        }
                                        else
                                        {
                                            $(parentBlock).find('.filter_name_error_input').html('<div class="alert alert-danger">' + res.error_text + '</div>').show();
                                            return;
                                        }
                                      }

                                       $.each(res.error_description, function(i, value) {
                                            
                                            $(parentBlock).find('.filter_name_error_input').html('<div class="alert alert-danger alert-flat">' + value + '</div>');
                                          
                                      });
                                      
                                  
                                },'json');;
                           });


                            $(document).on('click', '.btn-attribute-add', function(){
                                var parent = $(this).parents('.new_attribute').first();
                                var html = '<tr class="input-container"><td><input type="text" class="form-control name" name="name"/></td><td><select class="form-control show" name="shows"><option value="1">Yes</option><option value="0">No</option></select></td><td><button type="button" class="btn btn-success btn-sm single_attribute_add"><i class="fa fa-plus"></i></button></td></tr>';
                                $(parent).find('tbody').append(html);
                            });

                            $(document).on('click', '.single_attribute_add', function(){
                            var current = $(this);
                                var data = new Object();
                                var parent = $(this).parents('tr').first();
                                var parentTD = $(this).parents('td').first();
                                var parentBlock = $(this).parents('.block-container').first();
                                var id = $(parentBlock).attr('cluster-id');
                                data['name'] = $(parent).find('.name').val();
                                data['show'] = $(parent).find('.show').val();
                                $(current).attr('disabled', 'disabled');
                                $.post('../ajax/product/add_single_attribute?id=' + id, {data:data}, function(){
                                    $(current).removeAttr('disabled');
                                    $(parent).remove();
                                    getEachFilterAttributes();
                                },'json').fail(function(xhr) {

                                   $(current).removeAttr('disabled');
                                   $('.error-input').html('').show();
                                      var html = '';
                                      var res = xhr.responseJSON;
                                      if (typeof res === 'undefined') {
                                        $(parentTD).find('.error-input').html('<div class="alert alert-danger">Something went wrong</div>').show();
                                        return;
                                      }
                                      if (typeof res.error_description === 'undefined') {
                                        if(typeof res.error_text === 'undefined')
                                        {
                                            $(parentTD).find('.error-input').html('<div class="alert alert-danger">Something went wrong</div>').show();
                                            return;
                                        }
                                        else
                                        {
                                            $(parentTD).find('.error-input').html('<div class="alert alert-danger">' + res.error_text + '</div>').show();
                                            return;
                                        }
                                      }

                                       $.each(res.error_description, function(i, value) {
                                            var parent = $(current).find('[name="' + i + '"] .error-input').parents('.input-container').first();
                                            $(parent).find('.error-input').html('<div class="alert alert-danger alert-flat">' + value + '</div>');
                                          
                                      });
                                      
                                  
                                  },'json');;
                           });


                            $(document).on('click', '.btn-update-variable-product', function(){
                                var current = $(this);
                                var id = $(this).attr('cluster-id');
                                var data = new Object();
                                var parent = $(this).parents('tr').first();
                                data['supplier_price'] = $(parent).find('.supplier_price').val();
                                data['monthly_price'] = $(parent).find('.monthly_price').val();
                                data['weight'] = $(parent).find('.weight').val();
                                if($(parent).find('.show').is(':checked'))
                                {
                                    data['show'] = 1;
                                }
                                else
                                {
                                    data['show'] = 0;
                                }

                                if($(parent).find('.default').is(':checked'))
                                {
                                    data['default'] = 1;
                                }
                                else
                                {
                                    data['default'] = 0;
                                }

                                $(parent).find('.error-input').html('<div class="alert alert-warning">Updating.....</div>');
                                $(current).attr('disabled', 'disabled');
                                $.post('../ajax/product/update_single_attribute?id=' + id, {data:data, product_id:<?php echo $_GET['id'] ?>}, function(res){
                                     $(current).removeAttr('disabled');
                                     $(parent).find('.error-input').html('<div class="alert alert-success">' + res.message + '</div>');
                                },'json').fail(function(xhr) {

                                    $(current).removeAttr('disabled');
                                   
                                      var html = '';
                                      var res = xhr.responseJSON;
                                      if (typeof res === 'undefined') {
                                        $(parent).find('.error-input').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-input').html('<div class="alert alert-danger">Something went wrong</div>').show();
                                            
                                            return;
                                        }
                                        else
                                        {
                                            $(parent).find('.error-input').html('<div class="alert alert-danger">' + res.error_text + '</div>').show();
                                            
                                            return;
                                        }
                                      }
                                  
                                },'json');
                            });


                             function addVariableProduct(data)
                            {
                                $('#btn-add').attr('disabled', 'disabled');
                                $('.error-main').html('<div class="alert alert-warning">Adding...</div>').show();
                                $.post('../ajax/product/addVariableProduct?id=<?php echo $id ?>', {data:data}, function(res){
                                    $('#btn-add').removeAttr('disabled');
                                    $('.error-main').html('<div class="alert alert-success">' + res.message + '</div>').show();
                                    getAllVariableProducts();

                                },'json').fail(function(xhr) {

                                    $('#btn-add').removeAttr('disabled');
                                   
                                      var html = '';
                                      var res = xhr.responseJSON;
                                      if (typeof res === 'undefined') {
                                        $('.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')
                                        {
                                            $('.error-main').html('<div class="alert alert-danger">Something went wrong</div>').show();
                                          
                                            return;
                                        }
                                        else
                                        {
                                            $('.error-main').html('<div class="alert alert-danger">' + res.error_text + '</div>').show();
                                          
                                            return;
                                        }
                                      }
                                  
                                  },'json');;
                            }

                            $(document).on('click', '#btn-add-variable-product', function(){
                                var data = new Object();
                                var count = 0;
                                var error = 1;
                                data['filters'] = new Object();
                                $('.select_filter').each(function(i, value){
                                    if($(value).val() != '')
                                    {
                                        error = 0;
                                    }

                                    console.log(value);
                                    data['filters'][count] = new Object();
                                    data['filters'][count]['filter_id'] = $(value).attr('cluster-id');
                                    data['filters'][count]['value_id'] = $(value).val();
                                    count++;
                                });

                                if($('#input-checkbox').is(':checked'))
                                {
                                    data['default'] = 1;
                                }

                                if(error == 0)
                                {
                                    addVariableProduct(data);
                                }
                                else
                                {
                                    alert('Please select atleast one filter');
                                }
                            });





$(document).on('change', '.valueSelect', function(){
   var id = $(this).attr('cluster-id');
   var productID = '<?php echo $id ?>';
   var valueID = $(this).val();
   var parent = $(this).parents('tr').first();
   $(parent).find('.error-input').html('<div class="alert alert-warning">Please wait while we save</div>');
   $.post('../ajax/products/productFilterValueSave', {id:id, productID:productID, valueID:valueID}, function(res){
       $(parent).find('.error-input').html('<div class="alert alert-success">Saved Successfully</div>');
   }, 'json')
    .fail(function(xhr){
        var res = xhr.responseJSON;
        $(parent).find('.error-input').html('<div class="alert alert-warning">' + res.error_text + '</div>');
    }, 'json');
});

                        </script>

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