<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $model PortalPeople\paygate\models\SettingsForm */

$this->title = 'PayGate (Redsys) Settings';
$this->params['breadcrumbs'][] = ['label' => 'Settings', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

\PortalPeople\paygate\assets\SettingsAsset::register($this);
?>

<div class="paygate-config-index">
    <div class="row">
        <div class="col-md-10">
            <?php $form = ActiveForm::begin([
                'id' => 'paygate-settings-form',
                'action' => ['settings/index'],
                'method' => 'post',
                'options' => ['class' => 'form-horizontal'],
                'enableAjaxValidation' => true,
                'enableClientValidation' => true,
                'validationUrl' => ['settings/validate'],
                'fieldConfig' => [
                    'template' => "{label}\n<div class=\"col-sm-8\">{input}\n{hint}\n{error}</div>",
                    'labelOptions' => ['class' => 'col-sm-4 control-label'],
                ],
            ]); ?>

            <!-- Main Configuration Card -->
            <div class="card border-primary">
                <div class="card-header bg-primary text-white">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-credit-card"></i> PayGate Connection Settings
                    </h5>
                </div>
                <div class="card-body">
                    <?= $form->field($model, 'enabled')->checkbox(['label' => 'Enable PayGate/Redsys Integration']) ?>
                    
                    <?= $form->field($model, 'environment')->dropDownList([
                        'sandbox' => 'Sandbox (Testing)',
                        'production' => 'Production (Live)',
                    ], ['prompt' => 'Select Environment...']) ?>
                    
                    <?= $form->field($model, 'merchant_code')->textInput([
                        'maxlength' => true, 
                        'placeholder' => 'e.g., 999008881'
                    ]) ?>
                    
                    <?= $form->field($model, 'terminal')->textInput([
                        'maxlength' => true, 
                        'placeholder' => 'e.g., 001',
                        'value' => $model->terminal ?: '1'
                    ]) ?>
                    
                    <?= $form->field($model, 'currency')->dropDownList([
                        '978' => 'EUR (978)',
                        '826' => 'GBP (826)',
                        '840' => 'USD (840)',
                    ], ['prompt' => 'Select Currency...']) ?>
                    
                    <?= $form->field($model, 'transaction_type')->dropDownList([
                        '0' => '0 - Authorization',
                        'A' => 'A - Pre-authorization',
                        '1' => '1 - Pre-authorization Confirmation',
                    ], ['prompt' => 'Select Transaction Type...']) ?>
                    
                    <?= $form->field($model, 'secret_key')->passwordInput([
                        'maxlength' => true, 
                        'placeholder' => 'Secret Key (SHA-256)',
                        'autocomplete' => 'new-password'
                    ]) ?>
                </div>
            </div>

            <!-- Payment Settings Card -->
            <div class="card border-info mt-4">
                <div class="card-header bg-info text-white">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-cog"></i> Payment Settings
                    </h5>
                </div>
                <div class="card-body">
                    <?= $form->field($model, 'auto_capture')->checkbox([
                        'label' => 'Automatically capture payments after authorization'
                    ]) ?>
                    
                    <?= $form->field($model, 'debug')->checkbox([
                        'label' => 'Enable debug logging'
                    ]) ?>
                </div>
            </div>

            <!-- URL Configuration Card -->
            <div class="card border-warning mt-4">
                <div class="card-header bg-warning text-dark">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-link"></i> URL Configuration
                    </h5>
                </div>
                <div class="card-body">
                    <?= $form->field($model, 'notification_url')->textInput([
                        'maxlength' => true,
                        'placeholder' => 'https://example.com/paygate/notification'
                    ]) ?>
                    
                    <?= $form->field($model, 'success_url')->textInput([
                        'maxlength' => true,
                        'placeholder' => 'https://example.com/payment/success'
                    ]) ?>
                    
                    <?= $form->field($model, 'error_url')->textInput([
                        'maxlength' => true,
                        'placeholder' => 'https://example.com/payment/error'
                    ]) ?>
                </div>
            </div>

            <!-- Information Card -->
            <div class="card border-secondary mt-4">
                <div class="card-header bg-secondary text-white">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-info-circle"></i> Integration Information
                    </h5>
                </div>
                <div class="card-body">
                    <div class="alert alert-info">
                        <h6><strong>PayGate/Redsys Configuration Guide:</strong></h6>
                        <ul class="mb-0">
                            <li><strong>Merchant Code (FUC):</strong> Your unique merchant identification number provided by your bank</li>
                            <li><strong>Terminal:</strong> Usually "001" unless you have multiple terminals</li>
                            <li><strong>Currency:</strong> 978 for EUR, 826 for GBP, 840 for USD</li>
                            <li><strong>Secret Key:</strong> SHA-256 encryption key provided by your bank</li>
                            <li><strong>Environment:</strong> Use Sandbox for testing, Production for live payments</li>
                        </ul>
                    </div>
                    
                    <div class="alert alert-warning">
                        <strong><i class="fas fa-exclamation-triangle"></i> Important:</strong>
                        <ul class="mb-0">
                            <li>Always test in sandbox environment before going live</li>
                            <li>Keep your secret key secure and never share it</li>
                            <li>Configure notification URLs in your PayGate/Redsys merchant panel</li>
                            <li>Ensure your server can receive POST notifications from Redsys servers</li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- Action Buttons -->
            <div class="form-group mt-4">
                <div class="col-sm-offset-4 col-sm-8">
                    <?= Html::submitButton(
                        '<i class="fas fa-save"></i> Save Configuration', 
                        ['class' => 'btn btn-primary']
                    ) ?>
                    
                    <?= Html::button(
                        '<i class="fas fa-check-circle"></i> Test Configuration', 
                        [
                            'class' => 'btn btn-info', 
                            'id' => 'pg-test-connection-btn',
                            'data-url' => Url::to('/NGauge/paygate/settings/test-connection')
                        ]
                    ) ?>
                </div>
            </div>

            <?php ActiveForm::end(); ?>

            <div id="connection-result" class="mt-3"></div>
        </div>

        <!-- Sidebar with Quick Stats/Info -->
        <div class="col-md-2">
            <div class="card border-light">
                <div class="card-header bg-light">
                    <h6 class="card-title mb-0">Quick Links</h6>
                </div>
                <div class="card-body">
                    <ul class="list-unstyled">
                        <li><a href="https://sis.redsys.es/sis/Manual/PDF/Manual_integracion_2020_es.pdf" target="_blank" class="text-primary">
                            <i class="fas fa-book"></i> Integration Manual
                        </a></li>
                        <li><a href="https://pagosonline.redsys.es/conexion-insite.html" target="_blank" class="text-primary">
                            <i class="fas fa-code"></i> API Documentation
                        </a></li>
                        <li><a href="https://sis-t.redsys.es:25443/canales/" target="_blank" class="text-primary">
                            <i class="fas fa-flask"></i> Test Environment
                        </a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
