<?
global $db;

ob_start();
?>
  <?= \Html::form_text(
              array(
                'label' => 'Twoja wersja systemu',
                'id'    => 'ver',
                'text'  => $this -> ver
              )
        );?>
  <?= \Html::form_text(
              array(
                'label' => 'Aktualna wersja systemu',
                'text'  => $this -> new_ver,
                'id'    => 'new_ver'
              )
        );?>
<?
$ver_new = $this->new_ver;
$ver= $this->ver;
$valuemax = ($ver_new - $ver)*1000;
?>
<div class="progress-box hidden">
    <div class="version">
        <h3> Aktualizacja <p class="version_curent">0</p> / <p class="version_diff">0</p></h3>
    </div>
    <div class="progress ">
      <div class="progress-bar progress-bar-info progress-bar-striped active" role="progressbar"
           accesskey=""aria-valuenow="" aria-valuemin="0" aria-valuemax="<?=$valuemax?>" style="width:">
      </div>
    </div>
</div>
<div class="row">
  <? if ( $this -> ver < $this -> new_ver ):?>
  <div class="form-group col-lg-6 text-right">
    <div class="">
      <a href="#" class="btn btn-system btn-sm mb5" id="confirm">Aktualizuj do wyższej wersji</a>
    </div>
  </div>
  <? endif;?>
<? if ( $this -> ver < $this -> new_ver ):?>
<div class="form-group col-lg-6">
    <div class="">
      <a href="#" class="btn btn-system btn-sm mb5" id="confirmUpdateAll">Aktualizuj do najwyższej wersji</a>
    </div>
  </div>
<? endif;?>
</div>
  <? if ( $this -> ver < $this -> new_ver ):?>
 <div class="text-danger text-center">* Przed aktualizacją systemu zalecane jest wykonanie pełnej kopii zapasowej.</div>
  <div class="clear"></div>
  <?endif;?>
<?
$out = ob_get_clean();

$grid = new \gridEdit;
$grid -> id               = 'update-view';
$grid -> gdb_opt          = $gdb;
$grid -> include_plugins  = true;
$grid -> title            = 'Aktualizacja systemu';
$grid -> default_buttons  = false;
$grid -> external_code    = $out;
echo $grid -> draw();
?>
<?
ob_start();
echo $versions = file_get_contents( 'http://www.shoppro.project-dc.pl/updates/changelog.php' );
$out = ob_get_clean();

$grid                    = new \gridEdit;
$grid -> id              = 'changelog';
$grid -> gdb_opt         = $gdb;
$grid -> include_plugins = true;
$grid -> title           = 'Changelog';
$grid -> default_buttons = false;
$grid -> external_code   = $out;
echo $grid -> draw();
?>
<script type="text/javascript">
  var version_current = <?= $this -> ver;?>;
  var version_new     = <?= $this -> new_ver;?>;
  var version_diff    = Math.round((version_new  - version_current )* 1000);
  var width           = 0;
  var ac_lp           = 0;
  $( document ).ready( function()
  {
    $( 'body' ).on( 'click', '#confirm', function()
    {
      $.prompt( 'Na pewno chcesz dokonać aktualizacji systemu?',
      {
        title: 'Potwierdź?',
        submit: function(e,v,m,f)
        {
          if ( v == true )
            document.location.href = '/admin/update/update/';
        },
        buttons: {
          'tak': true, 'nie': false
        },
        focus: 1
      });
    });

    $( 'body' ).on( 'click', '#confirmUpdateAll', function()
    {
      $.prompt( 'Na pewno chcesz dokonać aktualizacji systemu?',
      {
        title: 'Potwierdź?',
        submit: function(e,v,m,f)
        {
          if ( v == true )
          {
            $('.progress-box').removeClass('hidden');
            $('#confirm').css('pointer-events','none');
            $('#confirmUpdateAll').css('pointer-events','none');
            $('.version_diff').html(version_diff);
            updateAll( version_current, version_new, version_diff, width, ac_lp);
          }
        },
        buttons: {
          'tak': true, 'nie': false
        }
      });
    });
  });

 function updateAll( version_current, version_new, version_diff, width, ac_lp)
  {
    $.ajax(
    {
      url: '/admin/update/updateAll/',
      type: 'POST',
      data:
      {
        version_current: version_current
      },
      success: function( data )
      {
          response = jQuery.parseJSON( data );
          if ( response.status == true )
          {
            ac_lp = ac_lp + 1;
            $('.version_curent').html(ac_lp);
            width = width + ((1/version_diff)*100);
            $('.progress-bar').attr("style", "width:"+ width +"%");
            if( response.version < version_new )
            {
              updateAll( response.version, version_new, version_diff, width, ac_lp );
            }
            else
            {
              $.prompt( "Aktualizacja przebiegła pomyślnie.",
              {
                title: 'Informacja',
                close: function(e,v,m,f){
                   window.location.href ="/admin/update/main_view/";
                }
              });
            }
          }
          else
          {
            $.prompt( "W trakcie aktualizacji systemu wystąpił błąd. Proszę spróbować ponownie." ,
            {
              title: 'Błąd',
              close: function(e,v,m,f){
                   window.location.href ="/admin/update/main_view/";
                }
            });
          }
      }
    }
  );
  };

</script>