<?php

use yii\helpers\Html;
use common\widgets\GridView;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\AssetsbeSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$assettypes = \backend\models\Attributesvalues::formarraytt('service_types');
$this->title = $title;
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="assetsbe-index">

    <?= GridView::widget([
    	'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'buttons' => false,
		'showFooter' => true,
        'columns' => [
			[
            	'class' => common\widgets\ActionColumn::class,	
				'controller' => 'asset', 
            ],
			[
				'header' => 'Merge',
				//'class' => yii\grid\CheckboxColumn::class,

				'format' => 'raw',
				'value' => function($model){

					$out = '';
					$out .= Html::Checkbox('mergeFrom[]', '', ['value' => $model->id, 'unchecked' => null]);
					$out .= '&nbsp;&nbsp;&nbsp;';
					$out .= Html::Radio('mergeTo', '', ['value' => $model->id, 'unchecked' => null]);
					return $out;
				},
				'filter' => '<b> S / P </b>',
				'footer' => Html::Button('Merge', ['class' => 'btn btn-primary mergebutton']),
				
			],
			'status',
            [
				'attribute' =>  'assettype',
				'value' =>  'assettypename',
				'filter' => $assettypes,
            ],
			[
				'format' => 'raw',
				'label' => 'Product Name',
				'value' => function($model){
                    return $model->product && $model->product->product_name == 'Seago Other' ? $model->product->product_name.' </br>'.$model->model : ($model->product ? $model->product->product_name : '<i>'. $model->model .'-'. $model->capacity.'-'.$model->packed.'</i>' );

                },
                
				'attribute' => 'product_name'
			],
            [
                'contentOptions' => function($dataModel){
                    return $dataModel->discountservice == true ? ['class' => 'text-success h4'] : [];
                },
                'label' => 'Serial Number',
                'attribute' => 'serialnumber',
                'value' => 'serialnumber',
            ],
            //'id',
            /*[
				'label' => 'Created By',
				'value' => 'createdby.fullname',
				'attribute' => 'createdbyusername'
				],
						[
			'label' => 'Updated By',
			'value' => 'updatedby.fullname',
			'attribute' => 'updatedbyusername'
			],*/
            // 'deleted_by',
            // 'deleted_at',
            // 'status',
            // 'relatedid',
            // 'notes',
            // 'productid',
            // 'model',
            // 'capacity',
            // 'packed',
			[
				'label' => 'Service Dates',
				'format' => 'html',
				'attribute' =>'servicedate',
				'contentOptions' => function($data){
					return ['style' => 'min-width: 225px;'];
				},
				'value' => function($dataModel){
					$data = $dataModel->data;
					$out = '<ul class="list-unstyled">';
					$out .= !empty($dataModel->purchasedate) ? '<li><b>Purchased:</b><span class="float-right">'. date('d/m/Y', strtotime(str_replace('/', '-', $dataModel->purchasedate))) .'</span></li>' :  '<li><b>Purchased:</b><span class="float-right"></span></li>';
					$out .= !empty($data->first_service) ? '<li><b>First Due:</b><span class="float-right">'. date('d/m/Y', strtotime(str_replace('/', '-', $data->first_service))) .'</span></li>' :  '<li><b>First Service:</b><span class="float-right"></span></li>';
					$out .= !empty($data->first_actual) ? '<li><b>First Actual:</b><span class="float-right">'. date('d/m/Y', strtotime(str_replace('/', '-', $data->first_actual))) .'</span></li>' :  '<li><b>First Service:</b><span class="float-right"></span></li>';
					$out .= !empty($data->last_service) ? '<li><b>Last Service:</b><span class="float-right">'. date('d/m/Y', strtotime(str_replace('/', '-', $data->last_service))) .'</span></li>' :  '<li><b>Last Service:</b><span class="float-right"></span></li>';
					$out .= !empty($data->next_service) ? '<li><b>Next Service:</b><span class="float-right">'. date('d/m/Y', strtotime(str_replace('/', '-', $data->next_service))) .'</span></li>' :  '<li><b>Next Service:</b><span class="float-right"></span></li>';
					$out .= !empty($data->no_of_services) ? '<li><b>No of Services:</b><span class="float-right">'. $data->no_of_services .'</span></li>' :  '<li><b>No of Services:</b><span class="float-right"></span></li>';
					$out .= '<li><b>Last Mail:</b><span class="float-right">'. $data->mail_sent .'</span></li>';
					$out .= '</ul>';
					return $out;
				}
			],
			[
				'label' => 'Emails',
				'format' => 'raw',
				'value' => function($dataModel){
					$out = '';
					foreach($dataModel->mail as $mail){
						$out .=  Html::A($mail->subject. ' - ' . Date('d/m/Y H:i', $mail->created_at), ['/mail/view', 'id' => $mail->id, 'language' => $mail->language], ['target' => '_blank']).' <br/>';
					}
					return $out;
				},
				'visible' => yii::$app->controller->action->id == 'needsserviceemails' ? true : false,
			],
			//'purchasedate',
            //'seagoserialnumber',
			[
				'label' => 'Name / Email',
				'format' => 'raw',
				'value' => function($model){
					if(is_object(($owner = $model->owner)) == true){
						return $owner->fullname. '<br/>'.$owner->email;
					}
				}
			],
			[
			'class' => \common\widgets\TraceabilityColumn::class,
            'attribute' => 'traceability',
            'format' => 'raw',
			],
            // 'agent',
            // 'invoicenumber',
            // 'registered',
            // 'ownerid',

            
            
        ],
    ]); ?>
</div>
<script>
	$('.mergebutton').on('click', function(){
		data1 = $("input[type='checkbox']:checked").serializeArray();
		$.merge(data1, $('.assetsbe-index').find('input[name="mergeTo"]').serializeArray());
		$.ajax({
			type: 'POST',
			url: '/NGauge/asset/merge',
			data:data1,
			success: function(result){
				$('#LGmodal .modal-body').html(result.body);
				$('#LGmodal .modal-footer').html(
					'<div class="float-right"><button type="button" class="btn btn-secondary mr-3" data-dismiss="modal">Close</button><button type="button" class="btn btn-success btn-merge">Submit</button></div>'
				);
				$('#LGmodal').modal();
			}

		})
	})
	
</script>