<?
$mobilenos = utility::getTableFieldById("job","phone_no","id",$job_id);
$mobilenos = utility::validate_crypt_data($mobilenos);
$mobilenos = trim($mobilenos);
$mobilenos = str_replace(" ","",$mobilenos);

if(!(isset($mobilenos) && $mobilenos !="")){
	$mobilenos = utility::getTableFieldById("job","landline_phone_no","id",$job_id);
	$mobilenos = utility::validate_crypt_data($mobilenos);
	$mobilenos = trim($mobilenos);
	$mobilenos = str_replace(" ","",$mobilenos);
}
?>
<!---------- Post Out ---------------->
<section class="content">
      <div class="container-fluid">
	  <div class="row">
	  <div class="col-12"> 
<div id="message_div" class="card">
<div class="card-header">
		<h3 class="card-title">Message</h3>

		<div class="card-tools">
			<div class="input-group input-group-sm" style="width: 150px;">
				<a href="" data-toggle="modal" data-backdrop="static"  data-target="#myModalAjaxforMessage" alt="<?=$_REQUEST['id'];?>" class="btn btn-primary add_new_message" style="float:right;margin-right:5px;margin-bottom:5px;">
		Send Message</a>
			</div>
		</div>
	</div>



	
			<div class="card-body table-responsive" style="height: auto;">
				<div class="box box-primary" id="overflow_div">
					<!-- Start  records header -->
					<table id="message_table" class="table table-bordered table-striped">
						<thead>
							<tr>
								<th>Job Id</th>
								<th>Message Name</th>
								<th style="width:30%;">Message</th>
								<th>Created By</th>
								<th>Date</th>
								<th>Edit</th>
								<th>Delete</th>
							</tr>
						</thead>
					</table>
				</div>
			</div>
		
	<!---------- Post Out ---------------->
</div>
</div>
</div>
</div>
</section>

<!-- Modal -->
<div class="modal fade" id="myModalAjaxforMessage" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <div class="col-lg-12">
		<div class="row">
			<div class="col-lg-8">
				<h4 class="modal-title" id="myModalLabelAjax">Modal title</h4>
			</div>
			<div class="col-lg-4">
				<button type="button" class="close close_button2" data-dismiss="modal" aria-hidden="true">&times;</button>
			</div>
		</div>
		</div>
      </div>
      <div class="modal-body modal_iframe2">
			<section class="content">
				<div class="row">
					<div class="col-lg-12">
						<div class="box box-primary">
						<div class="alert alert-info alert-dismissable alert_message" style="display:none;">
							<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
						</div>
							<div class="box-body">
							<form action="" method="post" name="add_form_message" id="add_form_message">
								<div class="col-lg-12">Message will send to <?=$mobilenos;?></div>
								<div class="col-lg-12">
								  <label for="Email Name" class="control-label">Message Name</label>
									<? 
										$gsi->getMessageTemplateList("message_id",$message_id,"100%",false,"form-control select2"); 
									?>
								</div>
								<div class="col-lg-12">
									<label for="Description" class="control-label">Message</label>
									<textarea class="form-control" rows="4" id="message_text" name="message_text" required><?php echo stripslashes($message_text);?></textarea>
								</div>
								<div class="col-lg-12 hide">
									<label for="Note" class="control-label">Date</label>
									<input type="text" class="form-control" id="message_date" name="message_date" value="<?php (isset($message_date) && $message_date !="") ? utility::getFormatedDate($message_date) : utility::getFormatedDate(date($config->dateFormat));?>" required>
								</div>							
								<div style="clear:both;margin-top:13px;"></div>
								<div class="col-lg-12">
									<input type="hidden" id="msg_id" name="id" value="">
									<input type="hidden" id="job_id" name="job_id" value="<?=$job_id;?>">
									<input type="hidden" id="page_action_message" name="page_action_message" value="add">
									<button type="submit" name="submit_form_message" id="submit_form_message" class="btn btn-primary">Add</button>
									<i class="fa fa-refresh fa-spin" style="display:none;" id="page_load_div_for_message"></i>
									<i class="fa fa-check" id="message_done" style="color:#bad23a;font-size: 20px;display:none;"></i>
								</div>
							</form>
							</div>
						</div>
					</div>
				</div>
			</section>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default close_button2" data-dismiss="modal">Close</button>
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<script type="text/javascript">		
$(document).ready(function() {
	$("#message_id").change(function(){
		var message_id = $(this).val();
		var job_id = $("#job_id").val();
		var form_data   = {'message_id':message_id};       
		$.ajax({
			async: false,
			type: "POST",
			url: site_url+"/modules/mail_schedule/index.php?page=ajax_actions&action=load_message_template",
			data: form_data,
			success: function(result){
					var resultObj = jQuery.parseJSON(result); 
					$("textarea#message_text").val(resultObj.message_body);
					//CKEDITOR.instances['mail_body'].setData(resultObj.mail_body)
			}
		});
	});
});
</script>

<script type="text/javascript">
var flag_for_table = 0;
$(function () {
	/*********************************** Post Out By Ajax and JSON *************************/
	var job_id = "<?=$_REQUEST['id'];?>";
	
	$('#message_table').DataTable( {
        "processing": true,
        "serverSide": true,
		"paging": false,
		"lengthChange": false,
		"searching": false,
		"ordering": false,
		"info": false,
		"autoWidth": false,
        "ajax": "<?php echo $siteURL?>/message/index?page=ajax_message&job_id="+job_id,
        "columns": [
			{ "data": "job_id" },
			{ "data": "message_name" },
			{ "data": "message_text" },
			{ "data": "message_by" },
            { "data": "message_date" },
			{
				sortable: false,
                 "render": function ( data, type, full, meta ){
                     var buttonID = full.id;
                     return '<span style="cursor:pointer;" data-toggle="modal" data-backdrop="static"  data-target="#myModalAjaxforMessage" class="message_edit" alt="'+buttonID+'"><i class="fa fa-edit" style="font-size:20px;"></i></span>';
                 }
            },
			{
				sortable: false,
                 "render": function ( data, type, full, meta ){
                     var buttonID = full.id;
                     return '<span style="cursor:pointer;" class="message_remove" alt="'+buttonID+'"><i class="fa fa-trash-o" style="font-size:20px;"></i></span><i class="fa fa-refresh fa-spin" style="display:none;" id="delete_loading_message_'+buttonID+'"></i>';
                 }
            }
        ]
    });
	$("#message_div").on("click",".add_new_message",function(){
		flag_for_table = 0;
		$("#add_form_message")[0].reset();
		$("#myModalLabelAjaxMessage").text("Message");
		$("#page_action_message").val("add");
		$("#submit_form_message").html("Add");
		var job_id = $(this).attr("alt");	
		$("#myModalAjaxforMessage").children().find("#job_id").val(job_id);
	});	
	$("#message_table").on("click",".message_edit",function(){	
		$("#myModalLabelAjaxMessage").text("Edit Message");
		var id = $(this).attr("alt");		
		var form_data   = {'id':id};
		$.ajax({
			async: true,
			type: "POST",
			url: "<?php echo $siteURL?>/message/index?page=ajax_message&action=get_edit_data_message",
			data: form_data,
			success: function(result){
				var resultObj = jQuery.parseJSON(result); 
				if(resultObj.status == "success"){
					$.each(resultObj.data_array,function(data_array_key,data_array_val){
						if(data_array_key == "job_id"){
							$("#myModalAjaxforMessage").children().find("#job_id").val(data_array_val);
						}else if(data_array_key == "message_id"){
							$("#myModalAjaxforMessage").children().find("#"+data_array_key).select2("val",data_array_val);
						}
						$("#"+data_array_key).val(data_array_val);
					});
					$("#msg_id").val(id);
					$("#page_action_message").val("update");
					$("#submit_form_message").html("Update");
				}
			}
		});	
	});
	var post_out_t = $('#message_table').DataTable();
	$('#message_table tbody').on('click', '.message_remove',function(){
		if(confirm("Do you want to delete.")){
			var message_id = $(this).attr("alt");
			$("#delete_loading_message_"+message_id).show();
			var form_data   = {'message_id':message_id};
			$.ajax({
				async: true,
				type: "POST",
				url: "<?php echo $siteURL?>/message/index?page=ajax_message&action=delete_message",
				data: form_data,
				success: function(result){
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "success"){
						$("#delete_loading_message_"+message_id).hide();
						flag_for_table = 1;
						post_out_t.draw( false );
					}
				}
			});	
		}
	});
	
	$("#myModalAjaxforMessage").on("click",".close_button2",function(){
		//alert(flag_for_table);
		if(flag_for_table == 1){
			flag_for_table = 0;
			post_out_t.draw( true );
		}
	});
	/******************************* End Post Out By Ajax and JSON *************************/
});

$(document).on('submit','form#add_form_message',function(e){
	var message_done = $(this).find('#message_done');
	message_done.hide();
	var page_load_div_for_message = $(this).find('#page_load_div_for_message');
	page_load_div_for_message.show();
	var submit_form_message = $(this).find("#submit_form_message");
	submit_form_message.attr("disabled", true);
	var add_form_message = $(this);
	
	e.preventDefault(); //prevent Default functionality
	var page_action_message = $("#page_action_message").val();
	var flag = 0;
	if(page_action_message == "add"){
		flag = 1;
	}
	
	var form_data = $('#add_form_message').serialize();     
	$.ajax({
		async: true,
		type: "POST",
		url: "<?php echo $siteURL?>/message/index?page=ajax_message&action=add_message",
		data: form_data,
		success: function(result){
			flag_for_table = 1;
			//alert(flag_for_table);
			if(flag == 1){
				add_form_message[0].reset();  // Reset all form data
				var alert_message = "Message added successfully";
		    }else if(flag == 0){
				var alert_message = "Message updated successfully";
			}
			message_done.show();
			page_load_div_for_message.hide();
			submit_form_message.attr("disabled", false);
			$(".alert_message").show();
			$(".alert_message").html(alert_message);
			setTimeout(function(){
				message_done.hide();
				$(".alert_message").hide();
			},500);
			/* setTimeout(function(){
				$('.close_button2').trigger("click");
			},500); */
		}
	});
});
</script>