@extends('admin.layouts.app')

@section('title', __('admin/license.title'))

@section('content')
<div class="container-fluid px-4">
    <x-admin.page-header
        :title="__('admin/license.title')"
        :subtitle="__('admin/license.subtitle')"
        icon="fas fa-key"
    />

    <x-admin.alert />

    @php
        $badge = ['self' => 'secondary', 'valid' => 'success', 'grace' => 'warning', 'unlicensed' => 'warning', 'invalid' => 'danger'];
        $color = $badge[$state['state']] ?? 'secondary';
    @endphp

    <div class="row g-3">
        
        <div class="col-lg-6">
            <x-admin.card class="border-0 shadow-sm h-100">
                <x-slot:header>
                    <h6 class="mb-0"><i class="fas fa-circle-info me-2"></i>{{ __('admin/license.status_title') }}</h6>
                </x-slot:header>
                <div class="d-flex align-items-center mb-3">
                    <span class="badge bg-{{ $color }} me-2">{{ strtoupper($state['state']) }}</span>
                    <span class="text-muted small">{{ $state['message'] }}</span>
                </div>
                <table class="table table-sm mb-0">
                    <tr><th style="width:40%">{{ __('admin/license.mode') }}</th><td>{{ $mode }}</td></tr>
                    <tr><th>{{ __('admin/license.key') }}</th><td><code>{{ $keyMasked }}</code></td></tr>
                    <tr><th>{{ __('admin/license.expires') }}</th><td>{{ $state['expires_at'] ? \Illuminate\Support\Carbon::parse($state['expires_at'])->toDateString() : '—' }}</td></tr>
                </table>
            </x-admin.card>
        </div>

        
        <div class="col-lg-6">
            <x-admin.card class="border-0 shadow-sm h-100">
                <x-slot:header>
                    <h6 class="mb-0"><i class="fas fa-sitemap me-2"></i>{{ __('admin/license.downstream_title') }}</h6>
                </x-slot:header>
                <p class="text-muted small">{{ __('admin/license.downstream_desc') }}</p>
                <div class="input-group">
                    <span class="input-group-text"><i class="fas fa-link"></i></span>
                    <input type="text" class="form-control" value="{{ $downstream }}" readonly onclick="this.select()">
                </div>
                <div class="form-text small mt-2">{{ __('admin/license.downstream_hint') }}</div>
            </x-admin.card>
        </div>
    </div>

    
    <x-admin.card class="border-0 shadow-sm mt-3">
        <x-slot:header>
            <h6 class="mb-0"><i class="fas fa-sliders me-2"></i>{{ __('admin/license.config_title') }}</h6>
        </x-slot:header>
        <form method="POST" action="{{ route('admin.system.license.update') }}">
            @csrf
            <div class="row g-3">
                <div class="col-md-4">
                    <label class="form-label">{{ __('admin/license.mode') }}</label>
                    <select name="mode" class="form-select">
                        <option value="self" @selected($mode === 'self')>self — {{ __('admin/license.mode_self') }}</option>
                        <option value="client" @selected($mode === 'client')>client — {{ __('admin/license.mode_client') }}</option>
                    </select>
                </div>
                <div class="col-md-8">
                    <label class="form-label">{{ __('admin/license.server_url') }}</label>
                    <input type="url" name="server_url" value="{{ old('server_url', $serverUrl) }}" class="form-control" placeholder="https://lisans.saglayici.com">
                    <div class="form-text small">{{ __('admin/license.server_url_hint') }}</div>
                </div>
            </div>
            <div class="mt-3 d-flex gap-2">
                <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i>{{ __('admin/license.save') }}</button>
            </div>
        </form>
        <hr>
        <form method="POST" action="{{ route('admin.system.license.test') }}">
            @csrf
            <button type="submit" class="btn btn-outline-secondary"><i class="fas fa-plug me-1"></i>{{ __('admin/license.test') }}</button>
            <span class="text-muted small ms-2">{{ __('admin/license.test_hint') }}</span>
        </form>
    </x-admin.card>
</div>
@endsection
