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

if($_REQUEST['action'] == 'Save'){
	$MrdNo = addslashes(trim($_REQUEST['MrdNo']));
	$IpdNo = addslashes(trim($_REQUEST['IpdNo']));
	$PatientName = addslashes(trim($_REQUEST['PatientName']));
	$Amount = addslashes(trim($_REQUEST['Amount']));
	$PayMode = addslashes(trim($_REQUEST['PayMode']));
	$ChequeNo = addslashes(trim($_REQUEST['ChequeNo']));
	$Narration = addslashes(trim($_REQUEST['Narration']));
	$AdmissionDate = addslashes(trim($_REQUEST['AdmissionDate']));
	$AdmissionTime = addslashes(trim($_REQUEST['AdmissionTime']));
	$DepositDate = addslashes(trim($_REQUEST['DepositDate']));
	$DepositTime = addslashes(trim($_REQUEST['DepositTime']));
	$CreatedDate = date('Y-m-d');

	$sql = "INSERT INTO tbl_ipd_receipts SET IpdNo='$IpdNo',MrdNo='$MrdNo',PatientName='$PatientName',AdmissionDate='$AdmissionDate',AdmissionTime='$AdmissionTime',DepositDate='$DepositDate',DepositTime='$DepositTime',Amount='$Amount',PayMode='$PayMode',ChequeNo='$ChequeNo',Narration='$Narration',CreatedBy='$user_id',CreatedDate='$CreatedDate'";
	$conn->query($sql);
$postid = mysqli_insert_id($conn);
$currDate = date('Y-m-d');
$sql2 = "SELECT * FROM tbl_ipd_receipts WHERE CreatedDate='$currDate'";
$rncnt2 = getRow($sql2);  
	echo json_encode(array('status'=>1,'count'=>$rncnt2,'id'=>$postid));
}

if($_REQUEST['action'] == 'fetch_record'){
 $id = $_POST['id'];
    $query = "SELECT * FROM tbl_ipd_receipts WHERE id = '$id'";
    $result = $conn->query($query);
    $row = $result->fetch_assoc();
    echo json_encode($row);


}

if($_REQUEST['action'] == 'Edit') {
	$id = $_REQUEST['id'];
	$MrdNo = addslashes(trim($_REQUEST['MrdNo']));
	$IpdNo = addslashes(trim($_REQUEST['IpdNo']));
	$PatientName = addslashes(trim($_REQUEST['PatientName']));
	$Amount = addslashes(trim($_REQUEST['Amount']));
	$PayMode = addslashes(trim($_REQUEST['PayMode']));
	$ChequeNo = addslashes(trim($_REQUEST['ChequeNo']));
	$Narration = addslashes(trim($_REQUEST['Narration']));
	$AdmissionDate = addslashes(trim($_REQUEST['AdmissionDate']));
	$AdmissionTime = addslashes(trim($_REQUEST['AdmissionTime']));
	$DepositDate = addslashes(trim($_REQUEST['DepositDate']));
	$DepositTime = addslashes(trim($_REQUEST['DepositTime']));
	$CreatedDate = date('Y-m-d');

	$sql = "UPDATE tbl_ipd_receipts SET IpdNo='$IpdNo',MrdNo='$MrdNo',PatientName='$PatientName',AdmissionDate='$AdmissionDate',AdmissionTime='$AdmissionTime',DepositDate='$DepositDate',DepositTime='$DepositTime',Amount='$Amount',PayMode='$PayMode',ChequeNo='$ChequeNo',Narration='$Narration',ModifiedBy='$user_id',ModifiedDate='$CreatedDate' WHERE id = '$id'";
	$conn->query($sql);
$postid = $id;
$currDate = date('Y-m-d');
$sql2 = "SELECT * FROM tbl_ipd_receipts WHERE CreatedDate='$currDate'";
$rncnt2 = getRow($sql2);  
echo json_encode(array('status'=>1,'count'=>$rncnt2,'id'=>$postid));
	}

  if($_REQUEST['action'] == 'delete') {
   
      $id = $_REQUEST['id'];
      $query = "DELETE FROM tbl_ipd_receipts WHERE id = '$id'";
      $conn->query($query);

      $currDate = date('Y-m-d');
      $sql2 = "SELECT * FROM tbl_ipd_receipts WHERE CreatedDate='$currDate'";
      $rncnt2 = getRow($sql2); 
      echo json_encode(array('status'=>'Delete Successfully','count'=>$rncnt2)); 

  }

 if($_REQUEST['action']=='view'){?>
<table id="example" class="table-striped table-bordered">
        <thead>
            <tr>
              <th>Sr.No</th>
              <th>Receipt No</th>
              <th>IPD. No</th>
              <th>MRD. No</th>
              <th>Name</th>
              <th>Amount</th>
              <th>Admission Date - Time</th>
              <th>Deposite Date - Time</th>
              <th>Payment Mode</th>
             <th>Action</th>
             <th>Print</th>
            </tr>
        </thead>
        <tbody>
          <?php 
 $srno = 1;
  $sql = "SELECT * FROM tbl_ipd_receipts ORDER BY id DESC";
   $rx = $conn->query($sql);
  while($nx = $rx->fetch_assoc()){
  ?>
           <tr>
             <td><?php echo $srno; ?></td>
            <td><?php echo $nx['id']; ?></td>
            <td><?php echo $nx['IpdNo']; ?></td>
             <td><?php echo $nx['MrdNo']; ?></td>
             <td><?php echo $nx['PatientName']; ?></td>
            
             <td>&#8377;<?php echo number_format($nx['Amount'],2); ?></td>
             <td><?php echo $nx['PayMode']; ?></td>
             <td><?php echo date("d/m/Y", strtotime(str_replace('-', '/',$nx['AdmissionDate'])))." - ".$nx['AdmissionTime']; ?></td>
             <td><?php echo date("d/m/Y", strtotime(str_replace('-', '/',$nx['DepositDate'])))." - ".$nx['DepositTime']; ?></td>
             <td><a data-id="<?php echo $nx['id']; ?>" href='javascript:void(0);' data-toggle="tooltip" data-placement="top" title="Edit" data-original-title="Edit" class="update"><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>
             <td><a href='ipd-receipt-print.php?id=<?php echo $nx['id']; ?>' class="btn btn-info btn-sm" target="_new"><i class="feather icon-printer"></i>&nbsp;Print </a></td>
            </tr>
             <?php $srno++;} ?>
        </tbody>
    </table>
    <script type="text/javascript">
      $(document).ready(function() {
      $('#example').DataTable( {
        responsive: true,
        "pageLength": 1000
      });
      });
    </script>
 <?php } ?>