<?php if (!defined('ABSPATH')) {
    exit;
}
global $wpdb;
$dbtables = $wpdb->get_col($wpdb->prepare('show tables', null));
$action = isset($_GET['action']) ? $_GET['action'] : 'default';
$tbl = isset($_GET['tbl']) ? $_GET['tbl'] : '';
?>
<div class="wrap dbmanager">
<div class="mk_export_db">
<form action="admin-post.php?action=mk_db_manager_export_db" method="post">
<?php wp_nonce_field('mk_export_db_field', 'export_db_field'); ?>
<input type="submit" name="mk_export_db" value="Export Database" class="button button-primary" />
</form>
</div>
<div class="clear"></div>
<div class="db_top">
  <span class="dbDetail dbDetailHost">
  <span class="icon"><img src="<?php echo plugins_url('images/host.png', dirname(__FILE__)); ?>" width="16" /></span> <strong> <?php _e('Host:', 'ffm'); ?> </strong> <?php echo $wpdb->dbhost; ?> &raquo; 
  </span>

  <span class="dbDetail dbDetailDatabase">
  <span class="icon"><img src="<?php echo plugins_url('images/database.png', dirname(__FILE__)); ?>" width="16"/></span> <strong><?php _e('Database:', 'ffm'); ?> </strong><a href="?page=wp_file_manager_db_manager&action=default"><?php echo $wpdb->dbname; ?></a><?php if (!empty($tbl)) {
    ?> &raquo;  </span>
      <span class="dbDetail dbDetailTable">
      <span class="icon"> <img src="<?php echo plugins_url('images/table.png', dirname(__FILE__)); ?>" width="16"/></span> <strong><?php _e('Table:', 'ffm'); ?> </strong> <?php echo $tbl; ?> <?php
} ?> 
  </span>
  
  <a href="?page=wp_file_manager_db_manager&amp;action=create_tbl" class="button button-primary"><?php _e('Create New Table', 'ffm'); ?></a>
</div>
<div class="db_main" id="db_main">
<div class="db_main_left">
<div id="db_left_resize">
<ul>
<li><span class="db_name"><a href="?page=wp_file_manager_db_manager&action=default"><?php echo $wpdb->dbname; ?> (<?php echo count($dbtables); ?>) </a></span>
<ul>
<?php if (!empty($dbtables) && is_array($dbtables)):
foreach ($dbtables as $dbtable): ?>
<li <?php echo($dbtable == $tbl) ? 'class="active"' : ''; ?>><a href="?page=wp_file_manager_db_manager&action=browse&tbl=<?php echo $dbtable; ?>"><?php echo $dbtable; ?></a></li>
<?php endforeach; endif; ?>
</ul>
</li>
</ul>
</div> <!--db_left_resize-->
</div>

<div class="db_main_right">
<div id="db_main_right_content">
<div id="msg_bar"></div>
<?php 
/* Add Record */
if (isset($_POST['save_tbl_data'])) {
    unset($_POST['save_tbl_data']);
    unset($_POST['reset']);
    $this->CreateRecord($_GET['tbl'], $_POST);
}
if ($action == 'default') {
    $this->show_tbls($dbtables);
} elseif ($action == 'browse') {
    $this->BrowseRecords($_GET['tbl'], $keys = '', $values = '', $offSet = 0, $eat_cols = 200, $fuzzy = '');
} elseif ($action == 'insert') {
    $this->TableDetails($_GET['tbl']);
} elseif ($action == 'delete') {
    $this->deleteRecord($_GET['tbl'], $_GET['pf'], intval($_GET['rid']));
} elseif ($action == 'create_tbl') {
    $this->create_tbl();
} elseif ($action == 'drop') {
    $this->drop_table($_GET['tbl']);
} elseif ($action == 'truncate') {
    $this->truncate_tbl($_GET['tbl']);
} elseif ($action == 'update') {
    $this->UpdateTableDetails($_GET['tbl'], $_GET['pf'], intval($_GET['rid'])); ?>
<script>
 jQuery(document).ready( function() {
   jQuery(".update_tbl_data").click( function(e) {
	   e.preventDefault();
	   var update_tbl_form = jQuery('#update_tbl_form').serialize();
      jQuery.ajax({
         type : "post",
         url : "<?php echo admin_url('admin-ajax.php'); ?>",
         data : {
			      action: "Update_Tbl_Record",
				  table2Edit : "<?php echo $_GET['tbl']; ?>",
				  fieldname:  "<?php echo $_GET['pf']; ?>",
				  rid:  "<?php echo $_GET['rid']; ?>",
				  values: update_tbl_form
				},
         success: function(response) {
			 var data = jQuery.parseJSON(response);
            if(data.type == "success") {
               jQuery("#msg_bar").html('<span class="success">Success:: '+data.msg+'</span>')
            }
            else {
               jQuery("#msg_bar").html('<span class="error">Error:: '+data.msg+'</span>')
            }
         }
      })   

   })

})
</script> 
<?php
}
?>
</div>  <!--db_main_right_content-->
</div>
</div>
</div>