<?php
/**
 * eTIMS Invoice Details
 * View detailed invoice information
 */

require_once 'layouts/main.php';
require_once 'includes/auth.php';

requireLogin();
requirePermission('view_etims_invoices');

$invoice_id = $_GET['id'] ?? null;

if (!$invoice_id) {
    header('Location: etims_integration.php');
    exit();
}

$invoice = $conn->query("SELECT * FROM etims_invoices WHERE invoice_record_id = $invoice_id")->fetch_assoc();

if (!$invoice) {
    header('Location: etims_integration.php');
    exit();
}

$etimsResponse = json_decode($invoice['etims_response'] ?? '{}', true);

$pageTitle = 'eTIMS Invoice Details';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'eTIMS Invoice Details')); ?>
    <link rel="stylesheet" href="assets/css/pension-dashboard.css">
    <?php include 'layouts/head-css.php'; ?>
</head>

<body data-sidebar="dark">
    <div id="layout-wrapper">
        <?php include 'layouts/sidebar.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="welcome-header" style="background: linear-gradient(135deg, #10b981 0%, #059669 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>eTIMS Invoice Details</h2>
                                <p>Invoice #<?php echo htmlspecialchars($invoice['invoice_number']); ?></p>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Invoice Information</h4>
                                </div>
                                <div class="page-card-body">
                                    <table class="table table-borderless">
                                        <tr>
                                            <td><strong>Invoice Number:</strong></td>
                                            <td><?php echo htmlspecialchars($invoice['invoice_number']); ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>Trader Invoice No:</strong></td>
                                            <td><?php echo htmlspecialchars($invoice['trader_invoice_no'] ?? 'N/A'); ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>Source Type:</strong></td>
                                            <td><span class="badge bg-info"><?php echo htmlspecialchars($invoice['source_type']); ?></span></td>
                                        </tr>
                                        <tr>
                                            <td><strong>Source ID:</strong></td>
                                            <td><?php echo $invoice['source_id']; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>Status:</strong></td>
                                            <td>
                                                <span class="status-badge status-<?php echo strtolower($invoice['status']); ?>">
                                                    <?php echo htmlspecialchars($invoice['status']); ?>
                                                </span>
                                            </td>
                                        </tr>
                                        <?php if ($invoice['receipt_signature']): ?>
                                        <tr>
                                            <td><strong>Receipt Signature:</strong></td>
                                            <td><code><?php echo htmlspecialchars(substr($invoice['receipt_signature'], 0, 50)); ?>...</code></td>
                                        </tr>
                                        <?php endif; ?>
                                        <?php if ($invoice['cu_serial']): ?>
                                        <tr>
                                            <td><strong>CU Serial:</strong></td>
                                            <td><?php echo htmlspecialchars($invoice['cu_serial']); ?></td>
                                        </tr>
                                        <?php endif; ?>
                                        <tr>
                                            <td><strong>Created:</strong></td>
                                            <td><?php echo date('d M Y, H:i:s', strtotime($invoice['created_at'])); ?></td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">eTIMS Response</h4>
                                </div>
                                <div class="page-card-body">
                                    <?php if (!empty($etimsResponse)): ?>
                                        <pre style="background: #f8f9fa; padding: 15px; border-radius: 4px; max-height: 400px; overflow-y: auto;"><?php echo json_encode($etimsResponse, JSON_PRETTY_PRINT); ?></pre>
                                    <?php else: ?>
                                        <p class="text-muted">No response data available</p>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <?php if ($invoice['error_message']): ?>
                    <div class="row">
                        <div class="col-12">
                            <div class="alert alert-danger">
                                <strong>Error:</strong> <?php echo htmlspecialchars($invoice['error_message']); ?>
                            </div>
                        </div>
                    </div>
                    <?php endif; ?>
                    
                    <div class="row mt-3">
                        <div class="col-12">
                            <a href="etims_integration.php" class="btn btn-secondary">
                                <i class="ri-arrow-left-line me-2"></i>Back to eTIMS Integration
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
