<?php

require_once '../../../includes/modal_header.php';

$client_id = intval($_GET['client_id']);

$sql = mysqli_query(
    $mysqli,
    "SELECT client_currency_code, client_name, contact_email, contact_name FROM clients
    LEFT JOIN contacts ON clients.client_id = contacts.contact_client_id AND contact_primary = 1
    WHERE client_id = $client_id"
);

$row = mysqli_fetch_assoc($sql);
$client_name = escapeHtml($row['client_name']);
$client_currency_code = escapeHtml($row['client_currency_code']);
$contact_name = escapeHtml($row['contact_name']);
$contact_email = escapeHtml($row['contact_email']);

//Add up all the payments for the invoice and get the total amount paid to the invoice
$sql_invoice_amounts = mysqli_query($mysqli, "SELECT SUM(invoice_amount) AS invoice_amounts FROM invoices WHERE invoice_client_id = $client_id AND invoice_status != 'Draft' AND invoice_status != 'Cancelled' AND invoice_status != 'Non-Billable'");
$row = mysqli_fetch_assoc($sql_invoice_amounts);

$invoice_amounts = floatval($row['invoice_amounts']);

$sql_amount_paid = mysqli_query($mysqli, "SELECT SUM(payment_amount) AS amount_paid FROM payments, invoices WHERE payment_invoice_id = invoice_id AND invoice_client_id = $client_id");
$row = mysqli_fetch_assoc($sql_amount_paid);

$amount_paid = floatval($row['amount_paid']);

$balance = $invoice_amounts - $amount_paid;

enforceClientAccess();

ob_start();

?>

<div class="modal-header bg-dark">
    <h5 class="modal-title"><i class="fa fa-fw fa-credit-card me-2"></i>Make Batch Payment</h5>
    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<form action="post.php" method="post" autocomplete="off">
    <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
    <input type="hidden" name="client_id" value="<?= $client_id ?>">
    <input type="hidden" name="balance" value="<?= $balance ?>">
    <input type="hidden" name="currency_code" value="<?= $client_currency_code ?>">
    <div class="modal-body">

        <div class="alert alert-info">
            <h5>Batch Payment Notice</h5>
            Batch Payment will settle invoices in order from the oldest to the newest.
        </div>

        <div class="row g-2">
            <div class="col-md">

                <div class="mb-3">
                    <label>Date <strong class="text-danger">*</strong></label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-calendar"></i></span>
                        <input type="date" class="form-control" name="date" max="2999-12-31" value="<?= date("Y-m-d") ?>" required>
                    </div>
                </div>

            </div>

            <div class="col-md">

                <div class="mb-3">
                    <label>Amount <strong class="text-danger">*</strong></label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-dollar-sign"></i></span>
                        <input type="text" class="form-control" inputmode="decimal" pattern="[0-9]*\.?[0-9]{0,2}" name="amount" value="<?= number_format($balance, 2, '.', '') ?>" placeholder="0.00" required>
                    </div>
                </div>

            </div>

        </div>

        <div class="mb-3">
            <label>Account <strong class="text-danger">*</strong></label>
            <div class="input-group">
                    <span class="input-group-text"><i class="fa fa-fw fa-piggy-bank"></i></span>
                <select class="form-select select2" name="account" required>
                    <option value="">- Select an Account -</option>
                    <?php

                    $sql = mysqli_query($mysqli, "SELECT account_id, account_name, opening_balance FROM accounts WHERE account_archived_at IS NULL ORDER BY account_name ASC");
                    while ($row = mysqli_fetch_assoc($sql)) {
                        $account_id = intval($row['account_id']);
                        $account_name = escapeHtml($row['account_name']);
                        $opening_balance = floatval($row['opening_balance']);

                        $sql_payments = mysqli_query($mysqli, "SELECT SUM(payment_amount) AS total_payments FROM payments WHERE payment_account_id = $account_id");
                        $row = mysqli_fetch_assoc($sql_payments);
                        $total_payments = floatval($row['total_payments']);

                        $sql_revenues = mysqli_query($mysqli, "SELECT SUM(revenue_amount) AS total_revenues FROM revenues WHERE revenue_account_id = $account_id");
                        $row = mysqli_fetch_assoc($sql_revenues);
                        $total_revenues = floatval($row['total_revenues']);

                        $sql_expenses = mysqli_query($mysqli, "SELECT SUM(expense_amount) AS total_expenses FROM expenses WHERE expense_account_id = $account_id");
                        $row = mysqli_fetch_assoc($sql_expenses);
                        $total_expenses = floatval($row['total_expenses']);

                        $account_balance = $opening_balance + $total_payments + $total_revenues - $total_expenses;

                    ?>
                        <option <?php if ($config_default_payment_account == $account_id) { echo "selected"; } ?>
                            value="<?= $account_id ?>">
                            <?= $account_name ?> [$<?= number_format($account_balance, 2) ?>]
                        </option>

                    <?php
                    }
                    ?>
                </select>
            </div>
        </div>

        <div class="mb-3">
            <label>Payment Method <strong class="text-danger">*</strong></label>
            <div class="input-group">
                    <span class="input-group-text"><i class="fa fa-fw fa-money-check-alt"></i></span>
                <select class="form-select select2" name="payment_method" required>
                    <option value="">- Method of Payment -</option>
                    <?php

                    $sql = mysqli_query($mysqli, "SELECT payment_method_name FROM payment_methods ORDER BY payment_method_name ASC");
                    while ($row = mysqli_fetch_assoc($sql)) {
                        $payment_method_name = escapeHtml($row['payment_method_name']);
                    ?>
                        <option <?php if ($config_default_payment_method == $payment_method_name) { echo "selected"; } ?>><?= $payment_method_name ?></option>

                    <?php
                    }
                    ?>
                </select>
            </div>
        </div>

        <div class="mb-3">
            <label>Reference</label>
            <div class="input-group">
                    <span class="input-group-text"><i class="fa fa-fw fa-file-alt"></i></span>
                <input type="text" class="form-control" name="reference" placeholder="Check #, Trans #, etc" maxlength="200">
            </div>
        </div>

        <?php if (!empty($config_smtp_provider) && !empty($contact_email)) { ?>

            <div class="mb-3">
                <label>Email Receipt</label>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" id="customControlAutosizing" name="email_receipt" value="1" checked>
                    <label class="form-check-label" for="customControlAutosizing"><?= $contact_email ?></label>
                </div>
            </div>

        <?php } ?>

    </div>

    <div class="modal-footer">
        <button type="submit" name="add_bulk_payment" class="btn btn-primary text-bold"><i class="fas fa-check me-2"></i>Pay</button>
        <button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
    </div>
</form>

<?php
require_once '../../../includes/modal_footer.php';
