<?php
/**
 * Order Management Dashboard
 * @var yii\web\View $this
 * @var PortalPeople\caterpoint\Module $module
 * @var string|null $currentCursor
 * @var bool $configured
 */

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

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

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

        <?php if (\Yii::$app->session->hasFlash('success')): ?>
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <?= \Yii::$app->session->getFlash('success') ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        <?php endif; ?>

        <?php if (\Yii::$app->session->hasFlash('error')): ?>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <?= \Yii::$app->session->getFlash('error') ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        <?php endif; ?>

        <div class="row mt-4">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5>Status</h5>
                    </div>
                    <div class="card-body">
                        <dl class="row">
                            <dt class="col-sm-4">Configured</dt>
                            <dd class="col-sm-8">
                                <?php if ($configured): ?>
                                    <span class="badge bg-success">Yes</span>
                                <?php else: ?>
                                    <span class="badge bg-danger">No</span>
                                <?php endif; ?>
                            </dd>
                            
                            <dt class="col-sm-4">Current Cursor</dt>
                            <dd class="col-sm-8">
                                <code><?= $currentCursor ? htmlspecialchars($currentCursor) : 'None' ?></code>
                            </dd>
                        </dl>
                    </div>
                </div>
            </div>

            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5>Actions</h5>
                    </div>
                    <div class="card-body">
                        <?php if ($configured): ?>
                            <?= Html::beginForm(['import'], 'post', ['class' => 'mb-3']) ?>
                                <div class="mb-3">
                                    <label for="limit" class="form-label">Limit per Import</label>
                                    <input type="number" class="form-control" id="limit" name="limit" value="50" min="1" max="1000">
                                </div>
                                <div class="mb-3 form-check">
                                    <input type="checkbox" class="form-check-input" id="dryRun" name="dryRun" value="1">
                                    <label class="form-check-label" for="dryRun">
                                        Dry Run (preview only)
                                    </label>
                                </div>
                                <?= Html::submitButton('Import Orders', ['class' => 'btn btn-primary']) ?>
                            <?= Html::endForm() ?>

                            <?= Html::beginForm(['reset'], 'post', ['style' => 'display:inline;']) ?>
                                <?= Html::submitButton('Reset Cursor', ['class' => 'btn btn-warning btn-sm', 'onclick' => 'return confirm("Reset cursor and start from beginning?");']) ?>
                            <?= Html::endForm() ?>
                        <?php else: ?>
                            <div class="alert alert-warning">
                                Module not configured. Please configure Caterpoint API settings first.
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <div class="row mt-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h5>Quick Links</h5>
                    </div>
                    <div class="card-body">
                        <ul class="list-unstyled">
                            <li>
                                <?= Html::a('View Raw Order Data (HTML)', ['fetch-raw', 'limit' => 1], ['class' => 'btn btn-sm btn-outline-primary']) ?>
                                <?= Html::a('View Raw Order Data (JSON)', ['fetch-raw', 'limit' => 1, 'format' => 'json'], ['class' => 'btn btn-sm btn-outline-secondary']) ?>
                                <?= Html::a('View Raw Order Data (XML)', ['fetch-raw', 'limit' => 1, 'format' => 'xml'], ['class' => 'btn btn-sm btn-outline-secondary']) ?>
                            </li>
                            <li class="mt-2">
                                API endpoint: <code><?= rtrim(Yii::$app->request->hostInfo, '/') . '/api/caterpoint/order/fetch-raw?limit=1' ?></code>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
