<?php global $wpdb;global $iris_db; 
$sql = $iris_db->get_row("SELECT * FROM iris_notice_data where position='popup' and active='active'");
$sql2 = $wpdb->get_row("SELECT * FROM ow_posts WHERE post_title='Notice' and post_status='publish'");
//print_r($sql);

if($sql2->post_title=='Notice'){
	$defaulttitle=$sql->title;
	$defaultcontent=$sql2->post_content;
}
else{
	$defaulttitle=$sql->title;
	$defaultcontent=$sql->post_content;
}

$getss=$wpdb->query("SHOW TABLES LIKE 'cookies_website_setting'");

if($getss==0){
	$wpdb->query("CREATE TABLE `cookies_website_setting` (
				  `id` int(11) NOT NULL,
				  `website` varchar(40) DEFAULT NULL,
				  `title` varchar(50) DEFAULT NULL,
				  `content` longtext DEFAULT NULL,
				  `enable_all_pages` varchar(10) DEFAULT NULL,
				  `enable_on_refresh` varchar(10) DEFAULT NULL,
				  `enable` varchar(15) DEFAULT NULL,
				  `type` varchar(100) DEFAULT NULL,
				  `datetime` timestamp NULL DEFAULT CURRENT_TIMESTAMP
				) ENGINE=MyISAM DEFAULT CHARSET=utf8");
	$wpdb->query("ALTER TABLE `cookies_website_setting` ADD PRIMARY KEY(`id`)");
	$wpdb->query("ALTER TABLE `cookies_website_setting` CHANGE `id` `id` INT(11) NOT NULL AUTO_INCREMENT");
	$wpdb->query("insert into cookies_website_setting (`website`,`title`,`content`,`enable_all_pages`,`enable_on_refresh`,`type`,`enable`) values('".$_SERVER['SERVER_NAME']."','".$defaulttitle."','".$defaultcontent."','','','".$defaulttitle."','no')");
	
	$iris_db->query("insert into cookies_website_setting (`website`,`title`,`content`,`enable_all_pages`,`enable_on_refresh`,`type`,`enable`) values('".$_SERVER['SERVER_NAME']."','".$defaulttitle."','".$defaultcontent."','','','".$defaulttitle."','no')");
	header("location:admin.php?page=popup_settings");
}

	
$sql = $iris_db->get_row("SELECT * FROM iris_notice_data where position='popup' and active='active'");
if($_POST){
	$qry=$wpdb->get_row("select id from cookies_website_setting where website='".$_POST['website']."' and type='".$sql->title."'");
	if($qry->id!=''){
		//updating old data
		$up_qry2=$wpdb->query("update cookies_website_setting set `website`='".$_POST['website']."',`title`='".$_POST['title']."',`content`='".$_POST['content']."',`enable_all_pages`='".$_POST['enable_all_pages']."',`enable_on_refresh`='".$_POST['enable_on_refresh']."',`type`='".$_POST['type']."',`enable`='".$_POST['enable']."' where `website`='".$_POST['website']."'");
		$up_qry=$iris_db->query("update cookies_website_setting set `website`='".$_POST['website']."',`title`='".$_POST['title']."',`content`='".$_POST['content']."',`enable_all_pages`='".$_POST['enable_all_pages']."',`enable_on_refresh`='".$_POST['enable_on_refresh']."',`type`='".$_POST['type']."',`enable`='".$_POST['enable']."' where `website`='".$_POST['website']."' ");
		$up_qrywp=$wpdb->query("update ow_posts set `post_content`='".$_POST['content']."' where post_title='Notice' and post_status='publish'");
		//echo "update cookies_website_setting set `website`='".$_POST['website']."',`title`='".$_POST['title']."',`content`='".$_POST['content']."',`enable_all_pages`='".$_POST['enable_all_pages']."',`enable_on_refresh`='".$_POST['enable_on_refresh']."',`type`='".$_POST['type']."',`enable`='".$_POST['enable']."' where `website`='".$_POST['website']."' "; exit();
		if($up_qry){
			header("location:admin.php?page=popup_settings&&status=Data Updated!");
		}
		else {
			header("location:admin.php?page=popup_settings&&status=Error in Data Updates!");
		}
	}
	else{
		//inserting new data
		$qryccx=$wpdb->get_row("select id from cookies_website_setting where website='".$_POST['website']."' and type='".$sql->title."'");
		if($qryccx->id==''){
			$up_qry2=$wpdb->query("insert into cookies_website_setting (`website`,`title`,`content`,`enable_all_pages`,`enable_on_refresh`,`type`,`enable`) values('".$_POST['website']."','".$_POST['title']."','".$_POST['content']."','".$_POST['enable_all_pages']."','".$_POST['enable_on_refresh']."','".$_POST['type']."','".$_POST['enable']."')");
		}else{
			$up_qry2=$wpdb->query("update cookies_website_setting set `website`='".$_POST['website']."',`title`='".$_POST['title']."',`content`='".$_POST['content']."',`enable_all_pages`='".$_POST['enable_all_pages']."',`enable_on_refresh`='".$_POST['enable_on_refresh']."',`type`='".$_POST['type']."',`enable`='".$_POST['enable']."' where `website`='".$_POST['website']."'");
		}
		$qrycc=$iris_db->get_row("select id from cookies_website_setting where website='".$_POST['website']."' and type='".$sql->title."'");
		if($qrycc->id==''){
			$up_qry=$iris_db->query("insert into cookies_website_setting (`website`,`title`,`content`,`enable_all_pages`,`enable_on_refresh`,`type`,`enable`) values('".$_POST['website']."','".$_POST['title']."','".$_POST['content']."','".$_POST['enable_all_pages']."','".$_POST['enable_on_refresh']."','".$_POST['type']."','".$_POST['enable']."')");
		}else{
			$up_qry=$iris_db->query("update cookies_website_setting set `website`='".$_POST['website']."',`title`='".$_POST['title']."',`content`='".$_POST['content']."',`enable_all_pages`='".$_POST['enable_all_pages']."',`enable_on_refresh`='".$_POST['enable_on_refresh']."',`type`='".$_POST['type']."',`enable`='".$_POST['enable']."' where `website`='".$_POST['website']."' ");
			$up_qrywp=$wpdb->query("update ow_posts set `post_content`='".$_POST['content']."' where post_title='Notice' and post_status='publish'");
		}		
		if($up_qry){
			header("location:admin.php?page=popup_settings&&status=Data Updated!");
		}
		else {
			header("location:admin.php?page=popup_settings&&status=Error in Data Updates!");
		}
	}
}
?>

<?php if(isset($_REQUEST['status'])){echo '<h2 style="width:97%; padding:1%; background-color:yellow;">'.$_REQUEST['status'].'</h2>';}?>

<?php $get_qry=$wpdb->get_row("select * from cookies_website_setting where website='".$_SERVER['SERVER_NAME']."' and type='".$sql->title."'");//echo "select * from cookies_website_setting where website='".$_SERVER['SERVER_NAME']."' and type='".$sql->title."'";?>
<script src="https://cdn.ckeditor.com/4.14.0/standard/ckeditor.js"></script>
<style>#wpbody-content{font-family: Arial; font-size:12px;}</style>
<h1>IRIS Pop-up Settings</h1>
<hr>
<h3 style="background-color: #62b5e5;    padding: 1%; margin:0;">Current Status : 
<?php
	if($get_qry->enable=='yes'){
		echo 'Pop-up enabled on the website.';
	}else{
		echo 'Pop-up  disabled on the website';
		}
	if($get_qry->enable_all_pages=='yes'){
		echo ' Pop-up enabled on homepage as well as inner-pages.';
	} else{
		echo ' Pop-up enabled on homepage only';
	}
	if($get_qry->enable_on_refresh=='yes'){
		echo ' Pop-up enabled on every refresh on homepage.';
	} else{
		echo ' Pop-up will be disabled once closed';
	}
	?>
</h3>
<hr>
<form action="" method="post" style="font-size:17px; line-height:2.5em; float:left; width:98%;">
	<input name="type" type="hidden" value="<?php echo $sql->title; ?>" >
	<input type="hidden" name="website" value="<?php echo $_SERVER['SERVER_NAME']; ?>"/>
	<strong>Enable Pop-up : </strong><input type="checkbox" name="enable" value="yes" <?php if($get_qry->enable=='yes'){echo 'checked';} ?>/><br>
	<strong>Enable Pop-up on all pages :</strong> <input type="checkbox" name="enable_all_pages" value="yes" <?php if($get_qry->enable_all_pages=='yes'){echo 'checked';} ?>/><small> <em>Disable this option if you want to display the pop-up in the homepage only</em></small><br>
	<strong>Enable Pop-up on every refresh :</strong> <input type="checkbox" name="enable_on_refresh" value="yes" <?php if($get_qry->enable_on_refresh=='yes'){echo 'checked';} ?>/><small> <em>Enabling this option will display the pop-up on every loading of homepage </em></small><br>
	<strong>Pop-up Title :</strong><input type="text" name="title" value="<?php if($get_qry->title!=''){echo $get_qry->title;}else{echo $sql->title;} ?>" style="width:50%;" /><br>
	<strong>Pop-up Content : </strong><textarea  name="content"><?php if($get_qry->content!=''){echo $get_qry->content;}else{echo $sql->content;} ?></textarea>
	<input type="submit" style="background-color: #62b5e5;margin-top:10px;padding: 1% 2%;border-radius: 7px;color: #fff;text-transform: uppercase;border: 0 none;cursor:pointer;"/>
</form>

<script>CKEDITOR.replace( 'content' );</script>