@extends('frontend.layouts.app')
@section('content')
    @php
        $breadCrump2 = 'Subscriptions';
        use Carbon\Carbon; // Import Carbon once

    @endphp
    <!--Job alerts start -->
    <!--Job alerts start -->
    <div class="wrapper-inner">
        <div class="container">
            <div class="profile-dash">
                @include('frontend.employer.sidebar')
                <div class="profile-dash-rt">
                    <div class="my-profile-inner">
                        <div class="headingBlock d-flex align-items-center justify-content-between">
                            <h4>My Subscriptions</h4>
                            <form id="filterForm" action="{{ route('jobSeeker.subscriptions') }}">
                                <div class="job-filter-box">
                                    {{-- <select class="form-control" name="filter" onchange="submitFilter(this)">
                                        <option value="">Filter</option>
                                        <option value="1" {{ request()->filter == '1' ? 'SELECTED' : '' }}>Pending
                                        </option>
                                        <option value="2" {{ request()->filter == '2' ? 'SELECTED' : '' }}>Shortlisted
                                        </option>
                                        <option value="3" {{ request()->filter == '3' ? 'SELECTED' : '' }}>Interview
                                            Schedule</option>
                                        <option value="4" {{ request()->filter == '4' ? 'SELECTED' : '' }}>Interview
                                            Completed</option>
                                        <option value="6" {{ request()->filter == '6' ? 'SELECTED' : '' }}>Hired</option>
                                        <option value="7" {{ request()->filter == '7' ? 'SELECTED' : '' }}>Rejected
                                        </option>
                                    </select> --}}
                                </div>
                            </form>
                        </div>
                        <div class="table-responsive my-job-table employer-job-table">
                            <table class="custom-table table table-condensed">
                                <tr>
                                    <th>Plan Manager</th>
                                    <th>Start Date</th>
                                    <th>End Date</th>
                                    <th>Amount</th>
                                    <th>Purchased At</th>
                                    <th>Status</th>
                                    <th>Action</th>
                                </tr>
                                @foreach ($transactions as $transaction)
                                    @php
                                        $planDetail = json_decode($transaction->plan_detail);
                                    @endphp

                                    <tr>
                                        <td>
                                            <a
                                                href="{{ route('employer.subscription.detail', $transaction->id) }}">{{ $planDetail->name }}</a>
                                        </td>
                                        <td>

                                            {{ \Carbon\Carbon::parse($transaction->start_date)->format(config('GET.front_date_format')) }}
                                        </td>
                                        <td>

                                            {{ \Carbon\Carbon::parse($transaction->end_date)->format(config('GET.front_date_format')) }}
                                        </td>

                                        <td style="text-align: center;">${{ $transaction->amount }}</td>
                                        @php
                                            $createdAt = $transaction->created_at;
                                            // Format the created_at date using Carbon
                                            $formattedDate = $createdAt
                                                ? \Carbon\Carbon::parse($createdAt)->format(config('GET.front_date_format', 'd-m-Y'))
                                                : 'N/A';

                                        @endphp
                                        <td>{{ $formattedDate }}</td>

                                        <td>

                                            @if (\Carbon\Carbon::parse($transaction->end_date) < today())
                                                <a href="#!" class="btn btn-danger">Expired</a>
                                            @elseif($transaction->subscriptions?->status == 'active')
                                                <a href="#!" class="btn btn-success">Active</a>
                                            @endif
                                        </td>

                                        <td>
                                            @if($transaction->subscriptions?->recurring_payment=="false")
                                            <a href="#!" class="btn btn-danger">Cancelled</button>
                                            @elseif (\Carbon\Carbon::parse($transaction->end_date) > today())
                                                <button onclick="confirmCancelModal('{{ $transaction->subscriptions?->subscription_id }}')"
                                                    class="btn btn-primary">Cancel Subscription</button>
                                            @endif
                                        </td>
                                    </tr>
                                @endforeach
                            </table>
                            <div class="pagination-wrapper">
                                {{ $transactions->links('frontend.layouts.pagination') }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    @endsection
    <div id="cancelModal" class="modal fade" role='dialog'>
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                    <p>Are you sure you want to cancel this subscription?</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" onclick=" $('#cancelModal').modal('hide');" data-dismiss="modal">No</button>
                    <span id='cancelButton'></span>
                </div>
            </div>
        </div>
    </div>
@push('front-script')
    <script>
        function submitFilter(selectElement) {
            const form = document.getElementById('filterForm');
            form.action = "{{ route('jobSeeker.subscriptions') }}" + '?status=' + selectElement.value;
            form.submit();
        }


        function confirmCancelModal(id) {
    $('#cancelModal').modal('show');
    // Use string concatenation to ensure the correct value is passed
    $('#cancelButton').html('<a class="btn btn-danger" style="color:white;" onclick="cancelSubscription(\'' + id + '\')">Yes</a>');
}

        function cancelSubscription(id) {
            location.replace("{{ route('subscription.cancel', '') }}/" + id);
        }
    </script>
@endpush
