<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model backend\models\Licensedatabe */
/* @var $form yii\widgets\ActiveForm */
$model->issuingbody = !$model->issuingbody ? 'DVLA' : $model->issuingbody;
$this->registerJsfile('/js/moment.js');
?>
<script>
	

function saveField(id, field, value) {
	$.ajax({
		type: "POST",
		url: "/NGauge/licensedata/savefield",
		data: {id: id, field: field, value: value},
		})
	.done(function(result) {
		result = JSON.parse(result);
			$('#message').html(result.message);
			$('#flashmessage').delay(500).fadeIn('normal', function() {
				$(this).delay(2500).fadeOut();
			});
	    $.pjax.reload({container: '#cats', async:false});
	});
}
function addRow() {
	id = "<?= $model->id ?>";
	$.ajax({
		type: "POST",
		url: "/NGauge/licensedata/addrow",
		data: {id: id},
		})
	.done(function(result) {
		result = JSON.parse(result);
			$('#message').html(result.message);
			$('#flashmessage').delay(500).fadeIn('normal', function() {
				$(this).delay(2500).fadeOut();
			});
	    $.pjax.reload({container: '#cats', async:false});
	});
}
</script>
<div class="licensedatabe-form">

    <?php $form = ActiveForm::begin(); ?>
    <div class="card card-default">
 		<div class="card-header">License Details</div>
 		<div class="card-body">
		    <div class="row">
		    	
			<div class="col-sm-2">Employee</div>
		    <div class="col-sm-4"><?= $form->field($model, 'employee')->dropdownlist($employees, ['prompt' => 'Please Select'])->label(false) ?></div>

			<div class="col-sm-2">Name on License</div>
		    <div class="col-sm-4"><?= $form->field($model, 'name')->textInput()->label(false) ?></div>

			</div>
		    <div class="row">
		 
			<div class="col-sm-2">License Number/Ref</div>
		    <div class="col-sm-4"><?= $form->field($model, 'licensenumber')->textInput()->label(false) ?></div>

			<div class="col-sm-2">Date of Birth</div>
		    <div class="col-sm-4"><?= $form->field($model, 'dob')->widget(\kartik\date\DatePicker::class, ['pluginOptions' => ['showMeridian' =>true, 'convertFormat' => true, 'format' => 'dd/mm/yyyy']])->label(false) ?></div>
		    	
			 </div>
		    <div class="row">
		    
			<div class="col-sm-2">Issuing Body</div>
		    <div class="col-sm-4"><?= $form->field($model, 'issuingbody')->textInput()->label(false) ?></div>
			</div>
		    <div class="row">
		 
			<div class="col-sm-2">Issued Date</div>
		    <div class="col-sm-4"><?= $form->field($model, 'issuedate')->widget(\kartik\date\DatePicker::class, ['pluginOptions' => ['showMeridian' =>true, 'convertFormat' => true, 'format' => 'dd/mm/yyyy']])->label(false) ?></div>

			<div class="col-sm-2">Expiry Date</div>
		    <div class="col-sm-4"><?= $form->field($model, 'expirydate')->widget(\kartik\date\DatePicker::class, ['pluginOptions' => ['showMeridian' =>true, 'convertFormat' => true, 'format' => 'dd/mm/yyyy']])->label(false) ?></div>
			</div>
		    <div class="row">
		 
			<div class="col-sm-2">Address</div>
		    <div class="col-sm-4"><?= $form->field($model, 'address')->textInput()->label(false) ?></div>

			</div>
		</div>
	</div>
 	 <div class="card card-default">
 		<div class="card-header">
 				<span >
 					License Categories
 				
 				</span>
 				<span>
					<?= HTML::button('<i class="fa fa-plus"></i>', ['onClick' => 'addRow()', 'class' => 'btn btn-sm btn-primary pull-right']); ?>
					<?= HTML::button('<i class="fa fa-minus"></i>', ['onClick' => 'removeRow()', 'class' => 'btn btn-sm btn-danger pull-right']); ?>
				</span>
			</div>
	
 		<div class="card-body">
			<?php \yii\widgets\Pjax::begin(['id' => 'cats']) ?>
 			<?php foreach ($model->cats as $a => $cat) { ?>
				<div class="row">
					<div class="col-sm-2">Category</div>
					<div class="col-sm-2"><?= $form->field($cat, 'category')->dropdownlist($cats, ['prompt' => 'Please Select...', 'onchange' => "saveField('$cat->id', 'category', $(this).val())"])->label(false) ?></div>
					<div class="col-sm-2">Start Date</div>
					<div class="col-sm-2"><?= $form->field($cat, 'startdate')->widget(\kartik\date\DatePicker::class, ['pluginEvents' => [
					 "changeDate" => "function(e) {saveField('$cat->id', 'startdate',moment(e.date).format('D/MM/Y'));}",], 'pluginOptions' => ['showMeridian' =>true, 'convertFormat' => true, 'format' => 'dd/mm/yyyy']])->label(false) ?></div>
					<div class="col-sm-2">End Date</div>
					<div class="col-sm-2"><?= $form->field($cat, 'enddate')->widget(\kartik\date\DatePicker::class, [
					'pluginOptions' => ['showMeridian' =>true, 'convertFormat' => true, 'format' => 'dd/mm/yyyy'], 
					'pluginEvents' => [
					 "changeDate" => "function(e) {saveField('$cat->id', 'enddate',  moment(e.date).format('D/MM/Y'));}",],])->label(false) ?></div>
				</div>
			<?php } ?>
			<?php \yii\widgets\Pjax::end() ?>

		</div>
 	</div>
 <div class="card card-default">
 		<div class="card-header">License Files</div>
 		<div class="card-body">
	
	<?= $this->render('/uploads/dbupload', ['relatedto' => 'license', 'relatedid' => $model->id]); ?>
	<?php if ($model->files) { ?>
		<?= $this->render('/uploads/files', ['files' => $model->files]); ?>
	<?php } ?> 
			</div>
 	</div>
 	
    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
    </div>

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

</div>
