<div class="panel panel-danger">
  <div class="panel-heading">
    <span class="panel-title">Ustawienia apilo.com</span>
  </div>
  <div class="panel-body">
    <div class="row">
      <div class="col-lg-6">
        <div class="form-group mb10">
          <label class="col-lg-3 control-label" for="inputDefault">Włącz synchronizację z apilo.com</label>
          <div class="col-lg-9">
            <div class="bs-component">
              <div class="input-group">
                <select class="form-control" id="enabled" name="enabled">
                  <option value="">--- wybierz ---</option>
                  <option value="0" <? if ( !$this -> settings['enabled'] ):?>selected<? endif;?>>nie</option>
                  <option value="1" <? if ( $this -> settings['enabled'] ):?>selected<? endif;?>>tak</option>
                </select>
                <span class="input-group-addon cursor" field-id="enabled">
                  <i class="fa fa-save"></i>
                </span>
              </div>
            </div>
          </div>
        </div>
        <div class="form-group mb10">
          <label class="col-lg-3 control-label" for="inputDefault">Włącz synchronizację produktów</label>
          <div class="col-lg-9">
            <div class="bs-component">
              <div class="input-group">
                <select class="form-control" id="sync_products" name="sync_products">
                  <option value="">--- wybierz ---</option>
                  <option value="0" <? if ( !$this -> settings['sync_products'] ):?>selected<? endif;?>>nie</option>
                  <option value="1" <? if ( $this -> settings['sync_products'] ):?>selected<? endif;?>>tak</option>
                </select>
                <span class="input-group-addon cursor" field-id="sync_products">
                  <i class="fa fa-save"></i>
                </span>
              </div>
            </div>
          </div>
        </div>
        <div class="form-group mb10">
          <label class="col-lg-3 control-label" for="inputDefault">Włącz synchronizację zamówień</label>
          <div class="col-lg-9">
            <div class="bs-component">
              <div class="input-group">
                <select class="form-control" id="sync_orders" name="sync_orders">
                  <option value="">--- wybierz ---</option>
                  <option value="0" <? if ( !$this -> settings['sync_orders'] ):?>selected<? endif;?>>nie</option>
                  <option value="1" <? if ( $this -> settings['sync_orders'] ):?>selected<? endif;?>>tak</option>
                </select>
                <span class="input-group-addon cursor" field-id="sync_orders">
                  <i class="fa fa-save"></i>
                </span>
              </div>
            </div>
          </div>
        </div>
        <div class="form-group">
          <label class="col-lg-3 control-label" for="inputDefault">Data rozpoczęcia synchronizacji zamówień</label>
          <div class="col-lg-9">
            <div class="bs-component">
              <div class="input-group">
                <input class="form-control" type="text" id="sync_orders_date_start" name="sync_orders_date_start" placeholder="" value="<?= $this -> settings['sync_orders_date_start'];?>">
                <span class="input-group-addon cursor" field-id="sync_orders_date_start">
                  <i class="fa fa-save"></i>
                </span>
              </div>
            </div>
          </div>
        </div>
        <div class="form-group mb10">
          <label class="col-lg-3 control-label" for="inputDefault">Platforma</label>
          <div class="col-lg-9">
            <div class="bs-component">
              <div class="input-group">
                <?
                  $platform_list = unserialize( $this ->  settings['platform-list'] );
                ?>
                <select class="form-control" id="platform-id" name="platform-id">
                  <option value="">--- wybierz platformę ---</option>
                  <? foreach ( $platform_list as $platform ):?>
                    <option value="<?= $platform['id'];?>" <? if ( $this -> settings['platform-id'] == $platform['id'] ):?>selected<? endif;?>><?= $platform['name'];?> <?= $platform['description'];?></option>
                  <? endforeach;?>
                </select>
                <span class="input-group-addon cursor" field-id="platform-id">
                  <i class="fa fa-save"></i>
                </span>
              </div>
            </div>
          </div>
        </div>
        <div class="form-group mb10">
          <label class="col-lg-3 control-label" for="inputDefault">Client ID</label>
          <div class="col-lg-9">
            <div class="bs-component">
              <div class="input-group">
                <input class="form-control" type="text" id="client-id" name="client-id" placeholder="" value="<?= $this -> settings['client-id'];?>">
                <span class="input-group-addon cursor" field-id="client-id">
                  <i class="fa fa-save"></i>
                </span>
              </div>
            </div>
          </div>
        </div>
        <div class="form-group mb10">
          <label class="col-lg-3 control-label" for="inputDefault">Client Secret</label>
          <div class="col-lg-9">
            <div class="bs-component">
              <div class="input-group">
                <input class="form-control" type="text" id="client-secret" name="client-secret" placeholder="" value="<?= $this -> settings['client-secret'];?>">
                <span class="input-group-addon cursor" field-id="client-secret">
                  <i class="fa fa-save"></i>
                </span>
              </div>
            </div>
          </div>
        </div>
        <div class="form-group mb10">
          <label class="col-lg-3 control-label" for="inputDefault">ID cennika</label>
          <div class="col-lg-9">
            <div class="bs-component">
              <div class="input-group">
                <input class="form-control" type="text" id="pricelist_id" name="pricelist_id" placeholder="" value="<?= $this -> settings['pricelist_id'];?>">
                <span class="input-group-addon cursor" field-id="pricelist_id">
                  <i class="fa fa-save"></i>
                </span>
              </div>
            </div>
          </div>
        </div>
        <div class="form-group">
          <label class="col-lg-3 control-label" for="inputDefault">Authorization Code</label>
          <div class="col-lg-9">
            <div class="bs-component">
              <div class="input-group">
                <input class="form-control" type="text" id="authorization-code" name="authorization-code" placeholder="" value="<?= $this -> settings['authorization-code'];?>" <? if ( $this -> settings['access-token'] ):?>readonly<? endif;?>>
                <? if ( !$this -> settings['access-token'] ):?>
                  <span class="input-group-addon cursor" field-id="authorization-code">
                    <i class="fa fa-save"></i>
                  </span>
                <? endif;?>
              </div>
            </div>
          </div>
        </div>
        <? if ( $this -> settings['access-token'] ):?>
          <div class="form-group">
            <label class="col-lg-3 control-label" for="inputDefault">Access Token</label>
            <div class="col-lg-9">
              <div class="bs-component">
                <div class="input-group">
                  <input class="form-control" type="text" id="access-token" name="access-token" placeholder="" value="<?= $this -> settings['access-token'];?>" readonly>
                </div>
              </div>
            </div>
          </div>
        <? endif;?>
        <? if ( $this -> settings['access-token-expire-at'] ):?>
          <div class="form-group">
            <label class="col-lg-3 control-label" for="inputDefault">Access Token Expire At</label>
            <div class="col-lg-9">
              <div class="bs-component">
                <div class="input-group">
                  <input class="form-control" type="text" id="access-token-expire-at" name="access-token-expire-at" placeholder="" value="<?= $this -> settings['access-token-expire-at'];?>" readonly>
                </div>
              </div>
            </div>
          </div>
        <? endif;?>
        <? if ( $this -> settings['refresh-token'] ):?>
          <div class="form-group">
            <label class="col-lg-3 control-label" for="inputDefault">Refresh Token</label>
            <div class="col-lg-9">
              <div class="bs-component">
                <div class="input-group">
                  <input class="form-control" type="text" id="refresh-token" name="refresh-token" placeholder="" value="<?= $this -> settings['refresh-token'];?>" readonly>
                </div>
              </div>
            </div>
          </div>
        <? endif;?>
        <? if ( $this -> settings['refresh-token-expire-at'] ):?>
          <div class="form-group">
            <label class="col-lg-3 control-label" for="inputDefault">Refresh Token Expire At</label>
            <div class="col-lg-9">
              <div class="bs-component">
                <div class="input-group">
                  <input class="form-control" type="text" id="refresh-token-expire-at" name="refresh-token-expire-at" placeholder="" value="<?= $this -> settings['refresh-token-expire-at'];?>" readonly>
                </div>
              </div>
            </div>
          </div>
        <? endif;?>
      </div>
      <div class="col-lg-6">
        <? if ( !$this -> settings['access-token'] ):?>
          <a href="#" class="btn btn-primary btn-block apilo-authorization">Autoryzuj się w apilo.com</a>
        <? else:?>
          <a href="/admin/integrations/get_payment_types_list/" class="btn btn-primary btn-block">Pobierz listę metod płatności</a>
          <a href="/admin/integrations/get_carrier_account_list/" class="btn btn-primary btn-block">Pobierz listę kont przewoźników</a>
          <a href="/admin/integrations/get_status_types_list/" class="btn btn-primary btn-block">Pobierz statusy zamówień</a>
          <a href="/admin/integrations/get_platform_list/" class="btn btn-primary btn-block">Pobierz listę platform</a>
        <? endif;?>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
  $(function() {
    $( 'body' ).on( 'click', '.apilo-authorization', function() {
      $.ajax({
        url: '/admin/integrations/apilo_authorization/',
        type: 'POST',
        success: function( response ) {
          response = JSON.parse( response );
          if ( response.status == 'ok' ) {
            location.reload();
          }
        }
      });
    });

    $('body').on('click', '.input-group-addon', function() {
      var field_id = $(this).attr('field-id');
      var value = $('#' + field_id).val();
      $.ajax({
        url: '/admin/integrations/apilo_settings_save/',
        type: 'POST',
        data: {
          field_id: field_id,
          value: value
        },
        success: function(response) {
          response = JSON.parse(response);
          if (response.status == 'ok') {
            $('#' + field_id).val(response.value);
            $('#' + field_id).closest('.form-group').addClass('has-success');
            setTimeout(function() {
              $('#' + field_id).closest('.form-group').removeClass('has-success');
            }, 5000);
          }
        }
      });
    })
  });
</script>