@extends('views.layouts.master')

@section('content')
<!--=====================================
                    BANNER PART START
        =======================================-->
<section class="inner-section single-banner">
  <div class="container">
    <h2>{{ $igroup['IgroupName'] }}</h2>
    <ol class="breadcrumb">
      <li class="breadcrumb-item"><a href="/">Home</a></li>
      <li class="breadcrumb-item active" aria-current="page">{{ $igroup['IgroupName'] }}</li>
    </ol>
  </div>
</section>
<!--=====================================
                    BANNER PART END
        =======================================-->

<!--=====================================
                    SHOP PART START
        =======================================-->
<section class="inner-section shop-part">
  <div class="container">
    <div class="row">
      <div class="col-lg-12">
          @component('views.partial.table-options')
          @endcomponent
      </div>
    </div>
    <div class="row row-cols-2 row-cols-md-3 row-cols-lg-3 row-cols-xl-4">
    @forelse($itemGallery['Page'] ?? [] as $item)
      <div class="col">
        <form class="product-card" action="{{ route('wish-list.favorite') }}" method="POST">
          @csrf
            <input type="hidden" name="item_id" value="{{ $item['Id'] }}">
          <div class="product-media">
            <div class="product-label">
              <label class="label-text new">new</label>
            </div>

              @if (empty($item['BdPage']))
                  <button type="submit" class="product-wish wish">
                      <i class="far fa-heart"></i>
                  </button>
              @else
                  <button type="submit" class="product-wish wish">
                      <input type="hidden" name="_method" value="delete" />
                      <i class="fas fa-heart"></i>
                  </button>
              @endif

            <a class="product-image" onclick="select_item({{ $item['Id'] }})" href="{{ route('item-details', $item['C9'] ?: $item['Id']) }}"> <img src="{{ env('MEDIA_URL') . $item['C1'] }}" alt="product" onerror="this.src='/images/folder.jpg'"> </a>
          </div>
          <div class="product-content">
            <h6 class="product-name"> <a onclick="select_item({{ $item['Id'] }})" href="{{ route('item-details', $item['C9'] ?: $item['Id']) }}">{{ $item['C5'] }}</a> </h6>
            <h6 class="product-price">
              <span>{{ number_format($item['C7']) }}원</span> </h6>
          </div>
        </form>
      </div>
    @empty
        <div class="pricing_box1 full_width">
            상품이 존재하지 않습니다
        </div>
    @endforelse
    </div>
    <div class="row">
      <div class="col-lg-12">
        <div class="bottom-paginate d-flex justify-content-end">
            {{ $itemGallery['Page']->appends(['limit' => request('limit'), 'sort' => request('sort')])->links() }}
        </div>
      </div>
    </div>
  </div>
</section>
<!--=====================================
                    SHOP PART END
        =======================================-->
@endsection

@push('js')
<script>
    $(document).ready(function() {
        const items = []
        itemGallery['Page']['data'] = itemGallery['Page']['data'].map((item, index) => {
            item['index'] = index
            return item
        })

        itemGallery['Page']['data'].forEach(item => {
            items.push({
                item_id: item['Id'],
                item_name: item['C5'],
                price: item['C7'],
                item_brand: item['C6'],
                item_category: item['C3'],
                item_list_name: item['C3'] + ' ' + item['C5'],
                index: item['index'],
            })
        });

        gtag("event",  "view_item_list",  {
            "items": items
        });
    });

    function select_item(item_id) {
        const item = itemGallery['Page']['data'].filter(item => {
            return item['Id'] === item_id
        })[0]

        gtag("event",  "select_item",  {
            "items": [{
                item_id: item['Id'],
                item_name: item['C5'],
                price: item['C7'],
                item_brand: item['C6'],
                item_category: item['C3'],
                index: item['index']
            }],
            item_list_name: item['C3'] + ' ' + item['C5'],
            item_list_id: item['Id'],
            currency: "KRW"
        });
    }

    const itemGallery = @json($itemGallery);
</script>
@endpush
