@extends('layouts.app-shell')
@section('title', 'Edit partner: ' . $user->name)
@section('sidebar')
    @include('admin._sidebar')
@endsection
@section('bottomnav')
    <a href="{{ route('admin.dashboard') }}" class="{{ request()->routeIs('admin.dashboard*') ? 'active' : '' }}"><x-icon name="home" :size="20" /><span>Home</span></a>
    <a href="{{ route('admin.bookings') }}" class="{{ request()->routeIs('admin.bookings*') ? 'active' : '' }}"><x-icon name="bookings" :size="20" /><span>Subs</span></a>
    <a href="{{ route('admin.partners') }}" class="{{ request()->routeIs('admin.partners*') ? 'active' : '' }}"><x-icon name="profile" :size="20" /><span>Partners</span></a>
    <a href="{{ route('admin.services.index') }}" class="{{ request()->routeIs('admin.services*') ? 'active' : '' }}"><x-icon name="services" :size="20" /><span>Catalogue</span></a>
@endsection

@section('content')
@php $pp = $user->partnerProfile; @endphp
<div class="flex-between mb-3">
    <div>
        <a href="{{ route('admin.partners.show', $user) }}" class="btn btn-ghost btn-sm">← Back to partner</a>
        <h2 style="font-size: 22px; font-weight: 700; margin-top: 8px;">Edit partner</h2>
        <p class="muted" style="font-size: 13px;">{{ $user->name }} · {{ $user->email }}</p>
    </div>
</div>

<form method="POST" action="{{ route('admin.partners.update', $user) }}" enctype="multipart/form-data">
    @csrf
    @method('PUT')

    <div class="panel">
        <div class="panel-head"><h3>Account</h3></div>
        <div class="form-grid" style="grid-template-columns: 1fr 1fr 1fr;">
            <div>
                <label>Name</label>
                <input type="text" name="name" value="{{ old('name', $user->name) }}" required>
            </div>
            <div>
                <label>Email</label>
                <input type="email" name="email" value="{{ old('email', $user->email) }}" required>
            </div>
            <div>
                <label>Phone</label>
                <input type="text" name="phone" value="{{ old('phone', $user->phone) }}" required>
            </div>
            <div style="grid-column: 1 / -1;">
                <label style="display: flex; align-items: center; gap: 8px;">
                    <input type="checkbox" name="is_partner_approved" value="1" @checked($user->is_partner_approved)>
                    <span>Approved to take bookings</span>
                </label>
                <p class="muted" style="font-size: 11.5px; margin: 4px 0 0 24px;">Uncheck to revoke approval. The partner will be unable to take any new bookings.</p>
            </div>
        </div>
    </div>

    <div class="panel">
        <div class="panel-head"><h3>Profile</h3></div>
        <div class="form-grid" style="grid-template-columns: 1fr 1fr;">
            <div style="grid-column: 1 / -1;">
                <label>Profile photo (shown to customers)</label>
                <div class="photo-upload-row">
                    <div class="photo-preview-box">
                        @if($pp?->photo_url)
                            <img src="{{ $pp->photo_url }}" alt="">
                        @else
                            <span class="photo-initial">{{ $pp?->initials() ?? strtoupper(substr($user->name, 0, 1)) }}</span>
                        @endif
                    </div>
                    <div class="file-input" style="flex: 1; min-width: 200px;">
                        <input type="file" name="photo" accept="image/*" id="photo-input">
                        <label for="photo-input" class="file-input-label">Tap to upload a new photo (jpg, png, max 2 MB)</label>
                    </div>
                </div>
            </div>
            <div>
                <label>Display name</label>
                <input type="text" name="display_name" value="{{ old('display_name', $pp?->display_name) }}">
            </div>
            <div>
                <label>Experience (years)</label>
                <input type="number" name="experience_years" min="0" max="60" value="{{ old('experience_years', $pp?->experience_years) }}">
            </div>
            <div style="grid-column: 1 / -1;">
                <label>Bio</label>
                <textarea name="bio" rows="3" placeholder="Short bio shown on partner profile">{{ old('bio', $pp?->bio) }}</textarea>
            </div>
        </div>
    </div>

    <div class="panel">
        <div class="panel-head"><h3>KYC verification</h3></div>
        <div class="form-grid" style="grid-template-columns: 1fr 1fr;">
            <div>
                <label>Aadhaar number</label>
                <input type="text" name="aadhaar_number" value="{{ old('aadhaar_number', $pp?->aadhaar_number) }}" maxlength="20" placeholder="XXXX-XXXX-XXXX">
            </div>
            <div style="display: flex; align-items: end; gap: 12px;">
                <label style="display: flex; align-items: center; gap: 6px;">
                    <input type="checkbox" name="aadhaar_verified" value="1" @checked($pp?->aadhaar_verified)>
                    <span>Aadhaar verified</span>
                </label>
                <label style="display: flex; align-items: center; gap: 6px;">
                    <input type="checkbox" name="police_verified" value="1" @checked($pp?->police_verified)>
                    <span>Police verified</span>
                </label>
            </div>
        </div>
    </div>

    <div class="panel">
        <div class="panel-head">
            <h3><x-icon name="money" :size="14" style="color: var(--bfk-primary);" /> Commission &amp; payout</h3>
            <span class="meta">Global default: {{ number_format(\App\Services\CommissionService::platformPercent(), 1) }}%</span>
        </div>
        @php
            $globalPercent = \App\Services\CommissionService::platformPercent();
            $tds = \App\Services\CommissionService::tdsPercent();
            $override = $pp?->commission_percent;
        @endphp
        <div class="form-grid" style="grid-template-columns: 1fr 1fr;">
            <div>
                <label>Per-partner commission % (override)</label>
                <input type="number" step="0.5" min="0" max="100" name="commission_percent" value="{{ old('commission_percent', $override) }}" placeholder="e.g. 12 (leave blank to use global {{ number_format($globalPercent, 1) }}%)">
                <span class="hint">Set a special rate for this partner. Leave blank to use the platform default of {{ number_format($globalPercent, 1) }}%.</span>
            </div>
            <div>
                <label>Wallet balance (₹) — read only</label>
                <input type="text" disabled value="₹{{ number_format(($pp?->wallet_balance_paise ?? 0)/100, 2) }}">
                <span class="hint">Auto-credited after each completed visit, minus platform commission and TDS.</span>
            </div>
            <div style="grid-column: 1 / -1;">
                <label>Notes to partner (shown in their earnings page)</label>
                <textarea name="commission_notes" rows="2" placeholder="e.g. You qualify for the top-rated 12% rate">{{ old('commission_notes', $pp?->commission_notes) }}</textarea>
            </div>
        </div>
        @if($override !== null)
            @php
                $bd = \App\Services\CommissionService::breakdown(100000, (float) $override);
            @endphp
            <div style="margin-top: 14px; padding: 12px 14px; background: linear-gradient(135deg, #fff7f3, #ffe7d2); border-radius: 12px; border: 1px solid #ffd9b8; font-size: 12.5px;">
                <strong style="display:block; margin-bottom: 4px; color: var(--bfk-text);">Live preview on ₹1,000 subscription:</strong>
                <span>Platform cut ({{ number_format($override, 1) }}%):</span>
                <strong style="color: var(--bfk-primary);">₹{{ number_format($bd['platform_paise']/100, 0) }}</strong>
                <span style="margin-left: 10px;">TDS ({{ number_format($tds, 1) }}%):</span>
                <strong style="color: var(--bfk-red);">₹{{ number_format($bd['tds_paise']/100, 0) }}</strong>
                <span style="margin-left: 10px;">Partner net:</span>
                <strong style="color: var(--bfk-green);">₹{{ number_format($bd['partner_net_paise']/100, 0) }}</strong>
            </div>
        @endif
    </div>

    <div class="panel">
        <div class="panel-head"><h3>Admin notes</h3></div>
        <textarea name="admin_notes" rows="3" placeholder="Internal notes about this partner — not visible to partner or customer">{{ old('admin_notes', $pp?->admin_notes) }}</textarea>
    </div>

    <div class="flex" style="gap: 8px; margin-top: 14px;">
        <button type="submit" class="btn btn-primary"><x-icon name="check" :size="14" /> Save changes</button>
        <a href="{{ route('admin.partners.show', $user) }}" class="btn btn-ghost">Cancel</a>
    </div>
</form>
@endsection
