<?
$settings = is_array( $this -> settings ) ? $this -> settings : [];

$apilo_status = is_array( $this -> apilo_status ) ? $this -> apilo_status : [
  'is_valid' => false,
  'severity' => 'warning',
  'message' => 'Brak statusu integracji.',
];

$status_class = 'alert-warning';
if ( isset( $apilo_status['severity'] ) && $apilo_status['severity'] == 'success' ) $status_class = 'alert-success';
if ( isset( $apilo_status['severity'] ) && $apilo_status['severity'] == 'danger' ) $status_class = 'alert-danger';

$status_message = trim( (string)($apilo_status['message'] ?? '') );
if ( $status_message == '' ) $status_message = 'Brak szczegolow statusu integracji.';

$platform_list_raw = isset( $settings['platform-list'] ) ? $settings['platform-list'] : '';
$platform_list = @unserialize( $platform_list_raw );
if ( !is_array( $platform_list ) ) $platform_list = [];
?>
<div class="panel panel-danger">
  <div class="panel-heading">
    <span class="panel-title">Ustawienia apilo.com</span>
  </div>
  <div class="panel-body">
    <div class="alert <?= $status_class;?> mb15">
      <b>Status integracji Apilo:</b> <?= $status_message;?>
    </div>

    <div class="row">
      <div class="col-lg-6">
        <div class="form-group mb10">
          <label class="col-lg-3 control-label" for="inputDefault">Wlacz synchronizacje 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 ( !isset($settings['enabled']) || !$settings['enabled'] ):?>selected<? endif;?>>nie</option>
                  <option value="1" <? if ( isset($settings['enabled']) && $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">Wlacz synchronizacje produktow</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 ( !isset($settings['sync_products']) || !$settings['sync_products'] ):?>selected<? endif;?>>nie</option>
                  <option value="1" <? if ( isset($settings['sync_products']) && $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">Wlacz synchronizacje zamowien</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 ( !isset($settings['sync_orders']) || !$settings['sync_orders'] ):?>selected<? endif;?>>nie</option>
                  <option value="1" <? if ( isset($settings['sync_orders']) && $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 rozpoczecia synchronizacji zamowien</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" value="<?= isset($settings['sync_orders_date_start']) ? $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">
                <select class="form-control" id="platform-id" name="platform-id">
                  <option value="">--- wybierz platforme ---</option>
                  <? foreach ( $platform_list as $platform ):?>
                    <? if ( is_array($platform) && isset($platform['id']) ):?>
                      <option value="<?= $platform['id'];?>" <? if ( isset($settings['platform-id']) && $settings['platform-id'] == $platform['id'] ):?>selected<? endif;?>>
                        <?= isset($platform['name']) ? $platform['name'] : '';?> <?= isset($platform['description']) ? $platform['description'] : '';?>
                      </option>
                    <? endif;?>
                  <? 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" value="<?= isset($settings['client-id']) ? $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" value="<?= isset($settings['client-secret']) ? $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" value="<?= isset($settings['pricelist_id']) ? $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" value="<?= isset($settings['authorization-code']) ? $settings['authorization-code'] : '';?>">
                <span class="input-group-addon cursor" field-id="authorization-code">
                  <i class="fa fa-save"></i>
                </span>
              </div>
            </div>
          </div>
        </div>

        <? if ( !empty($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" value="<?= $settings['access-token'];?>" readonly>
                </div>
              </div>
            </div>
          </div>
        <? endif;?>

        <? if ( !empty($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" value="<?= $settings['access-token-expire-at'];?>" readonly>
                </div>
              </div>
            </div>
          </div>
        <? endif;?>

        <? if ( !empty($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" value="<?= $settings['refresh-token'];?>" readonly>
                </div>
              </div>
            </div>
          </div>
        <? endif;?>

        <? if ( !empty($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" value="<?= $settings['refresh-token-expire-at'];?>" readonly>
                </div>
              </div>
            </div>
          </div>
        <? endif;?>
      </div>

      <div class="col-lg-6">
        <a href="#" class="btn btn-primary btn-block apilo-authorization"><? if ( !empty($apilo_status['is_valid']) ):?>Ponow autoryzacje w apilo.com<? else:?>Autoryzuj sie w apilo.com<? endif;?></a>
        <a href="/admin/integrations/get_payment_types_list/" class="btn btn-primary btn-block">Pobierz liste metod platnosci</a>
        <a href="/admin/integrations/get_carrier_account_list/" class="btn btn-primary btn-block">Pobierz liste kont przewoznikow</a>
        <a href="/admin/integrations/get_status_types_list/" class="btn btn-primary btn-block">Pobierz statusy zamowien</a>
        <a href="/admin/integrations/get_platform_list/" class="btn btn-primary btn-block">Pobierz liste platform</a>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript">
  $(function() {
    $( 'body' ).on( 'click', '.apilo-authorization', function(e) {
      e.preventDefault();
      $.ajax({
        url: '/admin/integrations/apilo_authorization/',
        type: 'POST',
        success: function( response ) {
          response = JSON.parse( response );
          if ( response.status == 'ok' ) {
            location.reload();
          } else {
            alert( response.msg || 'Autoryzacja nieudana.' );
          }
        },
        error: function() {
          alert('Nie udalo sie polaczyc z endpointem autoryzacji.');
        }
      });
    });

    $('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');
            }, 3000);
          }
        }
      });
    });
  });
</script>
