<?php
require_once 'vendor/autoload.php'; // Composer autoload

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
error_reporting(E_ALL);
			ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Example data (you can replace with database results)
$agreement = [
    'loan_tot_amount' => 500000,
    'loan_interest' => 6,
    'loan_no_of_terms' => 100,
    'loan_term_amount' => 5000,
    'loan_date' => '2025-01-01',
    'loan_close_date' => '2025-05-01',
    'borrower_name_1' => 'John Silva',
    'borrower_nic_1' => '852134567V',
    'borrower_name_2' => 'David Perera',
    'borrower_nic_2' => '901234567V',
    'company_name' => 'Beanstalk Investments Ltd',
    'company_reg_no' => 'PV12345',
    'address' => 'Colombo 03'
];

$phpWord = new PhpWord();
$section = $phpWord->addSection();

// Title
$section->addTitle('PROMISSORY NOTE', 1);
$section->addText('(Capital Sum: ' . number_format($agreement['loan_tot_amount'], 0) . '/-)', ['bold' => true]);

// Body text
$text = "{$agreement['borrower_name_1']} (holder of NIC No. {$agreement['borrower_nic_1']}) and "
      . "{$agreement['borrower_name_2']} (holder of NIC No. {$agreement['borrower_nic_2']}) both residing at "
      . "{$agreement['address']}, Directors of {$agreement['company_name']} (Reg No: {$agreement['company_reg_no']}) at "
      . "{$agreement['address']}, (hereinafter referred to as 'the Borrower'), promise to pay "
      . "{$agreement['company_name']} the sum of Rupees " . number_format($agreement['loan_tot_amount'], 0)
      . "/- with interest at {$agreement['loan_interest']}% per mensem, payable in {$agreement['loan_no_of_terms']} installments of "
      . number_format($agreement['loan_term_amount'], 0) . "/- each, starting from "
      . date('jS F Y', strtotime($agreement['loan_date'])) . " and ending on "
      . date('jS F Y', strtotime($agreement['loan_close_date'])) . ".";

$section->addText($text);
$section->addText('Provided however that demand shall not be made unless there is a default in payment as aforesaid.');
$section->addText('In the event of default, the lender has the right to charge a penal interest of 10% per month until payment in full.');
$section->addText('Signed at Colombo on this ' . date('jS F Y') . '.');

// Witness lines
$section->addTextBreak(2);
$section->addText('__________________________');
$section->addText('Borrower Signature');
$section->addTextBreak(1);
$section->addText('__________________________');
$section->addText('Lender Signature');

// Output the file
$filename = 'Loan_Agreement_' . date('Ymd_His') . '.docx';

// Clear output buffers (prevents unreadable Word files)
if (ob_get_length()) ob_end_clean();

header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Cache-Control: max-age=0');

$writer = IOFactory::createWriter($phpWord, 'Word2007');
$writer->save('php://output');
exit;
?>
