<?php 
session_start();
include_once '../config.php';
$user_id = $_SESSION['Admin']['id'];
$CurrDate = date('dmY');

if($_REQUEST['action'] == 'Save'){
$Name = addslashes(trim($_REQUEST['Name']));
$Details = addslashes(trim($_REQUEST['Details']));
$Status = addslashes(trim($_REQUEST['Status']));
$CreatedDate = date('Y-m-d');

	/*$sql = "INSERT INTO tbl_surgery_notes SET Name='$Name',Details='$Details',Status='$Status',CreatedBy='$user_id',CreatedDate='$CreatedDate'";
	$conn->query($sql);
	echo 1;*/

  try {
   $sql = "INSERT INTO tbl_surgery_notes (Name,Details,Status,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate) VALUES (:Name,:Details,:Status,:CreatedBy,:CreatedDate,:ModifiedBy,:ModifiedDate)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':Name', $Name);
$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 1;
}
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
}

if($_POST['action'] == 'fetch_record'){
 $id = $_POST['id'];
    $query = "SELECT * FROM tbl_surgery_notes WHERE id = '$id'";
   $row = getRecord($query);
    echo json_encode($row);


}

if($_POST['action'] == 'Edit') {
     $id = $_POST['id'];
$Name = addslashes(trim($_REQUEST['Name']));
$Details = addslashes(trim($_REQUEST['Details']));
$Status = addslashes(trim($_REQUEST['Status']));
$ModifiedDate = date('Y-m-d');

try{
  $query2 = "UPDATE tbl_surgery_notes SET Name='$Name',Details='$Details',Status='$Status',ModifiedDate='$ModifiedDate',ModifiedBy='$user_id' WHERE id = '$id'";
 	$conn->query($query2);
  echo 1;
  }
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

}

  if($_POST['action'] == 'delete') {
   
      $id = $_POST['id'];
      $query = "DELETE FROM tbl_surgery_notes WHERE id = '$id'";
      $conn->query($query);
      echo "Delete Successfully";

  }

 if($_POST['action']=='view'){?>
         <table id="example" class="table-striped table-bordered">
        <thead>
            <tr>
             <th>Sr.No.</th>
             <th>Type</th>
             <th>Notes</th>
             <th>Status</th>
             <th>Action</th>
            </tr>
        </thead>
        <tbody>
          <?php 
 $srno = 1;
  $sql = "SELECT * FROM tbl_surgery_notes ORDER BY id DESC";
 $rx = getList($sql);
  foreach($rx as $nx){

  ?>
           <tr>
             <td><?php echo $srno; ?></td>
           
              <td><?php echo $nx['Name']; ?></td>
              <td><?php echo $nx['Details']; ?></td>
       <td><?php if($nx['Status']=='1'){echo "<span style='color:green;'>Active</span>";} else { echo "<span style='color:red;'>Inactive</span>";} ?></td>
             <td><a href='add-ot-notes.php?id=<?php echo $nx['id']; ?>' data-toggle="tooltip" data-placement="top" title="Edit" data-original-title="Edit" class=""><i class="lnr lnr-pencil mr-2"></i></a>&nbsp;&nbsp;<a data-id="<?php echo $nx['id']; ?>" href='javascript:void(0);' data-toggle="tooltip" data-placement="top" title="Delete" data-original-title="Delete" class="delete" id="bootbox-confirm"><i class="lnr lnr-trash text-danger"></i></a>
             </td>
            </tr>
             <?php $srno++;} ?>
        </tbody>
    </table>
    <script type="text/javascript">
      $(document).ready(function() {
      $('#example').DataTable( {
        responsive: true
      });
      });
    </script>
 <?php }
?>