<?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
<?php } ?>
<?php if ($shipping_methods) { ?>
  <div class="row">
    <?php foreach ($shipping_methods as $shipping_method) { ?>
      <?php if (!$shipping_method['error']) { ?>
        <div class="col-xs-12 col-sm-6 col-md-4">
          <div class="content-box">
            <?php if (strrpos($shipping_method['code'], "iFieldSimuleFrete") === false) { ?>
              <?php foreach ($shipping_method['quote'] as $quote) { ?>
                <input class="card-controll" type="radio" name="shipping_method" value="<?php echo $quote['code']; ?>" id="<?php echo $quote['code']; ?>" <?php echo $quote['code'] == $code || !$code ? 'checked' : ''; ?>>
                <label class="card-label ft-rounded-medium <?php echo ($quote['code'] == 'fretedinamico.fretedinamico') ? 'tooltip' : '' ?>" <?php echo ($quote['code'] == 'fretedinamico.fretedinamico') ? 'style="min-height: 161px;"' : '' ?> for="<?php echo $quote['code']; ?>">
                  <?php if ($quote['code'] == 'fretedinamico.fretedinamico') { ?>
                    <span class="ft-text-center ft-text-medium"><?php echo $shipping_method['title']; ?></span>
                    <span class="ft-text-center"><?php echo $text_valor; ?></span>
                    <hr>
                    <span>
                      <?php echo $text_transportadora ?>
                    </span>
                  <?php } else { ?>
                    <span class="ft-text-center ft-text-medium"><?php echo $quote['title']; ?></span>
                    <span class="ft-text-center"><?php echo $text_retirar; ?>
                    <hr>
                    <span>
                      <?php echo str_ireplace('Loja Atacado e Show-room: ', '', $this->config->get('config_address')); ?>
                    </span>
                  <?php } ?>
                </label>
                <?php if ($quote['code'] == 'fretedinamico.fretedinamico') { ?>
                  <div class="seltransportadora ft-top-margin-large">
                  	<input type="text" MaxLength="100" name="comment" id="comment" value="<?php echo $quote['code'] == $code || !$code ? str_replace('Forma de envio: ','',$comment) : $comment; ?>" placeholder="Nome da transportadora" class="ft-padding-small">
                  	<b>
                      <p style="color:red;"><?php echo $text_fob ?></p>
                    </b>
                    <?php echo $shipping_address ?>
                  </div>
                <?php } ?>
              <?php } ?>
            <?php } else { ?>
              <div class="card-label-ifield ft-rounded-medium tooltip">
                <span class="ft-text-center ft-text-medium"><?php echo $shipping_method['title']; ?></span>
                <span class="ft-text-center"><?php echo $text_envio_ifield; ?></span>
                <hr>
                <div class="card-label-ifield-radio">
                  <?php foreach ($shipping_method['quote'] as $quote) { ?>
                    <label class="ft-radio-group ft-vertical-padding-x-small ft-visible-inline-block" for="<?php echo $quote['code']; ?>">
                      <input class="ft-field-radio" type="radio" name="shipping_method" value="<?php echo $quote['code']; ?>" id="<?php echo $quote['code']; ?>" <?php echo $quote['code'] == $code || !$code ? 'checked' : ''; ?>>
                      <span class="ft-field-radio-text"><?php echo $quote['title']; ?> - <?php echo $quote['text']; ?></span>
                    </label>
                  <?php } ?>
                </div>
              </div>
              <div class="seltransportadora ft-top-margin-large">
                <?php
                  $apelido     = ($address_default['apelido'] != '') ? '('. $address_default['apelido'] .') ' : '';
                  $bairro      = ($address_default['address_2'] != '') ? ', ' . $address_default['address_2'] : '';
                  $complemento = ($address_default['complemento'] != '') ? ', ' . $address_default['complemento'] : '';
                ?>
                <input type="radio" name="address_id" id="card-label-ifield-address" value="<?php echo $address_default['address_id'] ?>" class="card-controll">
                <label for="card-label-ifield-address" class="card-label ft-rounded-medium ft-bottom-margin-medium">
                  <?php echo $apelido . $address_default['firstname'] . $address_default['lastname']; ?>
                  <?php echo $address_default['address_1']; ?>,
                  <?php echo $address_default['numero'] . $bairro . $complemento; ?>,
                  <?php echo $address_default['city']; ?>,
                  <?php echo $address_default['zone']; ?>,
                  <?php echo $address_default['country']; ?>
                </label>
              </div>
            <?php } ?>
          </div>
        </div>
      <?php } else { ?>
        <div class="error"><?php echo $shipping_method['error']; ?></div>
      <?php } ?>
    <?php } ?>
  </div>
<?php } ?>
<button type="submit" id="button-shipping-method" class="ft-btn-default ft-btn-medium ft-btn-success ft-top-margin-large"><?php echo $button_continue ?></button>
<script>
  $(document).ready(function() {
    $('.card-label-ifield').on('click', function() {
      $(this).next('.seltransportadora').find('input[name=address_id]').prop('checked', true);
      if (!$(this).hasClass('active')) {
        $('.card-label-ifield-radio .ft-radio-group:first-child .ft-field-radio').prop('checked', true);
      }
      $(this).addClass('active');
    });
    $('.card-controll').on('change', function() {
      $('.card-label-ifield').removeClass('active');
    });
  });

  $('[name=shipping_address]').on('change', function() {
    if ($('.shipping_address').val() == 'existing') {
      $('.shipping-address-container').show();
    } else {
      $('.shipping-address-container').hide();
    }
  });
  $('[name=address_id]').on('change', function() {
    $.ajax({
      url : 'index.php?route=checkout/shipping_method/setShippingAddressId',
      type : 'post',
      data : {
        address_id: $('input[name=address_id]:checked').val()
      },
      success: function(teste){
        // console.log('success');
        // console.log(teste);
      },
      error: function(){
        console.log('error');
      }
    });
  });
</script>
