<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\bootstrap\Modal;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $searchModel app\models\TKandidatSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Kandidat';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php
        
    Modal::begin([
        'header' => '<center><h4>FORM DATA KANDIDAT</h4></center>',
        'id' => 'modal-data',
        'size' => 'modal-md',
        'options' => [
              'tabindex' => false, 
          ],
    ]);

    echo '<div id="content-modal-data"></div>';
    Modal::end();
?>

<div class="tkandidat-index">

    <center><h3><b class="text-danger">DATA KANDIDAT PADA VOTING</b></h3></center>

    <p align="right">

        <?= Html::a('<span class="glyphicon glyphicon-step-backward"></span>Kembali', ['voting/index'], ['class' => 'btn btn-default']) ?>

        <?= Html::button('<span class="glyphicon glyphicon-plus"></span> TAMBAH DATA KANDIDAT', ['id' => 'modalButton', 'value' => \yii\helpers\Url::to(['kandidat/create','id_voting' =>$_GET['id_voting']]), 'class' => 'btn btn-success']) ?>
    </p>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'nama_kandidat',
            'voting.nama_voting',

            [
                'options' => [
                    'width' => '150px',
                ],
                'header' => 'AKSI',

                'class' => 'yii\grid\ActionColumn',
                    'template' => '{update} {delete}',
                    'buttons' => [
                        
                        'update' => function($url, $model) {
                            return Html::a('<span class="glyphicon glyphicon-pencil"></span>', '#', [
                                        'class' => 'btn btn-warning',
                                        'title' => 'EDIT DATA',
                                        'id' => 'modalButton',
                                        'onclick' => "
                          $.ajax({
                            url: '$url',
                            success:function(data){
                              $('#title').html('Detail Organisasi');
                              $('#content-modal-data').html(data);
                              $('#modal-data').modal('show');
                            },
                            error:function(xhr, ajaxOptions, throwError){
                              alert(xhr.responseText);
                            }
                          })
                          ",
                                    ]);
                        },
                        'delete' => function ($url, $model) {
                            return  Html::a('<span class="glyphicon glyphicon-trash"></span>', Url::to(['kandidat/delete','id'=>$model->id_kandidat,'id_voting'=>$model->id_voting]), [
                                        'class' => 'btn btn-danger',
                                        'title' => 'HAPUS DATA',
                                        'data-toggle' => 'tooltip',
                                        'data-method' => 'post',
                                        'data-confirm' => 'Apakah anda yakin ingin menghapus data ini?'
                                    ]);
                        },
                    ]
            ],
        ],
    ]); ?>


</div>

<?php
 
$script = <<< JS
    $(function () {
        $('#modalButton').click(function () {
            $('#modal-data').modal('show')
                .find('#content-modal-data')
                .load($(this).attr('value'));
        });
        
    });
    
    
    
JS;
$this->registerJs($script)
?>
