<?php
use function App\Core\{e, logo_asset, url};

$providers = $providers ?? [];
$knownProviders = $knownProviders ?? [];
$csrfToken = (string) ($csrfToken ?? \App\Core\Csrf::token());
$active = null;
$zpanelLogo = logo_asset('zpanel');
$testMode = !empty(($GLOBALS['zalorox_config'] ?? [])['FEATURE_TEST_MODE']) && (($GLOBALS['zalorox_config']['APP_ENV'] ?? '') !== 'production');
foreach ($providers as $candidate) {
    if (!empty($candidate['is_active'])) {
        $active = $candidate;
        break;
    }
}
?>
<div class="panel zpanel payment-provider-page">
    <?php require BASE_PATH . '/app/Views/zpanel/_sidebar.php'; ?>

    <section class="panel-body payment-provider-body">
        <div class="section-head">
            <div>
                <p class="eyebrow">ZPanel</p>
                <h1>Sanal POS Ayarlari</h1>
            </div>
            <span class="payment-provider-active">Aktif: <?= e((string) ($active['provider_name'] ?? 'TestPaymentProvider')) ?></span>
        </div>

        <?php if (!empty($loadError)): ?>
            <div class="empty-state small"><strong><?= e((string) $loadError) ?></strong></div>
        <?php endif; ?>

        <div class="provider-grid">
            <?php foreach ($providers as $provider): ?>
                <?php $config = $provider['config_masked'] ?? []; ?>
                <article class="provider-card <?= !empty($provider['is_active']) ? 'is-active' : '' ?>">
                    <div class="provider-card-head">
                        <div>
                            <strong><?= e((string) ($provider['provider_name'] ?? 'Provider')) ?></strong>
                            <span><?= e((string) ($provider['provider_key'] ?? '')) ?></span>
                        </div>
                        <span><?= !empty($provider['is_active']) ? 'Aktif' : 'Pasif' ?></span>
                    </div>

                    <form data-provider-form class="provider-form">
                        <input type="hidden" name="_csrf" value="<?= e($csrfToken) ?>">
                        <input type="hidden" name="provider_key" value="<?= e((string) $provider['provider_key']) ?>">

                        <div class="provider-toggles">
                            <label><input type="checkbox" name="is_active" value="1" <?= !empty($provider['is_active']) ? 'checked' : '' ?>> Aktif provider</label>
                            <label><input type="checkbox" name="is_test_mode" value="1" <?= !empty($provider['is_test_mode']) ? 'checked' : '' ?>> Test modu</label>
                            <label><input type="checkbox" name="installment_enabled" value="1" <?= !empty($config['installment_enabled']) ? 'checked' : '' ?>> Taksit</label>
                            <label><input type="checkbox" name="three_d_secure_enabled" value="1" <?= !empty($config['three_d_secure_enabled']) ? 'checked' : '' ?>> 3D Secure</label>
                        </div>

                        <div class="provider-form-grid">
                            <label>Merchant ID<input name="merchant_id" value="<?= e((string) ($config['merchant_id'] ?? '')) ?>"></label>
                            <label>Merchant Key<input name="merchant_key" value="<?= e((string) ($config['merchant_key'] ?? '')) ?>"></label>
                            <label>API Key<input name="api_key" value="<?= e((string) ($config['api_key'] ?? '')) ?>"></label>
                            <label>API Secret<input name="api_secret" type="password" placeholder="<?= !empty($config['api_secret']) ? '********' : '' ?>" autocomplete="new-password"></label>
                            <label>Merchant Salt<input name="merchant_salt" type="password" placeholder="<?= !empty($config['merchant_salt']) ? '********' : '' ?>" autocomplete="new-password"></label>
                            <label>Test Secret<input name="test_secret" type="password" placeholder="<?= !empty($config['test_secret']) ? '********' : '' ?>" autocomplete="new-password"></label>
                            <label>Callback URL<input name="callback_url" value="<?= e((string) ($config['callback_url'] ?? url('/api/payment/callback/' . (string) $provider['provider_key']))) ?>"></label>
                            <label>Basarili URL<input name="success_url" value="<?= e((string) ($config['success_url'] ?? url('/payment/success/{order_no}'))) ?>"></label>
                            <label>Basarisiz URL<input name="failed_url" value="<?= e((string) ($config['failed_url'] ?? url('/payment/failed/{order_no}'))) ?>"></label>
                        </div>

                        <p class="muted">Secret alanlari maskelenir; bos birakilirsa mevcut deger korunur. Production encryption TODO olarak isaretli.</p>
                        <button class="pill primary" type="submit">Kaydet</button>
                    </form>
                </article>
            <?php endforeach; ?>
        </div>

        <?php if ($providers === [] && $knownProviders !== []): ?>
            <div class="empty-state small">
                <strong>Provider seed kayitlari bulunamadi</strong>
                <p>Migration ve seed calistirildiginde TestPaymentProvider aktif olacak.</p>
            </div>
        <?php endif; ?>
    </section>
</div>
