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

require_once '../../../resources/base_path_include.php';
require_once $base_path_to_learning_resources  . "includes/connect.php"; // use require for mandatory files
require_once $base_path_to_learning_resources . 'lib/function.php';
require_once $base_path_to_learning_resources . 'lib/validate_api_signature.php';
//valdation file
require_once $base_path_to_learning_resources . 'lib/validate_function.php';
require_once $base_path_to_learning . 'user_management/authenticate.php';
require_once $base_path_to_learning . 'functions/common_functions.php';
//db access function
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';
//aws image store
require_once $base_path_to_learning_resources . 'lib/bucket_functions/bucket_functions.php';
require_once $base_path_to_learning . "vendor/autoload.php";
require_once $base_path_to_learning_resources . 'includes/aws_bucket_config.php';
// require_once $base_path_to_learning .'modules/accounting/other_products/rejoice_learning/fees_management/functions/learning_functions.php';


$institution_data =  getInstitution($conn_rjllearning, $institution_id);
$additional_data = array();
$additional_data = ['id', 'name','address','contact_number','email'];
$branch = getBranchInfo($conn_rjllearning_specific, $institution_id, $branch_id, $additional_data);

$result = $success;
$data = $result['transaction_info']; // Indexed array of transactions
$ledger = $result['ledger_info']; // Associative array

// Extract ledger details
$ledger_name = $ledger['ledger_name'];
$class_section = $ledger['ledger_parent_group'];
$first_transaction = $data[0]; // Get the first transaction
    
$narration = $first_transaction['narration'];
$student_amount = $first_transaction['student_amount'];
$transaction_date = $first_transaction['transaction_date'];
$date = date('d-m-Y', strtotime($transaction_date));
$transaction_record = $first_transaction['transaction_record'];
$transaction_number = $first_transaction['transaction_number'];
$through_name = $first_transaction['transaction_record'][0]['ledger_name'];



$html = '<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>School Receipt</title>
    <style>
        @import url("https://fonts.googleapis.com/css2?family=Crimson+Pro:wght@400;600&family=Montserrat:wght@400;500;600;700&display=swap");

        :root {
            --primary: #1e3a8a;
            --secondary: #1d4ed8;
            --accent: #3b82f6;
            --gold: #ca8a04;
        }

        body {
            font-family: "Montserrat", sans-serif;
            margin: 0;
            padding: 20px;
            min-height: 100vh;
        }

        .receipt-container {
            width: 800px;
            background: white;
            padding: 5px;
            border: 2px solid var(--primary);
        }

   .container {
        display: flex;
        justify-content: center;
        width: 100%;
    }

    .school-header {
        display: flex;
       
        justify-content: center; /* Centers content horizontally */
        padding: 10px;
        color: #000;
        font-size: 13px;

        width: max-content; /* Ensures content width is limited */
        text-align: center;
    }

    .logo-container {
        width: 100px;
        height: 90px;
        background: white;
        padding: 0px;
        margin-right: 15px;
        flex-shrink: 0;
    }

    .school-logo {
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: 8px;
    }

    .school-info {
        text-align: left;
    }

    .school-name {
        font-size: 18px;
        font-weight: bold;
        color: var(--primary);
        margin-bottom: 4px;
    }

    .school-details {
        font-size: 12px;
        line-height: 1.4;
    }

    .school-name {
        font-size: 18px;
        font-weight: bold;
        color: var(--primary);
        margin-bottom: 4px;
    }

    .school-details {
        font-size: 12px;
        line-height: 1.4;
    }

        .receipt-title {
            text-align: center;
            font-size: 22px;
            font-weight: 700;
            color: var(--primary);
            margin: 5px 0;
            text-transform: uppercase;
        }

        .form-section {
            padding: 15px;
            border: 1px solid var(--secondary);
            border-radius: 8px;
            margin-bottom: 10px;
        }

        .form-row {
            display: flex;
            justify-content: space-between;
            padding: 8px 0;
            border-bottom: 1px dashed #e2e8f0;
        }

        .form-label {
            font-weight: 600;
            color: var(--primary);
        }

        .amount-section {
            display: flex;
            justify-content: space-between;
            margin-top: 35px;
            border-radius: 8px;
            font-size: 18px;
            font-weight: bold;
        }

        .signature-section {
            display: flex;
            justify-content: space-between;
            margin-top: 30px;
        }

        .signature-box {
            text-align: center;
            width: 200px;
        }

        .signature-line {
            border-top: 2px solid var(--primary);
            margin-bottom: 10px;
        }

        .stamp-area {
            text-align: center;
            margin-top: 20px;
            font-weight: bold;
            color: var(--accent);
            border: 2px dashed var(--accent);
            padding: 10px;
            width: 150px;
            margin: auto;
            border-radius: 8px;
        }
    </style>
    </head>
<body>
    <div class="receipt-container">
    <div class="container">
        <div class="school-header">
            ';
              if ($institution_data['logo'] != null) {
               $html .= '<div class="logo-container"> <img src='.$institution_data['logo'].' alt="School Logo" class="school-logo"> </div>';
              }
              
            $html .= '
             <div class="school-info">
            <div class="school-name">'.$institution_data['institution_name'].'</div>
            <div class="school-details">';
            if ($branch['address'] != null) {
                $html .= $branch['address'];
            }
            
            if (!empty($branch['address']) || !empty($branch['contact_number']) || !empty($branch['email'])) {
                $html .= ' <br>';
            }
            
            if ($branch['contact_number'] != null) {
                $html .=   $branch['contact_number'];
            }
            
            if ($branch['email'] != null) {
                $html .= ' |  ' . $branch['email'];
            }

             $html .= '</div></div>
        </div>
     </div>
     <hr  style= "border-bottom: 2px solid #1e3a8a;    padding: 0px; margin: 0px;"/>
        <div class="receipt-title">Fee Receipt</div>

        <div class="form-section">
       
            <div class="form-row">
                <span class="form-label">Receipt No.: '.$transaction_number.'</span>
                <span class="form-label">Date: '.$date.'</span>
            </div>
         <p>Received with thanks a sum of <strong>Rs.'.$student_amount.' </strong> from <strong>'.$ledger_name.'</strong> None through '. $through_name .' on account of '.$narration.'.</p>

        <div class="signature-section">
            <div class="signature-box">
                <div class="signature-line"></div>
                <div class="signature-title">Accounts Officer</div>
            </div>
            <div class="signature-box">
                <div class="signature-line"></div>
                <div class="signature-title">Principal</div>
            </div>
        </div>
    </div>
</body>
</html>';

echo $html;

