<?php
include('hoozgot.tablenames.php');

function delete_tables(){
	// ok
	global $wpdb;
	global $table_names;
	$sql ='';
	//
	echo '<br><div class="title">Tables deleted:</div><br>';
	foreach ($table_names as &$tableToDelete){
		echo $tableToDelete.'<br>';
		$wpdb->query( $wpdb->prepare("DROP TABLE IF EXISTS $tableToDelete") );
	}
	echo "<br><hr>";
	install_tables();
}
function install_tables() {
	// ok
	global $wpdb;
	global $table_names;
	$sql ='';
	// hoozgot_type
	if($wpdb->get_var("show tables like '$table_names[0]'") != $table_names[0]) 
	{
		$sql .= "CREATE TABLE ". $table_names[0] ." (
		id INT NOT NULL AUTO_INCREMENT,
		type_name VARCHAR(255) NOT NULL,
		checked TINYINT NOT NULL,
		submittedByUser INT NOT NULL,
		UNIQUE KEY id (id)) DEFAULT CHARACTER SET ".$wpdb->charset.";";
		
	}
	// hoozgot_subtype
	if($wpdb->get_var("show tables like '$table_names[1]'") != $table_names[1]) 
	{
		$sql .= "CREATE TABLE ". $table_names[1] ." (
		id INT NOT NULL AUTO_INCREMENT,
		type_id VARCHAR(255) NOT NULL,
		subtype_name VARCHAR(255) NOT NULL,
		checked TINYINT NOT NULL,
		submittedByUser INT NOT NULL,
		UNIQUE KEY id (id)) DEFAULT CHARACTER SET ".$wpdb->charset.";";
		
	}
	// hoozgot_brand
	if($wpdb->get_var("show tables like '$table_names[2]'") != $table_names[2]) 
	{
		$sql .= "CREATE TABLE " . $table_names[2] . " (
		id INT NOT NULL AUTO_INCREMENT,
		type_id INT NOT NULL,
		subtype_id INT NOT NULL,
		brand_name VARCHAR(255) NOT NULL,
		checked TINYINT NOT NULL,
		submittedByUser INT NOT NULL,
		UNIQUE KEY id (id)) DEFAULT CHARACTER SET ".$wpdb->charset.";";
		
	}
	// hoozgot_model
	if($wpdb->get_var("show tables like '$table_names[3]'") != $table_names[3]) 
	{
		$sql .= "CREATE TABLE " . $table_names[3] . " (
		  id INT NOT NULL AUTO_INCREMENT,
		  type_id VARCHAR(255) NOT NULL,
		  subtype_id INT NOT NULL,
		  brand_id VARCHAR(255) NOT NULL,
		  model_name VARCHAR(255) NOT NULL,
		  checked TINYINT NOT NULL,
		  submittedByUser INT NOT NULL,
		  UNIQUE KEY id (id)) DEFAULT CHARACTER SET ".$wpdb->charset.";";
		  
	}
	// hoozgot_year
	if($wpdb->get_var("show tables like '$table_names[4]'") != $table_names[4]) 
	{
		$sql .= "CREATE TABLE " . $table_names[4] . " (
		  id INT NOT NULL AUTO_INCREMENT,
		  year_name INT NOT NULL,
		  checked TINYINT NOT NULL,
		  UNIQUE KEY id (id)) DEFAULT CHARACTER SET ".$wpdb->charset.";";
		  
	}
	// hoozgot_color
	if($wpdb->get_var("show tables like '$table_names[5]'") != $table_names[5]) 
	{
		$sql .= "CREATE TABLE " . $table_names[5] . " (
		  id INT NOT NULL AUTO_INCREMENT,
		  type_id VARCHAR(255) NOT NULL,
		  subtype_id INT NOT NULL,
		  brand_id VARCHAR(255) NOT NULL,
		  model_id VARCHAR(255) NOT NULL,
		  color_name VARCHAR(255) NOT NULL,
		  checked TINYINT NOT NULL,
		  submittedByUser INT NOT NULL,
		  UNIQUE KEY id (id)) DEFAULT CHARACTER SET ".$wpdb->charset.";";
		  
	}
	// hoozgot_items
	  if($wpdb->get_var("show tables like '$table_names[6]'") != $table_names[6]) 
	{ 
		$sql .= "CREATE TABLE " . $table_names[6] . " (
		  id INT NOT NULL AUTO_INCREMENT,
		  creation_date VARCHAR(50),
		  modification_date VARCHAR(50),
		  submittedByUser INT NOT NULL,
		  type_id INT NOT NULL,
		  subtype_id INT NOT NULL,
		  brand_id INT NOT NULL,
		  model_id INT NOT NULL,
		  year_id SMALLINT NOT NULL,
		  color_id INT NOT NULL,
		  specs VARCHAR(255) NOT NULL,
		  proof_images VARCHAR(255) NOT NULL,
		  item_images VARCHAR(255) NOT NULL,
		  serial_number VARCHAR(255) NOT NULL,
		  owner_and_date VARCHAR(255) NOT NULL,
		  stolen INT NOT NULL,
		  UNIQUE KEY id (id)) DEFAULT CHARACTER SET ".$wpdb->charset.";";
	}
	
	   require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
	   dbDelta($sql);
	   if($sql != '') 
	{
	   $wpdb->insert($table_names[0], array( 'type_name' => 'Musical Instrument', 'checked'=> 1, 'submittedByUser'=> 1));
	   $wpdb->insert($table_names[1], array( 'type_id'=> 1, 'subtype_name' => 'Guitar Effect', 'checked'=> 1, 'submittedByUser'=> 1));
	   $wpdb->insert($table_names[2], array( 'type_id'=> 1, 'subtype_id'=> 1, 'brand_name' => 'Ibanez', 'checked'=> 1, 'submittedByUser'=> 1));
	   $wpdb->insert($table_names[3], array( 'type_id'=> 1, 'subtype_id'=> 1, 'brand_id'=> 1, 'model_name' => 'Tube Screamer', 'checked'=> 1, 'submittedByUser'=> 1));
	   $wpdb->insert($table_names[4], array( 'year_name'=> '1980', 'checked'=> 1 ));
	   $wpdb->insert($table_names[5], array( 'type_id'=> 1, 'subtype_id'=> 1, 'brand_id'=> 1, 'model_id'=> 1, 'color_name' => 'Green', 'checked'=> 1, 'submittedByUser'=> 1));
	   $wpdb->insert($table_names[6], array( 'creation_date'=> date('Y-m-d H:i:s').' GMT', 'modification_date'=> date('Y-m-d H:i:s').' GMT', 'submittedByUser'=> 1, 'type_id'=> 1, 'subtype_id'=> 1, 'brand_id'=> 1, 'model_id'=> 1, 'year_id'=> 1, 'color_id'=> 1, 'specs'=> 'Road worn. Scratches here and there. Bootiful piece of gear.', 'proof_images'=> '', 'item_images'=> '', 'serial_number'=> '123456789', 'owner_and_date'=> '[1, '.date("Y-m-d H:i:s").']', 'stolen'=> 0));
	}
	echo '<div class="title"><br>New database installed and populated succesfully.</div>';
}
function areyousure(){ ?>
<div class="title">
<br>
Are you sure ?<br>
This will delete existing data !
</div>
<form action="" method="post">
    <p class="submit">
        <input name="install" type="hidden" value="2"/>
      <input type="submit" class="button-primary" value="<?php _e('Yes, install database') ?>"/>
  </p>
</form>
<?php 
}
?>
<?php 
//echo $_POST['install'];
if ($_POST['install'] == 1){
	areyousure();
}
else if ($_POST['install'] == 2){
	delete_tables();
}
else {
?>
<form action="" method="post">
    <p class="submit">
        <input name="install" type="hidden" value="1"/>
        <input type="submit" class="button-primary" value="<?php _e('Install database') ?>"/>
    </p>
</form>
<?php 
}

include ('jQuery.functions.php');
include ('css.functions.php');
?>
