<x-admin.layout>
    <x-slot name="title">My Application</x-slot>
    <x-slot name="heading">My Application</x-slot>
    {{-- <x-slot name="subheading">Test</x-slot> --}}



    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header">
                    <div class="row">
                        <div class="col-sm-12">
                            <h4 class="card-title">
                                My Application
                            </h4>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table id="alternative-pagination" class="table table-bordered align-middle" style="width:100%">
                            <thead>
                                <tr>
                                    <th>Sr No</th>
                                    <th>Application No</th>
                                    <th>Service Name</th>
                                    <th>Date</th>
                                    {{-- <th>Payment Date</th> --}}
                                    {{-- <th>Current Status</th> --}}
                                    {{-- <th>Action</th> --}}
                                    <th>Certificate</th>
                                    <th>Receipt</th>
                                    <th>Payment</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($datas as $data)
                                    @php
                                        $status = $data->statuses;
                                        $payRecord = $paydata->where('service_id', $data->service_id)->where('table_id', $data->id)->first();

                                    @endphp
                                    <tr>
                                        <td>{{ $loop->iteration }}</td>
                                        <td>{{ isset($data->application_no) ? $data->application_no : 'N/A' }}</td>
                                        <td>{{ @$status->serviceName->service_name }} </td>
                                        <td>{{ isset($data->created_at) ? date('d-m-Y h:i A', strtotime($data->created_at)) : 'N/A' }}</td>

                                        <td>
                                            @php

                                                $mainStatus = $status->status ?? null;
                                                $statusdoc = $status->document_upload ?? null;
                                                $documentUpload = $status->offlinereceipt ?? null;
                                                $payStatus = $payRecord->status ?? null;
                                                $certificateRoute = $status->serviceName->certificate_route ?? null;
                                                $receiptroute = $status->serviceName->receipt_route ?? null;
                                                $demandnote = $status->serviceName->demandnote ?? null;
                                                $paymentroute = $status->serviceName->payment ?? null;
                                                $receiptUploaded = !empty($status->offlinereceipt);
                                                $receiptStatus = $status->receipt_status ?? 0; // 0=pending,1=approved,2=rejected
                                                $excludedServices = [8933, 8932, 9396, 37, 2026,24,18];
                                                $isOfflinePendingOrRejected = $receiptStatus == 2;

                                                // --- Determine Payment Eligibility ---

                                                // Case 1: Online payment is NOT done yet or pending
                                                $isOnlinePending = empty($payStatus) || $payStatus == 0;

                                                // Case 2: Offline payment is NOT verified yet (either not uploaded or rejected/pending)
                                                $isOfflinePending = !$receiptUploaded || in_array($receiptStatus, [ 2]);

                                                // Case 3: Offline payment is VERIFIED (completed)
                                                $isOfflineApproved = $receiptUploaded && $receiptStatus == 1;

                                                // --- Final Can Pay Condition ---

                                                $canPay =
                                                    $mainStatus == 1 &&
                                                    ($isOnlinePending && $isOfflinePending) && // both must indicate no successful payment yet
                                                    !$isOfflineApproved && // prevent paying again if offline is verified
                                                    !in_array($data->service_id , $excludedServices);

                                            @endphp

                                           <!--kishroi hide purana code 15/11/2025-->
                                            {{-- @if (($payStatus == 1 || $isOfflineApproved == 1) &&  $mainStatus == 1 )
                                                @if ($data->service_id == 8925  || $data->service_id == 6042 || $data->service_id == 8042 || $data->service_id == 9041 || $data->service_id == 7042 || $data->service_id == 8920)
                                                    @if( $statusdoc)
                                                        <a href="{{ asset('storage/' . $statusdoc) }}" class="btn btn-primary btn-sm p-1" target="_blank"> <i class="fa fa-download"></i> Certificate</a>
                                                    @endif

                                                @elseif($certificateRoute)
                                                    <a href="{{ $certificateRoute ? route($certificateRoute, encrypt($data->id)) : '#' }}" class="btn btn-primary btn-sm p-1" style="display:block;">
                                                        <i class="fa fa-download"></i> Certificate
                                                    </a>
                                                @endif
                                                <a href="#" class="btn btn-primary btn-sm p-1" style="display:none;">
                                                    <i class="fa fa-download"></i> Certificate
                                                </a>
                                            @endif --}}
                                            <!--kishori hide purana code 15/11/2025-->



                                            {{-- @if ($data->service_id != 187)   
                                                @if (($payStatus == 1 || $isOfflineApproved == 1) &&  $mainStatus == 1 )

                                                    @if ($data->service_id == 8925  || $data->service_id == 6042 || $data->service_id == 8042 || $data->service_id == 9041 || $data->service_id == 7042 || $data->service_id == 8920 || $data->service_id == 185)
                                                        @if( $statusdoc)
                                                            <a href="{{ asset('storage/' . $statusdoc) }}" class="btn btn-primary btn-sm p-1" target="_blank"> 
                                                                <i class="fa fa-download"></i> Certificate
                                                            </a>
                                                        @endif

                                                    @elseif($certificateRoute)
                                                        <a href="{{ route($certificateRoute, encrypt($data->id)) }}" class="btn btn-primary btn-sm p-1" style="display:block;">
                                                            <i class="fa fa-download"></i> Certificate
                                                        </a>
                                                    @endif

                                                    <a href="#" class="btn btn-primary btn-sm p-1" style="display:none;">
                                                        <i class="fa fa-download"></i> Certificate
                                                    </a>

                                                @endif
                                            @endif --}}


                                            {{-- @if ($data->service_id == 187) --}}
                                            @if ($data->service_id == 187 || $data->service_id == 2032)
                                                                @if ($data->show_certificate)
                                                                    <a href="{{ route($certificateRoute, encrypt($data->id)) }}" class="btn btn-primary btn-sm p-1">
                                                                        <i class="fa fa-download"></i> Certificate
                                                                    </a>
                                                                @endif
                                                            @else
                                                                @if (($payStatus == 1 || $isOfflineApproved == 1) &&  $mainStatus == 1 )
                                                                    @if ($data->service_id == 8925  || $data->service_id == 6042 || $data->service_id == 8042 || $data->service_id == 9041 || $data->service_id == 7042 || $data->service_id == 8920 || $data->service_id == 185)
                                                                        @if( $statusdoc)
                                                                            <a href="{{ asset('storage/' . $statusdoc) }}" class="btn btn-primary btn-sm p-1" target="_blank"> 
                                                                                <i class="fa fa-download"></i> Certificate
                                                                            </a>
                                                                        @endif
                                                                    @elseif($certificateRoute)
                                                                        <a href="{{ route($certificateRoute, encrypt($data->id)) }}" class="btn btn-primary btn-sm p-1" style="display:block;">
                                                                            <i class="fa fa-download"></i> Certificate
                                                                        </a>
                                                                    @endif

                                                                    <a href="#" class="btn btn-primary btn-sm p-1" style="display:none;">
                                                                        <i class="fa fa-download"></i> Certificate
                                                                    </a>
                                                                @endif
                                                            @endif








                                            {{-- @if ($isOfflineApproved && $mainStatus == 1 && $payStatus == 0)
                                                <a href="{{ $certificateRoute ? route($certificateRoute, encrypt($data->id)) : '#' }}" class="btn btn-primary btn-sm p-1 mt-1" style="display:block;">
                                                    <i class="fa fa-download"></i> Certificate
                                                </a>
                                            @endif --}}

                                            @if ($payStatus == 0 && in_array($data->service_id , $excludedServices) && $mainStatus == 1)
                                                <a href="{{ $certificateRoute ? route($certificateRoute, encrypt($data->id)) : '#' }}" class="btn btn-primary btn-sm p-1 mt-1" style="display:block;">
                                                    <i class="fa fa-download"></i> Certificate
                                                </a>
                                            @endif

                                            @if ($payStatus == 0 && $mainStatus == 1 && ($data->service_id == 187 || $data->service_id == 2032 || $data->service_id == 2005))
                                                <a href="{{ $demandnote ? route($demandnote, encrypt($data->id)) : '#' }}" class="btn btn-primary btn-sm p-1 mt-1" style="display:block;">
                                                    <i class="fa fa-download"></i> Demand Note
                                                </a>
                                            @endif
                                        </td>

                                        <!--start kishori hide the code 15/11/2025-->
                                        {{-- <td>
                                            @if ($payStatus == 1)
                                                <a href="{{ $receiptroute ? route($receiptroute, encrypt($data->id)) : '#' }}" class="btn btn-primary btn-sm p-1" style="display:block;">
                                                    <i class="fa fa-download"></i> Receipt
                                                </a>
                                            @elseif ($documentUpload)
                                                <a href="{{ asset('storage/' . $documentUpload) }}" target="_blank" class="btn btn-primary btn-sm p-1">
                                                    <i class="fa fa-download"></i> Receipt
                                                </a>
                                                @if (!empty($documentUpload) && in_array($status->receipt_status, [0, 2]))
                                                    <br>
                                                    {!! $status->receipt_badge !!}
                                                @endif
                                                @if ($isOfflinePendingOrRejected && !empty($status->receipt_remark))
                                                    <br>
                                                    <span class="text-danger small">


                                                        <strong>Remark:</strong> {{ $status->receipt_remark }}
                                                    </span>
                                                @endif
                                            @endif

                                        </td> --}}
                                        <!--end kishroi hide the code 15/11/2025-->

                                        <td>
                                                {{-- @if ($data->service_id != 187)   --}}

                                                    @if ($payStatus == 1)
                                                        <a href="{{ $receiptroute ? route($receiptroute, encrypt($data->id)) : '#' }}"
                                                        class="btn btn-primary btn-sm p-1" style="display:block;">
                                                            <i class="fa fa-download"></i> Receipt
                                                        </a>

                                                    @elseif ($documentUpload)
                                                        <a href="{{ asset('storage/' . $documentUpload) }}" target="_blank"
                                                        class="btn btn-primary btn-sm p-1">
                                                            <i class="fa fa-download"></i> Receipt
                                                        </a>

                                                        @if (!empty($documentUpload) && in_array($status->receipt_status, [0, 2]))
                                                            <br>
                                                            {!! $status->receipt_badge !!}
                                                        @endif

                                                        @if ($isOfflinePendingOrRejected && !empty($status->receipt_remark))
                                                            <br>
                                                            <span class="text-danger small">
                                                                <strong>Remark:</strong> {{ $status->receipt_remark }}
                                                            </span>
                                                        @endif

                                                    {{-- @endif --}}

                                                @endif
                                            </td>

                                        <td>

                                            @if ($canPay)
                                                <a href="{{ route('construction-road-cutting.paymentoptions', ['serviceId' => $data->service_id, 'id' => encrypt($data->id)]) }}" class="btn btn-primary btn-sm p-1" style="display:block;">
                                                    Pay Now
                                                </a>
                                            @elseif ($mainStatus == 1 && $payStatus == 0 && $isOfflineApproved)
                                                <span class="badge bg-success">Paid (Offline)</span>
                                            @elseif ($payStatus == 1)
                                                <span class="badge bg-success">Paid (Online)</span>
                                            @else
                                                <a href="#" style="display:none;">-</a>
                                            @endif
                                        </td>
                                        <td>
                                            {!! $data->statuses->status_badge !!}

                                        </td>

                                    </tr>
                                @empty
                                    <tr align="center">
                                        <td colspan="8">No Service Found</td>
                                    </tr>
                                @endforelse
                            </tbody>

                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>


</x-admin.layout>
