@extends('saas-admin.layout')

@section('title', $company->name)

@section('content')
    <div class="d-flex align-items-center mb-3">
        <a href="{{ route('saas-admin.companies.index') }}" class="text-decoration-none me-2">&larr;</a>
        <h4 class="m-0">{{ $company->name }}</h4>
        <span class="badge ms-2 {{ $company->statusBadgeClass() }}">{{ ucfirst($company->status) }}</span>
    </div>

    <div class="row g-3">
        <div class="col-md-6">
            <div class="card shadow-sm h-100">
                <div class="card-body">
                    <h6 class="text-uppercase text-muted small mb-3">{{ __('Overview') }}</h6>
                    <p class="mb-1"><strong>{{ __('Owner') }}:</strong>
                        {{ $company->owner?->name ?? '—' }}
                        @if ($company->owner)
                            <span class="text-muted">({{ $company->owner->email }})</span>
                        @endif
                    </p>
                    <p class="mb-1"><strong>{{ __('Domains') }}:</strong></p>
                    <ul class="mb-3">
                        @forelse ($company->domains as $domain)
                            <li>
                                {{ $domain->hostname }}
                                @if ($domain->is_primary)
                                    <span class="badge bg-primary">{{ __('Primary') }}</span>
                                @endif
                                <span class="badge {{ $domain->isVerified() ? 'bg-success' : 'bg-warning text-dark' }}">
                                    {{ $domain->isVerified() ? __('Verified') : __('Pending') }}
                                </span>
                            </li>
                        @empty
                            <li class="text-muted">{{ __('None — this company has no working URL yet.') }}</li>
                        @endforelse
                    </ul>

                    <form action="{{ route('saas-admin.companies.toggle-status', $company) }}" method="POST"
                        onsubmit="return confirm('{{ $company->isActive() ? __('Suspend this company? Its users keep the ability to log in and view data, but nothing can be added or deleted until reactivated.') : __('Reactivate this company?') }}')">
                        @csrf
                        <button type="submit" class="btn btn-sm {{ $company->isActive() ? 'btn-outline-danger' : 'btn-outline-success' }}">
                            {{ $company->isActive() ? __('Suspend') : __('Reactivate') }}
                        </button>
                    </form>
                    @if ($company->isSuspended())
                        <p class="form-text m-0 mt-1">{{ __('Suspended manually — won\'t change on its own until reactivated.') }}</p>
                    @elseif ($company->isPending())
                        <p class="form-text m-0 mt-1">{{ __('Waiting on its subscription\'s start date.') }}</p>
                    @elseif ($company->isExpired())
                        <p class="form-text m-0 mt-1">{{ __('Its subscription\'s end date has passed.') }}</p>
                    @endif
                </div>
            </div>
        </div>

        <div class="col-md-6">
            <div class="card shadow-sm h-100">
                <div class="card-body">
                    <h6 class="text-uppercase text-muted small mb-3">{{ __('Subscription') }}</h6>

                    @php $current = $company->currentSubscription(); @endphp
                    @if ($current)
                        <p class="mb-3">
                            {{ __('On') }} <strong>{{ $current->subscriptionPlan->name }}</strong>
                            @if ($current->started_at)
                                — {{ __('started') }} {{ $current->started_at->format('M j, Y') }}
                            @endif
                            @if ($current->expires_at)
                                — {{ __('ends') }} {{ $current->expires_at->format('M j, Y') }}
                            @else
                                — {{ __('no end date') }}
                            @endif
                        </p>
                    @else
                        <p class="text-muted mb-3">{{ __('No plan assigned yet — the company stays pending until one is.') }}</p>
                    @endif

                    <form action="{{ route('saas-admin.companies.subscription', $company) }}" method="POST" class="row g-2">
                        @csrf
                        <div class="col-12">
                            <select name="subscription_plan_id" class="form-select form-select-sm" required>
                                <option value="">{{ __('Choose a plan') }}</option>
                                @foreach ($plans as $plan)
                                    <option value="{{ $plan->id }}">
                                        {{ $plan->name }} — {{ number_format($plan->price, 2) }}/{{ $plan->billing_interval }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-5">
                            <label class="form-label small m-0">{{ __('Starts') }}</label>
                            <input type="date" name="started_at" class="form-control form-control-sm">
                        </div>
                        <div class="col-5">
                            <label class="form-label small m-0">{{ __('Ends') }}</label>
                            <input type="date" name="expires_at" class="form-control form-control-sm">
                        </div>
                        <div class="col-2 d-flex align-items-end">
                            <button type="submit" class="btn btn-sm btn-primary w-100">{{ __('Set') }}</button>
                        </div>
                    </form>
                    <p class="form-text m-0">{{ __('Blank start = today, blank end = never expires on its own.') }}</p>
                </div>
            </div>
        </div>

        <div class="col-12">
            <div class="card shadow-sm">
                <div class="card-body">
                    <h6 class="text-uppercase text-muted small mb-3">{{ __('Admins') }}</h6>

                    <div class="table-responsive mb-3">
                        <table class="table table-sm align-middle m-0">
                            <thead>
                                <tr>
                                    <th>{{ __('Name') }}</th>
                                    <th>{{ __('Email') }}</th>
                                    <th>{{ __('Status') }}</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($adminUsers as $admin)
                                    <tr>
                                        <td>
                                            {{ $admin->name }}
                                            @if ($admin->id === $company->owner_user_id)
                                                <span class="badge bg-secondary">{{ __('Owner') }}</span>
                                            @endif
                                        </td>
                                        <td>{{ $admin->email }}</td>
                                        <td>
                                            <span class="badge {{ $admin->trashed() ? 'bg-secondary' : 'bg-success' }}">
                                                {{ $admin->trashed() ? __('Deactivated') : __('Active') }}
                                            </span>
                                        </td>
                                        <td class="text-end">
                                            <button type="button" class="btn btn-sm btn-outline-primary"
                                                onclick="openResetPasswordModal('{{ route('saas-admin.companies.users.reset-password', [$company, $admin->id]) }}', '{{ $admin->name }}')">
                                                {{ __('Reset password') }}
                                            </button>
                                            @if ($admin->id !== $company->owner_user_id)
                                                <form action="{{ route('saas-admin.companies.users.toggle-active', [$company, $admin->id]) }}" method="POST" class="d-inline">
                                                    @csrf
                                                    <button type="submit" class="btn btn-sm {{ $admin->trashed() ? 'btn-outline-success' : 'btn-outline-danger' }}">
                                                        {{ $admin->trashed() ? __('Reactivate') : __('Deactivate') }}
                                                    </button>
                                                </form>
                                            @endif
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="4" class="text-center text-muted py-3">{{ __('No admins yet.') }}</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>

                    <form action="{{ route('saas-admin.companies.users.store', $company) }}" method="POST" class="row g-2 align-items-end">
                        @csrf
                        <div class="col-md-3">
                            <label class="form-label small m-0">{{ __('Name') }}</label>
                            <input type="text" name="name" class="form-control form-control-sm" required>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label small m-0">{{ __('Email') }}</label>
                            <input type="email" name="email" class="form-control form-control-sm" required>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label small m-0">{{ __('Password') }}</label>
                            <input type="password" name="password" class="form-control form-control-sm" autocomplete="new-password" required>
                        </div>
                        <div class="col-md-3">
                            <button type="submit" class="btn btn-sm btn-primary w-100">{{ __('+ Add admin') }}</button>
                        </div>
                    </form>
                    @error('name')
                        <p class="text-danger small mt-2 mb-0">{{ $message }}</p>
                    @enderror
                    @error('email')
                        <p class="text-danger small mt-2 mb-0">{{ $message }}</p>
                    @enderror
                </div>
            </div>
        </div>

        @if ($company->slug !== 'abic-e-learning')
            <div class="col-12">
                <div class="card border-danger shadow-sm">
                    <div class="card-body">
                        <h6 class="text-uppercase text-danger small mb-3">{{ __('Danger zone') }}</h6>
                        @if ($company->isExpired())
                            <p class="mb-3">
                                {{ __('Permanently deletes :name and every course, user, and record that belongs to it. A full platform backup is taken automatically first, but this can\'t be undone from here.', ['name' => $company->name]) }}
                            </p>
                            <form action="{{ route('saas-admin.companies.destroy', $company) }}" method="POST"
                                onsubmit="return confirmDelete(event, '{{ $company->name }}')">
                                @csrf
                                @method('DELETE')
                                <label class="form-label small">{{ __('Type the company name to confirm:') }} <strong>{{ $company->name }}</strong></label>
                                <div class="row g-2">
                                    <div class="col-md-8">
                                        <input type="text" id="deleteConfirmInput" class="form-control" autocomplete="off">
                                    </div>
                                    <div class="col-md-4">
                                        <button type="submit" class="btn btn-danger w-100">{{ __('Delete permanently') }}</button>
                                    </div>
                                </div>
                            </form>
                        @else
                            <p class="text-muted mb-0">
                                {{ __('Only an expired company can be deleted — suspend it or let its subscription lapse first. Its current status is') }}
                                <strong>{{ $company->status }}</strong>.
                            </p>
                        @endif
                    </div>
                </div>
            </div>
        @endif
    </div>

    <div class="modal fade" id="resetPasswordModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form id="resetPasswordForm" method="POST">
                    @csrf
                    @method('PUT')
                    <div class="modal-header">
                        <h5 class="modal-title">{{ __('Reset password for') }} <span id="resetPasswordUserName"></span></h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body">
                        <label class="form-label">{{ __('New password') }}</label>
                        <input type="password" name="password" class="form-control" minlength="8" required
                            placeholder="{{ __('Type the password directly — no email is sent') }}">
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">{{ __('Cancel') }}</button>
                        <button type="submit" class="btn btn-primary">{{ __('Set password') }}</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        function openResetPasswordModal(actionUrl, userName) {
            document.getElementById('resetPasswordForm').action = actionUrl;
            document.getElementById('resetPasswordUserName').textContent = userName;
            new bootstrap.Modal(document.getElementById('resetPasswordModal')).show();
        }

        function confirmDelete(event, companyName) {
            const typed = document.getElementById('deleteConfirmInput').value;
            if (typed !== companyName) {
                event.preventDefault();
                alert('{{ __('Type the company name exactly to confirm.') }}');
                return false;
            }
            return confirm('{{ __('This permanently deletes everything for this company. Continue?') }}');
        }
    </script>
@endsection
