<?php

use yii\helpers\{Html, Url};
use yii\widgets\DetailView;
use yii\data\ArrayDataProvider;
use common\widgets\GridView;
/* @var $this yii\web\View */
/* @var $model backend\models\Assetsbe */

$this->title = $model->serialnumber;
$this->params['breadcrumbs'][] = ['label' => 'Assets', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
$owner = $model->owner;
?>
<div class="assetsbe-view">
  
    <div class="card">
    	<div class="card-header bg-info">
    		<span class="h3">Details </span>
    		<div class="float-right no-print">
		        <?= Html::a('UPDATE', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
		        <?= Html::a('DELETE', ['delete', 'id' => $model->id], [
		            'class' => 'btn btn-danger',
		            'data' => [
		                'confirm' => 'Are you sure you want to delete this item?',
		                'method' => 'post',
		            ],
		        ]) ?>
		    </div>

    	</div>
    	
    	<div class="card-body">
    		<div class="row">
    			<div class="col-sm-3 col-lg-3">
					<span class="font-weight-bold">Type:</span> 
					<?= $model->assettypename ?>
				</div>
				<div class="col-sm-3 col-lg-3">
					<span class="font-weight-bold">Product Name:</span>
					<?= empty($model->productid) == false ? $model->product?->product_name : '(Not Set)'?>
				</div>
				<div class="col-sm-3 col-lg-2">
				<span class="font-weight-bold">Model:</span>
					<?= $model->model ?>
				</div>
				<div class="col-sm-3 col-lg-2">
				<span class="font-weight-bold">Capacity:</span>
					<?= $model->capacity ?>
				</div>
				<div class="col-sm-3 col-lg-2">
					<span class="font-weight-bold">Packed:</span>
					<?= $model->packed ?>
				</div>
    		</div>
    		<div class="row">
				<div class="col-sm-3 col-lg-3">
					<span class="font-weight-bold">Agent:</span>
					<?= $model->agent ?>
				</div>
				<div class="col-sm-3 col-lg-3">
					<span class="font-weight-bold">Invoice No:</span>
					<?= $model->invoiceno ?>
				</div>
				<div class="col-sm-3 col-lg-3">
					<span class="font-weight-bold">Date of Manufacturer:</span>
					<?= $model->dom ?>
				</div>
    		</div>
    		<div class="row">
				<div class="col-sm-3 col-lg-3">
					<span class="font-weight-bold">Purchase Date:</span>
					<?= $model->purchasedate ?>
				</div>
				<div class="col-sm-3 col-lg-3">
					<span class="font-weight-bold">Registered:</span>
					<?= Yii::$app->formatter->asBoolean($model->registered) ?>
				</div>
				<div class="col-sm-3 col-lg-3">
					<span class="font-weight-bold">Discounts:</span>
					<?= Yii::$app->formatter->asBoolean($model->discountservice) ?>
				</div>
    		</div>
    		<div class="row">
				<div class="col-sm-3 col-lg-3">
					<span class="font-weight-bold">Status:</span>
					<?= $model->status ?>
				</div>
				<div class="col-sm-3 col-lg-3">
					<span class="font-weight-bold">Serial Number:</span>
					<?= $model->serialnumber ?>
				</div>
				<div class="col-sm-3 col-lg-3">
					<span class="font-weight-bold">Seago Serial Number:</span>
					<?= $model->seagoserialnumber ?>
				</div>
    		</div>
			<div class="row mt-3">
				<div class="col">
					<span class="font-weight-bold">First Service Due:</span>
					<?= !empty($model->data->first_service) ? date('d/m/Y', strtotime(str_replace('/', '-', $model->data->first_service))) : '' ?>
				</div>
				<div class="col">
					<span class="font-weight-bold">First Service:</span>
					<?= !empty($model->data->first_actual) ? date('d/m/Y', strtotime(str_replace('/', '-', $model->data->first_actual))) : '' ?>
				</div>
				<div class="col">
					<span class="font-weight-bold">Last Service:</span>
					<?=  !empty($model->data->last_service) ? date('d/m/Y', strtotime(str_replace('/', '-', $model->data->last_service))) : '' ?>
				</div>
				<div class="col">
					<span class="font-weight-bold">Since Last Service:</span>
					<?php 
						if(!empty($model->data->last_service)){
						$date = DateTime::createfromformat('d/m/Y', $model->data->last_service);
						$diff = date_diff($date, date_create(date('Y-m-d', strtotime('today'))));
						echo $diff->format('%y Year, %m Months');
						} else {
							echo ' - ';						}
					?>
					<?=  !empty($model->data->lastserviceage) ? $model->data->lastserviceage : '' ?>
				</div>
				<div class="col">
					<span class="font-weight-bold">Next Service Due:</span>
					<?=  !empty($model->data->next_service) ? date('d/m/Y', strtotime(str_replace('/', '-', $model->data->next_service))) : '' ?>
				</div>
				<div class="col">
					<span class="font-weight-bold">No of Services:</span>
					<?=  $model->noofservices ?>
				</div>
    		</div>


    		<div class="row mt-3">
    			<div class="col-sm-2 data-label">No Reminder: <?= Yii::$app->formatter->asBoolean($model->noreminder); ?></div>
    			<div class="col-sm-10">
					<b>Reason: </b>
					<p><?= $model->noreminder_reason ?></p>
				</div>
    		</div>
			
    		<div class="row mt-3">
    			<div class="col-sm-2 data-label">Notes</div>
    			<div class="col-sm-10">
					<?php if (!empty($model->notes) && is_array( Json_decode($model->notes, 1))) { ?>
						<?php foreach (Json_decode($model->notes, 1) as $key => $note) { ?>
							<div class="row">
								<div class="col-sm-4">
									<?= $key ?>
								</div>
								<div class="col-sm-8">
									<?= $note ?>
								</div> 
								</div> 
							<?php } ?>
						<?php } else { ?>
							<?= $model->notes ?>
						<?php } ?>
				</div>
    		</div>
    		
    	</div>
    </div>
    <div class="card">
    	<div class="card-header bg-info">
    		<span class="h3">Owner</span>
    	</div>
    	<div class="card-body">
    		<div class="row">
    			
    			<div class="col-6">
    				<span class="data-label">Name: </span>
    				<?= $model->client; ?>
    				<?= !empty($model->clientid)  && $owner?->fullname == $model->client ? '' : (empty($model->clientid) == false && $owner?->fullname !== $model->client  ? Html::A($owner?->fullname, ['account/view', 'id' => $model->clientid], ['target'=> '_blank']) : '(not set)')?>
    			</div>
    			<div class="col-6">
    				<span class="data-label">Email:</span>
    				<?= !empty($model->clientid) ?  Html::A($owner?->email, 'mailto:'.$owner?->email) : '(not set)' ?>
    			</div>
    		</div>
    	</div>
    </div>

	<?php // print_r($model->getLastservice() , 1); ?>

	<?php if(($servicehistory = $model->servicehistory)){ ?>
		<?= GridView::widget([
		'dataProvider' => new ArrayDataProvider(['allModels' => $servicehistory, 'pagination' => false]),
		'new' => '&nbsp;',
		'buttons' => false,
		'title' => '<h3>Service History</h3>',
		'columns' => [
			[
				'format' => 'raw',
				'label' => 'Has Date',
				'attribute'=> 'nodate',
				'value'=> function($dataModel){
					return $dataModel->nodate;
				},
			],
			[
				'format' => 'datetime',
				'label' => 'Form Created',
				'attribute'=> 'created_at',
				'value'=> function($dataModel){
					return $dataModel->created_at;
				},
			],
			[
				'label' => 'Date of Service',
				'attribute'=> 'date_of_service',
				'value'=> function($dataModel){
					return $dataModel->date_of_service;
				},
			],
			[
				'label' => 'Date of Next Service',
				'attribute'=> 'date_of_next_service',
				'value'=> function($dataModel){
					return $dataModel->date_of_next_service;
				},
			],
			[
				'label' => 'Serial Number',
				'attribute'=> 'serial_number',
				'value'=> function($dataModel){
					return $dataModel->serial_number;
				},
			],
			[
				'label' => 'Actions',
				'format' => 'raw',
				'value' => function($dataModel) use ($model){
					
					$J = !empty($dataModel->actid) ? Html::A('<i class="fa-solid fa-briefcase"></i>', ['/activities/view', 'id' => $dataModel->actid], ['target' => '_blank']) : '';
					return Html::A('<i class="fab fa-wpforms"></i>', ['/forms/view', 'id' => $dataModel->id, 'form' => $dataModel->form], ['target' => '_blank'])
					.$J;
				}
				],
			],
	]) ?> 
	<?php } else { ?>
		<?= Html::A('Find and Link Services', ['asset/findforms', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
	<?php } ?>
	<?php if(($emails = $model->mail)){ ?>
		<div class="card">
			<div class="card-header">
				<span class="h3">E-Mails</span>
			</div>
			<div class="card-body p-0">
				<?= GridView::widget([
					'dataProvider' => new ArrayDataProvider(['allModels' => $emails, 'pagination' => false]),
					'new' => '&nbsp;',
					'columns' => [
						'subject',
						'to_email',
						'created_at:datetime',
						'message_sent:datetime',
						'status',
						'success',
						[
							'label' => 'Actions',
							'format' => 'raw',
							'value' => function($dataModel) use ($model){
								return Html::A(Html::tag('span', '', ['class' => 'fa fa-envelope']), ['/mail/view', 'id' => $dataModel['id']], ['target' => '_blank']);
							}
						],
					],
				]) ?> 
			</div>
		</div>
	<?php } ?>
	
</div>
