<div class="row">
  <div class="col col-xs-12">
    <div class="g-container" data="table:<?= $this -> values['id'];?>">
      <div class="panel panel-info panel-border top">
        <div class="panel-heading">
          <span class="panel-title"><?= $this -> values['title'];?></span>
        </div>
        <div class="panel-heading p10 pl15" id="g-menu" style="height: auto;">
          <? if ( $this -> values['default_buttons'] === true ):?>
            <? if ( $this -> values['persist_edit'] ):?>
              <a href="#" id="g-edit-save" class="btn btn-system btn-sm" persist_edit="0" back_url="<?= $this -> values['actions']['save']['back_url'];?>" url="<?= $this -> values['actions']['save']['url'];?>" <? if ( $this -> values['actions']['save']['js'] ):?>onclick="<?= htmlspecialchars( $this -> values['actions']['save']['js'] );?> return false;"<? endif;?>>
                <i class="fa fa-check-circle mr5"></i>Zatwierdź i zamknij
              </a>
            <? endif;?>
            <a href="#" id="g-edit-save" class="btn btn-success btn-sm" persist_edit="<?= $this -> values['persist_edit'];?>" id_param="<?= $this -> values['id_param'];?>"  back_url="<?= $this -> values['actions']['save']['back_url'];?>" url="<?= $this -> values['actions']['save']['url'];?>" <? if ( $this -> values['actions']['save']['js'] ):?>onclick="<?= htmlspecialchars( $this -> values['actions']['save']['js'] );?> return false;"<? endif;?>>
              <i class="fa fa-check-circle mr5"></i>Zatwierdź
            </a>
            <a href="<?= $this -> values['actions']['cancel']['url'] ? $this -> values['actions']['cancel']['url'] : $tmp = '#';?>" class="btn btn-dark btn-sm" id="g-edit-cancel">
              <i class="fa fa-reply mr5"></i>Wstecz
            </a>
          <? endif;?>
          <? 
            if ( is_array( $this -> values['buttons'] ) )
            {
              foreach ( $this -> values['buttons'] as $button )
              {
                echo \Html::button(
                              array(
                                'class' => $button['toggle'] === true ? $button['class'] . ' g-toggle btn-sm mr5' : $button['class'] . ' btn-sm mr5',
                                'url' => $button['url'] != '' ? $button['url'] : null,
                                'text' => $button['label'],
                                'icon' => $button['icon'],
                                'js' => $button['js'] != '' ? $button['js'] : null,
                                'style' => $button['toggle'] === true ? 'display: none; ' . $button['style'] : $button['style']
                              )
                        );
              }
            }
          ?>
        </div>
        <div class="panel-body">
          <? 
            if ( $this -> values['form'] === true )
            {
              echo '<form method="POST" id="fg-' . $this -> values['id'] . '" class="g-form form-horizontal" ';
                if ( $this -> values['post_action'] !== null )
                  echo 'action="' . $this -> values['post_action'] . '" ';
              echo 'enctype="multipart/form-data">';
            }
          ?>
          <? 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:?>
              <div class="form-group">
                <label for="<?= $field['db'];?>" class="col col-lg-3 control-label"><?= $field['name'];?><? if ( $field['name'] ) echo ':';?></label>
                <?
                if ( $field['type'] == 'text' )
                {
                  ?>
                    <div class="col col-lg-9">
                      <input type="text" class="form-control" 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'] );?>" />
                    </div>
                  <?
                }
                elseif ( $field['type'] == 'password' )
                {
                  ?>
                    <div class="col col-lg-9">
                      <input type="password" class="form-control" 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'] );?>" />
                    </div>
                  <?
                }
                elseif ( $field['type'] == 'select' )
                {
                  ?>
                    <div class="col col-lg-9">
                      <select name="<?= $field['db'];?>" id="<?= $field['db'];?>" class="form-control" <? if ( is_array( $field['params'] ) ): foreach( $field['params'] as $key => $val ): echo $key . '="' . $val . '"'; endforeach; endif;?>>
                        <option value="null">---- <?= mb_strtolower( $field['name'], 'UTF-8' );?> ----</option>
                        <? 
                          if ( $field['replace']['sql'] )
                          {
                            $results = $_SESSION[ 'g-edit-' . $this -> values['id'] ] -> connectToDb() -> query( $field['replace']['sql'] ) -> fetchAll();
                            if ( is_array( $results ) ) foreach ( $results as $row )
                            {
                              echo '<option value="' . $row[0] . '"';
                                if ( $row[0] == $field['value'] )
                                  echo ' selected="selected"';
                              echo '>' . $row[1] . '</option>';
                            }
                          }
                          else if ( is_array( $field['replace']['array'] ) )
                          {
                            foreach ( $field['replace']['array'] as $key => $val )
                            {
                              echo '<option value="' . $key . '"'; if ( $key == $field['value'] ) echo 'selected="selected"'; echo '>' . $val . '</option>';
                            }
                          }
                        ?>
                      </select>
                    </div>
                    <?
                  }
                  elseif ( $field['type'] == 'radio' )
                  {
                    if ( is_array( $field['replace']['array'] ) ) foreach ( $field['replace']['array'] as $key => $val )
                    {
                      ?>
                        <input type="radio" class="g-radio" name="<?= $field['db'];?>" id="<?= $field['db'] . '_' . $key;?>" value="<?= $key;?>" />
                        <label for="<?= $field['db'];?>" id="<?= $field['db'] . '_' . $key;?>"><?= $val;?></label>
                      <?
                    }
                  }
                  elseif ( $field['type'] == 'input_switch' )
                  {
                    ?>
                      <div class="col-lg-9 pt5">
                        <div class="switch switch-primary round switch-inline">
                          <input id="<?= $field['db'];?>" name="<?= $field['db'];?>" type="checkbox" <? if ( $field['checked'] ) echo 'checked="checked" ';?>>
                          <label for="<?= $field['db'];?>"></label>
                        </div>
                      </div>
                    <?
                  }
                  elseif ( $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>
                    <?
                  }
                  elseif ( $field['type'] == 'checkbox' )
                  {
                    if ( $field['replace']['sql'] )
                    {
                      $results = $_SESSION[ 'g-edit-' . $this -> values['id'] ] -> connectToDb() -> query( $field['replace']['sql'] ) -> fetchAll();
                      if ( is_array( $results ) ) foreach ( $results as $row )
                      {
                        ?>
                          <input type="checkbox" class="g-checkbox" name="<?= $field['db'];?>" value="<?= $row[0];?>" <? if ( is_array( $field['value'] ) and in_array( $row[0], $field['value'] ) ):?>checked="checked"<? endif;?> /><?= $row[1];?>
                        <?
                      }
                    }
                  }
                  elseif ( $field['type'] == 'button' )
                  {
                    ?>
                      <a href="<?= $field['button']['url'] ? $field['button']['url'] : $tmp = '#';?>" class="g-button-form"
                           <? if ( $field['button']['js'] ):?>onclick="<?= htmlspecialchars( $field['button']['js'] );?> return false;"<? endif;?>
                           style="<? if ( $field['button']['bgcolor'] ):?>background: <?= $field['button']['bgcolor'];?>;<? endif;?>"
                       >
                         <i class="fa <?= $field['button']['icon'];?>"></i><?= $field['button']['label'];?>
                       </a>
                    <?
                  }
                  elseif ( $field['type'] == 'label' )
                  {
                    if ( $field['replace']['sql'] )
                    {
                      $sql      = $_SESSION[ 'g-edit-' . $this -> values['id'] ] -> convertString( $field['replace']['sql'], $row );                   
                      $results  = $_SESSION[ 'g-edit-' . $this -> values['id'] ] -> connectToDb() -> query( $sql ) -> fetchAll();

                      if ( $results[0][0] )
                        echo $results[0][0];
                      else
                        echo '-';
                    }
                    else
                      echo '<div class="g-edit-text">' . $field['value'] . '</div>';
                  }
                ?>
              </div>
            <? endif;?>
          <? endforeach; endif;?>
          <?
            if ( $this -> values['form_html_src'] !== null )
              include_once $this -> values['form_html_src'];          

            if ( $this -> values['external_code'] !== null )
              echo $this -> values['external_code'];
          ?>
        <?
          if ( $this -> values['form'] === true )
            echo '</form>';
        ?>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
  jQuery( function() {
    jQuery( '.g-checkbox, .g-radio' ).iCheck({
      checkboxClass: 'icheckbox_minimal-blue',
      radioClass: 'iradio_minimal-blue'
    });
  });  
</script>
<? if ( $this -> values['include_plugins'] ):?>
  <script type="text/javascript">
    var dir = '<?= gdir;?>';
  </script>
  <!-- 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.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>
  <!-- context menu -->
  <link rel="stylesheet" type="text/css" href="<?= gdir;?>/plugins/jq-context-menu/context.standalone.css" />
  <script type="text/javascript" src="<?= gdir;?>/plugins/jq-context-menu/context.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;?>