<?php 
unset($wo['products_categories'][1]);
$sub_key = array_keys($wo['products_categories'])[0];
if (!empty($_GET['key']) && in_array($_GET['key'], array_keys($wo['products_categories']))) {
  $sub_key = $_GET['key'];
}
?>
<div class="container-fluid">
    <div class="block-header">
        <h2>Category > Manage Products Sub Categories</h2>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="header">
                    <h2>Add Sub Category</h2>
                </div>
                <div class="body">

                  <div class="row">
                       <div class="col-md-12" style="margin-bottom:0;">
                        <div class=" add_category_form_alert"></div>
                            <form method="POST" id="add_category_form">
                              <div class="row">
                                <div class="col-md-2" id="normal-query-form">
                                  <div class="form-group form-float">
                                    <div class="form-line">
                                      <select class="form-control show-tick" id="category_id" name="category_id">
                                        <?php foreach ($wo['products_categories'] as $category_id => $category_name) { ?>
                                          <option value="<?php echo($category_id) ?>"><?php echo $category_name; ?></option>
                                        <?php  } ?>
                                      </select>
                                    </div>
                                  </div>
                                </div>
                                <?php foreach (Wo_LangsNamesFromDB() as $wo['key_']) { ?>
                                    <div class="col-md-2" id="normal-query-form">
                                      <div class="form-group form-float">
                                          <div class="form-line">
                                              <input type="text" class="form-control" name="<?php echo($wo['key_']) ?>">
                                              <label class="form-label"><?php echo ucfirst($wo['key_']); ?></label>
                                          </div>
                                      </div>
                                    </div>
                                <?php } ?>
                                <div class="clearfix"></div>
                              <div class="col-md-2">
                                  <button class="btn btn-info">Add</button>
                              </div>
                              </div>
                              <div class="clearfix"></div>
                           </form>
                       </div>
                   </div>
                </div>
            </div>
        </div>
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="header">
                    <h2>Manage Sub Categories</h2>
                </div>
                <div class="body">
                  <div class="row">
                       <div class="col-md-6" style="margin-bottom:0;">
                            <form method="get" action="<?php echo Wo_LoadAdminLinkSettings('products-sub-categories'); ?>">
                              <div class="row">
                                  <div class="col-lg-6 col-md-6">
                                        <select class="form-control show-tick" name="key">
                                            <?php foreach($wo['products_categories'] as $key => $category) { ?>
                                                <option value="<?php echo $key?>" <?php echo (!empty($_GET['key']) && $_GET['key'] == $key) ? 'selected' : '' ?>><?php echo $category ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                <div class="col-md-1">
                                   <button class="btn btn-info">Show</button>
                                </div>
                              </div>

                              <div class="clearfix"></div>
                           </form>
                       </div>
                   </div>
                   <div class="clearfix"></div>
                   <div class="clearfix"></div>
                   <div class="table-responsive1">
                        <table class="table table-bordered table-striped table-hover">
                            <thead>
                                <tr>
                                      <th>ID</th>
					                  <th>Category Name</th>
					                  <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php 
                                if (!empty($wo['products_sub_categories'][$sub_key])) {
                                  foreach ($wo['products_sub_categories'][$sub_key] as $id => $sub_category) {
                                    $wo['category_key'] = $sub_category['id'];
                                    $wo['category_name'] = $sub_category['lang'];
                                    $wo['category_lang_key'] = $sub_category['lang_key'];
                                    echo Wo_LoadAdminPage('products-sub-categories/list');
                                  }
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->
<div id="delete-modal" class="modal fade" role="dialog" data-id="">
  <div class="modal-dialog">
    <div class="modal-content modal-col-red">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">DELETE Category</h4>
      </div>
      <div class="modal-body">
        <p>Are you sure you want to continue? this action can't be undo</p>
      </div>
      <div class="modal-footer">
      	<button type="button" class="btn btn-link waves-effect" data-dismiss="modal" data-type="user" id="delete_category">DELETE</button>
        <button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
      </div>
    </div>
  </div>
</div>

<div class="modal fade" id="editcategoryModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content modal_content_back">
      <div class="modal-header">
        <h5 class="modal-title" id="editcategoryModalLabel">Edit Category</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="edit_category_form_alert"></div>
        <form class="edit_category_lang" method="POST" id="modal-body-langs">
          <div class="data_lang"></div>
          <input type="hidden" name="hash_id" value="<?php echo Wo_CreateSession();?>">
          <input type="hidden" name="id_of_key" id="id_of_key" value="">
        </form>
        
      </div>
      <div class="modal-footer">
        
        <button type="button" class="btn btn-secondary modal_close_btn" data-dismiss="modal">CLOSE</button>
        <button type="button" class="btn btn-primary" id="save_edited_category">SAVE CHANGES</button>
      </div>
    </div>
  </div>
</div>


<script>

var add_category_form = $('form#add_category_form');
var edit_category_form = $('form.edit_category_lang');

add_category_form.ajaxForm({
    url: Wo_Ajax_Requests_File() + '?f=admin_setting&s=add_new_sub_category&type=product',
    beforeSend: function() {
        add_category_form.find('.waves-effect').text("Please wait..");
    },
    success: function(data) {
        if (data.status == 200) {
            add_category_form.find('.waves-effect').text('Save');
            $('.add_category_form_alert').html('<div class="alert alert-success"><i class="fa fa-check"></i> Sub Category successfully added</div>');
            setTimeout(function () {
                $('.add_category_form_alert').empty();
            }, 2000);
            window.location.reload();
        }
        else{
          $('.add_category_form_alert').html('<div class="alert alert-danger"><i class="fa fa-check"></i> '+data.message+'</div>');
            setTimeout(function () {
                $('.add_category_form_alert').empty();
            }, 2000);
        }
    }
});

edit_category_form.ajaxForm({
    url: Wo_Ajax_Requests_File() + '?f=admin_setting&s=update_lang_key&hash=' + $('.main_session').val(),
    beforeSend: function() {
        edit_category_form.find('.waves-effect').text("Please wait..");
    },
    success: function(data) {
        if (data.status == 200) {
            edit_category_form.find('.waves-effect').text('Save');
            $('.edit_category_form_alert').html('<div class="alert alert-success"><i class="fa fa-check"></i> Category successfully edited</div>');
            setTimeout(function () {
                $('.edit_category_form_alert').empty();
            }, 3000);
            window.location.reload();
        }
        else{
          $('.edit_category_form_alert').html('<div class="alert alert-danger"><i class="fa fa-check"></i> '+data.message+'</div>');
            setTimeout(function () {
                $('.edit_category_form_alert').empty();
            }, 2000);
        }
    }
});

$(document).on('click','#save_edited_category', function(event) {
  event.preventDefault();
  $('.edit_category_lang').submit();
});

function edit_category(id) {
  $('#id_of_key').val(id);
  $.post(Wo_Ajax_Requests_File() + '?f=admin_setting&s=get_category_langs', {lang_key: id}, function(data, textStatus, xhr) {
      if (data.status == 200) {
        $('.data_lang').html(data.html);
        $('#editcategoryModal').modal();
      }
  });
}

$('.delete-content').on('click', function(event) {
    event.preventDefault();
    $('#delete-modal').attr('data-id', $(this).attr('data-id')).modal('show');
  });

$(document).on('click', '#delete_category', function(event) {
    event.preventDefault();
    lang_key = $('#delete-modal').attr('data-id');
    $.post(Wo_Ajax_Requests_File() + '?f=admin_setting&s=delete_sub_category&type=product', {lang_key: lang_key}, function(data, textStatus, xhr) {
        if (data.status == 200) {
            $('#list-'+lang_key).slideUp('slow');
            setTimeout(function () {
                $('#list-'+lang_key).remove();
            },2000);
        }
    });
});



</script>