<?php
include("../../startup.php"); 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "mail_schedule";
$page_name      = "Add Mail Format";
$id	 = "";
$vat = "";
$query_string = "";

################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
    utility::addslashesOnAllInput(array("query_string"));
	
    $email_name = $_REQUEST['email_name'];
    $mail_track = $_REQUEST['mail_track'];
    $sending_trigger = $_REQUEST['sending_trigger'];
    $email_subject = $_REQUEST['email_subject'];
    $mail_body = addslashes(fck::addAbsolutePaths($_REQUEST['mail_body']));
    $mail_status = $_REQUEST['mail_status'];
    $link1 = $_REQUEST['link1'];
    $link2 = $_REQUEST['link2'];
    $link3 = $_REQUEST['link3'];
    $link4 = $_REQUEST['link4'];
    $link5 = $_REQUEST['link5'];
    $link6 = $_REQUEST['link6'];
    $link7 = $_REQUEST['link7'];
    $link8 = $_REQUEST['link8'];
    $link9 = $_REQUEST['link9'];
    $link10 = $_REQUEST['link10'];
    $created_on = date("Y-m-d");
    $updated_by = $_SESSION["user_id"];

    $sql= "INSERT INTO $table_name (email_name,mail_track,sending_trigger,email_subject,mail_body,mail_status,link1,link2,link3,link4,link5,link6,link7,link8,link9,link10,created_on,updated_by) VALUES('$email_name','$mail_track','$sending_trigger','$email_subject','$mail_body','$mail_status','$link1','$link2','$link3','$link4','$link5','$link6','$link7','$link8','$link9','$link10','$created_on','$updated_by')";
    mysqli_query($conn,$sql);

    $_SESSION['info_message'] = "Mail schedule added sucessfully";
    header("Location: $file_name");
    exit();
}
################################################# END INSERT #################################################

################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){	
	$id = $_REQUEST['id'];
    $form_action = "update";
    $button_value = "Update";
    $page_name = "Edit Mail Schedule";

    $query_string = (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];

    $sql= "SELECT * FROM $table_name WHERE id = '$id' ";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);

    $email_name= $row_edit['email_name'];
    $mail_track= $row_edit['mail_track'];
    $sending_trigger= $row_edit['sending_trigger'];
    $email_subject= $row_edit['email_subject'];
    $mail_body= $row_edit['mail_body'];
    $mail_status= $row_edit['mail_status'];
    $link1= $row_edit['link1'];
    $link2= $row_edit['link2'];
    $link3= $row_edit['link3'];
    $link4= $row_edit['link4'];
    $link5= $row_edit['link5'];
    $link6= $row_edit['link6'];
    $link7= $row_edit['link7'];
    $link8= $row_edit['link8'];
    $link9= $row_edit['link9'];
    $link10= $row_edit['link10'];
}
################################################# END EDIT #################################################


################################################## START UPDATE ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){ 
	utility::addslashesOnAllInput(array("query_string"));
	
	$id = $_REQUEST['id'];
    $email_name = $_REQUEST['email_name'];
    $mail_track = $_REQUEST['mail_track'];
    $sending_trigger = $_REQUEST['sending_trigger'];
    $email_subject = $_REQUEST['email_subject'];
    $mail_body = addslashes(fck::addAbsolutePaths($_REQUEST['mail_body']));
    $mail_status = $_REQUEST['mail_status'];
    $link1 = $_REQUEST['link1'];
    $link2 = $_REQUEST['link2'];
    $link3 = $_REQUEST['link3'];
    $link4 = $_REQUEST['link4'];
    $link5 = $_REQUEST['link5'];
    $link6 = $_REQUEST['link6'];
    $link7 = $_REQUEST['link7'];
    $link8 = $_REQUEST['link8'];
    $link9 = $_REQUEST['link9'];
    $link10 = $_REQUEST['link10'];
    $updated_by = $_SESSION["user_id"];

    $sql= "UPDATE $table_name SET email_name = '$email_name', mail_track = '$mail_track', sending_trigger = '$sending_trigger', email_subject = '$email_subject', mail_body = '$mail_body', mail_status = '$mail_status', link1 = '$link1', link2 = '$link2', link3 = '$link3', link4 = '$link4', link5 = '$link5', link6 = '$link6', link7 = '$link7', link8 = '$link8', link9 = '$link9', link10 = '$link10', updated_by = '$updated_by' WHERE id = '$id' ";
    mysqli_query($conn,$sql);

    $query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
    $query_string = str_ireplace("page=add", "page=view", $query_string);
    $query_string = "&".$query_string;

    $_SESSION['info_message'] = "Mail schedule updated sucessfully";
    header("Location: $file_name");
    exit();
}
################################################# END UPDATE #################################################
?>
<section class="content-header">
	<div class="container-fluid">
		<div class="row mb-2">
			<div class="col-sm-6">
				<h1>
				<?=$page_name;?>
				</h1>
			</div>
		</div>
	</div>
</section>
<section class="content">
	<div class="container-fluid">
	<div class="row">
		<div class="col-lg-12">
			<!-- general form elements -->
              <div class="card">
                <form class="form-horizontal" method="post" name="form1" id="form1" action="index?page=add&action=<? echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
                  <div class="card-body">
                    <div class="form-group">
                      <label for="Email Name" class="control-label">Email Name</label>
                      <div class="">
                        <input type="text" id="email_name" name="email_name" value="<?php echo $email_name;?>" class="form-control" placeholder="Email Name">
                      </div>
                    </div>
					<div class="form-group">
                      <label for="Mail Track" class="control-label">Mail Track</label>
                      <div class="">
                        <? $gsi->getEmailTrackList("mail_track",$mail_track,"100%",false,"form-control"); ?>
                      </div>
                    </div>
					<div class="form-group">
                      <label for="Sending Trigger" class="control-label">Sending Trigger</label>
                      <div class="">
                        <? $gsi->getSendingTriggerList("sending_trigger",$sending_trigger,"100%",false,"form-control"); ?>
                      </div>
                    </div>
					<div class="form-group">
                      <label for="Email Subject" class="control-label">Email Subject</label>
                      <div class="">
                        <input type="text" id="email_subject" name="email_subject" value="<?php echo $email_subject;?>" class="form-control" placeholder="Email Subject">
                      </div>
                    </div>
					<div class="form-group">
						<label for="Mail body" class="control-label">&nbsp;</label>
						<div class="">
							<? if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){ ?>
								<p><a href="" data-toggle="modal" data-target="#myModal" class="document" alt="<?=$row['id'];?>" style="">Image and Document Library</a></p>
							<? } ?>
							<b>Note: For best results, we highly recommend both Html AND Text content is used as email 'Open' statistics cannot be determined from Text only emails.</b>
							<p>(Merge fields you can use in your email to autofill lead details: [title] [firstname] [lastname] )</p>
						</div>
					</div>
					<div class="form-group">
                      <label for="Mail Body" class="control-label">Mail Body</label>
                      <div class="">
						<textarea class="form-control" id="mail_body" name="mail_body" rows="3" placeholder="Mail Body"><?php echo $mail_body;?></textarea>
                      </div>
                    </div>
					<div class="form-group">
                      <label for="Mail Status" class="control-label">Mail Status</label>
                      <div class="">
                        <? $gsi->getEmailStatusList("mail_status",$mail_status,"100%",false,"form-control"); ?>
                      </div>
                    </div>
					<?
						$display = "none";
						$label = "Give your links friendly names";
						if($link1 != "" || $link2 != "" || $link3 != "" || $link4 != "" || $link5 != "" || $link6 != "" || $link7 != "" || $link8 != "" || $link9 != "" || $link10 != ""){
							$display = "block";
							$label = "Hide";
						}
					?>
					<div class="form-group">
						<label for="Mail format body" class="control-label">&nbsp;</label>
						<div class="" id="links_friendly_names" style="cursor:pointer;color:blue;"><?=$label;?></div>
					</div>
					<div id="links_div" style="display:<?=$display;?>;">
						<div class="form-group">
							<label for="Mail format body" class="control-label">&nbsp;</label>
							<div class="">
								<p>Adding friendly names for your links (e.g. Home Page or PDF Brochure Download) can be helpful when viewing your reporting. You can name the first 10 of the links you have put in your email above in the order they appear from top to bottom. If no names are given then reporting will simply show 'Link1', 'Link2', etc</p>
							</div>
						</div>
						<div class="form-group"> 
							<label class="control-label" for="1st Contact First Name">&nbsp;</label> 
							<div class="">
								<label class="control-label" style="float:left;clear:right;" for="Link1">Link1</label> 
								<div class="col-sm-5">
									<input type="text" id="link1" name="link1" class="form-control" value="<?php echo $link1;?>" placeholder="Link1"> 
								</div>
								<label class="control-label" style="float:left;clear:right;" for="Link2">Link2</label> 
								<div class="col-sm-5">
									<input type="text" id="link2" name="link2" class="form-control" value="<?php echo $link2;?>" placeholder="Link2"> 
								</div>
							</div>
						</div>
						<div class="form-group"> 
							<label class="control-label" for="1st Contact First Name">&nbsp;</label> 
							<div class="">
								<label class="control-label" style="float:left;clear:right;" for="Link3">Link3</label> 
								<div class="col-sm-5">
									<input type="text" id="link3" name="link3" class="form-control" value="<?php echo $link3;?>" placeholder="Link3"> 
								</div>
								<label class="control-label" style="float:left;clear:right;" for="Link4">Link4</label> 
								<div class="col-sm-5">
									<input type="text" id="link4" name="link4" class="form-control" value="<?php echo $link4;?>" placeholder="Link4"> 
								</div>
							</div>
						</div>
						<div class="form-group"> 
							<label class="control-label" for="1st Contact First Name">&nbsp;</label> 
							<div class="">
								<label class="control-label" style="float:left;clear:right;" for="Link5">Link5</label> 
								<div class="col-sm-5">
									<input type="text" id="link5" name="link5" class="form-control" value="<?php echo $link5;?>" placeholder="Link5"> 
								</div>
								<label class="control-label" style="float:left;clear:right;" for="Link6">Link6</label> 
								<div class="col-sm-5">
									<input type="text" id="link6" name="link6" class="form-control" value="<?php echo $link6;?>" placeholder="Link6"> 
								</div>
							</div>
						</div>
						<div class="form-group"> 
							<label class="control-label" for="1st Contact First Name">&nbsp;</label> 
							<div class="">
								<label class="control-label" style="float:left;clear:right;" for="Link7">Link7</label> 
								<div class="col-sm-5">
									<input type="text" id="link7" name="link7" class="form-control" value="<?php echo $link7;?>" placeholder="Link7"> 
								</div>
								<label class="control-label" style="float:left;clear:right;" for="Link8">Link8</label> 
								<div class="col-sm-5">
									<input type="text" id="link8" name="link8" class="form-control" value="<?php echo $link8;?>" placeholder="Link8"> 
								</div>
							</div>
						</div>
						<div class="form-group"> 
							<label class="control-label" for="1st Contact First Name">&nbsp;</label> 
							<div class="">
								<label class="control-label" style="float:left;clear:right;" for="Link9">Link9</label> 
								<div class="col-sm-5">
									<input type="text" id="link9" name="link9" class="form-control" value="<?php echo $link9;?>" placeholder="Link9"> 
								</div>
								<label class="control-label" style="float:left;clear:right;" for="Link10">Link10</label> 
								<div class="col-sm-5">
									<input type="text" id="link10" name="link10" class="form-control" value="<?php echo $link10;?>" placeholder="Link10"> 
								</div>
							</div>
						</div>
					</div>
                  </div><!-- /.box-body -->
                  <div class="card-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
					<input type="hidden" id="query_string" name="query_string" value="<?php echo $query_string;?>">
                    <button type="submit" id="submit" name="submit" value="<?php echo $button_value;?>" class="btn btn-primary">Submit
						<i class="fa fa-refresh fa-spin" style="display:none;" id="page_load_div"></i>
					</button>
                  </div>
                </form>
              </div><!-- /.box -->
		</div>
	</div>
	</div>
</section>
<script>
CKEDITOR.replace('mail_body');  
</script>
<script type="text/javascript">
$(function(){
	$("#form1").submit(function() {
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
		//return true;
	});
	$(".document").click(function(){	
		$("#myModalLabel").text("Documents Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var mail_schedule_id = "<?=$id;?>";		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/document_management/index?page=view_schedule_document&mail_schedule_id="+mail_schedule_id+hide_top_left);
	});
	
	$("#links_friendly_names").click(function(){
		if($(this).html() == "Give your links friendly names"){
			$(this).html("Hide");
		}else{
			$(this).html("Give your links friendly names");
		}
		$("#links_div").toggle();
	});
});
</script>