<div class="g-container" data="table:order-details">
  <div class="panel panel-info panel-border top">
    <div class="panel-heading">
      <span class="panel-title">Kobinacje produktu: <?= $this -> product['languages'][ $this -> default_language ]['name'];?></span>
    </div>
    <div class="panel-heading p10 pl15" id="g-menu" style="height: auto;">
      <a class="btn btn btn-dark btn-sm mr5 btn-sm mr5" href="/admin/shop_product/view_list/"><i class="fa fa-reply mr5"></i>Wstecz</a>
    </div>
    <div class="panel-body">
      <div class="row">
        <div class="col-lg-7 col-xl-9">
          <div class="table-responsive">
            <table class="table table-hover table-bordered table-condensed">
              <thead>
                <tr>
                  <th></th>
                  <th>Kombinacja</th>
                  <th>SKU</th>
                  <th>Stan magazynowy</th>
                  <th>Cena netto</th>
                  <th>Zam. SM 0</th>
                  <th>Baselinker</th>
                  <th style="width: 100px;">Opcje</th>
                </tr>
              </thead>
              <tbody>
                <? if ( \S::is_array_fix( $this -> product_permutations ) ): foreach ( $this -> product_permutations as $product ):?>
                  <tr>
                    <td></td>
                    <td>
                      <?
                        $attributes = explode( '|', $product['permutation_hash'] );
                        foreach ( $attributes as $attribute ):
                          $attribute_tmp = explode( '-', $attribute );
                          echo \admin\factory\ShopAttribute::get_attribute_name_by_id( $attribute_tmp[0] ) . ' - <b>' . \admin\factory\ShopAttribute::get_attribute_value_by_id( $attribute_tmp[1] ) . '</b>';
                          if ( $attribute != end( $attributes ) )
                            echo ', ';
                        endforeach;
                      ?>
                    </td>
                    <td>
                      <input type="text" name="sku" value="<?= $product['sku'];?>" class="form-control" style="max-width: 100px;" onchange="$.ajax({ type: 'POST', cache: false, url: '/admin/shop_product/product_combination_sku_save/', data: { product_id: <?= $product['id'];?>, sku: $( this ).val() } } );">
                    </td>
                    <td>
                      <input type="text" name="quantity" value="<?= $product['quantity'];?>" class="form-control" style="max-width: 100px;" onchange="$.ajax({ type: 'POST', cache: false, url: '/admin/shop_product/product_combination_quantity_save/', data: { product_id: <?= $product['id'];?>, quantity: $( this ).val() } } );">
                    </td>
                    <td>
                      <input type="text" name="price" value="<?= $product['price_netto'];?>" class="form-control" style="max-width: 100px;" onchange="$.ajax({ type: 'POST', cache: false, url: '/admin/shop_product/product_combination_price_save/', data: { product_id: <?= $product['id'];?>, price: $( this ).val() } } );">
                    </td>
                    <td>
                      <input type="checkbox" name="stock_0_buy" <? if ( $product['stock_0_buy'] ): echo 'checked="checked"'; endif;?> onchange="$.ajax({ type: 'POST', cache: false, url: '/admin/shop_product/product_combination_stock_0_buy_save/', data: { product_id: <?= $product['id'];?>, stock_0_buy: $( this ).is( ':checked' ) } } );">
                    </td>
                    <td>
                      <?
                        if ( $product['baselinker_product_name'] != "" )
                          echo "<span title='" . $product['baselinker_product_name'] . "'>" . substr( $product['baselinker_product_name'], 0, 25 ) . "...</span>";
                        else
                          echo "<span class='text-danger baselinker-product-search' product-id='" . $product['id'] . "'>nie przypisano <i class='fa fa-search'></i></span>";
                      ?>
                    </td>
                    <td class="text-center">
                        <a href="/admin/shop_product/delete_combination/combination_id=<?= $product['id'];?>&product_id=<?= $product['parent_id'];?>" class="btn btn-danger btn-delete-permutation"><i class="fa fa-trash"></i></a>
                    </td>
                  </tr>
                <? endforeach; endif;?>
              </tbody>
            </table>
          </div>
        </div>
        <div class="col-lg-5 col-xl-3">
          <form action="/admin/shop_product/generate_combination/product_id=<?= $this -> product['id'];?>" method="POST">
            <button type="submit" class="btn btn-success" style="margin-bottom: 25px;">Wygeneruj kombinacje</button>
            <? foreach ( $this -> attributes as $attribute ):?>
              <div class="combination-attribute">
                <div class="title">
                  <?= $attribute['languages'][ $this -> default_language ]['name'];?>
                </div>
                  <ul class="values">
                    <? foreach ( $attribute['values'] as $value ):?>
                      <li>
                        <input type="checkbox" class="g-checkbox" name="attribute_<?= $attribute['id'];?>[]" value="<?= $value['id'];?>"> <?= $value['languages'][ $this -> default_language ]['name'];?>
                      </li>
                    <? endforeach;?>
                  </ul>
              </div>
            <? endforeach;?>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
<link rel="stylesheet" type="text/css" href="/libraries/grid/plugins/icheck/skins/minimal/minimal.css">
<link rel="stylesheet" type="text/css" href="/libraries/grid/plugins/icheck/skins/minimal/blue.css">
<script type="text/javascript" src="/libraries/grid/plugins/icheck/icheck.min.js"></script>
<script type="text/javascript">
  $( function()
  {
    $( '.g-checkbox, .g-radio' ).iCheck({
      checkboxClass: 'icheckbox_minimal-blue',
      radioClass: 'iradio_minimal-blue'
    });

    $( 'body' ).on( 'click', '.btn-delete-permutation', function(e)
    {
      e.preventDefault();
      var href = $( this ).attr( 'href' );

      $.alert(
      {
        title: 'Pytanie',
        content: 'Na pewno chcesz usunąć wybraną kombinację?',
        type: 'orange',
        closeIcon: true,
        closeIconClass: 'fa fa-times',
        typeAnimated: true,
        animation: 'opacity',
        columnClass: 'col-12 col-lg-10',
        theme: 'modern',
        icon: 'fa fa-question',
        buttons: {
          cancel: {
            text: 'Nie',
            btnClass: 'btn-dark',
            action: function() {}
          },
          confirm: {
            text: 'Tak',
            btnClass: 'btn-danger',
            keys: ['enter'],
            action: function() {
              document.location.href = href;
            }
          }
        }
      });

      return false;
    });

    $( 'body' ).on( 'click', '.baselinker-product-search', function() {
      var product_id = $( this ).attr( 'product-id' );

      $.ajax({
        type: 'POST',
        cache: false,
        url: '/admin/shop_product/baselinker_product_search/',
        data: {
          product_id: product_id
        },
        beforeSend: function() {
          $( '#overlay' ).show();
        },
        success: function( response ) {
          data = jQuery.parseJSON( response );

          if ( data.status == 'SUCCESS' ) {
            $('.baselinker-found-products').remove();
            var html = '<div class="baselinker-found-products">';
            html += '<p>Znaleziono ' + data.products.length + ' produktów</p>';
            html += '<select class="form-control baselinker-product-select" product-id="' + product_id + '">';
            $.each( data.products, function( index, value ) {
              html += '<option value="' + value.product_id + '">' + value.name + ' SKU: ' + value.sku + '</option>';
            });
            html += '</select>';
            html += '<button class="btn btn-success baselinker-product-select-save" product-id="' + product_id + '">Zapisz</button>';
            html += '</div>';
            $( 'span[product-id="' + product_id + '"]' ).closest( 'td' ).append( html );
          } else if ( data.status == 'error' ) {
            $.alert({
              title: 'Błąd',
              content: data.msg,
              type: 'red',
              closeIcon: true,
              closeIconClass: 'fa fa-times',
              typeAnimated: true,
              animation: 'opacity',
              columnClass: 'col-12 col-lg-10',
              theme: 'modern',
              icon: 'fa fa-exclamation-triangle',
              buttons: {
                confirm: {
                  text: 'OK',
                  btnClass: 'btn-danger',
                  keys: ['enter'],
                  action: function() {}
                }
              }
            });
          }
        }
      });
    });

    $( 'body' ).on( 'click', '.baselinker-product-select-save', function(){
      var product_id = $( this ).attr( 'product-id' );
      var baselinker_product_id = $( '.baselinker-product-select[product-id="' + product_id + '"]' ).val();
      var baselinker_product_name = $( '.baselinker-product-select[product-id="' + product_id + '"] option:selected' ).text();

      $.ajax({
        type: 'POST',
        cache: false,
        url: '/admin/shop_product/baselinker_product_select_save/',
        data: {
          product_id: product_id,
          baselinker_product_id: baselinker_product_id,
          baselinker_product_name: baselinker_product_name
        },
        beforeSend: function() {
          $( '#overlay' ).show();
        },
        success: function( response ) {
          data = jQuery.parseJSON( response );

          if ( data.status == 'ok' ) {
            $( '.baselinker-product-select[product-id="' + product_id + '"]' ).closest( '.baselinker-found-products' ).remove();
            $( 'span[product-id="' + product_id + '"]' ).html( '<span title="' + baselinker_product_name + '">' + baselinker_product_name.substr( 0, 25 ) + '...</span>' ).removeClass( 'baselinker-product-search' ).removeClass( 'text-danger' );
          }
        }
      });
    });
  });
</script>