<?php
/**
 * Fetch Raw Orders View
 * @var yii\web\View $this
 * @var array $result
 * @var string $sourceFormat
 * @var string|null $cursor
 * @var int $limit
 * @var string|null $error
 */

use yii\helpers\Url;
use yii\helpers\Html;

$this->title = 'Raw Order Data';
if (isset($this->params['breadcrumbs'])) {
    $this->params['breadcrumbs'][] = ['label' => 'Caterpoint', 'url' => ['index']];
    $this->params['breadcrumbs'][] = ['label' => 'Orders', 'url' => ['index']];
    $this->params['breadcrumbs'][] = $this->title;
}
?>

<div class="order-fetch-raw">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <h1><?= Html::encode($this->title) ?></h1>
            </div>
        </div>

        <?php if (!empty($error)): ?>
            <div class="row mt-3">
                <div class="col-md-12">
                    <div class="alert alert-danger" role="alert">
                        <?= Html::encode($error) ?>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <div class="row mt-3">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h5>Query Parameters</h5>
                    </div>
                    <div class="card-body">
                        <dl class="row">
                            <dt class="col-sm-3">Cursor</dt>
                            <dd class="col-sm-9"><code><?= $cursor ? htmlspecialchars($cursor) : 'None' ?></code></dd>
                            
                            <dt class="col-sm-3">Limit</dt>
                            <dd class="col-sm-9"><code><?= $limit ?></code></dd>
                            
                            <dt class="col-sm-3">Source Format</dt>
                            <dd class="col-sm-9">
                                <span class="badge bg-info"><?= htmlspecialchars($sourceFormat) ?></span>
                            </dd>
                        </dl>
                    </div>
                </div>
            </div>
        </div>

        <div class="row mt-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h5>Response Data</h5>
                    </div>
                    <div class="card-body">
                        <pre><code><?= htmlspecialchars(json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)) ?></code></pre>
                    </div>
                </div>
            </div>
        </div>

        <div class="row mt-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h5>Alternative Formats</h5>
                    </div>
                    <div class="card-body">
                        <p>View this data in other formats:</p>
                        <ul>
                            <li>
                                <?= Html::a('JSON', ['fetch-raw', 'limit' => $limit, 'cursor' => $cursor, 'format' => 'json'], ['class' => 'btn btn-sm btn-outline-primary']) ?>
                                <?= Html::a('XML', ['fetch-raw', 'limit' => $limit, 'cursor' => $cursor, 'format' => 'xml'], ['class' => 'btn btn-sm btn-outline-primary']) ?>
                                <?= Html::a('HTML', ['fetch-raw', 'limit' => $limit, 'cursor' => $cursor, 'format' => 'html'], ['class' => 'btn btn-sm btn-outline-primary']) ?>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <div class="row mt-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h5>API Endpoints</h5>
                    </div>
                    <div class="card-body">
                        <p>Access raw data via REST API:</p>
                        <ul>
                            <li>JSON: <code><?= rtrim(Yii::$app->request->hostInfo, '/') . '/api/caterpoint/order/fetch-raw?limit=' . (int) $limit . ($cursor ? '&cursor=' . urlencode((string) $cursor) : '') ?></code></li>
                            <li>Default: <code><?= Url::to(['fetch-raw', 'limit' => $limit, 'cursor' => $cursor], true) ?></code></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <div class="row mt-4">
            <div class="col-md-12">
                <?= Html::a('Back to Dashboard', ['index'], ['class' => 'btn btn-secondary']) ?>
            </div>
        </div>
    </div>
</div>
