<?php
session_start();
require_once '../includes/config.php';
require_once '../includes/database.php';
require_once '../includes/check_session.php';
require_once '../includes/auth.php';

$auth = array('customer_admin');
$authRes = checkAuth($auth, $user);
if(!$authRes)
{
    header('Location: ../index.php');
    exit;
}

class TableList extends database{
	
  public function all($page,$id)
  {
      $where['cl_user_id'] = $id;
        return $this->fetchAll("tb_tables", $where, 'created_at', 'DESC', ($page - 1) * NO_OF_RECORDS, NO_OF_RECORDS);
  }
}

$page = 1;
if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 1)
{
  $page = $_GET['page'];
}

$obj = new TableList();
$list = $obj->all($page,$user['cl_id']);


include '../header.php';
?>

<div class="content-wrapper">
    <div class="container-xxl flex-grow-1 container-p-y">
          
        <!-- /.row-->
        <div class="row">
            <div class="col-md-12">
                <div class="card ">
                    <div class="card-header">
                        <h4 class="fw-bold py-3 mb-4">Add Table</h4>
                    </div>
                    <div class="col-sm-12 card-body">
                        <div class="row">

                            <form method="post" class="form" cluster-url="ajax/add_ajax.php" cluster-redirect="list.php">
                                <div class="error-main"></div>
                                <?php include_once 'template/form.php' ?>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-12 mt-2">
                <div class="card mb-4">
                    <div class="card-header">Table List </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-sm-12">
                                   <?php include_once "../check_errors.php";?>

                                    <table class="table table-responsive-sm table-bordered datatable" id="abradant-table">
                                        <thead>
                                          <tr>
                                            <th data-priority="2">Table Name</th>

                                            <th data-priority="1">

                                            </th>
                                          </tr>
                                        </thead>
                                        <tbody>
                                          <?php
                                          if(!empty($list))
                                          {
                                            foreach($list AS $each)
                                            {

                                              ?>
                                              <tr>
                                                <td>
                                                  <input type="text" value="<?php echo htmlspecialchars($each['cl_table_name']); ?>" class="form-control table_name" />
                                                  <div class="error-input"></div>
                                                </td>

                                                <td class="action_container">
                                                    <button type="button" class="btn btn-primary update-table" data-id="<?php echo htmlspecialchars($each['cl_id']) ?>">Update</button>
                                                    <button type="button" class="btn btn-delete btn-danger" cluster-confirm="Are you sure you want to delete this table?" cluster-href="ajax/delete_ajax.php?id=<?php echo htmlspecialchars($each['cl_id']) ?>">Remove</button>
                                                    <div class="error-main"></div>
                                                </td>
                                              </tr>
                                              <?php

                                            }
                                          }
                                          ?>
                                        </tbody>

                                    </table>
                                    <nav aria-label="Page navigation example">
                                         <ul class="pagination justify-content-center">
                                           <?php
                                           if($page > 1)
                                             { ?>
                                           <li class="page-item"><a class="page-link" href="?page=<?php echo ($page - 1) ?>">Previous</a></li>
                                           <?php
                                         }
                                           if(count($list) >= NO_OF_RECORDS)
                                             { ?>
                                           <li class="page-item"><a class="page-link" href="?page=<?php echo ($page + 1) ?>">Next</a></li>
                                         <?php } ?>
                                         </ul>
                                     </nav>
                                </div>

                            </div>
                      <!-- /.row-->
                        </div>
                    </div>
            </div>
        </div>
    </div>
</div>


<script>
    $(document).on('click', '.update-table', function(){
        var id = $(this).attr('data-id');
        var parentMain = $(this).parents('tr').first();
        var data = new Object();
        data['table_name'] = $(parentMain).find('.table_name').val();
        
        var current = $(this);
        $(current).attr('disabled', 'disabled');
        $(parentMain).find('.error-main').html('<div class="alert alert-warning">Updating...</div>');
        $.post('ajax/edit_ajax.php?id=' + id, {data:data}, function(){
            $(parentMain).find('.error-main').html('<div class="alert alert-success">Updated Successfully</div>');
        }, 'json')
        .fail(function(xhr){
            
            $(current).removeAttr('disabled');
            $(parentMain).find('.error-main').html('<div class="alert alert-danger">Following errors are listed. Please check errors and resubmit the form.</div>').show();
            $('.error-input').html('');
            var html = '';
            var res = xhr.responseJSON;
            if (typeof res === 'undefined') {
                $(parentMain).find('.error-main').html('<div class="alert alert-danger">Something went wrong</div>').show();
                return;
            }

            if(typeof res.error_text === 'undefined')
            {
                $(parentMain).find('.error-main').html('<div class="alert alert-danger">Something went wrong</div>').show();
                window.location.hash = '#' + id;
                return;
            }
            else
            {
                $(current).find('.error-main').html('<div class="alert alert-danger">' + res.error_text + '</div>').show();

            }


            // var res1 = $.parseJSON('"' + res + '"');
             $.each(res.error_description, function(i, value) {
                            var parent = $(parentMain).find("." + i).parents('td').first();
                            $(parent).find('.error-input').append('<div class="alert alert-danger alert-flat">' + value + '</div>');

              });
        }, 'json');
    })
    </script>

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