
<script src="<?= BASE_ASSET; ?>/js/jquery.hotkeys.js"></script>

<script type="text/javascript">
//This page is a result of an autogenerated content made by running test.html with firefox.
function domo(){
 
   // Binding keys
   $('*').bind('keydown', 'Ctrl+a', function assets() {
       window.location.href = BASE_URL + '/administrator/Database/add';
       return false;
   });

   $('*').bind('keydown', 'Ctrl+f', function assets() {
       $('#sbtn').trigger('click');
       return false;
   });

   $('*').bind('keydown', 'Ctrl+x', function assets() {
       $('#reset').trigger('click');
       return false;
   });

   $('*').bind('keydown', 'Ctrl+b', function assets() {

       $('#reset').trigger('click');
       return false;
   });
}

jQuery(document).ready(domo);
</script>
<!-- Content Header (Page header) -->
<section class="content-header">
   <h1>
      <?= cclang('database') ?><small><?= cclang('list_all'); ?></small>
   </h1>
   <ol class="breadcrumb">
      <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
      <li class="active"><?= cclang('database') ?></li>
   </ol>
</section>
<!-- Main content -->
<section class="content">
   <div class="row" >
      
      <div class="col-md-12">
         <div class="box box-warning">
            <div class="box-body ">
               <!-- Widget: user widget style 1 -->
               <div class="box box-widget widget-user-2">
                  <!-- Add the bg color to the header using any of the bg-* classes -->
                  <div class="widget-user-header ">
                     <div class="row pull-right">
                        <?php is_allowed('database_add', function(){?>
                        <a class="btn btn-flat btn-success btn_add_new" id="btn_add_new" title="<?= cclang('add_new_button', [cclang('database')]); ?>  (Ctrl+a)" href="<?=  site_url('administrator/database/add'); ?>"><i class="fa fa-plus-square-o" ></i> <?= cclang('add_new_button', [cclang('database')]); ?></a>
                        <?php }) ?>
                        <a class="btn btn-flat btn-success" title="<?= cclang('export'); ?> <?= cclang('database') ?> " href="<?= site_url('administrator/database/backup/') ?>"><i class="fa fa-database" ></i> <?= cclang('backup'); ?> ZIP</a>
                     </div>
                     <div class="widget-user-image" style="text-align:left;">
                        <img class="img-circle" style="width:50px;" src="<?= BASE_ASSET; ?>/img/list.png" alt="User Avatar">
                     </div>
                     <!-- /.widget-user-image -->
                     <h3 class="widget-user-username"><?= cclang('database') ?></h3>
                     <h5 class="widget-user-desc"><?= cclang('list_all', [cclang('table')]); ?>  <i class="label bg-yellow"><?= $database_counts; ?>  <?= cclang('items'); ?></i></h5>
                  </div>

                  <form name="form_database" id="form_database" action="<?= base_url('administrator/database/index'); ?>">
                  

                  <div class="table-responsive"> 
                  <table class="table table-bordered table-striped dataTable">
                     <thead>
                        <tr class="" ">
                           
                           <th data-field="table_name"data-sort="1" data-primary-key="0"> <?= cclang('table_name') ?></th>
                           <th width="300px">Action</th>                        </tr>
                     </thead>
                     <tbody id="tbody_database">
                     <?php foreach($databases as $table_name): ?>
                        <tr>
                                                       
                           <td>
                              <div class="tree-table" data-name="<?= ($table_name) ?>"  data-name-crypt="<?= ccencrypt($table_name) ?>">

                              </div>
                           </td> 
                           <td width="200">
                            
                              <a href="<?= site_url('administrator/database/view/'.ccencrypt($table_name)); ?>" class="label-default"><i class="fa fa-bars "></i> <?= cclang('structure'); ?></a>
                              <a href="javascript:void(0);" data-href="<?= site_url('administrator/database/drop_table/'.ccencrypt($table_name)); ?>" class="label-default remove-data"><i class="fa fa-close"></i> <?= cclang('drop_table'); ?></a>
                              <a href="javascript:void(0);" data-href="<?= site_url('administrator/database/truncate/'.ccencrypt($table_name)); ?>" class="label-default remove-data"><i class="fa fa-refresh"></i> <?= cclang('empty'); ?></a>

                           </td>                        </tr>
                      <?php endforeach; ?>
                     

                     </tbody>
                  </table>
                  </div>
               </div>
               <hr>
               <!-- /.widget-user -->
               <div class="row">
                  <div class="col-md-8">
                     <div class="col-sm-2 padd-left-0 " >
                        <select type="text" class="form-control chosen chosen-select" name="bulk" id="bulk" placeholder="Site Email" >
                           <option value="">Bulk</option>
                        </select>
                     </div>
                     <div class="col-sm-2 padd-left-0 ">
                        <button type="button" class="btn btn-flat" name="apply" id="apply" title="<?= cclang('apply_bulk_action'); ?>"><?= cclang('apply_button'); ?></button>
                     </div>
                     <div class="col-sm-3 padd-left-0  " >
                        <input type="text" class="form-control" name="q" id="filter" placeholder="<?= cclang('filter'); ?>" value="<?= $this->input->get('q'); ?>">
                     </div>
                  
                     <div class="col-sm-1 padd-left-0 ">
                        <button type="submit" class="btn btn-flat" name="sbtn" id="sbtn" value="Apply" title="<?= cclang('filter_search'); ?>">
                        Filter
                        </button>
                     </div>
                     <div class="col-sm-1 padd-left-0 ">
                        <a class="btn btn-default btn-flat" name="reset" id="reset" value="Apply" href="<?= base_url('administrator/database');?>" title="<?= cclang('reset_filter'); ?>">
                        <i class="fa fa-undo"></i>
                        </a>
                     </div>
                  </div>
                  </form>                  <div class="col-md-4">
                     <div class="dataTables_paginate paging_simple_numbers pull-right" id="example2_paginate" >
                     </div>
                  </div>
               </div>
            </div>
            <!--/box body -->
         </div>
         <!--/box -->
      </div>
   </div>
</section>
<!-- /.content -->

<script src="<?= BASE_ASSET ?>treejs/tree.js"></script>
<link rel="stylesheet" href="<?= BASE_ASSET ?>treejs/tree.css">

<!-- Page script -->
<script>
  $(document).ready(function(){

   
   $('.tree-table').each(function(){

      var tree = new Tree(this, {
         navigate: true // allow navigate with ArrowUp and ArrowDown
      });
      tree.on('open', e => function(e) {

      });

      var table_name = $(this).data('name')
      var table_name_crypt = $(this).data('name-crypt')

      tree.on('action', function(e,f){
         console.log(e,f)
         window.location.href = BASE_URL + '/administrator/database/change_field/'+table_name_crypt+'?field='+e.node.name
      });

      tree.on('fetch',(folder) => {


         $.ajax({
            type: "GET",
            url: BASE_URL+'administrator/database/get_field',
            data: {
               table_name : $(folder).html()
            },
            dataType: "JSON",
            success: function (response) {
               if(response.success) {
                  $.each(response.data, function (indexInArray, val) { 
                     tree.file({
                        name: val.name
                     }, folder);
                  });
               }
            }
         });
        
         folder.resolve();
      });
   
      var structure = [ {
         name: $(this).data('name'),
         type: Tree.FOLDER,
         asynced: true
      }];
      // keep track of the original node objects
      tree.on('created', (e, node) => {
         e.node = node;
      });
      tree.json(structure);
   })

   
    $('.remove-data').click(function(){

      var url = $(this).attr('data-href');

      swal({
          title: "<?= cclang('are_you_sure'); ?>",
          text: "<?= cclang('data_to_be_deleted_can_not_be_restored'); ?>",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "<?= cclang('yes_delete_it'); ?>",
          cancelButtonText: "<?= cclang('no_cancel_plx'); ?>",
          closeOnConfirm: true,
          closeOnCancel: true
        },
        function(isConfirm){
          if (isConfirm) {
            document.location.href = url;            
          }
        });

      return false;
    });


    $('#apply').click(function(){

      var bulk = $('#bulk');
      var serialize_bulk = $('#form_database').serialize();

      if (bulk.val() == 'delete') {
         swal({
            title: "<?= cclang('are_you_sure'); ?>",
            text: "<?= cclang('data_to_be_deleted_can_not_be_restored'); ?>",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "<?= cclang('yes_delete_it'); ?>",
            cancelButtonText: "<?= cclang('no_cancel_plx'); ?>",
            closeOnConfirm: true,
            closeOnCancel: true
          },
          function(isConfirm){
            if (isConfirm) {
               document.location.href = BASE_URL + '/administrator/database/delete?' + serialize_bulk;      
            }
          });

        return false;

      } else if(bulk.val() == '')  {
          swal({
            title: "Upss",
            text: "<?= cclang('please_choose_bulk_action_first'); ?>",
            type: "warning",
            showCancelButton: false,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Okay!",
            closeOnConfirm: true,
            closeOnCancel: true
          });

        return false;
      }

      return false;

    });/*end appliy click*/

    


    //check all
    var checkAll = $('#check_all');
    var checkboxes = $('input.check');

    checkAll.on('ifChecked ifUnchecked', function(event) {   
        if (event.type == 'ifChecked') {
            checkboxes.iCheck('check');
        } else {
            checkboxes.iCheck('uncheck');
        }
    });

    checkboxes.on('ifChanged', function(event){
        if(checkboxes.filter(':checked').length == checkboxes.length) {
            checkAll.prop('checked', 'checked');
        } else {
            checkAll.removeProp('checked');
        }
        checkAll.iCheck('update');
    });
    initSortable('database', $('table.dataTable'));
  }); /*end doc ready*/
</script>