<div class="row">
  <div class="iblogs-post <?php echo !empty($info_list_post) ? 'col-md-9' : 'col-xs-12' ?>">

    <div class="row" style="margin-bottom: 20px;">
      <div class="col-sm-6">
        <input type="text" class="form-control input-sm list-search isl-inline-block" placeholder="To search, type and hit enter" style="padding:4px 6px; width:50%">
        <a onclick="$('.iblogs-post .list-search').val('');fetchListPost(urlPostList);" class="pointer text-muted isl-ml-5" data-toggle="tooltip" title="Clear search"><i class="fa fa-close"></i></a>
      </div>
      <div class="col-sm-6" style="text-align: right;">
        <a href="<?php echo $url_post_form; ?>" class="btn btn-success btn-sm isl-mr-10"><i class="fa fa-plus isl-mr-5"></i> <?php echo $text_add_post; ?></a>
        <a class="btn btn-primary btn-sm btn-refresh-post-list" data-toggle="tooltip" title="Reload list"><i class="fa fa-refresh"></i></a>
      </div>
    </div>

    <table class="table table-striped table-hover">
      <thead>
        <tr>
          <th class="text-center" style="width:40px"><?php echo $text_id; ?></th>
          <th class="text-center" style="width:300px"><?php echo $text_title; ?></th>
          <th class="text-center" style="min-width:200px"><?php echo $text_excerpt; ?></th>
          <th class="text-center" style="width:200px"><?php echo $text_publish_unpublish; ?></th>
          <th class="text-center" style="width:70px"><?php echo $text_status; ?></th>
          <th class="text-center" style="width:100px"><?php echo $text_action; ?></th>
        </tr>
      </thead>
      <tbody id="post-list">

      </tbody>
    </table>

    <hr>
    <div class="row pagination-wrapper">
      <div class="col-sm-7 pagination-number"></div>
      <div class="col-sm-5 text-right pagination-info"></div>
    </div>
  </div>

  <?php if (!empty($info_list_post)) { ?>
  <div class="col-md-3">
    <div class="panel panel-default panel-info">
      <div class="panel-heading"><?php echo $text_information; ?></div>
      <div class="panel-body">
        <ul class="isl-list">
          <?php foreach ($info_list_post as $info) { ?>
            <li><?php echo $info ?></li>
          <?php } ?>
        </ul>
      </div>
    </div>
  </div>
  <?php } ?>
</div>

<script>
var urlPostList   = 'index.php?route=<?php echo $islConfig["path"]; ?>/listTable&channel=post&store_id=<?php echo $store_id; ?>&<?php echo $url_token; ?>',
    urlPostUpdate = 'index.php?route=<?php echo $islConfig["path"]; ?>/listUpdate&channel=post&store_id=<?php echo $store_id; ?>&<?php echo $url_token; ?>';

$(document).ready(function()
{
  fetchListPost(urlPostList);
  $('.btn-refresh-post-list').on('click', function() {
    fetchListPost(urlPostList);
  });
  $('.iblogs-post .pagination-number').on('click', 'a', function(e) {
    e.preventDefault();
    fetchListPost($(this).attr('href'));
  });

  // Search list
  $('.iblogs-post').on('keydown', '.list-search', function(e) {
    if (e.which == 13) {
      fetchListPost(urlPostList);
    }
  });

  // Update sort order
  $('#post-list').on('change', '.item-sort-order', function(e) {
    $.ajax({
      url: urlPostUpdate + '&_='+ new Date().getTime(),
      type: 'POST',
      dataType: 'json',
      data: $(this).serializeArray(),
      cache: false,
      beforeSend: function() {
        $('#post-list .item-sort-order').attr('disabled', true).after('<i class="fa fa-spinner fa-spin"></i>');
      },
      success: function(data) {
        if (!data.error) {
          fetchListPost(urlPostList);
        }
      }
    });
  });

  // Update item
  $('#post-list').on('click', '[data-isl-update]', function(e) {
    e.preventDefault();

    var el = $(this),
        elData = el.data('isl-update');

    $.ajax({
      url: urlPostUpdate + '&_='+ new Date().getTime(),
      type: 'POST',
      dataType: 'json',
      data: elData,
      cache: false,
      beforeSend: function() {
        if (elData.action == 'delete') {
          if (!confirm("Are you sure you want to delete post?")) {
            return false;
          }
          el.after('<i class="fa fa-spinner fa-spin spinner-id-' + elData.id + '"></i>');
        }
      },
      success: function(data) {
        if (!data.error) {
          fetchListPost(urlPostList);
        }
      }
    });
  });

});

function fetchListPost(url) {
  $.ajax({
    url: url + '&search=' + $('.iblogs-post .list-search').val() + '&_='+ new Date().getTime(),
    type: 'POST',
    dataType: 'json',
    cache: false,
    beforeSend: function() {
      $('#post-list').html('<tr><td class="text-center" colspan="7" style="padding:20px 10px;"><i class="fa fa-spinner fa-spin"></i> Processing..</td></tr>');
    },
    success: function(data) {
      $('#post-list').html(data.output);

      if (!data.output) {
        $('#post-list').html('<tr><td class="text-center" colspan="7" style="padding:20px 10px;"><?php echo $text_no_data; ?></td></tr>');
      }

      if (data.pagination || data.pagination_info) {
        $('.iblogs-post .pagination-wrapper').show();
        $('.iblogs-post .pagination-number').html(data.pagination);
        $('.iblogs-post .pagination-info').html(data.pagination_info);
      } else {
        $('.iblogs-post .pagination-wrapper').hide();
      }
    }
  });
}
</script>
