{extends file="layout.tpl"}

{block name="content"}
<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <!-- Header -->
            <div class="d-flex justify-content-between align-items-center mb-4">
                <div>
                    <h4 class="mb-1">Sklonuj urządzenie</h4>
                    <p class="text-muted mb-0">Utwórz kopię urządzenia wraz z playlistami</p>
                </div>
                <a href="{$baseUrl}/devices" class="btn btn-outline-secondary">
                    <i class="bi bi-arrow-left me-1"></i>Powrót
                </a>
            </div>

            <div class="row">
                <!-- Źródło -->
                <div class="col-md-5">
                    <div class="card mb-4">
                        <div class="card-header">
                            <h6 class="mb-0"><i class="bi bi-device-hdd me-2"></i>Urządzenie źródłowe</h6>
                        </div>
                        <div class="card-body">
                            <table class="table table-sm mb-0">
                                <tr>
                                    <th class="text-muted" style="width: 40%">Nazwa:</th>
                                    <td><strong>{$sourceDevice.name|default:'Brak nazwy'}</strong></td>
                                </tr>
                                <tr>
                                    <th class="text-muted">MAC:</th>
                                    <td><code>{$sourceDevice.mac}</code></td>
                                </tr>
                                <tr>
                                    <th class="text-muted">Kod:</th>
                                    <td><code>{$sourceDevice.code}</code></td>
                                </tr>
                                <tr>
                                    <th class="text-muted">Właściciel:</th>
                                    <td>{$sourceDevice.username}</td>
                                </tr>
                                <tr>
                                    <th class="text-muted">Playlisty:</th>
                                    <td><span class="badge bg-primary">{$sourcePlaylists|count}</span></td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    <!-- Lista playlist do sklonowania -->
                    {if $sourcePlaylists|count > 0}
                    <div class="card">
                        <div class="card-header">
                            <h6 class="mb-0"><i class="bi bi-list-ul me-2"></i>Playlisty do skopiowania</h6>
                        </div>
                        <div class="card-body p-0">
                            <ul class="list-group list-group-flush">
                                {foreach $sourcePlaylists as $playlist}
                                <li class="list-group-item d-flex justify-content-between align-items-center">
                                    <span>
                                        {if $playlist.type == 'xtream'}
                                        <i class="bi bi-broadcast text-primary me-2"></i>
                                        {elseif $playlist.type == 'm3u'}
                                        <i class="bi bi-link-45deg text-success me-2"></i>
                                        {else}
                                        <i class="bi bi-hdd-network text-warning me-2"></i>
                                        {/if}
                                        {$playlist.name}
                                    </span>
                                    <span class="badge bg-secondary">{$playlist.type}</span>
                                </li>
                                {/foreach}
                            </ul>
                        </div>
                    </div>
                    {/if}
                </div>

                <!-- Strzałka -->
                <div class="col-md-2 d-flex align-items-center justify-content-center">
                    <i class="bi bi-arrow-right-circle text-primary" style="font-size: 2.5rem;"></i>
                </div>

                <!-- Formularz nowego urządzenia -->
                <div class="col-md-5">
                    <div class="card">
                        <div class="card-header">
                            <h6 class="mb-0"><i class="bi bi-plus-circle me-2"></i>Nowe urządzenie</h6>
                        </div>
                        <div class="card-body">
                            {if $error|default:false}
                            <div class="alert alert-danger">
                                <i class="bi bi-exclamation-circle me-2"></i>{$error}
                            </div>
                            {/if}

                            <form method="post" action="{$baseUrl}/devices/clone?id={$sourceDevice.id}">
                                <input type="hidden" name="csrf_token" value="{$csrfToken}">

                                {if $isAdmin}
                                <div class="mb-3">
                                    <label class="form-label">Właściciel <span class="text-danger">*</span></label>
                                    <select name="user_id" class="form-select" required>
                                        {foreach $users as $user}
                                        <option value="{$user.id}" 
                                                {if ($data.user_id|default:$currentUser.id) == $user.id}selected{/if}
                                                data-limit="{if $user.role == 'reseller'}{$user.max_devices}{elseif $user.role == 'admin'}999999{else}{$user.device_limit}{/if}"
                                                data-count="{if $user.role == 'reseller'}{$user.devices_count + $user.devices_allocated}{else}{$user.devices_count}{/if}">
                                            {$user.username}
                                            {if $user.role == 'reseller'}
                                                ({$user.devices_count + $user.devices_allocated}/{$user.max_devices} urządzeń)
                                            {elseif $user.role != 'admin'}
                                                ({$user.devices_count}/{$user.device_limit} urządzeń)
                                            {/if}
                                        </option>
                                        {/foreach}
                                    </select>
                                </div>
                                {/if}

                                <div class="mb-3">
                                    <label class="form-label">Nazwa urządzenia</label>
                                    <div class="input-group">
                                        <span class="input-group-text"><i class="bi bi-tag"></i></span>
                                        <input type="text" 
                                               name="name" 
                                               class="form-control" 
                                               value="{$data.name|default:"{$sourceDevice.name} (kopia)"}"
                                               placeholder="np. TV Salon">
                                    </div>
                                </div>

                                <div class="mb-3">
                                    <label class="form-label">Adres MAC <span class="text-danger">*</span></label>
                                    <div class="input-group">
                                        <span class="input-group-text"><i class="bi bi-ethernet"></i></span>
                                        <input type="text" 
                                               name="mac" 
                                               class="form-control" 
                                               value="{$data.mac|default:''}"
                                               placeholder="00:1A:2B:3C:4D:5E"
                                               required>
                                    </div>
                                    <small class="text-muted">Format: XX:XX:XX:XX:XX:XX</small>
                                </div>

                                <div class="mb-4">
                                    <label class="form-label">Kod dostępu <span class="text-danger">*</span></label>
                                    <div class="input-group">
                                        <span class="input-group-text"><i class="bi bi-key"></i></span>
                                        <input type="text" 
                                               name="code" 
                                               class="form-control" 
                                               value="{$data.code|default:''}"
                                               minlength="3"
                                               required>
                                    </div>
                                    <small class="text-muted">Kod wyświetlany na urządzeniu</small>
                                </div>

                                <div class="alert alert-info mb-4">
                                    <i class="bi bi-info-circle me-2"></i>
                                    <strong>{$sourcePlaylists|count}</strong> playlist zostanie skopiowanych do nowego urządzenia.
                                </div>

                                <div class="d-grid">
                                    <button type="submit" class="btn btn-primary btn-lg">
                                        <i class="bi bi-copy me-2"></i>Sklonuj urządzenie
                                    </button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}

{block name="scripts"}
<script>
// Formatowanie MAC address
document.querySelector('input[name="mac"]')?.addEventListener('input', function(e) {
    let value = e.target.value.replace(/[^0-9A-Fa-f]/g, '').toUpperCase();
    let formatted = '';
    for (let i = 0; i < value.length && i < 12; i++) {
        if (i > 0 && i % 2 === 0) formatted += ':';
        formatted += value[i];
    }
    e.target.value = formatted;
});

{if $isAdmin}
// Sprawdź limit przy zmianie użytkownika
document.querySelector('select[name="user_id"]')?.addEventListener('change', function() {
    const selected = this.options[this.selectedIndex];
    const limit = parseInt(selected.dataset.limit);
    const count = parseInt(selected.dataset.count);
    
    if (count >= limit) {
        alert('Wybrany użytkownik osiągnął limit urządzeń!');
    }
});
{/if}
</script>
{/block}
