@extends('layouts.app')

@section('title', 'Edit Company')
@section('heading', 'Edit Company')

@section('content')
    <div class="grid gap-6 xl:grid-cols-3">
        <x-card class="xl:col-span-2">
            <form action="{{ route('superadmin.companies.update', $company) }}" method="post" class="max-w-2xl space-y-6">
            @csrf
            @method('PUT')
            <div>
                <label for="name" class="block text-sm font-medium text-slate-700">Name <span class="text-red-500">*</span></label>
                <input type="text" name="name" id="name" value="{{ old('name', $company->name) }}" required
                       class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 focus:border-slate-500 focus:outline-none focus:ring-1 focus:ring-slate-500">
                @error('name') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
            </div>
            <div>
                <label for="short_name" class="block text-sm font-medium text-slate-700">Short name</label>
                <input type="text" name="short_name" id="short_name" value="{{ old('short_name', $company->short_name) }}" maxlength="100"
                       class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 focus:border-slate-500 focus:outline-none focus:ring-1 focus:ring-slate-500">
                @error('short_name') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
            </div>
            <div class="grid gap-4 sm:grid-cols-2">
                <div>
                    <label for="email" class="block text-sm font-medium text-slate-700">Email</label>
                    <input type="email" name="email" id="email" value="{{ old('email', $company->email) }}"
                           class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 focus:border-slate-500 focus:outline-none focus:ring-1 focus:ring-slate-500">
                    @error('email') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
                </div>
                <div>
                    <label for="phone" class="block text-sm font-medium text-slate-700">Phone</label>
                    <div class="mt-1 flex gap-2">
                        <div class="inline-flex items-center rounded-lg border border-slate-300 bg-slate-100 px-3 py-2 text-sm font-medium text-slate-700">+91</div>
                        <input type="text" name="phone" id="phone" value="{{ old('phone', $company->phone) }}" maxlength="50"
                               class="w-full rounded-lg border border-slate-300 px-3 py-2 focus:border-slate-500 focus:outline-none focus:ring-1 focus:ring-slate-500">
                    </div>
                    @error('phone') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
                </div>
            </div>
            <div>
                <label for="website" class="block text-sm font-medium text-slate-700">Website</label>
                <input type="text" name="website" id="website" value="{{ old('website', $company->website) }}" maxlength="255"
                       class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 focus:border-slate-500 focus:outline-none focus:ring-1 focus:ring-slate-500">
                @error('website') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
            </div>
            <div>
                <label for="address" class="block text-sm font-medium text-slate-700">Address</label>
                <textarea name="address" id="address" rows="2" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 focus:border-slate-500 focus:outline-none focus:ring-1 focus:ring-slate-500">{{ old('address', $company->address) }}</textarea>
                @error('address') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
            </div>
            <div class="grid gap-4 sm:grid-cols-2">
                <div>
                    <label for="status" class="block text-sm font-medium text-slate-700">Status <span class="text-red-500">*</span></label>
                    <select name="status" id="status" required class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 focus:border-slate-500 focus:outline-none focus:ring-1 focus:ring-slate-500 js-select2">
                        <option value="active" {{ old('status', $company->status) === 'active' ? 'selected' : '' }}>Active</option>
                        <option value="inactive" {{ old('status', $company->status) === 'inactive' ? 'selected' : '' }}>Inactive</option>
                        <option value="suspended" {{ old('status', $company->status) === 'suspended' ? 'selected' : '' }}>Suspended</option>
                    </select>
                    @error('status') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
                </div>
                <div>
                    <label for="subscription_plan_id" class="block text-sm font-medium text-slate-700">Subscription plan</label>
                    <select name="subscription_plan_id" id="subscription_plan_id" class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 focus:border-slate-500 focus:outline-none focus:ring-1 focus:ring-slate-500 js-select2">
                        <option value="">— None —</option>
                        @foreach($plans as $plan)
                            <option value="{{ $plan->id }}" {{ old('subscription_plan_id', $company->subscription_plan_id) == $plan->id ? 'selected' : '' }}>{{ $plan->name }}</option>
                        @endforeach
                    </select>
                    @error('subscription_plan_id') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
                </div>
            </div>
            <div class="grid gap-4 sm:grid-cols-2">
                <div>
                    <label for="licence_expire_on" class="block text-sm font-medium text-slate-700">Licence expire on</label>
                    <input type="date" name="licence_expire_on" id="licence_expire_on" value="{{ old('licence_expire_on', $company->licence_expire_on?->format('Y-m-d')) }}"
                           class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 focus:border-slate-500 focus:outline-none focus:ring-1 focus:ring-slate-500">
                    @error('licence_expire_on') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
                </div>
                <div>
                    <label for="trial_ends_at" class="block text-sm font-medium text-slate-700">Trial ends at</label>
                    <input type="date" name="trial_ends_at" id="trial_ends_at" value="{{ old('trial_ends_at', $company->trial_ends_at?->format('Y-m-d')) }}"
                           class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 focus:border-slate-500 focus:outline-none focus:ring-1 focus:ring-slate-500">
                    @error('trial_ends_at') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
                </div>
            </div>
            <div class="flex gap-3">
                <button type="submit" class="rounded-lg bg-primary px-4 py-2 text-sm font-medium text-white">Update Company</button>
                <a href="{{ route('superadmin.companies.index') }}" class="rounded-lg border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Cancel</a>
            </div>
            </form>
        </x-card>

        <x-card>
            <h3 class="text-base font-semibold text-slate-900">Admin account actions</h3>
            <p class="mt-1 text-sm text-slate-600">Update or reset this company admin password.</p>

            <div class="mt-4 rounded-lg border border-slate-200 bg-slate-50 p-3">
                <p class="text-xs uppercase tracking-wide text-slate-500">Current admin</p>
                <p class="mt-1 text-sm font-medium text-slate-800">{{ $adminUser?->name ?? 'No user found' }}</p>
                <p class="text-xs text-slate-500">{{ $adminUser?->email ?? 'Create company admin user first' }}</p>
            </div>

            <form method="post" action="{{ route('superadmin.companies.admin-password.update', $company) }}" class="mt-4 space-y-3">
                @csrf
                <div>
                    <label class="block text-sm font-medium text-slate-700">New password</label>
                    <input type="password" name="password" required minlength="8"
                           class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm focus:border-slate-500 focus:outline-none focus:ring-1 focus:ring-slate-500">
                </div>
                <div>
                    <label class="block text-sm font-medium text-slate-700">Confirm password</label>
                    <input type="password" name="password_confirmation" required minlength="8"
                           class="mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 text-sm focus:border-slate-500 focus:outline-none focus:ring-1 focus:ring-slate-500">
                </div>
                <button type="submit" class="w-full rounded-lg bg-indigo-600 px-3 py-2 text-sm font-medium text-white">Update password</button>
            </form>

            <form method="post" action="{{ route('superadmin.companies.admin-password.update', $company) }}" class="mt-3">
                @csrf
                <input type="hidden" name="reset" value="1">
                <button type="submit" class="w-full rounded-lg bg-amber-600 px-3 py-2 text-sm font-medium text-white"
                        onclick="return confirm('Reset admin password to Welcome@123?')">
                    Quick reset to Welcome@123
                </button>
            </form>
        </x-card>
    </div>
@endsection
