{% extends 'admin/layout/base.twig' %}
{% block title %}Edit Parsing Template - {{ app_name }}{% endblock %}
{% block content %}
<div class="op-page-header">
    <div>
        <h1>Edit Parsing Template</h1>
        <p class="op-page-subtitle">Edit parser regex templates and matching fields for the selected SMS sender. (Gateway: <code>{{ template.gateway_slug }}</code> | ID: #{{ template.id }})</p>
    </div>
    <a href="/admin/sms-center" class="op-btn op-btn-outline op-btn-sm">← Back to SMS Center</a>
</div>

<form method="POST" action="/admin/sms-center/{{ template.id }}/edit" class="op-form">
    <input type="hidden" name="_csrf_token" value="{{ csrf_token }}">

    <div class="op-card op-mb-4">
        <div class="op-card-header"><h3>Template Configuration</h3></div>
        <div class="op-card-body">
            <div class="op-form-row">
                <div class="op-form-group op-col-4">
                    <label>Gateway <span class="op-text-muted op-text-xs">(optional)</span></label>
                    <select name="gateway_slug" class="op-input">
                        <option value="">- None -</option>
                        {% for gw in gateways %}
                        <option value="{{ gw.slug }}" {{ gw.slug == template.gateway_slug ? 'selected' : '' }}>{{ gw.name }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="op-form-group op-col-4">
                    <label>Sender Pattern <span class="op-text-danger">*</span></label>
                    <input type="text" name="sender_pattern" class="op-input" value="{{ template.sender_pattern }}" required>
                    <small class="op-text-muted">Regex to match SMS sender (e.g. <code>bKash|16216</code>)</small>
                </div>
                <div class="op-form-group op-col-2">
                    <label>Priority</label>
                    <input type="number" name="priority" class="op-input" value="{{ template.priority }}" min="1" max="999">
                </div>
                <div class="op-form-group op-col-2">
                    <label>Status</label>
                    <select name="status" class="op-input">
                        <option value="active" {{ template.status == 'active' ? 'selected' : '' }}>Active</option>
                        <option value="inactive" {{ template.status == 'inactive' ? 'selected' : '' }}>Inactive</option>
                    </select>
                </div>
            </div>

            <hr class="op-divider">
            <h4 class="op-mb-3">Regex Patterns</h4>
            <p class="op-text-muted op-mb-3">Each regex should contain a <strong>capture group</strong> <code>(...)</code> for the value to extract. Group 1 is used.</p>

            <div class="op-form-group">
                <label>Amount Regex <span class="op-text-danger">*</span></label>
                <input type="text" name="amount_regex" id="edit-amount-regex" class="op-input op-font-mono" value="{{ template.amount_regex }}" required>
                <small class="op-text-muted">Example: <code>Tk\s*([\d,]+\.?\d*)</code> - extracts "1500.00" from "Tk 1,500.00"</small>
            </div>
            <div class="op-form-group">
                <label>Transaction ID Regex</label>
                <input type="text" name="trx_id_regex" id="edit-trxid-regex" class="op-input op-font-mono" value="{{ template.trx_id_regex }}">
                <small class="op-text-muted">Example: <code>TrxID\s*(\w+)</code> - extracts "ABC123XYZ" from "TrxID ABC123XYZ"</small>
            </div>
            <div class="op-form-group">
                <label>Sender Account Regex</label>
                <input type="text" name="sender_regex" id="edit-sender-regex" class="op-input op-font-mono" value="{{ template.sender_regex }}">
                <small class="op-text-muted">Example: <code>from\s*(01\d{9})</code> - extracts phone number</small>
            </div>
        </div>
    </div>

    {# Live tester inline #}
    <div class="op-card op-mb-4">
        <div class="op-card-header"><h3>🧪 Test Against Sample SMS</h3></div>
        <div class="op-card-body">
            <div class="op-form-group">
                <label>Paste Sample SMS</label>
                <textarea id="sample-sms" rows="3" class="op-input" placeholder="You have received Tk 1,500.00 from 01712345678. TrxID ABC123XYZ. Your bKash balance is Tk 5,000.00."></textarea>
            </div>
            <button type="button" class="op-btn op-btn-outline" id="test-all-btn">Test All Patterns</button>
            <div id="test-results" class="op-mt-3"></div>
        </div>
    </div>

    <div class="op-form-actions">
        <button type="submit" class="op-btn op-btn-primary op-btn-lg">Save Template</button>
        <a href="/admin/sms-center" class="op-btn op-btn-ghost">Cancel</a>
    </div>
</form>


{% endblock %}
{% block scripts %}<script nonce="{{ csp_nonce }}" src="/assets/js/pages/sms-template-edit.js"></script>{% endblock %}

