<?
global $db, $settings;

ob_start();
?>
<div class="logo">
  <? if ( file_exists( '../' . \front\factory\GlobelusFirms::logo_url( $this -> firm['id'] ) ) ):?>
    <img src="/logo/<?= $this -> firm['id'];?>/<?= \S::get_token();?>">
  <? else:?>
    <img src="/images/no-logo.jpg">
  <? endif;?>
</div>
<div class="row mb15">
</div>
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::select(
                array(
                  'label'   => 'Profil aktywny',
                  'class'   => 'form-edit',
                  'id'      => 'status',
                  'values'  => [ 0 => 'nie', 1 => 'tak' ],
                  'value'   => $this -> firm['status'],
                  'params'  => [ 'table' => 'globelus_users', 'column_id' => 'id', 'row_id' => $this -> firm['id'], 'column' => 'status', 'operation' => 'update' ]
                )
            );?>
  </div>
  <div class="col-xs-12 col-md-6">
    <?= \Html::select(
                array(
                  'label'   => 'Widoczny na liście',
                  'class'   => 'form-edit',
                  'id'      => 'firm_list',
                  'values'  => [ 0 => 'nie', 1 => 'tak' ],
                  'value'   => $this -> firm['firm_list'],
                  'params'  => [ 'table' => 'globelus_firms_data', 'column_id' => 'user_id', 'row_id' => $this -> firm['id'], 'column' => 'firm_list', 'operation' => 'update' ]
                )
            );?>
  </div>
</div>
<hr />
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::input( [
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'Liczba punktów',
                  'id'      => 'points',
                  'value'   => $this -> firm['points'],
                  'params'  => [ 'table' => 'globelus_users', 'column_id' => 'id', 'row_id' => $this -> firm['id'], 'column' => 'points', 'operation' => 'update' ]
                ]
            );?>
  </div>
</div>
<hr />
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::input( [
                  'type'    => 'text',
                  'class'   => 'form-edit date',
                  'label'   => 'Dostęp do bazy CV do',
                  'id'      => 'cv_access_date',
                  'value'   => $this -> firm['cv_access_date'],
                  'params'  => [ 'table' => 'globelus_users', 'column_id' => 'id', 'row_id' => $this -> firm['id'], 'column' => 'cv_access_date', 'operation' => 'update' ]
                ]
            );?>
  </div>
</div>
<hr />
<div class="row">
  <div class="col-xs-12">
    <div class="form-group">
      <label class="col-lg-2 control-label">Przypisane branże:</label>
      <div class="col-lg-8">
        <div class="row">
          <?
            $categories[0] = '--- brak przypisanej kategorii ---';
            if ( is_array( $this -> categories ) and count( $this -> categories ) ): foreach ( $this -> categories as $category ):
              echo '<div class="col-xs-4">';
                echo '<input type="checkbox" class="form-edit" table="globelus_firms_categories" operation="insert_delete" column_id="id" row_id="null" values="user_id;' . $this -> firm['id'] . '|category_id;' . $category['id'] . '" value="' . $category['id'] . '"'; if ( in_array( $category['id'], $this -> firm['categories'] ) ) echo ' checked="checked"'; echo '> ';
                echo $category['name'];
              echo '</div>';
            endforeach; endif;
          ?>
        </div>
      </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'   => 'Nazwa firmy',
                  'id'      => 'firm_name',
                  'value'   => $this -> firm['firm_name'],
                  'params'  => [ 'table' => 'globelus_firms_data', 'column_id' => 'user_id', 'row_id' => $this -> firm['id'], 'column' => 'firm_name', 'operation' => 'update' ]
                )
            );?>
  </div>
  <div class="col-xs-12 col-md-6">
    <?= \Html::input(
                array(
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'Nazwa firmy (wyświetlana)',
                  'id'      => 'firm_name_profile',
                  'value'   => $this -> firm['firm_name_profile'],
                  'params'  => [ 'table' => 'globelus_firms_data', 'column_id' => 'user_id', 'row_id' => $this -> firm['id'], 'column' => 'firm_name_profile', 'operation' => 'update' ]
                )
            );?>
  </div>
</div>
<div class='row'>
  <div class="col-xs-12 col-md-6">
    <?= \Html::input(
                array(
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'NIP',
                  'id'      => 'nip_vat',
                  'value'   => $this -> firm['nip_vat'],
                  'params'  => [ 'table' => 'globelus_firms_data', 'column_id' => 'user_id', 'row_id' => $this -> firm['id'], 'column' => 'nip_vat', 'operation' => 'update' ]
                )
            );?>
  </div>
</div>
<hr />
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::input(
                array(
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'Telefon',
                  'id'      => 'phone',
                  'value'    => $this -> firm['phone'],
                  'params'  => [ 'table' => 'globelus_firms_data', 'column_id' => 'user_id', 'row_id' => $this -> firm['id'], 'column' => 'phone', 'operation' => 'update' ]
                )
            );?>
  </div>
  <div class="col-xs-12 col-md-6">
    <?= \Html::form_text(
                array(
                  'label'   => 'Adres email',
                  'id'      => 'email',
                  'text'    => $this -> firm['email']
                )
            );?>
  </div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::input(
                array(
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'Strona www',
                  'id'      => 'www',
                  'value'    => $this -> firm['www'],
                  'params'  => [ 'table' => 'globelus_firms_data', 'column_id' => 'user_id', 'row_id' => $this -> firm['id'], 'column' => 'www', 'operation' => 'update' ]
                )
            );?>
  </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(
                  'label'   => 'Kraj',
                  'class'   => 'form-edit',
                  'id'      => 'country_id',
                  'values'  => $countries,
                  'value'   => $this -> firm['country_id'],
                  'params'  => [ 'table' => 'globelus_firms_data', 'column_id' => 'user_id', 'row_id' => $this -> firm['id'], 'column' => 'country_id', 'operation' => 'update' ]
                )
            );?>
  </div>
  <div class="col-xs-12 col-md-6">
    <?= \Html::input(
                array(
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'Region',
                  'id'      => 'region',
                  'value'   => $this -> firm['region'],
                  'params'  => [ 'table' => 'globelus_firms_data', 'column_id' => 'user_id', 'row_id' => $this -> firm['id'], 'column' => 'region', 'operation' => 'update' ]
                )
            );?>
  </div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::input(
                array(
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'Miasto',
                  'id'      => 'city',
                  'value'   => $this -> firm['city'],
                  'params'  => [ 'table' => 'globelus_firms_data', 'column_id' => 'user_id', 'row_id' => $this -> firm['id'], 'column' => 'city', 'operation' => 'update' ]
                )
            );?>
  </div>
  <div class="col-xs-12 col-md-6">
    <?= \Html::input(
                array(
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'Kod pocztowy',
                  'id'      => 'postal_code',
                  'value'   => $this -> firm['postal_code'],
                  'params'  => [ 'table' => 'globelus_firms_data', 'column_id' => 'user_id', 'row_id' => $this -> firm['id'], 'column' => 'postal_code', 'operation' => 'update' ]
                )
            );?>
  </div>
</div>
<div class="row">
  <div class="col-xs-12 col-md-6">
    <?= \Html::input(
                array(
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'Ulica',
                  'id'      => 'street',
                  'value'   => $this -> firm['street'],
                  'params'  => [ 'table' => 'globelus_firms_data', 'column_id' => 'user_id', 'row_id' => $this -> firm['id'], 'column' => 'street', 'operation' => 'update' ]
                )
            );?>
  </div>
  <div class="col-xs-12 col-md-6">
    <?= \Html::input(
                array(
                  'type'    => 'text',
                  'class'   => 'form-edit',
                  'label'   => 'Nr budynku/lokalu',
                  'id'      => 'apartment_number',
                  'value'   => $this -> firm['apartment_number'],
                  'params'  => [ 'table' => 'globelus_firms_data', 'column_id' => 'user_id', 'row_id' => $this -> firm['id'], 'column' => 'apartment_number', 'operation' => 'update' ]
                )
            );?>
  </div>
</div>
<hr />
<?= \Html::textarea(
            array(
              'label'   => 'Krótki opis firmy',
              'class'   => 'form-edit',
              'name'    => 'other_information',
              'id'      => 'other_information',
              'value'   => $this -> firm['other_information'],
              'inline'  => true,
              'params'  => [ 'table' => 'globelus_firms_data', 'column_id' => 'user_id', 'row_id' => $this -> firm['id'], 'column' => 'other_information', 'operation' => 'update' ]
            )
        );?>
<hr />
<? \Html::textarea(
            array(
              'label'   => 'Klauzula',
              'class'   => 'form-edit',
              'name'    => 'clauses',
              'id'      => 'clauses',
              'value'   => $this -> firm['clauses'],
              'inline'  => true,
              'params'  => [ 'table' => 'globelus_firms_data', 'column_id' => 'user_id', 'row_id' => $this -> firm['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 pracodawcy';
$grid -> actions         = [
                              'cancel' => [ 'url' => '/admin/globelus_firms/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()
  {
    $( '#other_information, #clauses' ).ckeditor( {
      toolbar : 'MyTool',
      height:'350'
    });

    var editor = $('#other_information').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_firms_data',
          column_id: 'user_id',
          row_id: '<?= $this -> firm['id'];?>',
          column: 'other_information',
          operation: 'update',
          value: text
        },
        beforeSend: function()
        {

        },
        success: function( data )
        {

        }
      });
    });

    $( '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' );
        },
        success: function( data )
        {
          input.removeAttr( 'readonly' );

          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' ) ] );
          }
        }
      });
    });

    $( 'body' ).on( 'click', '.status', function()
    {
      $.prompt( 'Na pewno chcesz aktywować wybrany profil?',
      {
        title: 'Potwierdź?',
        submit: function(e,v,m,f)
        {
          if ( v === true )
          {
            document.location.href="/admin/globelus_firms/firm_enabled/id=<?= $this -> firm['id'];?>";
          }
        },
        buttons: {
          'tak': true,
          'nie': false
        },
        focus: 0
      });
    });
  });
</script>
<style type="text/css">
  hr {
    margin: 10px 0;
  }
  #g-edit-save {
    display: none;
  }
  .g-container .panel-body {
    position: relative;
  }
  .logo {
    height: 150px;
    width: 150px;
    float: right;
    padding: 3px;
    background: #FFF;
    box-shadow: 0px 10px 25px 0px rgba(0, 0, 0, 0.2 );
  }
  .logo img {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
</style>