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

################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
    utility::addslashesOnAllInput(array("query_string"));
	
    $sms_name = $_REQUEST['sms_name'];
    $sms_track = $_REQUEST['sms_track'];
    $sending_trigger = $_REQUEST['sending_trigger'];
    $sms_discription = $_REQUEST['sms_discription'];
    $sms_status = $_REQUEST['sms_status'];
    $created_on = date("Y-m-d");
    $updated_by = $_SESSION["user_id"];

    $sql= "INSERT INTO $table_name (sms_name,sms_track,sending_trigger,sms_discription,sms_status,created_on,updated_by) VALUES('$sms_name','$sms_track','$sending_trigger','$sms_discription','$sms_status','$created_on','$updated_by')";
    mysqli_query($conn,$sql);

    $_SESSION['info_message'] = "SMS 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 SMS 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);

    $sms_name= $row_edit['sms_name'];
    $sms_track= $row_edit['sms_track'];
    $sending_trigger= $row_edit['sending_trigger'];
    $sms_discription= $row_edit['sms_discription'];
    $sms_status= $row_edit['sms_status'];
}
################################################# END EDIT #################################################


################################################## START UPDATE ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){ 
	utility::addslashesOnAllInput(array("query_string"));
	
	$id = $_REQUEST['id'];
    $sms_name = $_REQUEST['sms_name'];
    $sms_track = $_REQUEST['sms_track'];
    $sending_trigger = $_REQUEST['sending_trigger'];
    $sms_discription = $_REQUEST['sms_discription'];
    $sms_status = $_REQUEST['sms_status'];
    $updated_by = $_SESSION["user_id"];

    $sql= "UPDATE $table_name SET sms_name = '$sms_name', sms_track = '$sms_track', sending_trigger = '$sending_trigger', sms_discription = '$sms_discription', sms_status = '$sms_status', 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_sms_schedule", "page=view_sms_schedule", $query_string);
    $query_string = "&".$query_string;

    $_SESSION['info_message'] = "SMS 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_sms_schedule&action=<? echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
                  <div class="card-body">
                    <div class="form-group">
                      <label for="SMS Name" class="control-label">SMS Name</label>
                      <div class="">
                        <input type="text" id="sms_name" name="sms_name" value="<?php echo $sms_name;?>" class="form-control" placeholder="SMS Name">
                      </div>
                    </div>
					<div class="form-group">
                      <label for="SMS Track" class="control-label">SMS Track</label>
                      <div class="">
                        <? $gsi->getSMSTrackList("sms_track",$sms_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="Mail format body" class="control-label">&nbsp;</label>
						<div class="">
							<span style="color:red;font-weight:bold;">Messages are charged at &pound;0.06 per 160 characters sent: (We no longer require STOP messages to be added where SMS messages are being sent in response to direct enquiries). Longer messages can be sent however the cost will be per 160 character block required.</span>
						</div>
					</div>
					<div class="form-group">
                      <label for="SMS Description" class="control-label">SMS Description</label>
                      <div class="">
                        <textarea name="sms_discription" class="form-control" rows="4" value="<?php echo $sms_discription;?>" placeholder="SMS Description"><?=$sms_discription?></textarea> 
                      </div>
                    </div>
					<div class="form-group">
                      <label for="Mail Status" class="control-label">Mail Status</label>
                      <div class="">
                        <? $gsi->getEmailStatusList("sms_status",$sms_status,"100%",false,"form-control"); ?>
                      </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>
</section>
<script type="text/javascript">
$(function(){
	$("#form1").submit(function() {
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
		//return true;
	});
});
</script>