<?php 
session_start();
include_once 'config.php';
include_once 'auth.php';
$user_id = $_SESSION['Admin']['id'];
$MainPage="Master";
$Page = "Referral-Letter";
?>
<!DOCTYPE html>
<html lang="en" class="default-style">
<head>
<title><?php echo $Proj_Title; ?> - Referral Letter</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
<meta name="description" content="">
<meta name="author" content="" />

<?php include_once 'header_script.php'; ?>
<script src="ckeditor/ckeditor.js"></script>
</head>
<body>
<style type="text/css">
  .password-tog-info {
  display: inline-block;
cursor: pointer;
font-size: 12px;
font-weight: 600;
position: absolute;
right: 50px;
top: 30px;
text-transform: uppercase;
z-index: 2;
}


</style>
<div class="layout-wrapper layout-1 layout-without-sidenav">
        <div class="layout-inner">
         
         <?php include 'sidenavi.php'; ?>

 <?php 
 $query = "SELECT * FROM tbl_letter_content WHERE id = '1'";
$row = getRecord($query);

$id = $_GET['id'];
 $query2 = "SELECT * FROM tbl_referral_letter WHERE id = '$id'";
$row7 = getRecord($query2);

if(isset($_POST['submit'])){
   $PatientId = addslashes(trim($_REQUEST['PatientId']));
   $MrdNo = addslashes(trim($_REQUEST['MrdNo']));
   $PatientName = addslashes(trim($_REQUEST['PatientName']));
   $Age = addslashes(trim($_REQUEST['Age']));
   $Gender = addslashes(trim($_REQUEST['Gender']));
   $Address = addslashes(trim($_REQUEST['Address']));
   $Details = addslashes(trim($_REQUEST['Details']));
   $CreatedDate = date('Y-m-d');
   $Status = 1;
   $ModifiedBy = 0;
   $ModifiedDate = null;
    if($_GET['id']==''){
        try {
   $sql = "INSERT INTO tbl_referral_letter (PatientId,MrdNo,PatientName,Age,Gender,Address,Details,Status,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate) VALUES (:PatientId,:MrdNo,:PatientName,:Age,:Gender,:Address,:Details,:Status,:CreatedBy,:CreatedDate,:ModifiedBy,:ModifiedDate)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':PatientId', $PatientId);
$stmt->bindParam(':MrdNo', $MrdNo);
$stmt->bindParam(':PatientName', $PatientName);
$stmt->bindParam(':Age', $Age);
$stmt->bindParam(':Gender', $Gender);
$stmt->bindParam(':Address', $Address);
$stmt->bindParam(':Details', $Details);
$stmt->bindParam(':Status', $Status);
$stmt->bindParam(':CreatedBy', $user_id);
$stmt->bindParam(':CreatedDate', $CreatedDate);
$stmt->bindParam(':ModifiedBy', $ModifiedBy);
$stmt->bindParam(':ModifiedDate', $ModifiedDate);
if ($stmt->execute()) {
 echo "<script>alert('Refferal Letter Created Successfully!');window.location.href='view-referral-letter.php';</script>";
}
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
}
    else{

         try{
  $query2 = "UPDATE tbl_referral_letter SET PatientId='$PatientId',MrdNo='$MrdNo',PatientName='$PatientName',Age='$Age',Gender='$Gender',Address='$Address',Details='$Details',ModifiedDate='$CreatedDate',ModifiedBy='$user_id' WHERE id = '$id'";
  $conn->query($query2);
       echo "<script>alert('Refferal Letter Updated Successfully!');window.location.href='view-referral-letter.php';</script>";
       }
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

    }

}

?>        

<div class="layout-container">
<div class="layout-content">

<div class="container-fluid flex-grow-1 container-p-y">
<h4 class="font-weight-bold py-3 mb-0">Referral Letter
<!-- <span style="float: right;">
<a href="create-optical-receipt.php" class="btn btn-secondary btn-round"><i class="ion ion-md-add mr-2"></i> Create New Receipt</a></span><br> -->
</h4>
<div class="card mb-4">
<div class="card-body">
<div id="alert_message"></div>
<form id="validation-form" method="post" autocomplete="off" enctype="multipart/form-data">
<div class="form-row">

<div class="form-group col-md-12">
 <label class="floating-label" for="Name">Search Patients <span class="text-danger">*</span></label>
<input class="form-control" list="DataList2" name="PatientId" id="PatientId" value="<?php echo $row7['PatientId']; ?>">
        <datalist id="DataList2">
           <?php 
           $CreatedDate = 'all';
             //$sql4 = "CALL patient_read('$CreatedDate')";
            $sql4 = "SELECT MrdNo,PatientName,ConsultantName,Phone FROM tbl_patients";
            $row4 = getList($sql4);
            foreach($row4 as $result){
                if($result['Phone'] == ''){
                    $PatientName = $result['MrdNo']." / ".$result['PatientName'];
                }
                else{
                    $PatientName = $result['MrdNo']." / ".$result['PatientName']."/".$result['Phone'];
                }
          ?>
        <option value="<?php echo $PatientName; ?>">
       <?php } ?> 
    </datalist>
</div>

<div class="form-group col-md-3">
<label class="form-label">Mrd No <span class="text-danger">*</span></label>
<input type="text" name="MrdNo" id="MrdNo" class="form-control" placeholder="" value="<?php echo $row7['MrdNo']; ?>" readonly>
</div>

<div class="form-group col-md-9">
<label class="form-label">Patient Name <span class="text-danger">*</span></label>
<input type="text" name="PatientName" id="PatientName" class="form-control" placeholder="" value="<?php echo $row7['PatientName']; ?>" required>
</div>

<div class="form-group col-md-6">
<label class="form-label">Age <span class="text-danger">*</span></label>
<input type="text" name="Age" id="Age" class="form-control" placeholder="" value="<?php echo $row7['Age']; ?>" required>
</div>

<div class="form-group col-md-6">
<label class="form-label">Gender <span class="text-danger">*</span></label>
  <select class="form-control" id="Gender" name="Gender" required>
   <option value="" disabled="" selected="">Select Gender</option>
    <option value="Male" <?php if($row7['Gender'] == 'Male'){?> selected <?php } ?>>Male</option>
    <option value="Female" <?php if($row7['Gender'] == 'Female'){?> selected <?php } ?>>Female</option>
  </select>
</div>

<div class="form-group col-md-12">
<label class="form-label">Address <span class="text-danger">*</span></label>
<input type="text" name="Address" id="Address" class="form-control" placeholder="" value="<?php echo $row7['Address']; ?>" required>
</div>

  <div class="col-sm-12">
    <div class="form-group">
      <label class="floating-label" for="Name">Details <span class="text-danger">*</span></label>
        <textarea class="form-control" name="Details" id="editor1" placeholder="" required=""><?php echo $row['Details'];?></textarea>
    </div>
  </div>



</div>
<!-- <button id="growl-default" class="btn btn-default">Default</button> -->
<button type="submit" name="submit" class="btn btn-primary btn-finish" id="submit">Save</button>
</form>
</div>
</div>






</div>


<?php include_once 'footer.php'; ?>
</div>

</div>

</div>

<div class="layout-overlay layout-sidenav-toggle"></div>
</div>


<?php include_once 'footer_script.php'; ?>
 <script>
        CKEDITOR.replace( 'editor1');

  $(document).ready(function(){
        $(document).on("change", "#PatientId", function(event){
  var val = this.value;
  if(val == 'Search Patient'){
$('.showoldfields').hide();
  }
  else{
    $('.showoldfields').show();
  }
   var action = "getPatientDetails";
    $.ajax({
    url:"ajax_files/ajax_queue_management.php",
    method:"POST",
    data : {action:action,id:val},
    dataType:"json",  
    success:function(data)
    {
        console.log(data);
        $('#MrdNo').val(data.MrdNo);
        $('#PatientName').val(data.PatientName);
        $('#Age').val(data.Age);
        $('#Gender').val(data.Gender).attr("selected","selected");
        $('#Address').val(data.Address);
        
    }
    });

 });
         });
</script>
</body>
</html>
