<?php
global $wpdb;
global $iris_db; 
$row=$iris_db->get_row( "SELECT * FROM seasoncards where active=1" );
$season=$row->season;
$clientrow=$wpdb->get_row( "SELECT * FROM ow_card_2022 where season='".$season."'" );

// #################### POST
if($_POST){
	$iris_border_color=$_POST['iris_border'];
	$iris_swish_color=$_POST['iris_seasonc'];
	$iris_sgreet_color=$_POST['iris_greetc'];
	$iris_logo_bgcolor=$_POST['iris_logo'];
	$iris_cedit_color=$_POST['iris_clientc'];
	$iris_footbg_color=$_POST['iris_foot'];
	$iris_cardbg_color=$_POST['iris_carbg'];
	$iris_season=$_POST['iris_season'];
	$iris_season_wish=$_POST['iris_seasonw'];
	$iris_greetings=$_POST['iris_greet'];
	$iris_card_content=$_POST['iris_client'];
	if(isset($clientrow->greetings)){
		if(isset($_FILES["iris_card_image"]["name"]) && $_FILES["iris_card_image"]["name"]!=''){
			$filename=$_FILES["iris_card_image"]["name"];
			$imageType = pathinfo($filename,PATHINFO_EXTENSION);
			$imagenameNew = "seasonal_".date('dmyhis').".";
			$newfilename=$imagenameNew.$imageType;
			$upload_dir='../wp-content/uploads/';
			$createfolder='seasonal';
			if ((($_FILES["iris_card_image"]["type"] == "image/gif")
			|| ($_FILES["iris_card_image"]["type"] == "image/jpeg")
			|| ($_FILES["iris_card_image"]["type"] == "image/pjpeg")))
			  {
			  if ($_FILES["iris_card_image"]["error"] > 0)
				{
					echo '<div class"alert alert-danger"><i class="fa fa-exclamation-triangle"></i> Updation failed! Error uploading image!<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>';
				}
			  else{
				move_uploaded_file($_FILES["iris_card_image"]["tmp_name"],$upload_dir.$newfilename);
				  $wpdb->query("UPDATE `ow_card_2022` SET `border_color`='".$iris_border_color."',`swish_color`='".$iris_swish_color."',`sgreet_color`='".$iris_sgreet_color."',`logo_bgcolor`='".$iris_logo_bgcolor."',`cedit_color`='".$iris_cedit_color."',`footbg_color`='".$iris_footbg_color."',`cardbg_color`='".$iris_cardbg_color."',`season`='".$iris_season."',`season_wish`='".$iris_season_wish."',`greetings`='".$iris_greetings."',`card_image_name`='".$newfilename."',`card_content`='".$iris_card_content."' WHERE season='".$season."'");
				  
				}
			  }
			else
			  {
			  echo '<div class"alert alert-danger"><i class="fa fa-exclamation-triangle"></i> Updation failed! Error uploading image!Invalid file, File must be with .jpg, .jpeg, or .gif file extension. <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>';
			  }
			
		}
		else{
			
			$wpdb->query("UPDATE `ow_card_2022` SET `border_color`='".$iris_border_color."',`swish_color`='".$iris_swish_color."',`sgreet_color`='".$iris_sgreet_color."',`logo_bgcolor`='".$iris_logo_bgcolor."',`cedit_color`='".$iris_cedit_color."',`footbg_color`='".$iris_footbg_color."',`cardbg_color`='".$iris_cardbg_color."',`season`='".$iris_season."',`season_wish`='".$iris_season_wish."',`greetings`='".$iris_greetings."',`card_content`='".$iris_card_content."' WHERE season='".$season."'");
		}
	}else{
		if(isset($_POST['iris_card_image'])){
			$wpdb->query("insert into ow_card_2022 (`border_color`, `swish_color`, `sgreet_color`, `logo_bgcolor`, `cedit_color`, `footbg_color`, `cardbg_color`, `season`, `season_wish`, `greetings`, `card_image_name`, `card_content`) values('".$iris_border_color."','".$iris_swish_color."','".$iris_sgreet_color."','".$iris_logo_bgcolor."','".$iris_cedit_color."','".$iris_footbg_color."','".$iris_cardbg_color."','".$iris_season."','".$iris_season_wish."','".$iris_greetings."','".$iris_card_image_name."','".$iris_card_content."')");
		}else{
			$wpdb->query("insert into ow_card_2022 (`border_color`, `swish_color`, `sgreet_color`, `logo_bgcolor`, `cedit_color`, `footbg_color`, `cardbg_color`, `season`, `season_wish`, `greetings`,  `card_content`) values('".$iris_border_color."','".$iris_swish_color."','".$iris_sgreet_color."','".$iris_logo_bgcolor."','".$iris_cedit_color."','".$iris_footbg_color."','".$iris_cardbg_color."','".$iris_season."','".$iris_season_wish."','".$iris_greetings."','".$iris_card_content."')");
		}
		
	}
	header('location:'.site_url().'/wp-admin/admin.php?page=iris-card-settings&success');
}
//##################### POST END

	// ---- IRIS CARD
	
	if(isset($clientrow->greetings)){$greetings=$clientrow->greetings;}else{$greetings=$row->greetings;}
	if(isset($clientrow->season_wish)){$season_wish=$clientrow->season_wish;}else{$season_wish=$row->season_wish;}
	if(isset($clientrow->card_image_name)){$image_from='client';$card_image_name=site_url().'/wp-content/uploads/'.$clientrow->card_image_name;}else{$image_from='iris';$card_image_name='https://seasoncards.irisopenwebsite.co.uk/seasoncards/'.$row->card_image_name;}
	if(isset($clientrow->logo_bgcolor)){$logo_bgcolor=$clientrow->logo_bgcolor;}else{$logo_bgcolor=$row->logo_bgcolor;}
	if(isset($clientrow->cedit_color)){$cedit_color=$clientrow->cedit_color;}else{$cedit_color=$row->cedit_color;}
	if(isset($clientrow->footbg_color)){$footbg_color=$clientrow->footbg_color;}else{$footbg_color=$row->footbg_color;}
	if(isset($clientrow->cardbg_color)){$cardbg_color=$clientrow->cardbg_color;}else{$cardbg_color=$row->cardbg_color;}
	if(isset($clientrow->border_color)){$border_color=$clientrow->border_color;}else{$border_color=$row->border_color;}
	if(isset($clientrow->swish_color)){$swish_color=$clientrow->swish_color;}else{$swish_color=$row->swish_color;}
	if(isset($clientrow->sgreet_color)){$sgreet_color=$clientrow->sgreet_color;}else{$sgreet_color=$row->sgreet_color;}
	if(isset($clientrow->swish_color)){$card_content=$clientrow->card_content;}else {$card_content='';}

	// #####card active duration
	$start_date=$row->start_date;
	$end_date=$row->end_date;

?>

<script type="text/javascript" src="//code.jquery.com/jquery-1.8.3.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"  crossorigin="anonymous">
<script src="<?php echo get_plugin_cdn_link(); ?>ckeditor/ckeditor.js"></script>
<style>body{height: 150vh}.col-md-2{max-width:20%;font-size: 0.95em;}input[type="color"]{height:40px;padding: 1px;}</style>
<script type="text/javascript">
    $(document).ready(function() {
        function readURL(input) {
			if (input.files && input.files[0]) {
				var reader = new FileReader();
				reader.onload = function (e) {
					$('#image_upload_preview').attr('src', e.target.result);
                }
				reader.readAsDataURL(input.files[0]);
            }
        }

        $("#inputFile").on("change", function () {
            readURL(this);            
        });
        
    });
    $('document').ready(function(){
        $('textarea').each(function(){
                $(this).val($(this).val().trim());
            }
        );
		CKEDITOR.replace( 'iris_seasonw',{
			editorplaceholder: 'Enter content for Season Wish, eg: Happy Easter, Merry Christmas, etc'
		});
		CKEDITOR.replace( 'iris_greet',{
			editorplaceholder: 'Enter content for Greetings here'
		});
		CKEDITOR.replace( 'iris_client',{
			editorplaceholder: '*Please add content that you wish to have displayed within the Season Card ( i.e., Business hours, Greetings from CEO/MD... )'
		} );
		
    });
    
</script>
<div class="wrap" id="iris-openwebsite-admin">
	<h1 class="border-bottom mb-3"><?php echo PRODUCT_NAME; ?> Dashboard <a href="<?php echo site_url(); ?>/card" class="btn btn-primary" target="_blank">View Seasonal Card</a></h1>
	<?php if(isset($_REQUEST['success'])){?><div class="updated notice is-dismissible"><div class="ss-hide-notices"><p>Updated Successfully!</p></div><button type="button" class="notice-dismiss" onclick="$(this).parent().hide();"><span class="screen-reader-text" >Dismiss this notice.</span></button></div><?php }?>
	<form name="add-season" class="col-12" method="post" enctype="multipart/form-data">
			  <div class="row">
				  <input type="hidden" class="form-control" value="<?php echo $season;?>" name="iris_season" placeholder="Season name" id="seasonname">
				  <div class="form-group col-md-2 float-left border p-3 bg-secondary text-white">
					  <label for="iris_card_image" >Season Image: <small>(Only JPG/JPEG format)</small></label>
					  <input type="file" class="form-control" accept="image/jpg,image/jpeg" id="inputFile" name="iris_card_image" id="iris_card_image">
				  </div>
				  <div class="form-group col-md-2 float-left border p-3 bg-secondary text-white">
				  	<img src="<?php echo $card_image_name;?>" width="100%">
				  </div>
				  <div class="form-group col-md-2 float-left border p-3 bg-secondary text-white">
					  <label for="iris_carbg">Card background color:</label><br>
					  <input type="color" name="iris_carbg" id="iris_carbg" value="<?php echo $cardbg_color;?>" class="form-control">
				  </div>
				  <div class="form-group col-md-2 float-left border p-3 bg-secondary text-white">
					  <label for="iris_border">Card border color:</label><br>
					  <input type="color" name="iris_border" name="iris_border" value="<?php echo $border_color;?>" class="form-control">
				  </div>
				  <div class="form-group col-md-2 float-left border p-3 bg-secondary text-white">
					  <label for="iris_foot">Footer background color:</label><br>
					  <input type="color" name="iris_foot" name="iris_foot" value="<?php echo $footbg_color;?>" class="form-control">
				  </div>
				  <div class="form-group col-md-2 float-left border p-3 bg-secondary text-white">
					  <label for="iris_logo">Logo BG Color: <small>(if using white logo)</small></label><br>
					  <input type="color" name="iris_logo" name="iris_logo" value="<?php echo $logo_bgcolor;?>" class="form-control">
				  </div>
				  
				  </div>
			  <div class="clearfix"></div>
			<div class="row">
			  <div class="form-group pl-0 col-md-4 mt-4 mb-3 float-left">
				  <label for="iris_seasonw"><strong>Season Wish:</strong> </label>
				  <br><label for="iris_seasonc"><small>Choose the color for Season Wish:</small></label>
				  <input type="color" name="iris_seasonc" id="iris_seasonc" value="<?php echo $swish_color;?>" style="width:100px; margin-bottom:10px; height:30px;">
				  <textarea rows="5" class="form-control" name="iris_seasonw" id="iris_seasonw" placeholder="Season Wish"><?php echo $season_wish;?></textarea>
			  </div>
			  <div class="form-group col-md-4 mt-4 mb-3 float-left">
				  <label for="iris_greet"><strong>Season Greetings:</strong> </label>
				  <br><label for="iris_greetc"><small>Choose the color for Season greetings:</small></label>
				  <input type="color" name="iris_greetc" id="iris_greetc" value="<?php echo $sgreet_color;?>"  style="width:100px; margin-bottom:10px; height:30px;">
				  <textarea rows="5" class="form-control" name="iris_greet" id="iris_greet" placeholder="Season Greetings"><?php echo $greetings;?></textarea>
			  </div>
			<div class="form-group pr-0 col-md-4 mt-4 mb-3 float-left">
				  <label for="iris_client"><strong>Company notice/miscellaneous:</strong></label>
				  <br><label for="iris_clientc"><small>Choose the color for the text:</small></label>
				  <input type="color" name="iris_clientc" id="iris_clientc" value="<?php echo $cedit_color;?>" style="width:100px; margin-bottom:10px; height:30px;" >
				  <textarea rows="5" class="form-control" name="iris_client" id="iris_client" placeholder=""><?php echo $card_content;?></textarea>
			  </div>
		</div>
			<div class="row">
				<button type="submit"  class="btn btn-success mr-3">Click to save card changes</button>
			  <button type="reset"  class="btn btn-warning">Reset</button>
		</div>
			</form>
	<div class="clearfix"></div>
</div><div class="clearfix"></div>

