<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// generate_pdf.php
include 'db_connect.php';

// IMPORTANT: Ensure you have the TCPDF library folder in this directory
require_once('tcpdf/tcpdf_main/tcpdf.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $partner_id = intval($_POST['partner_id']);
    $filter_month = $_POST['filter_month'];
    $start_date = $filter_month . "-01";
    $end_date = date("Y-m-t", strtotime($start_date));

    // Fetch Data
    $sql_partner = "SELECT * FROM Partners WHERE partner_id = ?";
    $stmt = $conn->prepare($sql_partner);
    $stmt->bind_param("i", $partner_id);
    $stmt->execute();
    $partner = $stmt->get_result()->fetch_assoc();

    $sql_contribs = "
        SELECT fc.*, pa.agreement_code 
        FROM Financial_Contributions fc
        JOIN Partnership_Agreements pa ON fc.agreement_id = pa.agreement_id
        WHERE pa.partner_id = ? 
        AND fc.contribution_date BETWEEN ? AND ?
        ORDER BY fc.contribution_date ASC";
    
    $stmt_c = $conn->prepare($sql_contribs);
    $stmt_c->bind_param("iss", $partner_id, $start_date, $end_date);
    $stmt_c->execute();
    $result = $stmt_c->get_result();

    // Create PDF
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->SetCreator('System');
    $pdf->SetTitle('Monthly Contribution Report');
    $pdf->SetHeaderData('', 0, 'Partner Contribution Report', 'Month: ' . date("F Y", strtotime($start_date)));
    
    // Set margins & auto page break
    $pdf->SetMargins(15, 27, 15);
    $pdf->SetAutoPageBreak(TRUE, 25);
    $pdf->AddPage();

    // HTML Content
    $html = '<h3>Partner Details</h3>';
    $html .= '<p><strong>Name:</strong> ' . $partner['partner_name'] . '<br>';
    $html .= '<strong>Code:</strong> ' . $partner['partner_code'] . '</p>';
    $html .= '<hr>';
    
    $html .= '<h3>Contributions (' . date("F Y", strtotime($start_date)) . ')</h3>';
    $html .= '<table border="1" cellpadding="5" cellspacing="0">';
    $html .= '<tr style="background-color:#f2f2f2; font-weight:bold;">
                <th width="25%">Date</th>
                <th width="25%">Agreement</th>
                <th width="30%">Reference</th>
                <th width="20%" align="right">Amount</th>
              </tr>';

    $total = 0;
    while ($row = $result->fetch_assoc()) {
        $html .= '<tr>
                    <td>' . $row['contribution_date'] . '</td>
                    <td>' . $row['agreement_code'] . '</td>
                    <td>' . $row['receipt_number'] . '</td>
                    <td align="right">' . number_format($row['amount'], 2) . '</td>
                  </tr>';
        $total += $row['amount'];
    }

    $html .= '<tr style="font-weight:bold;">
                <td colspan="3" align="right">Total</td>
                <td align="right">' . number_format($total, 2) . '</td>
              </tr>';
    $html .= '</table>';

    $pdf->writeHTML($html, true, false, true, false, '');
    
    // Close and Output PDF
    $pdf->Output('report_' . $partner_id . '_' . $filter_month . '.pdf', 'I');
}
?>