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

############################################## START INSERT ############################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
    $mail_format_name = $_REQUEST['mail_format_name'];
    $mail_format_subject = $_REQUEST['mail_format_subject'];	
    $mail_format_body = addslashes(fck::addAbsolutePaths($_REQUEST['mail_format_body']));
	$site_id        = $_SESSION['global_site_id'];

    $sql= "INSERT INTO $table_name (mail_format_name,mail_format_subject,mail_format_body,site_id) VALUES('$mail_format_name','$mail_format_subject','$mail_format_body','$site_id')";
    mysqli_query($conn,$sql);
	
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	//header("Location: $file_name&message=$tmp added sucessfully");
	$_SESSION['info_message'] = "$tmp added sucessfully";
    header("Location: $file_name");
    exit();
}
############################################ END INSERT ###############################################

############################################## START EDIT #############################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){	
	$form_action = "update";
	$button_value = "Update";
	$page_name      = "Edit Mail Format";
	$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	
    $id = $_REQUEST['id'];
    $sql= "SELECT id,mail_format_name,mail_format_subject,mail_format_body,site_id FROM mail_format WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);

    $id= $row_edit['id'];
    $mail_format_name= $row_edit['mail_format_name'];
    $mail_format_subject= $row_edit['mail_format_subject'];
    $mail_format_body= $row_edit['mail_format_body'];
	$site_id		= $row_edit['site_id'];
}
############################################## END EDIT ##############################################


############################################## START UPDATE ############################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){ 
	$id= $_REQUEST['id'];
    $mail_format_name	= $_REQUEST['mail_format_name'];
    $mail_format_subject = $_REQUEST['mail_format_subject'];
    $mail_format_body = addslashes(fck::addAbsolutePaths($_REQUEST['mail_format_body']));
	$site_id			= (isset($_SESSION['global_site_id']) && $_SESSION['global_site_id'] !="") ? $_SESSION['global_site_id'] : $_REQUEST['site_id'];

    $sql= "UPDATE mail_format SET mail_format_name = '$mail_format_name', mail_format_subject = '$mail_format_subject', mail_format_body = '$mail_format_body',site_id = '$site_id' 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;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
    //header("Location: $file_name&message=$tmp updated sucessfully$query_string");
	$_SESSION['info_message'] = "$tmp updated sucessfully";
    header("Location: $file_name$query_string");
    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 box-primary">
                <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="Mail format name" class="control-label">Mail format name</label>
                      <div class="">
                        <input type="text" id="mail_format_name" name="mail_format_name" value="<?php echo $mail_format_name;?>" class="form-control" placeholder="Mail format name">
                      </div>
                    </div>
					<div class="form-group">
                      <label for="Mail format subject" class="control-label">Mail format subject</label>
                      <div class="">
                        <input type="text" id="mail_format_subject" name="mail_format_subject" value="<?php echo $mail_format_subject;?>" class="form-control" placeholder="Mail format subject">
                      </div>
                    </div>
					<div class="form-group">
                      <label for="Mail format body" class="control-label">Mail format body<br><b style="color:red;">
						Please don't change the text between [ ] like [name].<br>
						This is using as a placeholder in the mail.</b></label>
                      <div class="">
						<textarea class="form-control" id="mail_format_body" name="mail_format_body" rows="3" placeholder="Mail format body"><?php echo $mail_format_body;?></textarea>
                      </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="fas fa-sync-alt fa-spin" style="display:none;" id="page_load_div"></i>
					</button>
                  </div>
                </form>
              </div><!-- /.box -->
		</div>
	</div>
	</div>
</section>
<script>
CKEDITOR.replace('mail_format_body');  
$('#form1').submit(function(){
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
	});
</script>