<?php

use yii\helpers\{ArrayHelper, Html};
use yii\widgets\ActiveForm;
use kartik\select2\Select2;
use kartik\date\DatePicker;
use backend\models\Attributesvalues;
/* @var $this yii\web\View */
/* @var $model backend\models\Assetsbe */
/* @var $form yii\widgets\ActiveForm */
$cacheKey = 'Products';
$cacheDuration = 3600;

$assettypes = ['boat' => 'Boat', 'liferaft' => 'Life Raft', 'lifejacket' => 'Life Jacket', 'kayak' => 'Kayak'];
$products = Yii::$app->cache->getOrSet($cacheKey, function () {
    return ArrayHelper::map(
        \backend\models\Productsbe::find()
            ->select(['id', 'product_name'])
            ->where(['product_type' => 'product'])
            ->andWhere(['not', ['=', 'status', 'deleted']])
            ->orderBy(['product_name' => SORT_ASC])
            ->all(),
        'id',
        'product_name',
        'category.name'
    );
}, $cacheDuration);
?>

<div class="assetsbe-form">

    <?php $form = ActiveForm::begin(); ?>

    <div class="card">
    	<div class="card-header bg-info">
    		<span class="h3"><?= $this->title ?></span>
    			<div class="float-right no-print">
		        <?= Html::submitButton('SAVE', ['class' => 'btn btn-success']) ?>
		   		<?php if (yii::$app->controller->action->id == 'update') {?>
					<?= Html::a('VIEW', ['/asset/view', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
			        <?= Html::a('DELETE', ['/asset/update', 'id' => $model->id], ['class' => 'btn bg-danger text-white']) ?>
				<?php }?>
		    </div>
    	</div>
    	<div class="card-body">
		    <div class="row">
				<div class="col-md-4">
					<?= $form->field($model, 'assettype')->dropdownList($assettypes,
								[
									'prompt' => 'Please Select'
								]
							)
						->label('Product') ?>
				</div>
				<div class="col-md-4">
					<?= $form->field($model, 'productid')->dropdownList($products,
								[
									'prompt' => 'Please Select'
								]
							)
						->label('Product') ?>
				</div>
				<div class="col-md-4">
				<?= $form->field($model, 'status')
						->dropdownlist(Attributesvalues::formArray('sale_status'), ['prompt' => 'Please Select'])
						->label() 
					?>
				</div>
			</div>
			<div class="row">
				<div class="col-lg-6">
					<?= $form->field($model, 'serialnumber')->textInput()->label() ?>
				</div>
				<div class="col-lg-6">
					<?= $form->field($model, 'dom')->widget(DatePicker::class, ['type' => 1, 'pluginOptions' => ['convertFormat' => true, 'format' => 'dd/mm/yyyy']])
						->label() ?>
				</div>
			</div>
			<div class="row">
				<div class="col-4">
					<?= $form->field($model, 'clientid')->widget(Select2::class, [
                                'class' => 'form-control btn-group',
                                'maintainOrder' => true,
                                'options' => [
                                    'id' => 'clients',
                                    'placeholder' => 'Select Client...', 
                                    'multiple' => false
                                ],
								'initValueText' => $model->owner ? $model->owner->fullname : $model->client,
                                'pluginOptions' => [
                                    'allowClear' => true,
                                    'minimumInputLength' => 3,
                                    'ajax' => [
                                        'url' => "/NGauge/activities/getclients",
                                        'dataType' => 'json',
                                        'data' => new yii\web\JsExpression('function(params) { return {q:params.term, page: params.page}; }'),
                                    ],
                                    'escapeMarkup' => new yii\web\JsExpression('function (markup) { return markup; }'),
                                    'templateResult' => new yii\web\JsExpression('function(city) { return city.text; }'),
                                    'templateSelection' => new yii\web\JsExpression('function (city) { return city.text }'),
                                ],
                                'pluginEvents' => [
                                    //    "select2:select" => "function(data) {welderadd(data.params.data.text); }",
                                    //    "select2:unselect" => "function(data) {console.log(data); welderdel(data.params.data.text); }"

                                ],
                            ])
                            ->label('Owner')
							 ?>
							 <?= $form->field($model, 'client')->textInput()->label('End user Name') ?>
							 </div>
				<div class="col-4"><?= $form->field($model, 'agent')->textInput()->label() ?></div>
				<div class="col-4"><?= $form->field($model, 'invoicenumber')->textInput()->label() ?></div>
				<div class="col-4"><?= $form->field($model, 'invoicenumber')->textInput()->label() ?></div>
				<div class="col-4"></div>
			</div>
			<div class="row">
				<div class="col-12">
					<?= $form->field($model, 'noreminder')->checkbox(['label' => false, 'unselect' => false])->label() ?>
					<?= $form->field($model, 'noreminder_reason')->textArea() ?>
				</div>		
			</div>
			<div class="row">
				<div class="col-12">
					<?= $form->field($model, 'notes')->textArea()->label('Notes') ?>
				</div>		
			</div>
		</div>
    </div>
	<div class="card">
		<div class="card-header bg-info">
			Dates
		</div>
		<div class="card-body">
			<div class="row">
				<div class="col">
					<?= $form->field($model, 'purchasedate')
						->widget(DatePicker::class, ['type' => 1, 'pluginOptions' => ['convertFormat' => true, 'format' => 'dd/mm/yyyy']])
						->label()
					 ?>
					</div>
				<div class="col">
					<?= $form->field($data, 'first_service')->widget(Datepicker::class, ['type' => 1, 'pluginOptions' => ['convertFormat' => true, 'format' => 'dd/mm/yyyy']])->label('First Service Due') ?>
				</div>
				<div class="col">
					<?= $form->field($data, 'first_actual')->widget(Datepicker::class, ['type' => 1, 'pluginOptions' => ['convertFormat' => true, 'format' => 'dd/mm/yyyy']])->label('First Actual Service') ?>
				</div>
				<div class="col">
					<?= $form->field($data, 'last_service')->widget(Datepicker::class, ['type' => 1, 'pluginOptions' => ['convertFormat' => true, 'format' => 'dd/mm/yyyy']])->label('Last Service') ?>
				</div>
				<div class="col">
					<?= $form->field($data, 'next_service')->widget(Datepicker::class, ['type' => 1, 'pluginOptions' => ['convertFormat' => true, 'format' => 'dd/mm/yyyy']])->label('Next Service') ?>
				</div>
			</div>
		</div>

	</div>
	<?php ActiveForm::end(); ?>
</div>
