@extends('admin.master')
@section('title') Shop | SHOP @endsection
@section('shop') active @endsection
@section('shop.index') active @endsection
@section('styles')
@endsection

<!-- BEGIN: Content-->
@section('main_content_section')
<div class="row py-3 ps-2">
  <div class="heading d-flex justify-content-start heading-margin-bottom-minus">
    <h3 class="mb-0"><span>Shop List</span></h3>
  </div>
</div>


<div class="table_wrapper py-1 card">
  <div class="row my-2 aggregate-section-div">
    <div class="px-3 ">
      <div class=" d-flex justify-content-between align-items-center aggregate-section border">
        <div class="d-flex align-items-center py-1">
          <p class="mb-0"><a href="{{route('shop.index')}}" class="text-primary  py-2 px-3 active">All
              ({{ $allShopCount }})</a>
          </p>
        </div>
        <div class="d-none d-sm-block">
          @if (Auth::user()->can('shop-create'))
          <a href="javascript:void(0)" class="btn btn-success rounded-0 d-flex align-items-center px-2 border-0"
            data-bs-toggle="modal" data-bs-target="#addShop"><span class="">New Shop</span></a>
          @endif
        </div>
      </div>

      @if (Auth::user()->can('shop-create'))
      <div class="mt-2 btn btn-success d-block rounded-0 d-sm-none" data-bs-toggle="modal" data-bs-target="#addShop">
        New Shop</div>
      @endif
    </div>
  </div>



  <div class="row px-3">
    <div class="table-container table-responsive">
      <table id="" class="table table-bordered">
        <thead class="text-uppercase">
          <tr class="me-3">

            <th class="text-center" scope="col"><span>Shop details</span></th>
            <th class="text-center" scope="col"><span>Floor-Type</span></th>
            <th class="text-center" scope="col"><span>Status</span></th>

          </tr>
        </thead>
        <tbody class="text-center">

          @foreach ($shopData as $item)
          @if(isset($item) && $item != null)
          <tr class="text-start">
            <td>
              <b>Shop Name: </b>{{ $item->shop_name }} <br>
              <b>Shop No: </b>#{{ $item->shop_no }} <br>
              <b>Shop Size: </b>{{ $item->shop_size }} SF<br>
              <div class="row-actions">
                @if (Auth::user()->can('shop-edit'))
                <span><a href="{{route('shop-profile',$item->id)}}" class="edit_user_modal border-0 bg-transparent fw-bolder"> 
                  <span class="text-primary">Profile</span></a></span>
                
                <span> | <a href="javascript:void(0)" class="edit_user_modal border-0 bg-transparent fw-bolder"
                    onclick="updateShop({{$item->id}})"> <span class="text-info">Edit</span></a></span>


                @if($item->status == true)
                <span> | <a class="text-warning fw-bolder"
                    href="{{route('shop-inactive', $item->id)}}">Inactive</a></span>
                @else
                <span> | <a class="text-success fw-bolder" href="{{route('shop-active', $item->id)}}">Active</a></span>
                @endif

                @endif

                @if (Auth::user()->can('shop-delete'))
                <span> | <a class="text-danger fw-bolder delete" href="{{route('shop-delete', $item->id)}}">Delete</a></span>
                @endif
              </div>
            </td>

            <td class="text-start">
              <b>Shop Owner: </b>{{ Str::title($item->shopOwnerData->name) }} <br>
              <b>Shop Keeper: </b>{{ Str::title($item->shopKeeperData->name) }} <br>
              <b>Business Type: </b>{{ $item->businessTypeData->type_name }} <br>
              <b>Floor: </b>{{ $item->floorData->floor_name }} <br>
            </td>

            <td class="text-center">
              @if($item->status == true)
              <span class="badge bg-success">Active</span>
              @else
              <span class="badge bg-warning">Inactive</span>
              @endif
            </td>

          </tr>

          {{-- edit modal  --}}
          <div class="modal fade" id="updateShop{{$item->id}}" tabindex="-1" aria-labelledby="oneInputModalLabel"
            aria-hidden="true" data-bs-backdrop="static">
            <div class="modal-dialog modal-dialog-centered max-width-900px">
              <div class="modal-content">
                <div class="modal-header">
                  <h1 class="modal-title fs-5" id="oneInputModalLabel">Update Shop</h1>
                  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body p-0">
                  <form action="{{ route('shop.update', $item->id) }}" method="post" enctype="multipart/form-data"
                    onsubmit="return checkValidateForUpdate({{$item->id}})">
                    @csrf
                    @method('put')
                    <div class="row px-4">
                      <div class=" col-md-4 my-1">
                        <div class="">
                          <div class="form-group custom-select2-form">
                            <label for="floor_id">Floor <span class=" text-danger">(required)</span>
                            </label>
                            <select name="floor_id" id="floor_id{{$item->id}}" class="form-select select2" required>
                              <option value="" selected disabled>Select Floor</option>
                              @foreach($floorData as $singleFD)
                              <option value="{{$singleFD->id}}"
                                {{$item->floor_id == $singleFD->id ? 'selected' : ''}}
                                >{{$singleFD->floor_name}}</option>
                              @endforeach
                            </select>
                          </div>

                          @error('floor_id')
                          <span class=text-danger>{{$message}}</span>
                          @enderror
                        </div>
                      </div>

                      <div class=" col-md-4 my-1">
                        <div class="">
                          <div class="form-group custom-select2-form">
                            <label for="business_type_id">Business Type <span class=" text-danger">(required)</span>
                            </label>
                            <select name="business_type_id" id="business_type_id{{$item->id}}" class="form-select select2" required>
                              <option value="" selected disabled>Select Business Type</option>
                              @foreach($businessTypeData as $singleSTD)
                              <option value="{{$singleSTD->id}}"
                                {{$item->business_type_id == $singleSTD->id ? 'selected' : ''}}
                                >{{$singleSTD->type_name}}</option>
                              @endforeach
                            </select>
                          </div>

                          @error('business_type_id')
                          <span class=text-danger>{{$message}}</span>
                          @enderror
                        </div>
                      </div>
                      
                      <div class=" col-md-4 my-1">
                        <div class="">
                          <div class="form-group custom-select2-form">
                            <label for="service_charge_id{{$item->id}}">Service Charge <span class=" text-danger">(required)</span>
                            </label>
                            <select name="service_charge_id" id="service_charge_id{{$item->id}}" class="form-select select2" required>
                              <option value="" selected disabled>Select Service Charge</option>
                              @foreach($serviceChargeData as $singleSCD)
                              <option value="{{$singleSCD->id}}"
                                {{$item->service_charge_id == $singleSCD->id ? 'selected' : ''}}
                                >{{$singleSCD->service_charge}}</option>
                              @endforeach
                            </select>
                          </div>

                          @error('service_charge_id')
                          <span class=text-danger>{{$message}}</span>
                          @enderror
                        </div>
                      </div>

                      <div class=" col-md-4 my-1">
                        <label for="shop_name" class="form-label mb-2">Shop Name <span class="text-danger">(required)</span></label>
                        <input type="text" class="form-control  form-control-solid" name="shop_name" id="shop_name" placeholder="Shop Name"
                          value="{{$item->shop_name}}" required>
                      
                        @error('shop_name')
                        <span class=text-danger>{{$message}}</span>
                        @enderror
                      </div>

                      <div class=" col-md-4 my-1">
                        <label for="shop_no" class="form-label mb-2">Shop No <span
                            class="text-danger">(required)</span></label>
                        <input type="number" class="form-control  form-control-solid" name="shop_no" id="shop_no"
                          placeholder="Shop No" value="{{$item->shop_no}}" required>

                        @error('shop_no')
                        <span class=text-danger>{{$message}}</span>
                        @enderror
                      </div>

                      <div class=" col-md-4 my-1">
                        <label for="shop_size" class="form-label mb-2">Floor Size <span
                            class="text-danger">(required)</span></label>
                        <input type="number" class="form-control  form-control-solid" name="shop_size" id="shop_size"
                          placeholder="Floor Size" value="{{$item->shop_size}}" required>

                        @error('shop_size')
                        <span class=text-danger>{{$message}}</span>
                        @enderror
                      </div>

                      <div class=" col-md-6 my-1">
                        <div class="">
                          <div class="form-group custom-select2-form">
                            <label for="shop_owner_id{{$item->id}}">Shop Owner <span class=" text-danger">(required)</span>
                            </label>
                            <select name="shop_owner_id" id="shop_owner_id{{$item->id}}" class="form-select select2" required>
                              <option value="" selected disabled>Select Shop Owner</option>
                              @foreach($shopOwnerData as $singleSOD)
                              <option value="{{$singleSOD->id}}"
                                {{$item->shop_owner_id == $singleSOD->id ? 'selected' : ''}}
                                >{{Str::title($singleSOD->name)}} / {{$singleSOD->mobile}}</option>
                              @endforeach
                            </select>
                          </div>

                          @error('shop_owner_id')
                          <span class=text-danger>{{$message}}</span>
                          @enderror
                        </div>
                      </div>
                      
                      <div class=" col-md-6 my-1">
                        <div class="">
                          <div class="form-group custom-select2-form">
                            <label for="shop_keeper_id{{$item->id}}">Shop Keeper <span class=" text-danger">(required)</span>
                            </label>
                            <select name="shop_keeper_id" id="shop_keeper_id{{$item->id}}" class="form-select select2" required>
                              <option value="" selected disabled>Select Shop Keeper</option>
                              @foreach($shopKeeperData as $singleSKD)
                              <option value="{{$singleSKD->id}}"
                                {{$item->shop_keeper_id == $singleSKD->id ? 'selected' : ''}}
                                >{{Str::title($singleSKD->name)}} / {{$singleSKD->mobile}}</option>
                              @endforeach
                            </select>
                          </div>

                          @error('shop_keeper_id')
                          <span class=text-danger>{{$message}}</span>
                          @enderror
                        </div>
                      </div>

                      <div class="col-md-12 my-1">
                        <div class="form-group">
                          <label for="shop_description">Shop Details<span class="text-danger"></span>
                          </label>
                      
                          <textarea name="shop_description" id="shop_description" cols="10" rows="2" placeholder="Details"
                            class="form-control form-control-solid">{{$item->shop_description}}</textarea>
                        </div>
                      
                        @error('shop_description')
                        <span class=text-danger>{{$message}}</span>
                        @enderror
                      </div>

                    </div>
                    <div class="modal-footer">
                      <div class="">
                        <button type="submit" class="btn btn-success">Update</button>
                        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Cancel</button>
                      </div>
                    </div>
                  </form>
                </div>
              </div>
            </div>
          </div>
          @endif
          @endforeach


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

    @if(isset($allShopCount) && $allShopCount > 10)
    <div class="clearfix d-flex pb-2">
      <div class="float-left">
        <p class="text-muted">
          {{ __('Showing') }}
          <span class="font-weight-bold">{{ $shopData->firstItem() }}</span>
          {{ __('to') }}
          <span class="font-weight-bold">{{ $shopData->lastItem() }}</span>
          {{ __('of') }}
          <span class="font-weight-bold">{{ $shopData->total() }}</span>
          {{ __('results') }}
        </p>
      </div>

      <div class="float-right custom-table-pagination">
        {!! $shopData->links('pagination::bootstrap-4') !!}
      </div>
    </div>
    @endif

  </div>

  {{-- create modal  --}}
  <div class="modal fade" id="addShop" tabindex="-1" aria-labelledby="oneInputModalLabel" aria-hidden="true"
    data-bs-backdrop="static">
    <div class="modal-dialog modal-dialog-centered max-width-900px">
      <div class="modal-content">
        <div class="modal-header">
          <h1 class="modal-title fs-5" id="oneInputModalLabel">New Shop</h1>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body p-0">
          <form action="{{ route('shop.store') }}" method="POST" onsubmit="return checkValidate()">
            @csrf
            <div class="row px-4">
              <div class=" col-md-4 my-1">
                <div class="">
                  <div class="form-group custom-select2-form">
                    <label for="floor_id">Floor <span class=" text-danger">(required)</span>
                    </label>
                    <select name="floor_id" id="floor_id" class="form-select select2" required>
                      <option value="" selected disabled>Select Floor</option>
                      @foreach($floorData as $singleFD)
                      <option value="{{$singleFD->id}}">{{$singleFD->floor_name}}</option>
                      @endforeach
                    </select>
                  </div>

                  @error('floor_id')
                  <span class=text-danger>{{$message}}</span>
                  @enderror
                </div>
              </div>

              <div class=" col-md-4 my-1">
                <div class="">
                  <div class="form-group custom-select2-form">
                    <label for="business_type_id">Business Type <span class=" text-danger">(required)</span>
                    </label>
                    <select name="business_type_id" id="business_type_id" class="form-select select2" required>
                      <option value="" selected disabled>Select Business Type</option>
                      @foreach($businessTypeData as $singleSTD)
                      <option value="{{$singleSTD->id}}">{{$singleSTD->type_name}}</option>
                      @endforeach
                    </select>
                  </div>

                  @error('business_type_id')
                  <span class=text-danger>{{$message}}</span>
                  @enderror
                </div>
              </div>
              
              <div class=" col-md-4 my-1">
                <div class="">
                  <div class="form-group custom-select2-form">
                    <label for="service_charge_id">Service Charge <span class=" text-danger">(required)</span>
                    </label>
                    <select name="service_charge_id" id="service_charge_id" class="form-select select2" required>
                      <option value="" selected disabled>Select Service Charge</option>
                      @foreach($serviceChargeData as $singleSCD)
                      <option value="{{$singleSCD->id}}">{{$singleSCD->service_charge}}</option>
                      @endforeach
                    </select>
                  </div>

                  @error('service_charge_id')
                  <span class=text-danger>{{$message}}</span>
                  @enderror
                </div>
              </div>

              <div class=" col-md-4 my-1">
                <label for="shop_name" class="form-label mb-2">Shop Name <span class="text-danger">(required)</span></label>
                <input type="text" class="form-control  form-control-solid" name="shop_name" id="shop_name"
                  placeholder="Shop Name" value="" required>

                @error('shop_name')
                <span class=text-danger>{{$message}}</span>
                @enderror
              </div>
              
              <div class=" col-md-4 my-1">
                <label for="shop_no" class="form-label mb-2">Shop No <span class="text-danger">(required)</span></label>
                <input type="number" class="form-control  form-control-solid" name="shop_no" id="shop_no"
                  placeholder="Shop No" value="" required>

                @error('shop_no')
                <span class=text-danger>{{$message}}</span>
                @enderror
              </div>

              <div class=" col-md-4 my-1">
                <label for="shop_size" class="form-label mb-2">Shop Size <span
                    class="text-danger">(required)</span></label>
                <input type="number" class="form-control  form-control-solid" name="shop_size" id="shop_size"
                  placeholder="Shop Size" value="" required>

                @error('shop_size')
                <span class=text-danger>{{$message}}</span>
                @enderror
              </div>

              <div class=" col-md-6 my-1">
                <div class="">
                  <div class="form-group custom-select2-form ">
                    <label for="shop_owner_id">Shop Owner <span class=" text-danger">(required)</span>
                    </label>
                    <select name="shop_owner_id" id="shop_owner_id" class="form-select select2" required>
                      <option value="" selected disabled>Select Shop Owner</option>
                      @foreach($shopOwnerData as $singleSOD)
                      <option value="{{$singleSOD->id}}">{{Str::title($singleSOD->name)}} / {{$singleSOD->mobile}}</option>
                      @endforeach
                    </select>
                  </div>

                  @error('shop_owner_id')
                  <span class=text-danger>{{$message}}</span>
                  @enderror
                </div>
              </div>

              <div class=" col-md-6 my-1">
                  <div class="">
                    <div class="form-group custom-select2-form">
                      <label for="shop_keeper_id">Shop Keeper <span class=" text-danger">(required)</span>
                      </label>
                      <select name="shop_keeper_id" id="shop_keeper_id" class="form-select select2" required>
                        <option value="" selected disabled>Select Shop Keeper</option>
                        @foreach($shopKeeperData as $singleSKD)
                        <option value="{{$singleSKD->id}}">{{Str::title($singleSKD->name)}} / {{$singleSKD->mobile}}</option>
                        @endforeach
                      </select>
                    </div>

                    @error('shop_keeper_id')
                    <span class=text-danger>{{$message}}</span>
                    @enderror
                  </div>
                </div>

              <div class="col-md-12 my-1">
                <div class="form-group">
                  <label for="shop_description">Shop Details<span class="text-danger"></span>
                  </label>

                  <textarea name="shop_description" id="shop_description" cols="10" rows="2" placeholder="Details"
                    class="form-control form-control-solid"></textarea>
                </div>

                @error('shop_description')
                <span class=text-danger>{{$message}}</span>
                @enderror
              </div>

            </div>
            <div class="modal-footer">
              <div class="">
                <button type="submit" class="btn btn-success">Save</button>
                <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Cancel</button>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>

</div>
@endsection


@section('scripts')
<script>
  $("#floor_id").select2({
    dropdownParent: $('#addShop')
  });
  $("#business_type_id").select2({
    dropdownParent: $('#addShop')
  });
  $("#service_charge_id").select2({
    dropdownParent: $('#addShop')
  });
  $("#shop_owner_id").select2({
    dropdownParent: $('#addShop')
  });
  $("#shop_keeper_id").select2({
    dropdownParent: $('#addShop')
  });

    function checkValidate() {

        return true;
    }
    
    //For update...
    function checkValidateForUpdate(id) {

        return true;
    }
    
    //To show update modal...
    function updateShop(id) {
      $("#updateShop"+id).modal('show');

      $("#floor_id"+id).select2({
        dropdownParent: $('#updateShop'+id)
      });
      
      $("#business_type_id"+id).select2({
        dropdownParent: $('#updateShop'+id)
      });
      $("#service_charge_id"+id).select2({
      dropdownParent: $('#updateShop'+id)
      });
      $("#shop_owner_id"+id).select2({
      dropdownParent: $('#updateShop'+id)
      });
      $("#shop_keeper_id"+id).select2({
      dropdownParent: $('#updateShop'+id)
      });
    }
</script>
@endsection