@extends('layouts.front')

@section('content')
@php
    $profileFields = [
        $usersdata->name,
        $studentsprofile->fathername ?? null,
        $studentsprofile->mothername ?? null,
        $studentsprofile->classname ?? null,
        $studentsprofile->dateofbirth ?? null,
        $studentsprofile->studentupiid ?? null,
        $studentsprofile->pancardnumber ?? null,
        $studentsprofile->bankaccountnumber ?? null,
        $studentsprofile->bankaccountholdername ?? null,
        $studentsprofile->bankifsccode ?? null,
        $usersdata->aadharcard,
    ];
    $filledProfileFields = collect($profileFields)->filter(fn ($value) => filled($value))->count();
    $totalProfileFields = count($profileFields);
    $completionPercent = (int) round(($filledProfileFields / max($totalProfileFields, 1)) * 100);
    $bankReady = filled($studentsprofile->bankaccountnumber ?? null)
        && filled($studentsprofile->bankaccountholdername ?? null)
        && filled($studentsprofile->bankifsccode ?? null);
    $upiLocked = filled($studentsprofile->studentupiid ?? null);
    $studentEmail = trim((string) (($usersdata->useremailid ?? '') ?: ($usersdata->email ?? '')));
@endphp
<div class="jel-portal-page">
    @include('front.partials.flash-messages')

    <section class="jel-profile-layout">
        <div class="jel-profile-intro">
            <span class="jel-portal-kicker">Student Profile</span>
            <h1>Complete your exam profile details.</h1>
            <p>Profile details complete rahenge to class-wise competition mapping, reward flow aur verification process smooth rahega.</p>

            <div class="jel-result-summary-grid jel-history-summary-grid">
                <article>
                    <span>Profile Complete</span>
                    <strong>{{ $completionPercent }}%</strong>
                </article>
                <article>
                    <span>Current Class</span>
                    <strong>{{ $studentsprofile->classname ?? 'Not selected' }}</strong>
                </article>
                <article>
                    <span>Bank Ready</span>
                    <strong>{{ $bankReady ? 'Yes' : 'Pending' }}</strong>
                </article>
                <article>
                    <span>Mobile Status</span>
                    <strong>{{ $usersdata->mobilevarification == 1 ? 'Verified' : 'Verify now' }}</strong>
                </article>
            </div>

            @if (($studentsprofile->classid ?? null) > 0)
                <a class="jel-portal-button jel-profile-back" href="{{ route('competitionlist') }}">Back to Home</a>
            @endif
        </div>

        <section class="jel-profile-card">
            <form class="jel-profile-form" action="{{ route('saveprofile') }}" method="post">
                @csrf

                <div class="jel-profile-avatar-card">
                    <a href="{{ route('profileimg') }}" class="jel-profile-avatar-link">
                        <img src="{{ $profileImageUrl }}" alt="{{ $usersdata->name }}">
                    </a>
                    <div>
                        <h2>{{ $usersdata->name }}</h2>
                        <p>Profile photo update karne ke liye image par click kijiye.</p>
                    </div>
                </div>

                <div class="jel-profile-grid">
                    <label class="jel-portal-field">
                        <span>Student Name</span>
                        <input type="text" name="username" id="username" value="{{ old('username', $usersdata->name) }}" placeholder="Enter your name" maxlength="100" required>
                    </label>

                    <label class="jel-portal-field">
                        <span>Father Name</span>
                        <input type="text" name="fathername" id="fathername" value="{{ old('fathername', $studentsprofile->fathername ?? '') }}" placeholder="Enter father name" maxlength="100" required>
                    </label>

                    <label class="jel-portal-field">
                        <span>Mother Name</span>
                        <input type="text" name="mothername" id="mothername" value="{{ old('mothername', $studentsprofile->mothername ?? '') }}" placeholder="Enter mother name" maxlength="100" required>
                    </label>

                    <label class="jel-portal-field">
                        <span>Student Class (Group)</span>
                        <select name="questiongoupsclass" id="questiongoupsclass" required>
                            <option value="">Select student class</option>
                            @foreach ($studentsgroup as $studentsgroupdata)
                                <option
                                    value="{{ $studentsgroupdata->id }}<=>{{ $studentsgroupdata->studentsgroupname }}"
                                    @selected(old('questiongoupsclass', ($studentsprofile->classid ?? null) ? $studentsgroupdata->id . '<=>' . $studentsgroupdata->studentsgroupname : null) === ($studentsgroupdata->id . '<=>' . $studentsgroupdata->studentsgroupname))
                                >
                                    {{ $studentsgroupdata->studentsgroupname }}
                                </option>
                            @endforeach
                        </select>
                    </label>

                    <label class="jel-portal-field">
                        <span>Date of Birth</span>
                        <input type="date" name="dateofbirth" id="dateofbirth" value="{{ old('dateofbirth', $studentsprofile->dateofbirth ?? '') }}" required>
                    </label>

                    <label class="jel-portal-field">
                        <span>Enrollment Number</span>
                        <input type="text" name="srnumber" id="srnumber" value="{{ old('srnumber', $studentsprofile->srnumber ?? '') }}" placeholder="Enter enrollment number" maxlength="20">
                    </label>

                    <label class="jel-portal-field">
                        <span>Parent PAN Card Number</span>
                        <input type="text" name="pancardnumber" id="pancardnumber" value="{{ old('pancardnumber', $studentsprofile->pancardnumber ?? '') }}" placeholder="Enter PAN number" maxlength="10" required>
                    </label>

                    <label class="jel-portal-field">
                        <span>Bank Account Number</span>
                        <input type="text" name="bankaccountnumber" id="bankaccountnumber" value="{{ old('bankaccountnumber', $studentsprofile->bankaccountnumber ?? '') }}" placeholder="Enter bank account number" maxlength="20" inputmode="numeric" oninput="this.value = this.value.replace(/\D/g, '')" required>
                    </label>

                    <label class="jel-portal-field">
                        <span>Bank Account Holder Name</span>
                        <input type="text" name="bankholdername" id="bankholdername" value="{{ old('bankholdername', $studentsprofile->bankaccountholdername ?? '') }}" placeholder="Enter account holder name" maxlength="150" required>
                    </label>

                    <label class="jel-portal-field">
                        <span>Branch Name</span>
                        <input type="text" name="bankbranchname" id="bankbranchname" value="{{ old('bankbranchname', $studentsprofile->bankbranch ?? '') }}" placeholder="Enter branch name" maxlength="50">
                    </label>

                    <label class="jel-portal-field">
                        <span>IFSC Code</span>
                        <input type="text" name="bankifscocde" id="bankifscocde" value="{{ old('bankifscocde', $studentsprofile->bankifsccode ?? '') }}" placeholder="Enter IFSC code" maxlength="15" required>
                    </label>

                    <label class="jel-portal-field">
                        <span>Religion</span>
                        <select name="userreligion" id="userreligion" required>
                            <option value="">Select religion</option>
                            <option @selected(old('userreligion', $studentsprofile->religion ?? '') === 'Hindus') value="Hindus">Hindus</option>
                            <option @selected(old('userreligion', $studentsprofile->religion ?? '') === 'Muslims') value="Muslims">Muslims</option>
                            <option @selected(old('userreligion', $studentsprofile->religion ?? '') === 'Sikh') value="Sikh">Sikh</option>
                            <option @selected(old('userreligion', $studentsprofile->religion ?? '') === 'Christians') value="Christians">Christians</option>
                            <option @selected(old('userreligion', $studentsprofile->religion ?? '') === 'Buddhists') value="Buddhists">Buddhists</option>
                            <option @selected(old('userreligion', $studentsprofile->religion ?? '') === 'Jains') value="Jains">Jains</option>
                        </select>
                    </label>

                    <label class="jel-portal-field">
                        <span>UPI ID</span>
                        <input type="text" name="emailid" id="emailid" value="{{ old('emailid', $studentsprofile->studentupiid ?? '') }}" placeholder="Enter UPI ID" maxlength="150" @readonly(($studentsprofile->studentupiid ?? '') !== '') required>
                        <small class="jel-field-helper">{{ $upiLocked ? 'UPI ID safety lock enabled. Update ke liye admin support chahiye hoga.' : 'UPI ID ek baar save hone ke baad safety ke liye lock ho jayegi.' }}</small>
                    </label>

                    <label class="jel-portal-field">
                        <span>Aadhaar Number</span>
                        <input type="text" name="aadharcardnumber" id="aadharcardnumber" value="{{ old('aadharcardnumber', $usersdata->aadharcard) }}" placeholder="Enter Aadhaar number" minlength="12" maxlength="12" inputmode="numeric" oninput="this.value = this.value.replace(/\D/g, '').slice(0, 12)" required>
                    </label>
                </div>

                <div class="jel-profile-verification">
                    <div class="jel-profile-verify-card">
                        <span>Mobile Number</span>
                        <strong>{{ $usersdata->mobileno }}</strong>
                        @if ($usersdata->mobilevarification == 1)
                            <p class="jel-verify-ok">Verified</p>
                        @else
                            <form action="{{ route('mobileverify', ['mobile' => $usersdata->mobileno]) }}" method="post" class="d-inline">
                                @csrf
                                <button class="jel-profile-inline-link" type="submit">Verify mobile number</button>
                            </form>
                        @endif
                    </div>

                    <div class="jel-profile-verify-card">
                        <span>Email Address</span>
                        <strong>{{ $studentEmail !== '' ? $studentEmail : 'Email not available' }}</strong>
                        @if ($studentEmail === '')
                            <p class="jel-field-helper mb-0">Current profile mein email address available nahi hai.</p>
                        @elseif (($usersdata->emailvarify ?? 0) == 1)
                            <p class="jel-verify-ok">Verified</p>
                        @else
                            <form action="{{ route('emailvarificarion', ['emailid' => $studentEmail]) }}" method="post" class="d-inline">
                                @csrf
                                <button class="jel-profile-inline-link" type="submit">Verify email address</button>
                            </form>
                        @endif
                    </div>
                </div>

                <div class="jel-inline-actions">
                    <a class="jel-portal-button jel-portal-button-muted" href="{{ route('profileimg') }}">Update Profile Photo</a>
                    <a class="jel-portal-button jel-portal-button-secondary" href="{{ route('competitionlist') }}">Back to Dashboard</a>
                </div>

                <button class="jel-portal-button" type="submit">Update Profile</button>
            </form>
        </section>
    </section>
</div>
@endsection
