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

if($_REQUEST['action'] == 'Save'){
$Name = addslashes(trim($_REQUEST['Name']));
$Days = addslashes(trim($_REQUEST['Days']));
$Rate = addslashes(trim($_REQUEST['Rate']));
$Amount = addslashes(trim($_REQUEST['Amount']));
$PackageName = addslashes(trim($_REQUEST['PackageName']));
$PackageAmt = addslashes(trim($_REQUEST['PackageAmt']));
$BalanceAmt = addslashes(trim($_REQUEST['BalanceAmt']));
$CreatedDate = date('Y-m-d');
$Status = 1;
$ModifiedBy = 0;
$ModifiedDate = null;
$sql = "SELECT * FROM tbl_charges WHERE PackageName='$PackageName' AND Name='$Name'";
$rncnt = getRow($sql);
if($rncnt > 0){
	echo 0;
}
else{
	/*$sql = "INSERT INTO tbl_charges SET PackageName='$PackageName',PackageAmt='$PackageAmt',BalanceAmt='$BalanceAmt',Name='$Name',Amount='$Amount',Days='$Days',Rate='$Rate',CreatedBy='$user_id',CreatedDate='$CreatedDate'";
	$conn->query($sql);
	echo 1;*/
  try {
   $sql = "INSERT INTO tbl_charges (PackageName,PackageAmt,BalanceAmt,Name,Amount,Days,Rate,Status,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate) VALUES (:PackageName,:PackageAmt,:BalanceAmt,:Name,:Amount,:Days,:Rate,:Status,:CreatedBy,:CreatedDate,:ModifiedBy,:ModifiedDate)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':PackageName', $PackageName);
$stmt->bindParam(':PackageAmt', $PackageAmt);
$stmt->bindParam(':BalanceAmt', $BalanceAmt);
$stmt->bindParam(':Name', $Name);
$stmt->bindParam(':Amount', $Amount);
$stmt->bindParam(':Days', $Days);
$stmt->bindParam(':Rate', $Rate);
$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_charges WHERE id = '$id'";
   $row = getRecord($query);
    echo json_encode($row);


}

if($_POST['action'] == 'Edit') {
     $id = $_POST['id'];
$Name = addslashes(trim($_REQUEST['Name']));
$Amount = addslashes(trim($_REQUEST['Amount']));
$Days = addslashes(trim($_REQUEST['Days']));
$Rate = addslashes(trim($_REQUEST['Rate']));
$PackageName = addslashes(trim($_REQUEST['PackageName']));
$PackageAmt = addslashes(trim($_REQUEST['PackageAmt']));
$BalanceAmt = addslashes(trim($_REQUEST['BalanceAmt']));
$ModifiedDate = date('Y-m-d');
$query = "SELECT * FROM tbl_charges WHERE PackageName='$PackageName' AND Name='$Name' AND id != '$id'";
$row_cnt = getRow($query);
if($row_cnt > 0){
  echo 0;
}
else{
  try{
  $query2 = "UPDATE tbl_charges SET PackageName='$PackageName',PackageAmt='$PackageAmt',BalanceAmt='$BalanceAmt',Name='$Name',Amount='$Amount',Days='$Days',Rate='$Rate',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_charges WHERE id = '$id'";
      $conn->query($query);
      echo "Delete Successfully";

  }

 if($_POST['action']=='view'){?>
<table id="example" class="table table-striped table-bordered dt-responsive nowrap" style="width:100%">
        <thead>
            <tr>
             <th>Sr.No</th>
             <th>Charges Name</th>
             <th>Days / Unit</th>
             <th>Rate</th>
             <th>Amount</th>
             <th>Action</th>
            </tr>
        </thead>
        <tbody>
          <?php 
 $srno = 1;
 $PackageName = addslashes(trim($_REQUEST['PackageName']));
  $sql = "SELECT * FROM tbl_charges WHERE PackageName='$PackageName' 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['Days']; ?></td>
              <td><?php echo $nx['Rate']; ?></td>
        
             <td>&#8377;<?php echo number_format($nx['Amount'],2); ?></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>
            </tr>
             <?php $srno++;} ?>
        </tbody>
    </table>
    <script type="text/javascript">
      $(document).ready(function() {
      $('#example').DataTable( {
        responsive: true
      });
      });
    </script>
 <?php }

   if($_POST['action'] == 'getPackageDetails') {
    $PackageName = addslashes(trim($_REQUEST['PackageName']));
    $query = "SELECT * FROM tbl_charges WHERE PackageName = '$PackageName'";
    $row = getRecord($query);
    echo json_encode($row);

   }

   if($_POST['action'] == 'calBalanceAmt') {
    $PackageName = addslashes(trim($_REQUEST['PackageName']));
    $query = "SELECT SUM(Amount) As Amount,PackageAmt FROM tbl_charges WHERE PackageName = '$PackageName'";
    $row = getRecord($query);
    $PackageAmt = $row['PackageAmt'];
    $TotAmt = $row['Amount'];
    $BalanceAmt = $PackageAmt - $TotAmt;
    echo $BalanceAmt;

   }

    if($_POST['action'] == 'getChargesDetails') {
    $ChargesName = addslashes(trim($_REQUEST['ChargesName']));
    $query = "SELECT * FROM tbl_charges WHERE Name = '$ChargesName'";
    $row = getRecord($query);
    echo json_encode($row);

   }

?>