<?php 
use yii\helpers\Html;
use yii\widgets\Pjax;
use frontend\models\Vehicle;

use common\widgets\GridView;
use common\widgets\ActionColumn;
use yii\data\ArrayDataProvider;

$timeProvider = new ArrayDataProvider([
	'allModels' => $model->times
])
?>
<style>
    td .fa{
        color: red;
        cursor: pointer;
    }
</style>
<?php Pjax::begin(['id' => 'travel']) ?>

<div class="time-grid">
<?= GridView::Widget([
    'new' => '&nbsp;',
    'dataProvider' => $timeProvider,
    'rowOptions' => function($model, $index){
        return ['id' => 'row-'.$model->id]; 
    },
    'columns' => [
        [
            'format' => 'raw',
            'value' => function($model) use($form){
                return Html::tag('span', '', ['class' => 'fa fa-trash btn-delete', 'data-id' => $model->id]);
            }
        ],
        'operativename',
        [
            'format' => 'raw',
            'attribute' => 'vehicle.reg',
            'value' => function($model) use($form){
                return $form->field($model, "[$model->id]vehicleid")->dropdownlist(Vehicle::GetActive(), ['prompt' => 'Please Select'])->label(false);
            }
        ],
        
        [
            'format' => 'raw',
            'attribute' => 'direct',
            'value' => function($model) use($form){
                return $form->field($model, "[$model->id]direct")->checkbox()->label(false);
            }
        ],

        'postcodefrom',
        'postcodeto',
        'distance',
        [
            'attribute' => 'traveltime',
            'value' => function($model) {
                $t = $model->traveltime;
                return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
            }
        ],
        'shifttime',
        [
            'attribute' => 'totaltime',
            'value' => function($model) {
                $t = $model->totaltime;
                return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
            }
        ],
        [
            'format' => 'raw',
            'attribute' => 'comments',
            'value' => function($model) use($form){
                return $form->field($model, "[$model->id]comments")->textarea(['rows' => 2])->label(false);
            }
        ],
      
    ],
]);
?>
</div>
<script>
    $('.btn-delete').on('click', function(ev){
        id = $(this).data('id');
        $.ajax({
            type:'post',
            url:'/activities/deletetime',
            data: {id:id},
            success: function(result, b, c){
                $('#row-'+id).remove();
            },
        });
    })
</script>
<?php Pjax::end() ?>