<?php
use BLR\Backline\Core\Money;
use BLR\Backline\Modules\Finances\Module as FinancesModule;

if (!defined('ABSPATH')) {
    exit;
}

$base = rtrim((string) ($ctx['base_url'] ?? ''), '/') . '/';
$id = (int) ($ctx['id'] ?? 0);
$post = $id ? get_post($id) : null;

if (!$post instanceof WP_Post || $post->post_type !== FinancesModule::POST_TYPE) {
    echo '<div class="container-fluid p-0"><div class="alert alert-danger">' . esc_html__('Finance record not found.', 'blr-backline') . '</div></div>';
    return;
}

$kind = (string) get_post_meta($id, 'blr_finance_kind', true);
$status = (string) get_post_meta($id, 'blr_finance_status', true);
$amount = (string) get_post_meta($id, 'blr_finance_amount', true);
$currency = (string) get_post_meta($id, 'blr_finance_currency', true);
$finance_date = (string) get_post_meta($id, 'blr_finance_date', true);
$due_date = (string) get_post_meta($id, 'blr_finance_due_date', true);
$category = (string) get_post_meta($id, 'blr_finance_category', true);
$reference = (string) get_post_meta($id, 'blr_finance_reference', true);
$payment_method = (string) get_post_meta($id, 'blr_finance_payment_method', true);
$contact_id = (int) get_post_meta($id, 'blr_finance_contact_id', true);
$attachment_id = FinancesModule::validate_attachment_id((int) get_post_meta($id, FinancesModule::ATTACHMENT_META_KEY, true));
$attachment = $attachment_id > 0 ? FinancesModule::attachment_data($attachment_id) : [];
$contact_label = $contact_id > 0 ? FinancesModule::contact_label($contact_id) : '';
?>
<div class="container-fluid p-0">
    <div class="d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-3 mb-3">
        <div>
            <h1 class="h3 mb-1"><?php echo esc_html(get_the_title($post)); ?></h1>
            <div class="d-flex gap-2 flex-wrap">
                <span><?php echo wp_kses_post(FinancesModule::badge($kind, 'kind')); ?></span>
                <span><?php echo wp_kses_post(FinancesModule::badge($status)); ?></span>
                <span class="badge bg-light text-dark border"><?php echo esc_html(Money::money_label($amount, $currency)); ?></span>
            </div>
        </div>
        <div class="d-flex gap-2 flex-wrap">
            <a class="btn btn-outline-secondary" href="<?php echo esc_url($base . 'finances/'); ?>"><?php echo esc_html__('Back', 'blr-backline'); ?></a>
            <a class="btn btn-primary" href="<?php echo esc_url($base . 'finances/edit/' . $id . '/'); ?>"><?php echo esc_html__('Edit', 'blr-backline'); ?></a>
            <form method="post" action="<?php echo esc_url($base . 'finances/delete/' . $id . '/'); ?>" class="blr-ba-delete-form d-inline" data-item-label="<?php echo esc_attr(__('finance record', 'blr-backline')); ?>">
                <?php wp_nonce_field('blr_ba_delete_blr_finance_record_' . $id); ?>
                <button type="submit" class="btn btn-outline-danger"><?php echo esc_html__('Delete', 'blr-backline'); ?></button>
            </form>
        </div>
    </div>

    <div class="row g-3">
        <div class="col-12 col-lg-4">
            <div class="card shadow-sm border-0">
                <div class="card-header"><h2 class="h5 card-title mb-0"><?php echo esc_html__('Record Details', 'blr-backline'); ?></h2></div>
                <div class="card-body">
                    <div class="mb-2"><strong><?php echo esc_html__('Amount:', 'blr-backline'); ?></strong> <?php echo esc_html(Money::money_label($amount, $currency)); ?></div>
                    <div class="mb-2"><strong><?php echo esc_html__('Type:', 'blr-backline'); ?></strong> <?php echo esc_html(FinancesModule::kind_label($kind) ?: '—'); ?></div>
                    <div class="mb-2"><strong><?php echo esc_html__('Status:', 'blr-backline'); ?></strong> <?php echo esc_html(FinancesModule::status_label($status) ?: '—'); ?></div>
                    <div class="mb-2"><strong><?php echo esc_html__('Category:', 'blr-backline'); ?></strong> <?php echo esc_html($category !== '' ? $category : '—'); ?></div>
                    <div class="mb-2"><strong><?php echo esc_html__('Reference:', 'blr-backline'); ?></strong> <?php echo esc_html($reference !== '' ? $reference : '—'); ?></div>
                    <div><strong><?php echo esc_html__('Payment Method:', 'blr-backline'); ?></strong> <?php echo esc_html($payment_method !== '' ? $payment_method : '—'); ?></div>
                </div>
            </div>

            <div class="card shadow-sm border-0 mt-3">
                <div class="card-header"><h2 class="h5 card-title mb-0"><?php echo esc_html__('Dates & Contact', 'blr-backline'); ?></h2></div>
                <div class="card-body">
                    <div class="mb-2"><strong><?php echo esc_html__('Date:', 'blr-backline'); ?></strong> <?php echo esc_html($finance_date !== '' ? $finance_date : '—'); ?></div>
                    <div class="mb-2"><strong><?php echo esc_html__('Due Date:', 'blr-backline'); ?></strong> <?php echo esc_html($due_date !== '' ? $due_date : '—'); ?></div>
                    <div><strong><?php echo esc_html__('Contact:', 'blr-backline'); ?></strong> <?php echo esc_html($contact_label !== '' ? $contact_label : '—'); ?></div>
                </div>
            </div>
        </div>

        <div class="col-12 col-lg-8">
            <div class="card shadow-sm border-0 mb-3">
                <div class="card-header"><h2 class="h5 card-title mb-0"><?php echo esc_html__('Notes', 'blr-backline'); ?></h2></div>
                <div class="card-body">
                    <?php if (trim((string) $post->post_content) !== '') : ?>
                        <?php echo wp_kses_post(wpautop($post->post_content)); ?>
                    <?php else : ?>
                        <div class="text-muted"><?php echo esc_html__('No notes yet.', 'blr-backline'); ?></div>
                    <?php endif; ?>
                </div>
            </div>

            <div class="card shadow-sm border-0">
                <div class="card-header"><h2 class="h5 card-title mb-0"><?php echo esc_html__('Attachment', 'blr-backline'); ?></h2></div>
                <div class="card-body">
                    <?php if (empty($attachment)) : ?>
                        <div class="text-muted"><?php echo esc_html__('No attachment linked.', 'blr-backline'); ?></div>
                    <?php else : ?>
                        <div class="row g-3 align-items-start">
                            <div class="col-12 col-md-5">
                                <?php if (!empty($attachment['preview_url'])) : ?>
                                    <img class="img-fluid rounded border" src="<?php echo esc_url((string) $attachment['preview_url']); ?>" alt="<?php echo esc_attr(FinancesModule::attachment_label($attachment)); ?>">
                                <?php endif; ?>
                            </div>
                            <div class="col-12 col-md-7">
                                <div class="fw-semibold mb-1"><?php echo esc_html(FinancesModule::attachment_label($attachment)); ?></div>
                                <?php if (!empty($attachment['filename'])) : ?>
                                    <div class="small text-muted mb-1"><?php echo esc_html((string) $attachment['filename']); ?></div>
                                <?php endif; ?>
                                <?php if (!empty($attachment['mime_type'])) : ?>
                                    <div class="small text-muted mb-3"><?php echo esc_html((string) $attachment['mime_type']); ?></div>
                                <?php endif; ?>
                                <?php if (!empty($attachment['url'])) : ?>
                                    <a class="btn btn-outline-secondary" href="<?php echo esc_url((string) $attachment['url']); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html__('Open attachment', 'blr-backline'); ?></a>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
<p class="small text-muted mb-0"><?php printf(/* translators: 1: modified date, 2: last editor display name */ esc_html__('Last edited %1$s by %2$s', 'blr-backline'), esc_html(get_the_modified_date(get_option('date_format') . ' ' . get_option('time_format'))), esc_html(($r = wp_get_post_revisions(get_the_ID(), ['numberposts' => 1])) ? get_the_author_meta('display_name', (int) array_shift($r)->post_author) : get_the_author_meta('display_name', (int) get_post_field('post_author', get_the_ID())))); ?></p>
