<?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_subcategory', $where);
    }

    public function getFilters()
    {
        return $this->fetchAll('tb_filter');
    }
}

$obj = new edit();

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

$filters = $obj->getFilters();

$htmlFilters = '<option value="">---Select----</option>';
foreach($filters AS $filter)
{
    $htmlFilters.= '<option value="' . $filter["cl_id"] . '">' . $filter["cl_name"] . '</option>';
}

$templateType = 'edit';
include '../header.php';
?>
               <!-- 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">Product Subcategory (<?php echo $data['cl_name'] ?>) Filters</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">
                                       <table class="table table-bordered table-striped" id="existing_table_filter" style="width:100%">
                                            <thead>
                                                <tr>
                                                    <th colspan="3">
                                                        Existing Filters
                                                    </th>
                                                </tr>
                                                <tr>
                                                    <th>
                                                        Filter
                                                    </th>
                                                    <th>
                                                        Show this filter?
                                                    </th>
                                                    <th>
                                                        Action
                                                    </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="3">
                                                        New Filter
                                                        <button type="button" class="btn btn-danger" id="btn-add">Add</button>
                                                    </th>
                                                </tr>
                                                <tr>
                                                    <th>
                                                        Filter
                                                    </th>
                                                    <th>
                                                        Show this filter?
                                                    </th>
                                                    <th>
                                                        Action
                                                    </th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                
                                            </tbody>
                                       </table>
                                   </div>
                                </div>
                            </div>
                          
                        </div>
                    </div>
                </div>
            </div>
                        
                        
                        <div class="modal fade" id="valueModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="valueName"></h1>
        <button type="button" class="btn-close" data-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
          <table class="table table-bordered table-values-existing">
              <thead>
                  <tr>
                      <th colspan="2">
                          Existing Values
                      </th>
                  </tr>
              
                  <tr>
                      <td>
                          Name
                      </td>
                      
                  </tr>
              </thead>
              <tbody>
                  
              </tbody>
          </table>
          
          <table class="table table-bordered table-values-new mt-1">
              <thead>
                  <tr>
                      <th colspan="2">
                          New Values
                      </th>
                  </tr>
              
                  <tr>
                      <td>
                          Name
                      </td>
                      
                  </tr>
              </thead>
              <tbody>
                  <tr>
                      <td>
                          <input type="text" class="form-control" id="value_name" />
                      </td>
                      <td>
                          <button type="button" class="btn btn-danger" id="btn-add-values">Add</button>
                          <div class="error-input"></div>
                      </td>
                  </tr>
              </tbody>
          </table>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
       
      </div>
    </div>
  </div>
</div>

                        <script>
                            var valueID = 0;
                            $(document).ready(function(){
                                getAllFiltersFromSubCategories(<?php echo $id ?>);
                                $('#btn-add').click(function(){
                                    var html = '<tr><td class="input-container"><input type="text" name="name" class="form-control name" /><div class="error-input"></div></td><td class="input-container"><select class="form-control show_in_filter" name="show_in_filter"><option value="1">Yes</option><option value="0">No</option></select><div class="error-input"></div></td><td><button type="button" class="btn btn-danger btn-add"><i class="fa fa-plus"></i></button><div class="error-main"></div></td></tr>';
                                    $('#new_filter_table tbody').html(html);
                                });
                            });


                            function setAllFilters()
                            {
                                $('.existing_tr').each(function(i, value){
                                    var filterID = $(value).find('.filter_id').attr('cluster-id');
                                    $(value).find('.filter_id').val(filterID);
                                });
                            }

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


                                       var show = value.cl_show_in_filter;
                                       var showChecked = '';
                                       

                                        html = '<tr class="existing_tr"><td><input type="text" class="form-control filter_id" cluster-id="' + value.cl_id + '" value="' + value.cl_name + '"/></td><td><select class="form-control cluster_show"><option value="1" ';

                                        if(show == 1)
                                       {    
                                            html+= ' selected="selected" ';
                                       }


                                        html+= ' >Yes</option><option value="0" ';

                                         if(show == 0)
                                       {    
                                            html+= ' selected="selected" ';
                                       }


                                        html+= '>No</option></select></td><td><button type="button" class="btn btn-success btn-update" cluster-id="' + value.cl_id + '"><i class="fa fa-edit"></i></button><button type="button" class="btn btn-primary btn-values" cluster-id="' + value.cl_id + '" style="margin-left:5px">Values</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();
                                 data['show_in_filter'] = $(parent).find('.show_in_filter').val();
                                 $(parent).find('.error-main').html('<div class="alert alert-warning">Adding....</div>').show();
                                $.post('../ajax/product_subcategory/add_filter?id=<?php echo $id ?>', {data:data}, function(){
                                    $(parent).remove();
                                    getAllFiltersFromSubCategories(<?php echo $id ?>);
                                }, 'json') .fail(function(xhr) {
                                     $(parent).find('.error-main').html('').show();
                                    $(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;
                                        }
                                      }
                                    
                                    // var res1 = $.parseJSON('"' + res + '"');
                                     $.each(res.error_description, function(i, value) {
                                            var insideParent = $(parent).find("[name='" + i + "']").parents('.input-container').first();
                                            $(insideParent).find('.error-input').append('<div class="alert alert-danger alert-flat">' + value + '</div>');
                                          
                                      });
                                      
                                      
                                      window.location.href = '#body_part';
                                  },'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('.filter_id').val();
                                data['show_in_filter'] = $(parent).find('.cluster_show').val();

                                   $(parent).find('.error-main').html('<div class="alert alert-warning">Updating....</div>').show();
                                $.post('../ajax/product_subcategory/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');;
                            });
                            
                            
                            $(document).on('click', '.btn-values', function(){
                                $('#valueModal').modal('show');
                                valueID = $(this).attr('cluster-id');
                                getValues();
                                
                            });
                            
                            
                            $(document).on('click', '#btn-add-values', function(){
                                var name = $('#value_name').val();
                                $('#btn-add-values').attr('disabled', 'disabled');
                                var parent = $(this).parents('tr').first();
                                $(parent).find('.error-input').html('<div class="alert alert-warning">Adding.....</div>');
                                $.post('../ajax/product_subcategory/valueAdd', {name:name, id:valueID}, function(){
                                    $('#value_name').val('');
                                    $('#btn-add-values').removeAttr('disabled');
                                    $(parent).find('.error-input').html('');
                                    getValues();
                                    
                                }, 'json').
                                fail(function(xhr){
                                    var res = xhr.responseJSON;
                                    $('#btn-add-values').removeAttr('disabled');
                                    $(parent).find('.error-input').html('<div class="alert alert-danger">' + res.error_text + '</div>');
                                   // getValues();
                                }, 'json');
                            });
                            
                            function getValues()
                            {
                                $('#valueModal').find('.modal-body .table-values-existing tbody').html('<tr><td colspan="2"><div class="alert alert-warning">Getting Values....</div></td></tr>');
                                
                                
                                $.get('../ajax/product_subcategory/getValues', {id:valueID}, function(res){
                                    var html = '';
                                    $(res.data).each(function(i, value){
                                        html+= '<tr cluster-id="' + value.cl_id + '">';
                                            html+= '<td><input type="text" class="form-control name" value="' + value.cl_name + '" /></td><td><button type="button" class="btn btn-success btn-edit-values">Edit</button><button type="button" class="btn btn-danger btn-delete-values" style="margin-left:5px">Delete</button><div class="error-input"></div></td>';
                                        html+= '</tr>';
                                    });
                                    
                                    $('#valueName').text(res.name);
                                    
                                    $('#valueModal').find('.modal-body .table-values-existing tbody').html(html);
                                }, 'json')
                            }
                            
                            
                            
                            
                            
                            
                            
                            $(document).on('click', '.btn-edit-values', function(){
                                var parent = $(this).parents('tr').first();
                                var valueID = $(parent).attr('cluster-id');
                                
                                var name = $(parent).find('.name').val();
                                
                                var parent = $(this).parents('tr').first();
                                $(parent).find('.error-input').html('<div class="alert alert-warning">Updating.....</div>');
                                
                                $.post('../ajax/product_subcategory/updateValues', {name:name, id:valueID}, function(res){
                                    //getValues();
                                    $(parent).find('.error-input').html('<div class="alert alert-success">' + res.message + '</div>');
                                }, 'json').
                                fail(function(xhr){
                                    var res = xhr.responseJSON;
                                    $('#btn-add-values').removeAttr('disabled');
                                    $(parent).find('.error-input').html('<div class="alert alert-danger">' + res.error_text + '</div>');
                                  //  getValues();
                                }, 'json');;
                            });
                            
                            $(document).on('click', '.btn-delete-values', function(){
                                var parent = $(this).parents('tr').first();
                                var valueID = $(parent).attr('cluster-id');
                                
                                $(parent).find('.error-input').html('<div class="alert alert-warning">Deleting.....</div>');
                                
                                $.post('../ajax/product_subcategory/deleteValues', {name:name, id:valueID}, function(res){
                                    $(parent).remove();
                                    //getValues();
                                    //$(parent).find('.error-input').html('<div class="alert alert-success">' + res.message + '</div>');
                                }, 'json').
                                fail(function(xhr){
                                    var res = xhr.responseJSON;
                                    $('#btn-add-values').removeAttr('disabled');
                                    $(parent).find('.error-input').html('<div class="alert alert-danger">' + res.error_text + '</div>');
                                  //  getValues();
                                }, 'json');;
                            });
                            
                        </script>
                        
<?php
include '../footer.php';