<?php
require_once '../../library/config.php';
require_once '../library/functions.php';
require_once '../library/logged.php';
require_once '../../library/validation.php';

if (isset($_POST['del_id'])){
	$table = txt_only($_POST['table']);
	$id_name = txt_only($_POST['id_name']);
	$id = isNumber(trim($_POST['del_id']));
	$error = false;
	$error_txt = '';
	
	if($table == 'news'){
		$sql_del_img = mysql_query("SELECT `ep_pic` FROM `news` WHERE `news_id` = ".$id);
		$row_del_img = mysql_fetch_assoc($sql_del_img);
		$delete_im = $row_del_img['ep_pic'];
		unlink('../../upload/news_images/'.$delete_im);
	}
	
	if($table == 'album'){
		$sql_del_subimg  = mysql_query("SELECT `image` FROM `photo` WHERE `p_album_id` = ".$id);
		$row_del_subimg = mysql_fetch_assoc($sql_del_subimg);
		if($row_del_subimg){
			do {
				$delete_subimg = $row_del_subimg['image'];
				unlink('../../upload/photos/'.$delete_subimg);
			} while ($row_del_subimg = mysql_fetch_assoc($sql_del_subimg));
			mysql_query('DELETE FROM `photo` WHERE `p_album_id` ='.$id);
		}
		$sql_del_img = mysql_query("SELECT `ep_pic` FROM `album` WHERE `album_id` = ".$id);
		$row_del_img = mysql_fetch_assoc($sql_del_img);
		$delete_im = $row_del_img['ep_pic'];
		unlink('../../upload/album_images/'.$delete_im);
	}
	
	if($table == 'options'){
		$sql_del_value  = mysql_query("SELECT `match_option_id` FROM `options_value` WHERE `match_option_id` = ".$id);
		$row_del_value = mysql_fetch_assoc($sql_del_value);
		if($row_del_value){
			mysql_query('DELETE FROM `options_value` WHERE `match_option_id` ='.$id);
		}
	}
	
	if($table == 'cate'){
		$sql_sub = mysql_query("SELECT `match_cat_id` FROM `subcate` WHERE `match_cat_id` = ".$id);
		$row_sub = mysql_fetch_assoc($sql_sub);
		
		$sql_product = mysql_query("SELECT `p_cat_id` FROM `product_list` WHERE `p_cat_id` = ".$id);
		$row_product = mysql_fetch_assoc($sql_product);
		if($row_product){
			$error = true;
			$error_txt .= '<strong>Delete fail!</strong> Reason: There is product(s) in this category<br>';
		} 
		if($row_sub){
			$error = true;
			$error_txt .= '<strong>Delete fail!</strong> Reason: There is sub-catergory(s) in this category<br>';
		} 
	}
	
	if($table == 'section'){
		$sql_content = mysql_query("SELECT `ep_web` FROM `content` WHERE `ep_web` = ".$id);
		$row_content = mysql_fetch_assoc($sql_content);
		
		$sql_news = mysql_query("SELECT `ep_web` FROM `news` WHERE `ep_web` = ".$id);
		$row_news = mysql_fetch_assoc($sql_news);
		
		if($row_content){
			$error = true;
			$error_txt .= '<strong>Delete fail!</strong> Reason: There is content(s) in this section<br>';
		} 
		if($row_news){
			$error = true;
			$error_txt .= '<strong>Delete fail!</strong> Reason: There is news(s) in this section<br>';
		} 
	}
	
	if($table == 'subcate'){
		$sql_product = mysql_query("SELECT `p_subcat_id` FROM `product_list` WHERE `p_subcat_id` = ".$id);
		$row_product = mysql_fetch_assoc($sql_product);
		
		if($row_product){
			$error = true;
			$error_txt .= '<strong>Delete fail!</strong> Reason: There is product(s) in this sub-category<br>';
		} 
	}
	
	if($table == 'banner'){
		$sql_del_img = mysql_query("SELECT `ep_pic` FROM `banner` WHERE `banner_id` = ".$id);
		$row_del_img = mysql_fetch_assoc($sql_del_img);
		$delete_im = $row_del_img['ep_pic'];
		unlink('../../upload/slide_images/'.$delete_im);
		unlink('../../upload/slide_images/m_'.$delete_im);
		unlink('../../upload/slide_images/s_'.$delete_im);
	}
	if($table == 'product_list'){
		$sql_del_subimg  = mysql_query("SELECT `image` FROM `photo` WHERE `p_product_id` = ".$id);
		$row_del_subimg = mysql_fetch_assoc($sql_del_subimg);
		if($row_del_subimg){
			do {
				$delete_subimg = $row_del_subimg['image'];
				unlink('../../upload/photos/'.$delete_subimg);
			} while ($row_del_subimg = mysql_fetch_assoc($sql_del_subimg));
			mysql_query('DELETE FROM `photo` WHERE `p_product_id` ='.$id);
		}
		$sql_del_img = mysql_query("SELECT `p_image` FROM `product_list` WHERE `p_id` = ".$id);
		$row_del_img = mysql_fetch_assoc($sql_del_img);
		$delete_im = $row_del_img['p_image'];
		unlink('../../upload/product_images/'.$delete_im);
	}
	
	if ($error == true){
		echo '<span style="color:red; font-size: 15px;">'.$error_txt.'</span>';
	} else {
		mysql_query("DELETE FROM `".$table."` WHERE `".$id_name."` = ".$id);
		echo '<strong style="color:red; font-size: 15px;">Delete Success</strong>';
	}
}

mysql_close($connePaper);

?>
