<?
global $db;

ob_start();
?>
<?=
\Html::input(
  [
      'label'  => 'Nazwa',
      'name'   => 'name',
      'id'     => 'name',
      'value'  => $this -> task[ 'name' ],
      'class'  => 'require',
      'inline' => true
  ]
);
?>
<?=
\Html::textarea(
  [
      'label' => 'Treść',
      'name'  => 'text',
      'id'    => 'text',
      'value' => $this -> task[ 'text' ],
      'rows'  => 10,
      'skip_encoding' => true
  ]
);
?>
<?=
\Html::input(
  [
      'label' => 'Data rozpoczęcia',
      'name' => 'date_start',
      'id' => 'date_start',
      'value' => $this -> task[ 'date_start' ],
      'class' => 'datetime',
      'inline'       => true,
      'autocomplete' => 'off'
  ]
);
?>
<?=
\Html::input(
  [
      'label'        => 'Data zakończenia',
      'name'         => 'date_end',
      'id'           => 'date_end',
      'value'        => $this -> task[ 'date_end' ],
      'class'        => 'datetime',
      'inline'       => true,
      'autocomplete' => 'off'
  ]
);
?>
<?
$projects[ 0 ] = '--- wybierz projekt ---';
if ( is_array( $this -> projects ) )
  foreach ( $this -> projects as $project )
    $projects[ $project[ 'id' ] ] = $project[ 'name' ];
?>
<?=
\Html::select( [
    'label'  => 'Projekt',
    'name'   => 'project_id',
    'id'     => 'project_id',
    'value'  => $this -> task[ 'id' ] ? $this -> task[ 'project_id' ] : $this -> project_id,
    'values' => $projects
] );
?>
<?
$clients[ 0 ] = '--- wybierz klienta ---';
if ( is_array( $this -> clients ) )
  foreach ( $this -> clients as $client )
    $clients[ $client[ 'id' ] ] = $client[ 'firm' ];
?>
<?=
\Html::select( [
    'label'  => 'Klient',
    'name'   => 'client_id',
    'id'     => 'client_id',
    'value'  => $this -> task[ 'id' ] ? $this -> task[ 'client_id' ] : 0,
    'values' => $clients
] );
?>
<? if ( $this -> user['id'] == 1 ):?>
  <?= \Html::input([
    'label' => 'Stawka',
    'name' => 'pay_rate',
    'id' => 'pay_rate',
    'value' => $this -> task[ 'pay_rate' ],
    'class' => 'price',
    'inline' => true,
    'autocomplete' => 'off'
  ]);?>
<? endif;?>
<?=
\Html::input_switch( [
    'label' => 'Powiadom o zmianie statusu',
    'name' => 'status_change_mail',
    'id' => 'status_change_mail',
    'checked' => $this -> task[ 'status_change_mail' ] ? true : false,
    'type' => 'checkbox'
] );
?>
<div class="form-group row">
  <label class="col-md-3 col-form-label">Uczestnicy:</label>
  <div class="col-md-9">
    <div class="row">
      <? if ( is_array( $this -> users ) ): foreach ( $this -> users as $user ):?>
                <div class="group col-lg-4">
                  <div class="name" id="user-<?= $user[ 'id' ];?>">
                    <label for="user-<?= $user[ 'id' ];?>">
                      <input type="checkbox" class="g-checkbox" name="users" value="<?= $user[ 'id' ];?>" <? if ( in_array( $user[ 'id' ], $this -> task[ 'users' ] ) ):?>checked="checked"<? endif;?>>
          <?= $user[ 'name' ] . ' ' . $user[ 'surname' ];?>
                    </label>
                  </div>
                </div>
        <? endforeach;
      endif;?>
    </div>
  </div>
</div>
<?=
\Html::input( [
    'label'  => 'Przypomnij x dni przed',
    'name'   => 'reminders_interval',
    'value'  => $this -> task[ 'reminders_interval' ],
    'inline' => true
] );
?>
<?=
\Html::input_switch( [
    'label'   => 'Powtarzaj',
    'name'    => 'recursively',
    'id'      => 'recursively',
    'checked' => $this -> task[ 'recursively' ] ? true : false,
    'type'    => 'checkbox'
] );
?>
<div class="form-group row" id="recursive-details">
  <label class="col-md-3 col-form-label">Powtarzaj co:</label>
  <div class="col-md-9">
    <div class="row">
      <div class="col-12 col-sm-6">
        <input type="text" class="form-control" name="frequency" value="<?= $this -> task[ 'id' ] ? $this -> task[ 'frequency' ] : 1;?>">
      </div>
      <div class="col-12 col-sm-6">
        <select name="period" class="form-control">
          <option value="1" <? if ( $this -> task[ 'period' ] == '1' ):?>selected="selected"<? endif;?>>dni</option>
          <option value="2" <? if ( $this -> task[ 'period' ] == '2' ):?>selected="selected"<? endif;?>>m-ce</option>
          <option value="3" <? if ( $this -> task[ 'period' ] == '3' ):?>selected="selected"<? endif;?>>lata</option>
        </select>
      </div>
    </div>
  </div>
</div>
<?=
\Html::input_switch( [
    'label'   => 'Powiadom użytkowników mailem',
    'name'    => 'send_email_notification',
    'id'      => 'send_email_notification',
    'checked' => false,
    'type'    => 'checkbox'
] );
?>
<? if ( $this -> task['id'] ):?>
  <div class="form-group row">
    <label class="col-md-3 col-form-label"></label>
    <div class="col-md-9">
      <a href="#" class="btn btn-danger task-remove" task-id="<?= $this -> task['id'];?>">
        <i class="fa fa-trash"></i>
        Usuń zadanie
      </a>
    </div>
  </div>
<? endif;?>
<?
$this -> task[ 'id' ] ? $project_id = $this -> task[ 'project_id' ] : $project_id = $this -> project_id;

$out = ob_get_clean();

$grid = new \gridEdit;
$grid -> id = 'task-edit';
$grid -> gdb_opt = $gdb;
$grid -> include_plugins = true;
$grid -> title = 'edycja <strong>zadania</strong>';
$grid -> title_small = 'Zarządzaj ustawieniami zadania';
$grid -> fields = [
    [
        'db'    => 'id',
        'type'  => 'hidden',
        'value' => $this -> task[ 'id' ]
    ]
];
$grid -> external_code = $out;
$grid -> actions = [
    'save'   => [ 'url' => '/projects/task_save/', 'back_url' => ( $this -> task[ 'project_id' ] != "" ? '/projects/tasks/project_id=' . $this -> task['project_id'] : $this -> referer ) ],
    'cancel' => [ 'url' => $this -> referer ]
];
$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">
  $(document).ready(function ()
  {
    $( 'body' ).on( 'click', '.task-remove', function(e)
    {
      e.preventDefault();
      var task_id = $( this ).attr( 'task-id' );

      $.confirm(
      {
        title: 'Potwierdź',
        type: 'orange',
        columnClass: 'col-md-8 col-md-offset-2 col-12',
        closeIcon: true,
        closeIconClass: 'fa fa-close',
        content: 'Na pewno chcesz usunąć wybrane zadanie?',
        theme: 'modern',
        buttons: {
          submit: {
            text: '<i class="fa fa-check"></i>Zatwierdź',
            btnClass: 'btn-success',
            keys: ['enter'],
            action: function ()
            {
              $.ajax(
              {
                type: 'POST',
                cache: false,
                url: '/projects/task_delete/',
                data:
                {
                  task_id: task_id
                },
                beforeSend: function()
                {},
                success: function( response )
                {
                  document.location.href = "<?= $this -> referer;?>";
                }
              });
            }
          },
          cancel: {
            text: '<i class="fa fa-close"></i>Anuluj',
            btnClass: 'btn-danger',
            action: function() {}
          }
        }
      });
    });

    $('textarea#text').ckeditor(
    {
      toolbar: 'Basic',
      language: 'pl',
      height: '100'
    });
  });
</script>