<?php
include_once 'includes/processes.php';
$Process = new Login_Process;

$pageLink = basename($_SERVER['REQUEST_URI']);
$Process->check_status($pageLink);

$Process->check_status($pageLink);

if(isset($_GET['st'])) {
	$dbLink = $Process->connect_db();
	if($_GET['st']=="close"){
		$caseData = $Process->getCaseInfo($_GET['lid']);
		if($caseData->log_status=="Empty") {
			$sql  = "delete from apl_case_log where log_id = '".$_GET['lid']."'";
			$runQ = $Process->RunQuery($sql,$dbLink);
			header("location:case-logger.php");
		}else{
			$sql  = "update apl_case_log set log_status = 'Closed', close_agent_id = '".$_SESSION['userid']."', time_closed = '".time()."' where log_id = '".$_GET['lid']."'";
			$runQ = $Process->RunQuery($sql,$dbLink);
			header("location:case-logger.php?st=1");
		}
	}elseif($_GET['st']=="open") {
		$sql  = "update apl_case_log set log_status = 'Open', close_agent_id = '0', time_closed = '0' where log_id = '".$_GET['lid']."'";
		$runQ = $Process->RunQuery($sql,$dbLink);
	}
}

$caseData = $Process->getCaseInfo($_GET['lid']);

$getOpenAgentData = $Process->getEmployeeInfo($caseData->open_agent_id);
$openAgentName = $getOpenAgentData->fullname;
$getCloseAgentData = $Process->getEmployeeInfo($caseData->close_agent_id);
$closeAgentName = $getCloseAgentData->fullname;
?>
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
  <title>#<?php echo $caseData->log_id; ?> Case Log - APL Portal</title>

  <meta charset="utf-8">
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">

  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,700italic,400,600,700">
  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,300,700">
  <link rel="stylesheet" href="./css/font-awesome.min.css">
  <link rel="stylesheet" href="./js/libs/css/ui-lightness/jquery-ui-1.9.2.custom.min.css">
  <link rel="stylesheet" href="./css/bootstrap.min.css">

  <!-- App CSS -->
  <link rel="stylesheet" href="./css/target-admin.css">
  <link rel="stylesheet" href="./css/custom.css">


  <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
  <![endif]-->
<script>

function openModal() {
	document.getElementById('modal').style.display = 'block';
}

function closeModal() {
    document.getElementById('modal').style.display = 'none';
}

function saveLog() {
	openModal();
	var log_id = document.getElementById("callLogger").elements.namedItem("log_id").value;
	//alert('hey here');
	var case_category = document.getElementById("callLogger").elements.namedItem("case_category").value;
	var caller_name = document.getElementById("callLogger").elements.namedItem("caller_name").value;
	var caller_phone = document.getElementById("callLogger").elements.namedItem("caller_phone").value;
	var caller_email = document.getElementById("callLogger").elements.namedItem("caller_email").value;
	var case_pnr = document.getElementById("callLogger").elements.namedItem("case_pnr").value;
	var case_description = document.getElementById("callLogger").elements.namedItem("case_description").value;
	var case_comment = document.getElementById("callLogger").elements.namedItem("case_comment").value;
	var case_dept = document.getElementById("callLogger").elements.namedItem("case_dept").value;

	var currentDept = <?php echo $caseData->log_dept; ?>



    //if (str == "") {
    //    document.getElementById("txtHint").innerHTML = "";
    //    return;
   // } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
				closeModal();
                document.getElementById("updateResult").innerHTML = this.responseText;
				$("#comment_section").load("<?php echo $_SERVER['PHP_SELF']; ?>?lid=<?php echo $_GET['lid']; ?> #comment_section");
				$("#case_comment").val('');
            }
        };
        xmlhttp.open("GET","update-case-log.php?log_id="+log_id+"&case_category="+case_category+"&caller_name="+caller_name+"&caller_phone="+caller_phone+"&caller_email="+caller_email+"&case_pnr="+case_pnr+"&case_description="+case_description+"&case_comment="+case_comment+"&case_dept="+case_dept,true);
        xmlhttp.send();
    //}
	//disble fields if case has been moved to another department
	if(case_dept!=currentDept){
		document.getElementById("callLogger").elements.namedItem("case_category").disabled = true;
		document.getElementById("caller_name").disabled = true;
		document.getElementById("caller_phone").disabled = true;
		document.getElementById("caller_email").disabled = true;
		document.getElementById("case_description").disabled = true;
		document.getElementById("case_comment").disabled = true;
		document.getElementById("case_dept").disabled = true;
		document.getElementById("save_case").remove();
		document.getElementById("close_case").remove();
	}
}
</script>
<style>
body, html {
    margin:0;
    padding;
    height:100%
}

#content {
    padding:25px;
}

#fade {
    display: none;
    position:absolute;
    top: 0%;
    left: 0%;
    width: 100%;
    height: 100%;
    background-color: #ababab;
    z-index: 1001;
    -moz-opacity: 0.8;
    opacity: .70;
    filter: alpha(opacity=80);
}

#modal {
    display: none;
    position: absolute;
    top: 45%;
    left: 45%;
    width: 64px;
    height: 64px;
    padding:10px 5px 0px;
    border: 3px solid #ababab;
    box-shadow:1px 1px 10px #ababab;
    border-radius:20px;
    background-color: white;
    z-index: 1002;
    text-align:center;
}

</style>
</head>

<body>

<?php include'includes/header.php'; ?>

<div class="container">

  <div class="content">

    <div class="content-container">



      <div class="content-header">
        <h2 class="content-header-title">Case Logger</h2>
        <ol class="breadcrumb">
          <li><a href="./">Home</a></li>
          <li><a href="case-logger.php">Case Logger</a></li>
          <li class="active">Case Log / <span style="color:red;">opened:<?php echo $openAgentName;?> (<?php echo date("d-m-Y : H:ma",$caseData->time_opened); ?>)</span></li>
					<li class="pull-right"><a href="case-logger.php" class="btn btn-warning pull-right"><i class="fa fa-reply"></i> Back to Case Logger</a></li>
				</ol>

      </div> <!-- /.content-header -->



      <div class="row">
        <div class="col-md-12">
			<?php //echo $addAccessRole; ?>
		</div>
		 <form  name="callLogger" id="callLogger" method="get">
			<input type="hidden" name="log_id" value="<?php echo $caseData->log_id; ?>" />
        <div class="col-md-8">

		 <div class="portlet">

            <div class="portlet-header">

              <h3>
                <i class="fa fa-file"></i>
                Case Info
              </h3>
            </div> <!-- /.portlet-header -->

            <div class="portlet-content">
				<div class="form-group col-lg-3 col-md-3 col-sm-12">
					<label for="item_desc">Case Ref</label>
					<input type="text" value="#<?php echo $caseData->log_id; ?>" class="form-control" disabled />
				</div>
				<div class="col-sm-12 col-lg-4 col-md-4">
				  <label for="case_category">Case Category</label>
				  <select name="case_category" id="case_category" class="form-control" <?php if($caseData->log_status=="Closed" || !in_array("log_case",$_SESSION['privileges']) || $caseData->log_dept!=$_SESSION['dept_id']){ echo "disabled"; } ?>>
					<option value="">-- category --</option>
					<option value="Refund" <?php if($caseData->log_category=="Refund"){echo"selected";} ?>>Refund</option>
					<option value="Debit" <?php if($caseData->log_category=="Debit Issue"){echo"selected";} ?>>Debit</option>
					<option value="Name Change" <?php if($caseData->log_category=="Name Change"){echo"selected";} ?>>Name Change</option>
					<option value="Date Change" <?php if($caseData->log_category=="Date Change"){echo"selected";} ?>>Date Change</option>
					<option value="Distruption" <?php if($caseData->log_category=="Distruption"){echo"selected";} ?>>Distruption</option>
					<option value="Open Ticket" <?php if($caseData->log_category=="Open Ticket"){echo"selected";} ?>>Open Ticket</option>
					<option value="Reservation" <?php if($caseData->log_category=="Reservation"){echo"selected";} ?>>Reservation</option>
					<option value="Missing Item" <?php if($caseData->log_category=="Missing Item"){echo"selected";} ?>>Missing Item</option>
					<option value="Website not Opening" <?php if($caseData->log_category=="Website not Opening"){echo"selected";} ?>>Website not Opening</option>
					<option value="Unable to pay online" <?php if($caseData->log_category=="Unable to pay online"){echo"selected";} ?>>Unable to pay online</option>
					<option value="Unclear Calls" <?php if($caseData->log_category=="Unclear Calls"){echo"selected";} ?>>Unclear Calls</option>
					<option value="Other Issues" <?php if($caseData->log_category=="Other Issues"){echo"selected";} ?>>Other Issues</option>
				  </select>
				</div> <!-- /.col -->
				<div class="form-group col-lg-5 col-md-5 col-sm-12">
					<label for="item_desc">Caller Name</label>
					<input type="text" id="caller_name" name="caller_name" value="<?php echo $caseData->caller_name; ?>" class="form-control" <?php if($caseData->log_status=="Closed" || !in_array("log_case",$_SESSION['privileges']) || $caseData->log_dept!=$_SESSION['dept_id']){ echo "disabled"; } ?> required />
				</div>
				<div class="form-group col-lg-4 col-md-4  col-sm-12">
					<label for="case_pnr">Case PNR <span style="font-size:10px;color:#a01b16">(if applicable)</span></label>
					<input type="text" id="case_pnr" name="case_pnr" value="<?php echo $caseData->case_pnr; ?>" class="form-control" <?php if($caseData->log_status=="Closed" || !in_array("log_case",$_SESSION['privileges']) || $caseData->log_dept!=$_SESSION['dept_id']){ echo "disabled"; } ?> />
				</div>
				<div class="form-group col-lg-4 col-md-4  col-sm-12">
					<label for="caller_phone">Caller Phone Number</label>
					<input type="text" id="caller_phone" name="caller_phone" value="<?php echo $caseData->caller_phone; ?>" class="form-control" <?php if($caseData->log_status=="Closed" || !in_array("log_case",$_SESSION['privileges']) || $caseData->log_dept!=$_SESSION['dept_id']){ echo "disabled"; } ?> required />
				</div>
				<div class="form-group col-lg-4 col-md-4 col-sm-12">
					<label for="item_desc">Caller Email</label>
					<input type="email" id="caller_email" name="caller_email" value="<?php echo $caseData->caller_email; ?>" class="form-control" <?php if($caseData->log_status=="Closed" || !in_array("log_case",$_SESSION['privileges']) || $caseData->log_dept!=$_SESSION['dept_id']){ echo "disabled"; } ?> />
				</div>
			</div> <!-- /.portlet-content -->

          </div> <!-- /.portlet -->


        </div> <!-- /.col -->

        <div class="col-md-4">
		 <div class="portlet">
            <div class="portlet-header">
              <h3>
                <i class="fa fa-file"></i>
                Case details
              </h3>
            </div> <!-- /.portlet-header -->
            <div class="portlet-content">
				<div class="form-group col-sm-12">
					<label for="case_description">Case Description</label>
					<textarea name="case_description" id="case_description" rows="4" class="form-control" required <?php if($caseData->log_status=="Closed" || !in_array("log_case",$_SESSION['privileges']) || $caseData->log_dept!=$_SESSION['dept_id']){ echo "disabled"; } ?>><?php echo $caseData->case_description; ?></textarea>
            </div> <!-- /.portlet-content -->
          </div> <!-- /.portlet -->
        </div> <!-- /.col -->
        </div> <!-- /.col -->

			<div class="col-md-6">
			 <div class="portlet">

				<div class="portlet-header">

				  <h3>
					<i class="fa fa-pencil"></i>
					Add comment to this case
				  </h3>
				</div> <!-- /.portlet-header -->

				<div class="portlet-content">
					<div class="form-group col-sm-12">
						<textarea name="case_comment" id="case_comment" class="form-control" placeholder="input comment here" <?php if($caseData->log_status=="Closed" || $caseData->log_dept!=$_SESSION['dept_id']){ echo "disabled"; } ?>></textarea>
					</div>
				</div> <!-- /.portlet-content -->

			  </div> <!-- /.portlet -->
			 <div class="portlet">

				<div class="portlet-header">

				  <h3>
					<i class="fa fa-mail-forward "></i>
					Forward case to department
				  </h3>
				  <ul class="portlet-tools pull-right">
					<li>
					  <a data-toggle="modal" href="case-history.php?lid=<?php echo $_GET['lid']; ?>" data-toggle="modal" data-target="#fhModal" class="btn btn-sm btn-default">
						history
					  </a>
					</li>
				  </ul>
				</div> <!-- /.portlet-header -->

				<div class="portlet-content">
					<div class="col-sm-12 col-lg-12 col-md-12">
					  <select name="case_dept" id="case_dept" class="form-control" <?php if($caseData->log_status=="Closed" || $caseData->log_dept!=$_SESSION['dept_id']){ echo "disabled"; } ?>>
						<option value="">-- Department --</option>
						<?php $Process->listDepartment($caseData->log_dept); ?>
					  </select>
					</div> <!-- /.col -->
					<br />
					<br />
				</div> <!-- /.portlet-content -->

			  </div> <!-- /.portlet -->
			</div> <!-- /.col -->

			<div class="col-md-6">

			  <div class="portlet">

				<div class="portlet-header">

				  <h3>
					<i class="fa fa-comment"></i>
					Comments Panel
				  </h3>

				</div> <!-- /.portlet-header -->

				<div class="portlet-content panel-thread scrollable-panel">

				  <ul class="panel-lists" id="comment_section">

					<?php $Process->getCaseLogComment($_GET['lid']); ?>

				  </ul>


				</div> <!-- /.portlet-content -->

			  </div> <!-- /.portlet -->
			  </div> <!-- /.portlet -->

		<div class="col-md-12">

			<?php if($caseData->log_status=="Empty" || $caseData->log_status=="Open"): ?>

				<?php if($caseData->log_dept==$_SESSION['dept_id']): ?>

					<button type="button" name="save" id="save_case" class="btn btn-success"  onclick="saveLog()"><i class="fa fa-save"></i> Save Case</button>


					<?php 	if(in_array("close_case_log",$_SESSION['privileges'])): ?>
					<a href="open-case-log.php?lid=<?php echo $caseData->log_id; ?>&st=close" class="btn btn-danger pull-right" id="close_case" onclick="return confirm('Are you sure you want to close this case');"><i class="fa fa-times"></i> Close Case</a>
					<?php endif; ?>

				<?php else: ?>
					<div class="alert alert-info">
						This Case is been handled by <strong><?php $deptData = $Process->getDepartmentInfo($caseData->log_dept); echo $deptData->dept_name; ?> department</strong>
					</div>
				<?php endif; ?>

			<?php endif; ?>
			<a href="case-logger.php" class="btn btn-warning"><i class="fa fa-reply"></i> Back to Case Logger</a>
			<?php if($caseData->log_status=="Closed"): ?>

				<div class="alert alert-info">
					This Case has been closed since <strong><?php echo date("d M, Y; h:ma",$caseData->time_closed); ?> by <?php echo $closeAgentName; ?></strong>

					<?php 	if(in_array("reopen_case_log",$_SESSION['privileges'])): ?>
					<a href="open-case-log.php?lid=<?php echo $caseData->log_id; ?>&st=open" class="btn btn-danger pull-right" onclick="return confirm('Are you sure you want to re-open this case');">Re-open Case</a>
					<?php endif; ?>

				</div>

			<?php endif; ?>
			<div id="updateResult"><!-- Results are displayed here --></div>
				<div id="modal">
					<img id="loader" src="img/loading.gif" />
				</div>
        </div> <!-- /.col -->

		</form>

      </div> <!-- /.row -->

    </div> <!-- /.content-container -->

  </div> <!-- /.content -->

</div> <!-- /.container -->
	<?php include"includes/footer.php"; ?>


	<div id="fhModal" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">



    </div><!-- /.modal-content -->
	 </form>
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


 <script src="./js/libs/jquery-1.10.1.min.js"></script>
  <script src="./js/libs/jquery-ui-1.9.2.custom.min.js"></script>
  <script src="./js/libs/bootstrap.min.js"></script>

  <!--[if lt IE 9]>
  <script src="./js/libs/excanvas.compiled.js"></script>
  <![endif]-->

  <!-- Plugin JS -->
  <script src="./js/plugins/icheck/jquery.icheck.js"></script>
  <script src="./js/plugins/select2/select2.js"></script>
  <script src="./js/plugins/datepicker/bootstrap-datepicker.js"></script>
  <script src="./js/plugins/timepicker/bootstrap-timepicker.js"></script>
  <script src="./js/plugins/simplecolorpicker/jquery.simplecolorpicker.js"></script>
  <script src="./js/plugins/autosize/jquery.autosize.min.js"></script>
  <script src="./js/plugins/textarea-counter/jquery.textarea-counter.js"></script>
  <script src="./js/plugins/fileupload/bootstrap-fileupload.js"></script>
  <script src="./js/plugins/nicescroll/jquery.nicescroll.min.js"></script>


  <!-- App JS -->
  <script src="./js/target-admin.js"></script>

  <!-- Plugin JS -->
  <script src="./js/demos/form-extended.js"></script>
  <script type="text/javascript">
$(document).on("hidden.bs.modal", function (e) {
    $(e.target).removeData("bs.modal").find(".modal-content").empty();
});
</script>
</body>
</html>
