@extends('layouts.admin') @section('content')
<div class="card mb-3">
  <div class="card-body pt-0">
    <div class="row">
      <!-- Search by Date -->
      <div class="col-lg-6 pt-3 pt-lg-0">
        <div class="d-flex flex-between-center mb-3">
          <div class="d-flex align-items-center">
            <div class="icon-item icon-item-sm bg-soft-primary shadow-none me-2 bg-soft-success">
              <span class="fas fa-calendar-week text-success"></span>
            </div>
            <h6 class="mb-0">Search Per Date</h6>
          </div>
        </div>
        <form class="form-material" method="POST" action="{{ url('/search_date_member') }}">
          @csrf
          Cover:
          <select required name="cover" class="form-control">
            <option value="" disabled selected>--Select Option--</option>
            <option>All</option>
            @foreach ($covers as $cover1)
            <option value="{{ $cover1->id }}">{{ $cover1->name }}</option>
            @endforeach
          </select>
          <br>
          Start Date:
          <input type="date" value="{{ date('Y-m-d') }}" required class="form-control" name="date1"><br>
          End Date:
          <input type="date" value="{{ date('Y-m-d') }}" required class="form-control" name="date2">
          <br>
          <button type="submit" class="btn btn-primary btn-sm"><i class="fas fa-search"></i> Preview</button>
        </form>
      </div>

      <!-- Search by Group -->
      <div class="col-lg-6 pt-3 pt-lg-0">
        <div class="d-flex flex-between-center mb-3">
          <div class="d-flex align-items-center">
            <div class="icon-item icon-item-sm bg-soft-primary shadow-none me-2 bg-soft-info">
              <span class="fas fa-users text-info"></span>
            </div>
            <h6 class="mb-0">Search Per Group</h6>
          </div>
        </div>
        <form class="form-material" method="POST" action="{{ url('/search_group_member') }}">
          @csrf
          Group:
          <select required name="group_id" class="form-control">
            <option value="" disabled selected>--Select Group--</option>

            @foreach ($groups as $group)
            <option value="{{ $group->id }}">{{ $group->name }}</option>
            @endforeach
          </select>
          <br>
          Start Date:
          <input type="date" name="date1" value="{{ date('Y-m-d') }}" class="form-control" required><br>
          End Date:
          <input type="date" name="date2" value="{{ date('Y-m-d') }}" class="form-control" required><br>

          <button type="submit" class="btn btn-info btn-sm"><i class="fas fa-search"></i> Preview</button>
        </form>
      </div>

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

<div class="card mb-3">
  <div class="card-header">
    <div class="row flex-between-end">
      <div class="col-auto align-self-center">
        <form class="form-material" method="POST" action="{{ url ('/search_member')}}">
          @csrf
          <input type="text" required class="form-control" placeholder="Search by National ID" name="search" id="search">
          <br><button type="submit" class="btn btn-primary btn-sm" aria-placeholder="Search"><i class="fas fa-search"></i> Advanced Search</button>
        </form>
      </div>

      <div class="col-auto align-self-center">
        <form class="form-material" method="POST" action="{{ url ('/search_member2')}}">
          @csrf
          <input type="text" required class="form-control" name="search" placeholder="Search by Cellphone number" id="search">
          <br><button type="submit" class="btn btn-primary btn-sm" aria-placeholder="Search"><i class="fas fa-search"></i> Advanced Search</button>
        </form>
      </div>

      <div class="col-auto align-self-center">
        <form class="form-material" method="POST" action="{{ url ('/search_member1')}}">
          @csrf
          <input type="text" required class="form-control" name="search" placeholder="Search by Name" id="search">
          <br><button type="submit" class="btn btn-primary btn-sm" aria-placeholder="Search"><i class="fas fa-search"></i> Advanced Search</button>
        </form>
      </div>
    </div>
  </div>
  <div class="card-body pt-0">
    &nbsp;
    <!-- <a href="{{url ('allstatus')}}" class="btn btn-success btn-sm"> Update Status</a>-->
    <br>
    <br>
    <h5 class="mb-0" data-anchor="data-anchor">List of all members</h5>
    <p>Displaying {{ $members->count() }} of {{ $members->total() }} members</p>
    <small>If you can find the member you are looking for, <br> please use the advanced search option.</small>
    <div class="dt-responsive table-sm table-responsive">
      <small>
        <table id="basic-btn" class="table table-sm text-sm table-striped table-bordered nowrap">
          <thead class="bg-200 text-900">
            <tr>
              <th>#</th>
              <th>Payment</th>
              <th>Name</th>
              <th>Surname</th>
              <th>Group</th>
              <th>Cover</th> <!-- New -->
              <th>Monthly Pay</th> <!-- New -->
              <th>National ID</th>
              <th>Cell Number</th>
              <th>Status</th>
              <th>Action</th>
            </tr>
          </thead>
          <tbody>
            @foreach ($members as $member)
            @php
            $isRestart = $member->status1 === "Restart";
            $canRestart = $isRestart && Auth::user()->role == 3;
            $rowClass = $member->deceased ? 'table-danger text-danger fw-bold' : ''; // 🔴 highlight deceased
            @endphp
            <tr class="{{ $rowClass }}">
              <td>{{ $loop->iteration }}</td>
              @php
              $shouldDisable = ($member->status1 === "Restart" && Auth::user()->role != 3);
              $disableStyle = $shouldDisable ? 'style="pointer-events:none; opacity:0.5;"' : '';
              @endphp

              <!-- Payment & Profile buttons -->
              <td>
                {{-- Only show Payment if status is NOT Restart --}}
                @if(!$isRestart && optional($member->premiumPayments)->isNotEmpty())
                <a href="{{ url('/memberpayments', $member->id) }}" class="btn btn-primary btn-sm">
                  <i class="fas fa-money-check"></i> Payment
                </a>
                &nbsp;
                @endif
                <a href='{{ url("/member_profile", $member->id) }}' class="btn btn-primary btn-sm" {!! $disableStyle !!}>
                  <i class="far fa-user-circle"></i> Profile
                </a>
              </td>
              <!-- Member Details -->
              <td>{{ $member->name }}</td>
              <td>{{ $member->surname }}</td>
              <td>{{ $member->group ? $member->group->name : 'N/A' }}</td>

              <!-- Cover Name -->
              <td>
                {{ optional(optional($member->premium)->cover)->name ?? 'N/A' }}
              </td>

              <!-- Monthly Pay -->
              <td>
                @if(optional(optional($member->premium)->cover)->monthly_pay)
                E{{ number_format($member->premium->cover->monthly_pay, 2) }}
                @else
                N/A
                @endif
              </td>

              <td>{{ $member->national_id }}</td>
              <td>{{ $member->cell_number }}</td>
              <td>
                @if($member->deceased)
                <span class="badge bg-danger">Deceased</span>
                @else
                <span class="badge bg-success">{{$member->status1}}</span>
                @endif
              </td>

              <!-- Action Buttons -->
              <td>
                &nbsp;
                @if(optional($member->premiumPayments)->isEmpty())
                <a href="{{ url('/member', $member->id) }}"
                  class="btn btn-primary btn-sm"
                  {!! $disableStyle !!}>
                  <i class="far fa-edit"></i> Edit
                </a>
                @endif

                @if ($member->status1 == "Restart" && Auth::user()->role == 3)
                <a href='{{ url("/restart", $member->id) }}' class="btn btn-secondary btn-sm">
                  <i class="far fa-clock"></i> Restart
                </a>              
                @endif


              </td>
            </tr>
            @endforeach
          </tbody>
        </table>



      </small>
    </div>


  </div>

</div>

<script>
  var deleteLinks = document.querySelectorAll('.delete');

  for (var i = 0; i < deleteLinks.length; i++) {
    deleteLinks[i].addEventListener('click', function(event) {
      event.preventDefault();

      var choice = confirm(this.getAttribute('data-confirm'));

      if (choice) {
        window.location.href = this.getAttribute('href');
      }
    });
  }
</script>

@endsection