@extends('layouts.app')

@section('title', $app_setting['name'] . ' | ' . __('Custom Domain'))

@section('content')
    <div class="app-main-outer">
        <div class="app-main-inner">
            <div class="page-title-actions px-3 d-flex">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="{{ route('admin.dashboard') }}">{{ __('Dashboard') }}</a></li>
                        <li class="breadcrumb-item active" aria-current="page">{{ __('Custom Domain') }}</li>
                    </ol>
                </nav>
            </div>

            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body">
                            <h3 class="m-0 p-0">{{ __('Custom Domain') }}</h3>
                            <p class="text-muted mb-0">
                                {{ __('Point a domain you already own at your account — visitors see it as your own site, nothing to install on your end beyond the DNS records below.') }}
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            @if (session('success'))
                <div class="alert alert-success mt-3">{{ session('success') }}</div>
            @endif
            @if (session('error'))
                <div class="alert alert-danger mt-3">{{ session('error') }}</div>
            @endif
            @if ($errors->any())
                <div class="alert alert-danger mt-3">
                    <ul class="m-0">
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <div class="row my-3">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table align-middle">
                                    <thead>
                                        <tr>
                                            <th>{{ __('Domain') }}</th>
                                            <th>{{ __('Type') }}</th>
                                            <th>{{ __('Status') }}</th>
                                            <th></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach ($domains as $domain)
                                            <tr>
                                                <td>
                                                    {{ $domain->hostname }}
                                                    @if ($domain->is_primary)
                                                        <span class="badge bg-primary ms-1">{{ __('Primary') }}</span>
                                                    @endif
                                                </td>
                                                <td>{{ $domain->is_primary ? __('Subdomain') : __('Custom') }}</td>
                                                <td>
                                                    @if ($domain->isVerified())
                                                        <span class="badge bg-success">{{ __('Verified') }}</span>
                                                    @else
                                                        <span class="badge bg-warning text-dark">{{ __('Pending verification') }}</span>
                                                    @endif
                                                </td>
                                                <td class="text-end">
                                                    @unless ($domain->isVerified())
                                                        <form action="{{ route('domain.verify', $domain) }}" method="POST" class="d-inline">
                                                            @csrf
                                                            <button type="submit" class="btn btn-sm btn-outline-primary">
                                                                {{ __('Check verification') }}
                                                            </button>
                                                        </form>
                                                    @endunless
                                                    @unless ($domain->is_primary)
                                                        <a href="{{ route('domain.destroy', $domain) }}"
                                                            class="btn btn-sm btn-outline-danger"
                                                            onclick="return confirm('{{ __('Remove this domain?') }}')">
                                                            {{ __('Remove') }}
                                                        </a>
                                                    @endunless
                                                </td>
                                            </tr>
                                            @unless ($domain->isVerified())
                                                <tr>
                                                    <td colspan="4" class="bg-light">
                                                        <p class="mb-2 fw-bold">{{ __('Add these two DNS records at your registrar, then check verification above:') }}</p>
                                                        <div class="table-responsive">
                                                            <table class="table table-sm table-bordered bg-white mb-0">
                                                                <thead>
                                                                    <tr>
                                                                        <th>{{ __('Type') }}</th>
                                                                        <th>{{ __('Host') }}</th>
                                                                        <th>{{ __('Value') }}</th>
                                                                    </tr>
                                                                </thead>
                                                                <tbody>
                                                                    <tr>
                                                                        <td>TXT</td>
                                                                        <td><code>_abic-verify.{{ $domain->hostname }}</code></td>
                                                                        <td><code>{{ $domain->verification_token }}</code></td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td>CNAME</td>
                                                                        <td><code>{{ $domain->hostname }}</code></td>
                                                                        <td><code>{{ tenantRootDomain() }}</code></td>
                                                                    </tr>
                                                                </tbody>
                                                            </table>
                                                        </div>
                                                        <p class="text-muted small mt-2 mb-0">
                                                            {{ __('DNS changes can take a few minutes to a few hours to reach us. Once the TXT record is visible, a certificate for this domain is issued automatically on its first visit — nothing further to configure.') }}
                                                        </p>
                                                    </td>
                                                </tr>
                                            @endunless
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row my-3">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="mb-3">{{ __('Add a custom domain') }}</h5>
                            <form action="{{ route('domain.store') }}" method="POST" class="row g-2 align-items-start">
                                @csrf
                                <div class="col-md-6">
                                    <input type="text" name="hostname" class="form-control"
                                        placeholder="{{ __('e.g. www.yourcompany.com') }}" value="{{ old('hostname') }}">
                                </div>
                                <div class="col-md-auto">
                                    <button type="submit" class="btn btn-primary">{{ __('Add domain') }}</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
