<div class="g-container" data="table:<?= $this -> values['id'];?>">
  <?
    if ( is_array( $this -> values['buttons'] ) )
    {
      echo '<div id="g-menu">';
      foreach ( $this -> values['buttons'] as $button )
      {
        $button['url'] ? $url = $button['url'] : $url = '#';
        $button['toggle'] ? $toggle = 'g-tooggle' : $toggle = '';
        $button['toggle'] ? $display = 'display: none; ' : $toggle = '';
        $button['bgcolor'] ? $background = 'background: ' . $button['bgcolor'] : $background = '';
        
        echo '<a href="' . $url . '" ' .
                'class="' . $toggle . '" ';
                if ( $button['js'] )
                  echo 'onclick="' . htmlspecialchars( $button['js'] ) . ' return false;" ';
          echo 'style="' . $display . $background . '" ' .
              '> ' .
                '<i class="fa ' . $button['icon'] . '"></i><p>' . $button['label'] . '</p>' .
        '</a>';
      }
      echo '<div class="g-clear"></div>';
      echo '</div>';
    }
  ?>
  <div class="g-form-edit">
    <div class="g-title">
      <i class="fa fa-bars fa-lg"></i>
      <p><?= $this -> values['title'];?></p>
      <a href="#" class="g-collapse">
        <i class="fa fa-chevron-up"></i>
      </a>
    </div>
    <div class="g-content">
      <form id="<?= $this -> values['id'];?>" action="<?= gdir;?>/upload.php" method="post" enctype="multipart/form-data">
        <input type="hidden" name="gtable" value="<?= $this -> values['id'];?>">
        <? 
          if ( is_array( $this -> values['fields'] ) ) foreach ( $this -> values['fields'] as $field )
          {
            if ( $field['type'] == 'hidden' )
            {
              ?>
                <input type="hidden" name="<?= $field['db'];?>" id="<?= $field['db'];?>" <? if ( is_array( $field['params'] ) ): foreach( $field['params'] as $key => $val ): echo $key . '="' . $val . '"'; endforeach; endif;?> value="<?= htmlspecialchars( $field['value'] );?>" />
              <?              
            }
            else
            {
              echo '<div class="g-form-data">';
                echo '<label for="' . $field['db'] . '">' . $field['name']; if ( $field['name'] ) echo ':'; echo '</label>';
                if ( $field['type'] == 'text' )
                {
                  ?>
                    <input type="text" name="<?= $field['db'];?>" id="<?= $field['db'];?>" <? if ( is_array( $field['params'] ) ): foreach( $field['params'] as $key => $val ): echo $key . '="' . $val . '"'; endforeach; endif;?> value="<?= htmlspecialchars( $field['value'] );?>" />
                  <?
                }
                else if ( $field['type'] == 'textarea' )
                {
                  ?>
                    <textarea name="<?= $field['db'];?>" id="<?= $field['db'];?>" <? if ( is_array( $field['params'] ) ): foreach( $field['params'] as $key => $val ): echo $key . '="' . $val . '"'; endforeach; endif;?>><?= $field['value'];?></textarea>
                  <?
                }
              echo '</div>';
            }
          }            
        ?>
        <div class="g-form-data">
          <label>Plik:</label>
          <input type="file" name="file">
        </div>
        <div class="g-form-data">
          <a href="#" class="g-button-form" onclick="$( '#<?= $this -> values['id'];?>' ).submit();">wyślij</a>
        </div>
        <div class="g-form-data">
          <label></label>
          <div class="g-progress" style="margin-left: 150px; display: none;">
            <div class="g-progress-bar">
              <span class="g-progress-percent">0%</span>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>
<script type="text/javascript">
  var dir = '<?= gdir;?>';
</script>
<? if ( $this -> values['include_plugins'] ):?>
  <!-- fontawsome -->
  <link rel="stylesheet" type="text/css" href="<?= gdir;?>/plugins/font-awesome-4.2.0/css/font-awesome.min.css" />
  <!-- iradio -->
  <link rel='stylesheet' type='text/css' href='<?= gdir;?>/plugins/icheck/skins/minimal/minimal.css' />
  <link rel='stylesheet' type='text/css' href='<?= gdir;?>/plugins/icheck/skins/minimal/blue.css' />
  <script type="text/javascript" src="<?= gdir;?>/plugins/icheck/icheck.min.js"></script>
  <!-- ipromptu -->
  <link rel='stylesheet' type='text/css' href='<?= gdir;?>/plugins/impromptu/jquery-impromptu.min.css' />
  <script type='text/javascript' src='<?= gdir;?>/plugins/impromptu/jquery-impromptu.js'></script>
  <!-- tooltip -->
  <link rel='stylesheet' type='text/css' href='<?= gdir;?>/plugins/tooltipster-master/css/tooltipster.css' />
  <link rel='stylesheet' type='text/css' href='<?= gdir;?>/plugins/tooltipster-master/css/themes/tooltipster-punk.css' />
  <script type='text/javascript' src='<?= gdir;?>/plugins/tooltipster-master/js/jquery.tooltipster.js'></script>
  <!-- grid -->
  <link rel="stylesheet" type="text/css" href="<?= gdir;?>/css/grid.css" />
  <script type="text/javascript" src="<?= gdir;?>/js/grid.js"></script>
<? endif;?>
<script type="text/javascript" src="<?= gdir;?>/plugins/jquery-form/jquery.form.js"></script>
<script type="text/javascript">  
    
  jQuery(function() {      

    var options = { 
      beforeSend: function() 
      {
        jQuery( ".g-progress" ).show();
        jQuery( ".g-progress .g-progress-bar" ).width( '0%' );
        jQuery( ".g-progress .g-progress-bar .g-progress-percent" ).html( "0%" );
      },
      uploadProgress: function( event, position, total, percentComplete ) 
      {
        jQuery( ".g-progress .g-progress-bar" ).width( percentComplete + '%' );
        jQuery( ".g-progress .g-progress-bar .g-progress-percent" ).html( percentComplete + '%' );
      },
      success: function() 
      {
        jQuery( ".g-progress .g-progress-bar" ).width( '100%' );
        jQuery( ".g-progress .g-progress-bar .g-progress-percent" ).html( '100%' );
      },
      complete: function( data ) 
      {
        response = jQuery.parseJSON( data.responseText );
        if ( response.status === 'ok' )
        {
          if ( response.msg ) 
            var msg = response.msg; 
          else 
            var msg = 'Plik został wysłany na serwer.';
          create_message( msg );
          <? 
            if ( $this -> values['after_send_js'] )
              echo $this -> values['after_send_js'] . '( response.file );';
          ?>
        } 
        else 
        {
          if ( response.msg ) 
            var msg = response.msg; 
          else 
            var msg = "Przepraszamy. Podczas wysyłania pliku wystąpił błąd. Prosimy spróbować ponownie.";
          create_error( msg );
        }
      }
    }; 
    jQuery( "#<?= $this -> values['id'];?>" ).ajaxForm( options );
  });
</script>
