<?php
    if (! defined('ABSPATH')) {
        exit;
    }
    $receipt_id_encoded = $receipt_id = $type = $status = $total_display = $email = $full_name = $total_display = '';

    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    $receipt_id_encoded = isset($_GET['receipt-id']) ? sanitize_text_field( wp_unslash( $_GET['receipt-id'] ) ) : '';
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    $type = isset($_GET['type']) ? sanitize_text_field( wp_unslash( $_GET['type'] ) ) : '';

  
    if ($receipt_id_encoded && $type) {
        $receipt_id        = base64_decode($receipt_id_encoded);
        $email             = get_post_meta($receipt_id, 'chama_email', true);
        $full_name         = get_post_meta($receipt_id, 'chama_full_name', true);
        $status            = get_post_meta($receipt_id, 'chama_payment_status', true);
        $description       = '';
        $total             = '';
        $total_display     = '';
        $sub_total_label   = '';
        $total_label       = '';
        $description_label = '';
        $thank_you_message = '';
        $display_sub_total = false;
        $order_summary     = [];

        switch ($type) {
            case 'one-time':
                $total             = get_post_meta($receipt_id, 'chama_donation_total_amount', true);
                $sub_total_label   = 'Donation Amount';
                $total_label       = 'Donation Total';
                $thank_you_message = 'Thank you for your support!';
                $display_sub_total = true;
                break;
            case 'membership':
                $thank_you_message = 'Thank you for your business!';
                break;
            case 'crowdfunding':
                $total             = get_post_meta($receipt_id, 'chama_fund_total_amount', true);
                $description_label = 'Crowdfunding campaign';
                $description       = get_post_meta($receipt_id, 'chama_fund_description', true);
                $sub_total_label   = 'Donation Amount';
                $total_label       = 'Donation Total';
                $thank_you_message = 'Thank you for your support!';
                $display_sub_total = true;
                break;
            case 'commission':
                $total            = get_post_meta($receipt_id, 'chama_com_order_total_amount', true);
                $total_label      = 'Total';
                $commission_price = get_post_meta($receipt_id, 'chama_com_order_price', true);

                $price_cents   = $commission_price * CHAMA_CURRENCY_MINIMUM;
                $price_display = chama_get_formatted_price($price_cents);
                $order_summary =
                    [
                    ["detail" => get_post_meta($receipt_id, 'chama_com_order_description', true), "value" => $price_display],

                ];
                if (get_post_meta($receipt_id, 'chama_com_order_addons', true)) {
                    $addons = explode(',', get_post_meta($receipt_id, 'chama_com_order_addons', true));

                    foreach ($addons as $addon) {
                        $item                = explode('|', $addon);
                        $item_amount         = $item[1];
                        $item_amount_cents   = $item_amount * CHAMA_CURRENCY_MINIMUM;
                        $item_display_amount = chama_get_formatted_price($item_amount_cents);
                        $item_array          = ["detail" => $item[0], "value" => $item_display_amount];
                        $order_summary[]     = $item_array;
                    }
                }
                if (get_post_meta($receipt_id, 'chama_com_order_shipping', true)) {
                    $shipping              = explode('|', get_post_meta($receipt_id, 'chama_com_order_shipping', true));
                    $shipping_cost         = $shipping[1];
                    $shipping_cost_cents   = $shipping_cost * CHAMA_CURRENCY_MINIMUM;
                    $shipping_display_cost = chama_get_formatted_price($shipping_cost_cents);
                    $order_summary[]       = ["detail" => 'Shipping to ' . $shipping[0], "value" => $shipping_display_cost];
                }

                $thank_you_message = 'Thank you for your business!';
                break;
            default:
        }
        $total_display = CHAMA_SYMBOL . number_format((float) $total, 2, '.', '');
    }
?>


    <div class="chama-card">
    <div class="chama-card-body">

            <h3>Receipt</h3>
            <em><?php echo esc_html($thank_you_message); ?></em>
            <hr class="chama-hr chama-receipt-hr" />
            <dl class="chama-receipt-table">
            <div class="chama-receipt-details-row">
                <dt class="chama-receipt-detail">Email Address</dt>
                <dd class="chama-receipt-value"><?php echo esc_html($email); ?></dd>
                </div>
                <?php if ($full_name): ?>
                    <div class="chama-receipt-details-row">
                <dt class="chama-receipt-detail">Full Name</dt>
                <dd class="chama-receipt-value"><?php echo esc_html($full_name); ?></dd>
                </div>
                    <?php endif; ?>
                <div class="chama-receipt-details-row chama-receipt-payment-status <?php echo esc_attr($status); ?>">
                <dt class="chama-receipt-detail">Payment Status</dt>
                <dd class="chama-receipt-value"><?php echo esc_html(ucwords($status)); ?>	</dd>
                </div>
                <?php if ($description): ?>
                    <div class="chama-receipt-details-row">
                <dt class="chama-receipt-detail"><?php echo esc_html($description_label) ?></dt>
                <dd class="chama-receipt-value"><?php echo esc_attr($description); ?></dd>
                </div>
                <?php endif; ?>
<?php if ($display_sub_total): ?>
                <div class="chama-receipt-details-row">
                <dt class="chama-receipt-detail"><?php echo esc_html($sub_total_label) ?></dt>
                <dd class="chama-receipt-value"><?php echo esc_attr($total_display); ?></dd>
                </div>
                <?php endif; ?>
<?php
    foreach ($order_summary as $key => $value) {
        echo '<div class="chama-receipt-details-row">';
        foreach ($value as $sub_key => $sub_val) {
            echo '<div class="chama-receipt-' . esc_attr($sub_key) . '">' . esc_html($sub_val) . '</div>';
        }
        echo "</div>";
    }
?>
                <div class="chama-receipt-details-row chama-receipt-total">
                <dt class="chama-receipt-detail"><?php echo esc_html($total_label) ?></dt>
                <dd class="chama-receipt-value"><?php echo esc_attr($total_display); ?></dd>
                </div>

            </dl>


    </div>
    </div>
