<?php
/**
 * Payment View Template
 *
 * @package Easy_Invoice
 */

namespace EasyInvoice\Templates\Payments;

use EasyInvoice\Models\Payment;
use EasyInvoice\Models\Invoice;

if (!isset($payment) || !($payment instanceof Payment)) {
    wp_die(__('Invalid payment', 'easy-invoice'));
}

$invoice_post = get_post($payment->getInvoiceId());
$invoice = $invoice_post ? new Invoice($invoice_post) : null;


?>

<div class="p-8">
    <div class="flex justify-between items-center mb-6">
        <h1 class="text-2xl font-bold text-gray-900">
            <?php printf(__('Payment #%s', 'easy-invoice'), $payment->getId()); ?>
        </h1>
        <a href="<?php echo admin_url('admin.php?page=easy-invoice-payments'); ?>" 
           class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
            <?php _e('Back to Payments', 'easy-invoice'); ?>
        </a>
    </div>

    <div class="bg-white shadow overflow-hidden sm:rounded-lg">
        <!-- Payment Information -->
        <div class="px-4 py-5 sm:px-6 border-b border-gray-200">
            <h2 class="text-lg font-medium text-gray-900"><?php _e('Payment Information', 'easy-invoice'); ?></h2>
        </div>
        <div class="border-t border-gray-200">
            <dl>
                <div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
                    <dt class="text-sm font-medium text-gray-500"><?php _e('Payment ID', 'easy-invoice'); ?></dt>
                    <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">#<?php echo $payment->getId(); ?></dd>
                </div>
                <div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
                    <dt class="text-sm font-medium text-gray-500"><?php _e('Invoice', 'easy-invoice'); ?></dt>
                    <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
                        <?php if ($invoice): ?>
                        <a href="<?php echo admin_url('admin.php?page=easy-invoice&action=edit&id=' . $invoice->getId()); ?>" 
                           class="text-indigo-600 hover:text-indigo-900">
                            <?php echo $invoice->getNumber(); ?>
                        </a>
                        <?php else: ?>
                        <span class="text-gray-400"><?php _e('Invoice not found', 'easy-invoice'); ?></span>
                        <?php endif; ?>
                    </dd>
                </div>
                <div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
                    <dt class="text-sm font-medium text-gray-500"><?php _e('Amount', 'easy-invoice'); ?></dt>
                    <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
                        <?php echo $payment->getCurrencySymbol() . number_format($payment->getAmount(), 2); ?>
                    </dd>
                </div>
                <div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
                    <dt class="text-sm font-medium text-gray-500"><?php _e('Payment Method', 'easy-invoice'); ?></dt>
                    <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
                        <?php 
                        $payment_method = $payment->getPaymentMethod();
                        if (empty($payment_method)) {
                            // Try direct property access
                            $payment_method = $payment->payment_method;
                        }
                        if (empty($payment_method)) {
                            // Try direct meta access
                            $payment_method = get_post_meta($payment->getId(), '_payment_method', true);
                        }
                        

                        
                        echo ucfirst($payment_method ?: __('Not specified', 'easy-invoice')); 
                        ?>
                    </dd>
                </div>
                <div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
                    <dt class="text-sm font-medium text-gray-500"><?php _e('Status', 'easy-invoice'); ?></dt>
                    <dd class="mt-1 text-sm sm:mt-0 sm:col-span-2">
                        <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full 
                            <?php
                            switch ($payment->getStatus()) {
                                case 'completed':
                                    echo 'bg-green-100 text-green-800';
                                    break;
                                case 'pending':
                                    echo 'bg-yellow-100 text-yellow-800';
                                    break;
                                case 'failed':
                                    echo 'bg-red-100 text-red-800';
                                    break;
                                default:
                                    echo 'bg-gray-100 text-gray-800';
                            }
                            ?>">
                            <?php echo ucfirst($payment->getStatus()); ?>
                        </span>
                    </dd>
                </div>
                <div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
                    <dt class="text-sm font-medium text-gray-500"><?php _e('Date', 'easy-invoice'); ?></dt>
                    <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
                        <?php echo date_i18n(get_option('date_format'), strtotime($payment->getPaymentDate())); ?>
                    </dd>
                </div>
                <div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
                    <dt class="text-sm font-medium text-gray-500"><?php _e('Type', 'easy-invoice'); ?></dt>
                    <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
                        <?php echo ucfirst($payment->getPaymentType()); ?>
                    </dd>
                </div>
                <?php if ($payment->getRecurringId()) : ?>
                <div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
                    <dt class="text-sm font-medium text-gray-500"><?php _e('Recurring ID', 'easy-invoice'); ?></dt>
                    <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
                        <?php echo $payment->getRecurringId(); ?>
                    </dd>
                </div>
                <?php endif; ?>
                <?php if ($payment->getParentPaymentId()) : ?>
                <div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
                    <dt class="text-sm font-medium text-gray-500"><?php _e('Parent Payment', 'easy-invoice'); ?></dt>
                    <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
                        <a href="<?php echo admin_url('admin.php?page=easy-invoice-payments&action=view&id=' . $payment->getParentPaymentId()); ?>" 
                           class="text-indigo-600 hover:text-indigo-900">
                            #<?php echo $payment->getParentPaymentId(); ?>
                        </a>
                    </dd>
                </div>
                <?php endif; ?>
                <?php if ($payment->getNotes()) : ?>
                <div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
                    <dt class="text-sm font-medium text-gray-500"><?php _e('Notes', 'easy-invoice'); ?></dt>
                    <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
                        <?php echo nl2br(esc_html($payment->getNotes())); ?>
                    </dd>
                </div>
                <?php endif; ?>
            </dl>
        </div>

        <?php 
        $gateway_response_raw = $payment->getGatewayResponse();
        $gateway_response = null;
        
        if ($gateway_response_raw) {
            // Try to decode JSON if it's a string
            if (is_string($gateway_response_raw)) {
                $gateway_response = json_decode($gateway_response_raw, true);
            } elseif (is_array($gateway_response_raw)) {
                $gateway_response = $gateway_response_raw;
            }
        }
        
        if ($gateway_response && is_array($gateway_response)) : 
        ?>
        <!-- Gateway Response -->
        <div class="px-4 py-5 sm:px-6 border-t border-gray-200">
            <h2 class="text-lg font-medium text-gray-900"><?php _e('Gateway Response', 'easy-invoice'); ?></h2>
        </div>
        <div class="border-t border-gray-200">
            <dl>
                <?php 
                $loop = 0;
                foreach ($gateway_response as $key => $value) : 
                    $loop++;
                ?>
                <div class="<?php echo $loop % 2 === 0 ? 'bg-white' : 'bg-gray-50'; ?> px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
                    <dt class="text-sm font-medium text-gray-500"><?php echo ucwords(str_replace('_', ' ', $key)); ?></dt>
                    <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
                        <?php 
                        if (is_array($value)) {
                            echo '<pre class="text-xs bg-gray-100 p-2 rounded overflow-x-auto">' . esc_html(json_encode($value, JSON_PRETTY_PRINT)) . '</pre>';
                        } elseif (is_bool($value)) {
                            echo $value ? 'Yes' : 'No';
                        } elseif (is_null($value)) {
                            echo '<em>null</em>';
                        } else {
                            echo esc_html($value);
                        }
                        ?>
                    </dd>
                </div>
                <?php endforeach; ?>
            </dl>
        </div>
        <?php endif; ?>
    </div>
</div> 