<?
global $db, $settings;

ob_start();
?>
<div class="logo">
  <? if ( file_exists( '../' . \front\factory\GlobelusFirms::logo_url( $this -> advert['user_id'] ) ) ):?>
    <img src="/logo/<?= $this -> advert['user_id'];?>/<?= \S::get_token();?>">
  <? else:?>
    <img src="/images/no-logo.jpg">
  <? endif;?>
</div>
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::select(
                array(
                  'label'   => 'Wyłączone przez administratora',
                  'class'   => 'form-edit',
                  'id'      => 'disabled_by_admin',
                  'values'  => [ 0 => 'nie', 1 => 'tak' ],
                  'value'   => $this -> advert['disabled_by_admin'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'disabled_by_admin', 'operation' => 'update' ]
                )
            );?>
  </div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::input(
                array(
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'Tytuł',
                  'id'      => 'title',
                  'value'    => $this -> advert['title'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'title', 'operation' => 'update' ]
                )
            );?>
  </div>
  <div class="col-xs-12 col-md-6">
    <?= \Html::form_text(
                array(
                  'label'   => 'Pracodawca',
                  'id'      => 'firm_name',
                  'text'    => $this -> advert['firm_name']
                )
            );?>
  </div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?
    $countries[0] = '--- brak przypisanego kraju ---';
    if ( is_array( $this -> countries ) and count( $this -> countries ) ): foreach ( $this -> countries as $country ):
      $countries[ $country['id'] ] = $country['name'];
    endforeach; endif;
    ?>
    <?= \Html::select(
                array(
                  'class'   => 'form-edit',
                  'label'   => 'Kraj',
                  'id'      => 'country_id',
                  'values'  => $countries,
                  'value'   => $this -> advert['country_id'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'country_id', 'operation' => 'update' ]
                )
            );?>
  </div>
  <div class="col-xs-12 col-md-6">
    <?= \Html::input(
                array(
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'Miasto',
                  'id'      => 'city',
                  'value'    => $this -> advert['city'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'city', 'operation' => 'update' ]
                )
            );?>
  </div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?
    $voivodeships[''] = '--- brak przypisanego województwa ---';
    if ( is_array( $this -> voivodeships ) ): foreach ( $this -> voivodeships as $voivodeship ):
      $voivodeships[ $voivodeship['id'] ] = $voivodeship['name'];
    endforeach; endif;
    ?>
    <?= \Html::select(
                array(
                  'class'   => 'form-edit',
                  'label'   => 'Województwo',
                  'id'      => 'id_voivodeship',
                  'values'  => $voivodeships,
                  'value'   => $this -> advert['id_voivodeship'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'id_voivodeship', 'operation' => 'update' ]
                )
            );?>
  </div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?
    $categories[0] = '--- brak przypisanej kategorii ---';
    if ( is_array( $this -> categories ) and count( $this -> categories ) ): foreach ( $this -> categories as $category ):
      $categories[ $category['id'] ] = $category['name'];
    endforeach; endif;
    ?>
    <?= \Html::select(
                array(
                  'label'   => 'Branża',
                  'class'   => 'form-edit',
                  'id'      => 'category_id',
                  'values'  => $categories,
                  'value'   => $this -> advert['category_id'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'category_id', 'operation' => 'update' ]
                )
            );?>
  </div>
</div>
<hr />
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::select(
                array(
                  'label'   => 'Ogłoszenie wyróżnione',
                  'id'      => 'highlight',
                  'values'  => [ 0 => 'nie', 1 => 'tak' ],
                  'value'   => $this -> advert['highlight'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'highlight', 'operation' => 'update' ]
                )
            );?>
    <div class="highlight_date">
      <label class="col-lg-3 control-label">Wyróżnione do:</label>
      <div class="col-lg-9">
        <p class='form-control-static'><?= $this -> advert['highlight_to'];?></p>
      </div>
    </div>
  </div>
  <div class="col-xs-12 col-md-6">
    <?= \Html::select(
                array(
                  'label'   => 'Ogłoszenie polecane',
                  'class'   => 'form-edit',
                  'id'      => 'main_page',
                  'values'  => [ 0 => 'nie', 1 => 'tak' ],
                  'value'   => $this -> advert['main_page'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'main_page', 'operation' => 'update' ]
                )
            );?>
    <div class="main_page_date">
      <label class="col-lg-3 control-label">Na stronie głównej do:</label>
      <div class="col-lg-9">
        <p class='form-control-static'><?= $this -> advert['main_page_to'];?></p>
      </div>
    </div>
  </div>
</div>
<hr />
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::input(
                array(
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'Wynagrodzenie',
                  'id'      => 'salary',
                  'value'    => $this -> advert['salary'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'salary', 'operation' => 'update' ]
                )
            );?>
  </div>
  <div class="col-xs-12 col-md-6">
    <?= \Html::select(
                array(
                  'label'   => 'Rodzaj pracy',
                  'class'   => 'form-edit',
                  'id'      => 'work_type',
                  //'values'  => [ 1 => 'praca stała', 2 => 'praca tymczasowa', 3 => 'praca sezonowa', 4 => 'praktyka/staż', 5 => 'inna ],
                  'values'  => [ 6 => 'praca na czas nieokreślony', 7 => 'praca na czas określony/tymczasowa', 8 => 'staż/praktyka', 9 => 'B2B', 10 => 'umowa agencyjna', 11 => 'praca zdalna' ],
                  'value'   => $this -> advert['work_type'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'work_type', 'operation' => 'update' ]
                )
            );?>
  </div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::select(
                array(
                  'label'   => 'Bez znajomości języka',
                  'class'   => 'form-edit',
                  'id'      => 'without_language',
                  'values'  => [ 0 => 'nie', 1 => 'tak' ],
                  'value'   => $this -> advert['without_language'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'without_language', 'operation' => 'update' ]
                )
            );?>
  </div>
  <div class="col-xs-12 col-md-6">
    <?= \Html::select(
                array(
                  'label'   => 'Bez doświadczenia',
                  'class'   => 'form-edit',
                  'id'      => 'without_experience',
                  'values'  => [ 0 => 'nie', 1 => 'tak' ],
                  'value'   => $this -> advert['without_experience'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'without_experience', 'operation' => 'update' ]
                )
            );?>
  </div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::select(
                array(
                  'label'   => 'Dla par',
                  'class'   => 'form-edit',
                  'id'      => 'for_couples',
                  'values'  => [ 0 => 'nie', 1 => 'tak' ],
                  'value'   => $this -> advert['for_couples'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'for_couples', 'operation' => 'update' ]
                )
            );?>
  </div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::select( [
                  'label'   => 'Zapewnione zakwaterowanie',
                  'class'   => 'form-edit',
                  'id'      => 'accommodation',
                  'values'  => [ 0 => 'nie', 1 => 'tak' ],
                  'value'   => $this -> advert['accommodation'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'accommodation', 'operation' => 'update' ]
                ] );?>
  </div>
  <div class="col-xs-12 col-md-6">
    <?= \Html::input( [
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'Jaki koszt dla pracownika?',
                  'id'      => 'accommodation_cost',
                  'value'    => $this -> advert['accommodation_cost'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'accommodation_cost', 'operation' => 'update' ]
                ] );?>
  </div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::select( [
                  'label'   => 'Gwarancja godzin',
                  'class'   => 'form-edit',
                  'id'      => 'overtime',
                  'values'  => [ 0 => 'nie', 1 => 'tak' ],
                  'value'   => $this -> advert['overtime'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'overtime', 'operation' => 'update' ]
                ] );?>
  </div>
  <div class="col-xs-12 col-md-6">
    <?= \Html::input( [
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'Ile godzin gwarantuje pracodawca?',
                  'id'      => 'overtime_quantity',
                  'value'    => $this -> advert['overtime_quantity'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'overtime_quantity', 'operation' => 'update' ]
                ] );?>
  </div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::select( [
                  'label'   => 'Pracodawca refunduje przejazd/przelot',
                  'class'   => 'form-edit',
                  'id'      => 'travel_refund',
                  'values'  => [ 0 => 'nie', 1 => 'tak' ],
                  'value'   => $this -> advert['travel_refund'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'travel_refund', 'operation' => 'update' ]
                ] );?>
  </div>
  <div class="col-xs-12 col-md-6">
    <?= \Html::select( [
                  'label'   => 'Praca dla obywateli spoza UE',
                  'class'   => 'form-edit',
                  'id'      => 'outside_ue',
                  'values'  => [ 0 => 'nie', 1 => 'tak' ],
                  'value'   => $this -> advert['outside_ue'],
                  'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'outside_ue', 'operation' => 'update' ]
                ] );?>
  </div>
</div>
<hr />
<?= \Html::textarea(
            array(
              'label'   => 'Treść',
              'class'   => 'form-edit',
              'name'    => 'text',
              'id'      => 'text',
              'value'   => $this -> advert['text'],
              'inline'  => true,
              'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'text', 'operation' => 'update' ]
            )
        );?>
<hr />
<? \Html::textarea(
            array(
              'label'   => 'Klauzule',
              'class'   => 'form-edit',
              'name'    => 'clauses',
              'id'      => 'clauses',
              'value'   => $this -> advert['clauses'],
              'inline'  => true,
              'params'  => [ 'table' => 'globelus_adverts', 'column_id' => 'id', 'row_id' => $this -> advert['id'], 'column' => 'clauses', 'operation' => 'update' ]
            )
        );?>
<?
$out = ob_get_clean();

$grid                    = new \gridEdit;
$grid -> id              = 'candidate-preview';
$grid -> gdb_opt         = $gdb;
$grid -> include_plugins = true;
$grid -> title           = 'Podgląd ogłoszenia';
$grid -> actions         = [
        'cancel' => [ 'url' => '/admin/globelus_adverts/main_view/' ]
];
$grid -> external_code   = $out;
$grid -> persist_edit    = true;
$grid -> id_param        = 'id';

echo $grid -> draw();
?>
<script type="text/javascript" src="/libraries/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="/libraries/ckeditor/adapters/jquery.js"></script>
<script type="text/javascript">
  var old_data = [];
  $( function()
  {
    $( 'body' ).on( 'change', '#highlight', function()
    {
      var advert_id = $( this ).attr( 'row_id' );
      var highlight_status = $( this ).val();

      $.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/admin/globelus/highlight_save/',
        data:
        {
          advert_id: advert_id,
          highlight_status: highlight_status
        },
        beforeSend: function()
        {
          $( '#g-edit-cancel' ).attr( 'disabled', 'disabled' );
        },
        success: function( response )
        {
          $( '#g-edit-cancel' ).removeAttr( 'disabled' );
          data = jQuery.parseJSON( response );

          if ( data.status == 'ok' )
            $( '.highlight_date p' ).html( data.highlight_to );
        }
      });
    });

    $( 'body' ).on( 'change', '#main_page', function()
    {
      var advert_id = $( this ).attr( 'row_id' );
      var main_page_status = $( this ).val();

      $.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/admin/globelus/main_page_save/',
        data:
        {
          advert_id: advert_id,
          main_page_status: main_page_status
        },
        beforeSend: function()
        {
          $( '#g-edit-cancel' ).attr( 'disabled', 'disabled' );
        },
        success: function( response )
        {
          $( '#g-edit-cancel' ).removeAttr( 'disabled' );
          data = jQuery.parseJSON( response );

          if ( data.status == 'ok' )
            $( '.main_page_date p' ).html( data.main_page_to );
        }
      });
    });

    $( '#text' ).ckeditor( {
      toolbar : 'MyTool',
      height:'350'
    });

    var editor = $('#text').ckeditorGet();
    editor.on( 'blur', function( evt ) {
      var text = evt.editor.getData();
      $.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/admin/globelus/save_to_db/',
        data:
        {
          table: 'globelus_adverts',
          column_id: 'id',
          row_id: '<?= $this -> advert['id'];?>',
          column: 'text',
          operation: 'update',
          value: text
        },
        beforeSend: function()
        {
          $( '#g-edit-cancel' ).attr( 'disabled', 'disabled' );
        },
        success: function( data )
        {
          $( '#g-edit-cancel' ).removeAttr( 'disabled' );
        }
      });
    });

    $( '#clauses' ).ckeditor( {
      toolbar : 'MyTool',
      height:'350'
    });

    var editor1 = $('#clauses').ckeditorGet();
    editor1.on( 'blur', function( evt ) {
      var text = evt.editor.getData();
      $.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/admin/globelus/save_to_db/',
        data:
        {
          table: 'globelus_adverts',
          column_id: 'id',
          row_id: '<?= $this -> advert['id'];?>',
          column: 'clauses',
          operation: 'update',
          value: text
        },
        beforeSend: function()
        {
          $( '#g-edit-cancel' ).attr( 'disabled', 'disabled' );
        },
        success: function( data )
        {
          $( '#g-edit-cancel' ).removeAttr( 'disabled' );
        }
      });
    });

    $( 'body' ).on( 'focus', '.form-edit', function()
    {
      old_data[ $( this ).attr( 'name' ) ] = $( this ).val();
    });

    $( 'body' ).on( 'change', '.form-edit', function()
    {
      var input     = $( this );
      var table     = $( this ).attr( 'table' );
      var column_id = $( this ).attr( 'column_id' );
      var row_id    = $( this ).attr( 'row_id' );
      var column    = $( this ).attr( 'column' );
      var operation = $( this ).attr( 'operation' );
      var value     = $( this ).val();
      var values    = $( this ).attr( 'values' );

      if ( input.attr( 'type' ) === 'checkbox' )
      {
        if ( !input.is( ':checked' ) )
          value = null;
      }

      $.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/admin/globelus/save_to_db/',
        data:
        {
          table: table,
          column_id: column_id,
          row_id: row_id,
          column: column,
          operation: operation,
          value: value,
          values: values
        },
        beforeSend: function()
        {
          input.attr( 'readonly', 'readonly' );
          $( '#g-edit-cancel' ).attr( 'disabled', 'disabled' );
        },
        success: function( data )
        {
          input.removeAttr( 'readonly' );
          $( '#g-edit-cancel' ).removeAttr( 'disabled' );

          response = jQuery.parseJSON( data );

          if ( response.status !== 'ok' )
          {
            if ( input.attr( 'type' ) === 'checkbox' )
            {
              if ( input.is( ':checked' ) === false )
                input.prop( 'checked', true );
              else
                input.prop( 'checked', false );
            }
            else
              input.val( old_data[ input.attr( 'name' ) ] );
          }
        }
      });
    });
  });
</script>
<style type="text/css">
  hr {
    margin: 10px 0;
  }
  #g-edit-save {
    display: none;
  }
  .g-container .panel-body {
    position: relative;
  }
  .logo {
    position: absolute;
    top: 10px;
    right: 10px;
    height: 150px;
    width: 150px;
    overflow: hidden;
    float: right;
    padding: 10px;
    background: #FFF;
    box-shadow: 0px 10px 25px 0px rgba(0, 0, 0, 0.2 );
  }
  .logo img {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
</style>