<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

ob_start(); // Start output buffering

include('include/config.php');
include('dompdf/autoload.inc.php');
use Dompdf\Dompdf;

// Fetch dynamic data from the database
if(isset($_GET['iddd'])) {
    $iddddd = $_GET['iddd'];
    $to_date_formatted = $_GET['to_date'];
    $from_date_formatted = $_GET['from_date'];
    $statement          = $_GET['statement'];
    
    switch($statement){
        case 'cd_st':
            
           $select_cd = mysqli_query($conn, "SELECT * FROM `cd` WHERE `userid`='$iddddd' AND `cd_date` BETWEEN '$to_date_formatted' AND '$from_date_formatted'");
            break;
        case 'share_st':
           

            $select_share = mysqli_query($conn, "SELECT * FROM `share_money` WHERE `userid`='$iddddd' AND `share_money_date` BETWEEN '$to_date_formatted' AND '$from_date_formatted'");
            break;
        case 'loan_st':
            
            $select_loan = mysqli_query($conn, "SELECT * FROM `loan_desc` WHERE `userid`='$iddddd' AND `date` BETWEEN '$to_date_formatted' AND '$from_date_formatted'");
            break;
    }
} 
if(isset($select_cd) && mysqli_num_rows($select_cd) > 0) { 

$userData = '';
$i = 1;
while ($res = mysqli_fetch_assoc($select_cd)) {
    $select_profilee = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM `user` WHERE `id` = '" . $res['userid'] . "' ORDER BY id ASC"));

    $userData .= '<tr>';
    $userData .= '<td>' .$i. '</td>';
    $userData .= '<td>' . $res['userid'] . '</td>';
    $userData .= '<td>' . $select_profilee['first_name'] . ' ' . $select_profilee['last_name'] . '</td>';
    $userData .= '<td>' . $res['cd_amount'] . '</td>';
    $userData .= '<td>' . $res['cd_Interest'] . '</td>';
    $userData .= '<td>' . $res['cd_date'] . '</td>';
    $userData .= '<td>' . $res['cd_month'] . '</td>';
    $userData .= '</tr>';
    $i++;
}

// HTML content for table heading
$tableHeading = <<<HTML
<thead>
    <tr>
        <th style="width: 5%;">S.no</th>
        <th>User ID</th>
        <th>User Name</th>
        <th>CD Amount</th>
        <th>CD Interest</th>
        <th>CD Money Date</th>
        <th>CD Money Month</th>
    </tr>
</thead>
HTML;

// Concatenate table heading with table body
$tableContent = $tableHeading . '<tbody>' . $userData . '</tbody>' . '<tbody><tr><td colspan="7" style="height: 50px;"></td></tr></tbody>' . $tableHeading . '<tbody>' . $userData . '</tbody>';

$htmlContent = <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <title>CD Statement</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    /* Custom CSS for table */
    .container {
      padding-top: 20px;
    }
    .table-container {
      margin-bottom: 20px;
    }
    .table {
      width: 100%; /* Set table width to 100% */
      font-size: 12px; /* Adjust font size */
    }
    .table th {
      text-align: center;
      vertical-align: middle !important;
    }
    .table td {
      vertical-align: middle !important;
    }
    .table thead th {
      font-weight: bold;
      background-color: #f8f9fa;
    }
    .table tbody tr:nth-child(odd) {
      background-color: #f2f2f2;
    }
  </style>
</head>
<body>
<h1 style="margin-left:300px; margin-bottom:50px;">CD Statement</h1>

<div class="container">
  <div class="table-container">
    <table class="table table-striped table-bordered">
      <?=$tableContent;?>
    </table>
  </div>
</div>

</body>
</html>
HTML;

// Set header for PDF
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="sample.pdf"');

// Generate PDF
$obj = new Dompdf();
$obj->loadHtml($htmlContent);
$obj->setPaper('A4', 'landscape');
$obj->render();

// Output PDF
echo $obj->output();

}
  if(isset($select_loan) && mysqli_num_rows($select_loan) > 0) { 
      $userData = '';
$i = 1;
while ($res = mysqli_fetch_assoc($select_loan)) {
    $select_profilee = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM `user` WHERE `id` = '" . $res['userid'] . "' ORDER BY id ASC"));

    $userData .= '<tr>';
    $userData .= '<td>' .$i. '</td>';
    $userData .= '<td>' . $res['userid'] . '</td>';
    $userData .= '<td>' . $select_profilee['first_name'] . ' ' . $select_profilee['last_name'] . '</td>';
    $userData .= '<td>' . $res['available_balance'] . '</td>';
    $userData .= '<td>' . $res['month_payment'] . '</td>';
    $userData .= '<td>' . $res['monthly_Interest'] . '</td>';
    $userData .= '<td>' . $res['cd_deduction'] . '</td>';
    $userData .= '<td>' . $res['total_deduct_amount'] . '</td>';
    $userData .= '<td>' . $res['loan_return'] . '</td>';
    $userData .= '<td>' . $res['available_balance'] . '</td>';
    $userData .= '<td>' . $res['date'] . '</td>';
    $userData .= '<td>' . $res['month_name'] . '</td>';
    $userData .= '</tr>';
    $i++;
}

// HTML content for table heading
$tableHeading = <<<HTML
<thead>
    <tr>
        <th style="width: 5%;">S.no</th>
        <th>User ID</th>
        <th>User Name</th>
        <th>Loan Amount</th>
        <th>Month Amount</th>
        <th>Monthly Intrest</th>
        <th>Monthly Penalty</th>
        <th>CD Deduction Amount:</th>
        <th>Total Deduct Amount:</th>
        <th>Loan Returan:</th>
        <th>Available Amount:</th>
         <th>Loan Date:</th>
    </tr>
</thead>
HTML;

// Concatenate table heading with table body
$tableContent = $tableHeading . '<tbody>' . $userData . '</tbody>' . '<tbody><tr><td colspan="7" style="height: 50px;"></td></tr></tbody>' . $tableHeading . '<tbody>' . $userData . '</tbody>';

$htmlContent = <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <title>CD Statement</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    /* Custom CSS for table */
    .container {
      padding-top: 20px;
    }
    .table-container {
      margin-bottom: 20px;
    }
    .table {
      width: 100%; /* Set table width to 100% */
      font-size: 12px; /* Adjust font size */
    }
    .table th {
      text-align: center;
      vertical-align: middle !important;
    }
    .table td {
      vertical-align: middle !important;
    }
    .table thead th {
      font-weight: bold;
      background-color: #f8f9fa;
    }
    .table tbody tr:nth-child(odd) {
      background-color: #f2f2f2;
    }
  </style>
</head>
<body>
<h1 style="margin-left:300px; margin-bottom:50px;">Loan Statement</h1>

<div class="container">
  <div class="table-container">
    <table class="table table-striped table-bordered">
      <?=$tableContent;?>
    </table>
  </div>
</div>

</body>
</html>
HTML;

// Set header for PDF
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="sample.pdf"');

// Generate PDF
$obj = new Dompdf();
$obj->loadHtml($htmlContent);
$obj->setPaper('A4', 'landscape');
$obj->render();

// Output PDF
echo $obj->output();

}
 if(isset($select_share) && mysqli_num_rows($select_share) > 0) { 
     
           $userData = '';
$i = 1;
while ($res = mysqli_fetch_assoc($select_share)) {
    $select_profilee = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM `user` WHERE `id` = '" . $res['userid'] . "' ORDER BY id ASC"));

    $userData .= '<tr>';
    $userData .= '<td>' .$i. '</td>';
    $userData .= '<td>' . $res['userid'] . '</td>';
    $userData .= '<td>' . $select_profilee['first_name'] . ' ' . $select_profilee['last_name'] . '</td>';
    $userData .= '<td>' . $res['share_money_amount'] . '</td>';
    $userData .= '<td>' . $res['share_money_Interest'] . '</td>';
    $userData .= '<td>' . $res['share_money_date'] . '</td>';
    $userData .= '<td>' . $res['month_name'] . '</td>';
    $userData .= '</tr>';
    $i++;
}

// HTML content for table heading
$tableHeading = <<<HTML
<thead>
    <tr>
        <th style="width: 5%;">S.no</th>
        <th>User ID</th>
        <th>User Name</th>
        <th>Share Money Amount</th>
        <th>Share Money Interest</th>
        <th>Date</th>
        <th>Month</th>
    </tr>
</thead>
HTML;

// Concatenate table heading with table body
$tableContent = $tableHeading . '<tbody>' . $userData . '</tbody>' . '<tbody><tr><td colspan="7" style="height: 50px;"></td></tr></tbody>' . $tableHeading . '<tbody>' . $userData . '</tbody>';

$htmlContent = <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <title>CD Statement</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    /* Custom CSS for table */
    .container {
      padding-top: 20px;
    }
    .table-container {
      margin-bottom: 20px;
    }
    .table {
      width: 100%; /* Set table width to 100% */
      font-size: 12px; /* Adjust font size */
    }
    .table th {
      text-align: center;
      vertical-align: middle !important;
    }
    .table td {
      vertical-align: middle !important;
    }
    .table thead th {
      font-weight: bold;
      background-color: #f8f9fa;
    }
    .table tbody tr:nth-child(odd) {
      background-color: #f2f2f2;
    }
  </style>
</head>
<body>
<h1 style="margin-left:300px; margin-bottom:50px;">Share Money Statement</h1>

<div class="container">
  <div class="table-container">
    <table class="table table-striped table-bordered">
      <?=$tableContent;?>
    </table>
  </div>
</div>

</body>
</html>
HTML;

// Set header for PDF
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="sample.pdf"');

// Generate PDF
$obj = new Dompdf();
$obj->loadHtml($htmlContent);
$obj->setPaper('A4', 'landscape');
$obj->render();

// Output PDF
echo $obj->output();

}
?>
