<?php
use yii\helpers\Html;
use backend\models\Attributesvalues;
use backend\models\Servicebaysbe;
use yii\helpers\ArrayHelper;
use yii\grid\GridView;
use yii\data\ArrayDataProvider;
$status = Attributesvalues::formarray('status');

$types = \backend\models\Attributesvalues::formarraytt('service_types');
?>

<div class="items p-0">
    <?php if (is_array($items) && count($items) > 0  && empty(($description = $items[array_key_first($items)]->activity->description)) == false){ ?>
    <div class="card">
        <div class="card-header  bg-info text-white">
            Information
        </div>
        <div class="card-body">
            <?= $description ?>
        </div>
    </div>
    <?php } ?>
    <?= GridView::widget([
        'dataProvider' => new ArrayDataProvider(
            ['allModels' => $items,'pagination' => false],
        ),
        'tableOptions' => [
            'class' => 'table table-bordered',
        ],
        'options' => ['class' => 'w-100 table-responsive'],
        'summary' => false,
        'pager' => false,
        'columns' => [
            
            [
                'class' => common\widgets\ActionColumn::class,
                'template' => '{update} {view}',
                'buttons' => [
                    'view' => function ($url, $actitem, $key) {
                        return ($servicesheet = $actitem->servicesheet) ? HTML::A("<i class='fab fa-wpforms'></i>", ['/forms/view','form' => $servicesheet->form, 'id' => $servicesheet->id], ['title' => 'View Service']) : html::tag('i', '',['class'=> 'fas fa-exclamation-triangle']);
                    },
                    'update' => function ($url, $actitem, $key) {
                        return ($servicesheet = $actitem->servicesheet) ? HTML::A("<i class='fa fa-gears'></i>", ['/forms/update','form' => $servicesheet->form,  'id' => $servicesheet->id], ['title' => 'Update Service']) : html::tag('i', '',['class'=> 'fas fa-exclamation-triangle']);
                    },
                ]
            ],
            [
                'attribute' => 'type',
                'value' => function($dataModel) use ($types){
                    $dataModel->type = $dataModel->type == 'lifejacketbulk' ? 'lifejacket' : $dataModel->type;
                    return !empty($dataModel->type) ? $types[$dataModel->type] : false;
                }
            ],
            [
                'attribute' => 'product',
                'format' => 'raw',
                'value' => function($actitem){
                    $product = $actitem->productl?->product_name;
                    $out = empty($product) == false ? $product :  $actitem->productname;
                    $out .=  empty($product) == false && $product == 'Seago Other' ? '<br /> - '.$actitem->typea : '';
                    return $out;

                }
            ],
			[
				'label' => 'Service/Turn Around',
				'value' => 'turnaround',
			],
            [
                'contentOptions' => function($dataModel){
                    return !empty($dataModel->assetid) && $dataModel->asset?->discountservice == true ? ['class' => 'text-success h4'] : [];
                },
                'label' => 'Serial Number',
                'value' => 'serial'
            ],
            [
                'attribute' => 'Notes',
                'format' => 'raw',
                'value' => function($model){
                    return Html::textArea('notes', $model->notes, ['class' => 'form-control item_notes',' data-id'=>$model->id]);
                }
            ],
            [
                'attribute' => 'location',
                'format' => 'raw',
                'value' => function($model) use ($status){
                    return Html::dropDownList('location', $model->locationid, $model->freelocations($model->id),
                        ['prompt' => 'Please Select','class' => 'form-control location_dropdown',' data-id'=>$model->id]);
                    
                }
            ],
            [
                'attribute' => 'status',
                'format' => 'raw',
                'value' => function($model) use ($status){
                    return Html::dropDownList('location', $model->status, $status, ['prompt' => 'Please Select','class' => 'form-control status_dropdown',' data-id'=>$model->id]);
                    
                }
            ],
            
            [
                'attribute' => 'traceability',
                'format' => 'raw',
                'value' => function ($actitem){
                    return 'U: '.$actitem->updatedby_name.' | '.$actitem->updated_at;
                }
            ],
        ]
        ]);?>

    </div>
<script>

    function saveItem(id, value, attrib){

        if(value == ""){
            return false;
        }
        else{
            
            $.ajax({
                type: "POST",
                url: "/NGauge/activities/itemfield",
                data: {
                    id: id,
                    attribute: attrib,
                    value: value
                },
            })
            .done(function(result) {
                console.log(result.message);
            });
        }

    }
    
    $('.item_notes').on('blur', function(){
        var id = $(this).data('id');
        var value = $(this).val();
        var attrib = 'notes';
        saveItem(id, value, attrib);
    });
    $('.location_dropdown').on('change', function(){
        var id = $(this).data('id');
        var value = $(this).val();
        var attrib = 'locationid';

        saveItem(id, value, attrib);
    });

    
    $('.status_dropdown').on('change', function(){
        var id = $(this).data('id');
        var value = $(this).val();
        var attrib = 'status';

        saveItem(id, value, attrib);
    });

</script>