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

   
}

$obj = new edit();

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

$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">Edit Filter</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">
                                        <form method="post" class="form" cluster-url="../ajax/filter/edit_ajax?id=<?php echo $id;?>" cluster-redirect="list">
									       <?php include_once "template/filter_form.php" ?>
                                       </form>
                                   </div>


                                </div>

                                <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 Values
                                                    </th>
                                                </tr>
                                                <tr>
                                                    <th>
                                                        Value
                                                    </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 Value
                                                        <button type="button" class="btn btn-danger" id="btn-add">Add</button>
                                                    </th>
                                                </tr>
                                                <tr>
                                                    <th>
                                                        Value
                                                    </th>
                                                   
                                                    <th>
                                                        Action
                                                    </th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                
                                            </tbody>
                                       </table>
                                    </div>
                                </div>
                            </div>
                          
                        </div>

                         <script>
                            $(document).ready(function(){
                                getAllValues(<?php echo $id ?>);
                                $('#btn-add').click(function(){
                                    var html = '<tr><td><input type="text" class="form-control value" name="data[value]" /></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 getAllValues(id)
                            {
                                $('#existing_table_filter tbody').html('');
                                $.get('../ajax/filter/getAllValues?id=' + id, function(res){
                                    var html = '';
                                    $(res).each(function(i, value){


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

                                        html = '<tr class="existing_tr"><td><input type="text" class="form-control value" value="' + value.cl_value + '"/></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['value'] = $(parent).find('.value').val();
                                   $(parent).find('.error-main').html('<div class="alert alert-warning">Adding....</div>').show();
                                $.post('../ajax/filter/add_value?id=<?php echo $id ?>', {data:data}, function(){
                                    $(parent).remove();
                                    getAllValues(<?php echo $id ?>);
                                }) .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;
                                        }
                                      }
                                    
                                  },'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['value'] = $(parent).find('.value').val();

                                   $(parent).find('.error-main').html('<div class="alert alert-warning">Updating....</div>').show();
                                $.post('../ajax/filter/update_value?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;
                                        }
                                      }
                                  
                                      
                                  },'json');;
                            });
                        </script>
                        
                   <?php
				   include '../footer.php';
				   ?>