@extends('layouts.sadmin')
@section('title', 'Add Company')

@section('content')
<div class="d-flex align-items-center gap-3 mb-4">
    <a href="{{ route('sadmin.companies') }}" class="btn btn-outline-secondary btn-sm">
        <i class="bi bi-arrow-left"></i>
    </a>
    <div>
        <h1 class="page-title mb-0">Add New Company</h1>
        <p class="text-muted mb-0" style="font-size:13px">Create a new tenant company and admin user</p>
    </div>
</div>

@if($errors->any())
    <div class="alert alert-danger mb-4">
        <div class="fw-semibold mb-1"><i class="bi bi-exclamation-triangle me-1"></i>Please fix the following:</div>
        <ul class="mb-0 ps-3" style="font-size:13px">
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

<form method="POST" action="{{ route('sadmin.companies.store') }}" id="createForm">
@csrf
<div class="row g-4">

    <!-- Company Info -->
    <div class="col-12 col-xl-6">
        <div class="card h-100">
            <div class="card-header"><i class="bi bi-building me-2 text-primary"></i>Company Information</div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12">
                        <label class="form-label">Company Name <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control @error('name') is-invalid @enderror"
                               value="{{ old('name') }}" placeholder="e.g. Acme Corporation Ltd" required>
                        @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-6">
                        <label class="form-label">Company Code <span class="text-danger">*</span></label>
                        <input type="text" name="company_code" class="form-control @error('company_code') is-invalid @enderror"
                               value="{{ old('company_code') }}" placeholder="e.g. ACME" maxlength="20"
                               style="text-transform:uppercase" required>
                        <div class="form-text">Unique ID used at login. Uppercase letters/numbers.</div>
                        @error('company_code')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-6">
                        <label class="form-label">Country</label>
                        <input type="text" name="country" class="form-control"
                               value="{{ old('country') }}" placeholder="e.g. Bangladesh">
                    </div>
                    <div class="col-6">
                        <label class="form-label">Email</label>
                        <input type="email" name="email" class="form-control"
                               value="{{ old('email') }}" placeholder="info@company.com">
                    </div>
                    <div class="col-6">
                        <label class="form-label">Phone</label>
                        <input type="text" name="phone" class="form-control"
                               value="{{ old('phone') }}" placeholder="+880 1234 567890">
                    </div>
                    <div class="col-12">
                        <label class="form-label">Address</label>
                        <textarea name="address" class="form-control" rows="2"
                                  placeholder="Company address">{{ old('address') }}</textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Admin User -->
    <div class="col-12 col-xl-6">
        <div class="card">
            <div class="card-header"><i class="bi bi-person-badge me-2 text-primary"></i>Company Admin Account</div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12">
                        <label class="form-label">Admin Full Name <span class="text-danger">*</span></label>
                        <input type="text" name="admin_name" class="form-control @error('admin_name') is-invalid @enderror"
                               value="{{ old('admin_name') }}" placeholder="e.g. John Smith" required>
                        @error('admin_name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-6">
                        <label class="form-label">Username <span class="text-danger">*</span></label>
                        <input type="text" name="admin_username" class="form-control @error('admin_username') is-invalid @enderror"
                               value="{{ old('admin_username') }}" placeholder="e.g. admin" required>
                        <div class="form-text">Login username (lowercase)</div>
                        @error('admin_username')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-6">
                        <label class="form-label">Admin Email</label>
                        <input type="email" name="admin_email" class="form-control"
                               value="{{ old('admin_email') }}" placeholder="admin@company.com">
                    </div>
                    <div class="col-6">
                        <label class="form-label">Password <span class="text-danger">*</span></label>
                        <input type="password" name="admin_password" class="form-control @error('admin_password') is-invalid @enderror"
                               placeholder="Min 8 characters" required>
                        @error('admin_password')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-6">
                        <label class="form-label">Confirm Password <span class="text-danger">*</span></label>
                        <input type="password" name="admin_password_confirmation"
                               class="form-control @error('admin_password') is-invalid @enderror"
                               placeholder="Repeat password" required>
                        @error('admin_password')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                </div>
            </div>
        </div>

        <!-- Subscription -->
        <div class="card mt-4">
            <div class="card-header"><i class="bi bi-credit-card me-2 text-primary"></i>Subscription Settings</div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12">
                        <label class="form-label">Group of Companies</label>
                        <select name="company_group_id" class="form-select @error('company_group_id') is-invalid @enderror">
                            <option value="">— No group —</option>
                            @foreach($groups as $group)
                                <option value="{{ $group->id }}" {{ old('company_group_id') == $group->id ? 'selected' : '' }}>
                                    {{ $group->group_code }} — {{ $group->name }}
                                </option>
                            @endforeach
                        </select>
                        @if($groups->isEmpty())
                            <div class="form-text text-warning">
                                <i class="bi bi-info-circle me-1"></i>
                                No groups yet.
                                <a href="{{ route('sadmin.companies') }}">Create a group</a> on the Subscription page first.
                            </div>
                        @else
                            <div class="form-text">Tag this company with a Group ID and name for shared login / company selection.</div>
                        @endif
                        @error('company_group_id')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-6">
                        <label class="form-label">Max Concurrent Users <span class="text-danger">*</span></label>
                        <input type="number" name="max_concurrent_users" class="form-control"
                               value="{{ old('max_concurrent_users', 5) }}" min="1" max="1000" required>
                    </div>
                    <div class="col-6">
                        <label class="form-label">Session Timeout (mins) <span class="text-danger">*</span></label>
                        <input type="number" name="session_timeout_minutes" class="form-control"
                               value="{{ old('session_timeout_minutes', 120) }}" min="15" max="1440" required>
                    </div>
                    <div class="col-6">
                        <label class="form-label">Trial Ends At</label>
                        <input type="date" name="trial_ends_at" class="form-control"
                               value="{{ old('trial_ends_at', now()->addDays(30)->format('Y-m-d')) }}">
                    </div>
                    <div class="col-6">
                        <label class="form-label">Subscription Ends At</label>
                        <input type="date" name="subscription_ends_at" class="form-control"
                               value="{{ old('subscription_ends_at') }}">
                    </div>
                    <div class="col-6">
                        <label class="form-label">Monthly Fee</label>
                        <div class="input-group">
                            <span class="input-group-text">$</span>
                            <input type="number" name="monthly_fee" class="form-control"
                                   value="{{ old('monthly_fee', 0) }}" step="0.01" min="0">
                        </div>
                    </div>
                    <div class="col-12">
                        <label class="form-label">Internal Notes</label>
                        <textarea name="notes" class="form-control" rows="2"
                                  placeholder="Internal notes about this client">{{ old('notes') }}</textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Actions -->
    <div class="col-12">
        <div class="d-flex gap-3 justify-content-end">
            <a href="{{ route('sadmin.companies') }}" class="btn btn-outline-secondary">
                <i class="bi bi-x me-1"></i>Cancel
            </a>
            <button type="submit" class="btn btn-primary px-4" id="submitBtn">
                <i class="bi bi-check-circle me-1"></i>Create Company
            </button>
        </div>
    </div>
</div>
</form>
@endsection

@push('scripts')
<script>
document.getElementById('createForm').addEventListener('submit', function (e) {
    if (!this.checkValidity()) {
        return;
    }
    const btn = document.getElementById('submitBtn');
    btn.innerHTML = '<span class="spinner-border spinner-border-sm me-2"></span>Creating...';
    btn.disabled = true;
});
window.addEventListener('pageshow', function () {
    const btn = document.getElementById('submitBtn');
    if (btn) {
        btn.disabled = false;
        btn.innerHTML = '<i class="bi bi-check-circle me-1"></i>Create Company';
    }
});
// Auto uppercase company code
document.querySelector('[name="company_code"]').addEventListener('input', function() {
    this.value = this.value.toUpperCase().replace(/[^A-Z0-9]/g, '');
});
</script>
@endpush
