@extends('layouts.app')
@push('title')
    {{ $title }}
@endpush
@section('content')
    <div class="p-30">
        <p class="d-none d-sm-inline-block fs-15 fw-500 lh-25 text-black py-10 px-26 bg-cdef84 bd-ra-12 hover-bg-one">
            {{__('1. Each user’s email and mobile number must be unique. Duplicate entries will cause the data to be rejected during import.')}}
        </p>
        <p class="d-none d-sm-inline-block fs-15 fw-500 lh-25 text-black py-10 px-26 bg-cdef84 bd-ra-12 hover-bg-one">
            {{__('2. You can use the "Birth Date" column in the Excel sheet this format YYYY/MM/DD (2000/02/09).')}}
        </p>
        <div class="bg-white bd-half bd-c-ebedf0 bd-ra-25">
            <div class="page-content">
                <div class="card">
                    <div class="card-header">
                        <h4>{{__('Alumni Data Preview')}}</h4>
                    </div>
                    <div class="card-body">
                        <form action="{{ route('admin.alumni.import-store') }}" method="POST"
                              class="ajax" data-handler="commonResponseRedirect"
                              data-redirect-url="{{route('admin.alumni.import')}}">
                            @csrf
                            <div class="table-responsive">
                                <table class="table table-bordered">
                                    <thead>
                                    <tr>
                                        <th>{{__("Full Name")}}</th>
                                        <th class="text-nowrap">{{__("Batch Name")}}</th>
                                        <th>{{__("Mobile")}}</th>
                                        <th>{{__("Email")}}</th>
                                        <th class="text-nowrap">{{__("Department Name")}}</th>
                                        <th class="text-nowrap">{{__("Passing Year")}}</th>
                                        <th class="text-nowrap">{{__("Id Number")}}</th>
                                        <th class="text-nowrap">{{__("Date of Birth")}}</th>
                                        <th>{{__("Gender")}}</th>
                                        <th>{{__("Password")}}</th>
                                        @foreach($customFieldKeys as $customFieldKey)
                                            <th class="text-nowrap">{{ $customFieldKey }}</th>
                                        @endforeach
                                        <th class="text-center">{{__("Status")}}</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($rows as $index => $row)
                                            @if(!empty($row[0]))
                                            <tr>
                                                <td><input class="form-control min-w-160" type="text" name="rows[{{ $index }}][name]" value="{{ $row[0] ?? '' }}"></td>
                                                <td>
                                                    <select class="min-w-160 sf-select-dropdown" name="rows[{{ $index }}][batch_id]">
                                                        @php
                                                            $excelBatch = trim($row[1] ?? '');
                                                            $matchedBatch = $batches->firstWhere('name', $excelBatch);
                                                        @endphp

                                                        @if(!$matchedBatch)
                                                            <option selected disabled>{{__('Not Matched')}}</option>
                                                        @endif

                                                        @foreach($batches as $batch)
                                                            <option value="{{ $batch->id }}" {{ $batch->name == $excelBatch ? 'selected' : '' }}>
                                                                {{ $batch->name }}
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                </td>
                                                <td><input type="text" class="form-control min-w-160" name="rows[{{ $index }}][mobile]" value="{{ $row[2] ?? '' }}"></td>
                                                <td><input type="text" class="form-control min-w-160" name="rows[{{ $index }}][email]" value="{{ $row[3] ?? '' }}"></td>
                                                <td>
                                                    <select class="min-w-160 sf-select-dropdown" name="rows[{{ $index }}][department_id]">
                                                        @php
                                                            $excelDept = trim($row[4] ?? '');
                                                            $matchedDept = $department->firstWhere('name', $excelDept);
                                                        @endphp

                                                        @if(!$matchedDept)
                                                            <option selected disabled>{{__('Not Matched')}}</option>
                                                        @endif

                                                        @foreach($department as $data)
                                                            <option value="{{ $data->id }}" {{ $data->name == $excelDept ? 'selected' : '' }}>
                                                                {{ $data->name }}
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                </td>

                                                <td>
                                                    <select class="min-w-160 sf-select-dropdown" name="rows[{{ $index }}][passing_year_id]">
                                                        @php
                                                            $excelYear = trim($row[5] ?? '');
                                                            $matchedYear = $passing_year->firstWhere('name', $excelYear);
                                                        @endphp

                                                        @if(!$matchedYear)
                                                            <option selected disabled>{{__('Not Matched')}}</option>
                                                        @endif

                                                        @foreach($passing_year as $year)
                                                            <option value="{{ $year->id }}" {{ $year->name == $excelYear ? 'selected' : '' }}>
                                                                {{ $year->name }}
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                </td>
                                                <td><input type="text" class="form-control min-w-160" name="rows[{{ $index }}][id_number]" value="{{ $row[6] ?? '' }}"></td>
                                                <td><input type="text" class="form-control min-w-160" name="rows[{{ $index }}][date_of_birth]" value="{{ $row[7] ?? '' }}"></td>
                                                <td><input type="text" class="form-control min-w-160" name="rows[{{ $index }}][gender]" value="{{ $row[8] ?? '' }}"></td>
                                                <td><input type="text" class="form-control min-w-160" name="rows[{{ $index }}][password]" value="{{ $row[9] ?? '' }}"></td>
                                                @foreach($customFieldKeys as $i => $customKey)
                                                    @php
                                                        $customValue = $row[10 + $i] ?? '';
                                                    @endphp
                                                    <td>
                                                        <input type="text" class="form-control min-w-160"
                                                               name="rows[{{ $index }}][custom_fields][{{ $customKey }}]"
                                                               value="{{ $customValue }}">
                                                    </td>
                                                @endforeach

                                                <td>
                                                    <input type="text" class="form-control min-w-160 text-center p-0" readonly value="{{ $row['status'] ?? 'Unknown' }}"
                                                           style="background-color: {{ ($row['status'] ?? '') == 'Valid' ? '#d4edda' : '#f8d7da' }}; color: #000;">
                                                </td>
                                            </tr>
                                            @endif
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                            <button type="submit" class="py-13 px-26 bg-cdef84 border-0 bd-ra-12 fs-15 fw-500 lh-25 text-black hover-bg-one mt-3">Confirm & Import</button>
                        </form>
                    </div>
                 </div>
             </div>
        </div>
    </div>
@endsection
