<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use kartik\time\TimePicker;
use kartik\date\DatePicker;
use kartik\select2\Select2;
use yii\imagine\Image;
Use backend\models\Attributesvalues;
use modules\custom\models\Accounts;
use modules\users\models\User;

use common\widgets\TinyMce;
/* @var $this yii\web\View */
/* @var $model modules\forms\models\Dt126 */
/* @var $form yii\widgets\ActiveForm */
//$form = ActiveForm::begin()
//Select2 user query
if(Yii::$app->controller->action->id == 'create'){

    $model->ncrdescription = Yii::$app->request->get('nonconr1') ? Yii::$app->request->get('nonconr1') : null;
	$model->date = Yii::$app->request->get('starttime', null) !== null ? Yii::$app->request->get('date') : $model->date;
	$model->location = Yii::$app->request->get('location', null) !== null ? Yii::$app->request->get('location') : $model->location;
	$model->injobno = Yii::$app->request->get('injobno', null) !== null ? Yii::$app->request->get('injobno') : $model->injobno;
	$model->weld = Yii::$app->request->get('weld');
	$model->esrrequired = Yii::$app->request->get('esr');
	$model->actid = Yii::$app->request->get('actid');
	$model->road = Yii::$app->request->get('trackid');
	$model->mileage = Yii::$app->request->get('m1');
	$model->mileageb = Yii::$app->request->get('m2');
	$model->mileagec = Yii::$app->request->get('m3');
	$model->mileaged = Yii::$app->request->get('m4');
	$model->weldersname = explode(',', Yii::$app->request->get('welders'));
	$model->printname = Yii::$app->request->get('welders');
	$model->date = Yii::$app->request->get('date', null) !== null ? Yii::$app->request->get('date') : $model->date;
	$model->rail = Yii::$app->request->get('rail');
	$model->stampno = Yii::$app->request->get('stamp') ? Yii::$app->request->get('stamp') :  Yii::$app->user->identity->stampno;
}

$users = User::EmployeesU();


$textInput = '<div class="input-group">
	<div class="input-group-append w-25 my-auto">
	<span class="input-group-text DarkerBlue w-100">
		{label}
	</span>
	</div>
	{input}
	</div>{hint}{error}';
	$welderOptions =
	[
		'data' => $users,
		'options' => [
			'id' => 'welders',
			'placeholder' => 'Select Operatives...', 'prompt'=>'Please Select'
		],
		'pluginOptions' => [
			'tags' => true,
			'multiple' => true,
			'tokenSeparators' => [',', ''],
		],
	];
//$this->registerJsFile('/js/touch.js'); //load canvas drawing
$this->registerJsFile('/js/touch_1.js');//load canvas_1
$this->registerJsFile('/js/touch_2.js'); //load canvas_2
$this->registerJsFile('/js/touch_3.js'); //load canvas_3
$this->registerJsFile('/js/updatesignatures.js'); //load images
 ?>
<script>
$(function() {
    //init(); //Drawing
    init1(); //canvas_1
    init2(); //canvas_2
    init3(); //canvas_3
});
</script>

<div class="dt128-form">
    <?php $form = ActiveForm::begin(); ?>
    <div class="row">
        <div class="col-12 col-md-4">
            <?= $form->field($model, 'date', ['template' => $textInput])->widget(DatePicker::class, ['type' => 1, 'pluginOptions' => [ 'format' => 'dd/mm/yyyy','todayHighlight' => true ] ]) ?>

            <div class="input-group">
                <?= $form->field($model, 'mileage', ['options' => ['class' => 'w-50']])->textInput(['maxlength' => 100]) ->label(false) ?>
                <?= $form->field($model, 'mileageb', ['options' => ['class' => 'w-50']])->textInput(['maxlength' => 100])->dropDownList(Attributesvalues::formarray('milemeters'), ['prompt' => 'Please Select...']) ->label(false)?>
                <?= $form->field($model, 'mileagec', ['options' => ['class' => 'w-50']])->textInput(['maxlength' => 100]) ->label(false)?>
                <?= $form->field($model, 'mileaged', ['options' => ['class' => 'w-50']])->textInput(['maxlength' => 100])->dropDownList(Attributesvalues::formarray('yardschain'), ['prompt' => 'Please Select...']) ->label(false) ?>
            </div>

            <?=$form->field($model, 'weldersname', ['template' => $textInput])->widget(Select2::class, $welderOptions ) ?>
            <?= $form->field($model, 'stampno', ['template' => $textInput])->textInput(['maxlength' => 100])  ?>
        </div>
        <div class="col-12 col-md-4">
            <?= $form->field($model, 'location', ['template' => $textInput])->textInput(['maxlength' => 100]) ?>
            <?= $form->field($model, 'road', ['template' => $textInput])->textInput(['maxlength' => 100]) ?>
            <div id="canvas2" class="input-group">
            <div class="input-group-append ">
				<span class="input-group-text DarkerBlue">
					<label class="control-label">Welders Signature</label>
				</span>
            </div>
                <canvas id="canvas_2" class="Canvas"></canvas>
            <div class="input-group-append ">
				<span class="input-group-text bg-danger text-white" onclick='ClearCanvasX(2);'>
					Reset
				</span>
			</div>
		</div>
        	<?= $form->field($model, 'wsignatureloc')->hiddenInput(['id' => 'signature2']) ->label(false)?>
            <?= $form->field($model, 'printname', ['template' => $textInput])->textInput(['maxlength' => 100]) ?>


        </div>
        <div class="col-12 col-md-4">
            <?= $form->field($model, 'jcn', ['template' => $textInput])->textInput(['maxlength' => 100]) ?>
            <?= $form->field($model, 'rail', ['template' => $textInput])->dropDownList(Attributesvalues::formarray('rail'), ['prompt'=>'Please Select']) ?>
            <?= $form->field($model, 'client', ['template' => $textInput])->dropDownList(Accounts::getActive(), ['id' => 'client', 'prompt' => 'Please Select...'])?>
            <?= $form->field($model, 'injobno', ['template' => $textInput])->textInput()?>
        </div>
    </div>
    <div class="row">
        <div class="col-12">
            Detail in the following section the reason for the NCR - i.e. the weld does not conform to
            NR/L2/TRK/0032 or NR/L2/TRK/2102 or NR/L2/TRK/0132 due to proximity to another weld, proximity to a bolthole, process
            abnormalities, defects found on inspection before welding or track conditions.
        </div>
    </div>
    <div class="row">
        <div class="col-12">
			<?= $form->field($model, 'ncrdescription')->widget(TinyMce::class, [])->label('NCR DESCRIPTION (full description of non-conformance)')?>
        </div>
    </div>
    <div class="row">
        <div class="col-12 h3 text-center">
			RAIL OR TRACK CONDITIONS UNSUITABLE FOR WELDING
        </div>
    </div>
    <div class="row">
        <div class="col-12 text-center">
				As the Client representative, I have been made aware of the Rail / Track conditions where welding is to
                take place that does not conform to NR/L2/TRK/0032 or NR/L2/TRK/2102 or NR/L2/TRK/0132.
                <br>
                Removal of weld(s) cast may be required.
                <br>
                I have authorised the welding work knowing that required standards may not be met and I will ensure that
                the weld(s) are clamped.
        </div>
    </div>

    <div class="row">
        <div class="col-12 col-md-6">
			<?= $form->field($model, 'ncrclientrep', ['template' => $textInput])->textInput(['maxlength' => 100]) ?>
    	</div>
        <div class="col-12 col-md-6">
			<div id="canvas1" class="input-group">
				<div class="input-group-append ">
					<span class="input-group-text DarkerBlue">
						<label class="control-label">Signature</label>
					</span>
				</div>
					<canvas id="canvas_1" class="Canvas"></canvas>
				<div class="input-group-append ">
					<span class="input-group-text bg-danger text-white" onclick='ClearCanvasX(1);'>
						Reset
					</span>
				</div>
			</div>
                <?= $form->field($model, 'signatureloc')->hiddenInput(['id' => 'signature1']) ->label(false)?>
    	</div>
    </div>

    <div class="row">
        <div class="col-12 h3 text-center">
			INSPECTION DEFECTS & PROCESS ABNORMALITIES
        </div>
    </div>
    <div class="row">
        <div class="col-12 text-center">
			As the Client representative, I have been informed by the welder that a weld cast is defective and
            requires minimum actions as detailed in NR/L2/TRK/001/B01 to be applied prior to re-opening the line to normal traffic.
        </div>
    </div>
    <div class="row">
        <div class="col-12 col-md-6">
			<?= $form->field($model, 'actioncode', ['template' => $textInput])->dropDownList(Attributesvalues::formarray('dt122minactioncode'), ['prompt'=>'Please Select']) ?>
			<span class="font-weight-bold">Note:</span> <span class="font-italic">Minimum action code 1a requires a 20mph ESR, emergency clamp plates fitting and removal in
                36hrs</span>
		</div>
        <div class="col-12 col-md-6">
			<?= $form->field($model, 'esrrequired', ['template' => $textInput])->dropDownList(Attributesvalues::formarray('yesno'), ['prompt'=>'Please Select']) ?>
    	</div>
    </div>
    <div class="row">
        <div class="col-12 col-md-6">
			<?= $form->field($model, 'defclientrep', ['template' => $textInput])->textInput() ?>
			
		</div>
        <div class="col-12 col-md-6">
			<div id="canvas3" class="input-group">
				<div class="input-group-append ">
					<span class="input-group-text DarkerBlue">
						<label class="control-label">Signature</label>
					</span>
				</div>
					<canvas id="canvas_3" class="Canvas"></canvas>
				<div class="input-group-append ">
					<span class="input-group-text bg-danger text-white" onclick='ClearCanvasX(3);'>
						Reset
					</span>
				</div>
			</div>
                <?= $form->field($model, 'signature3loc')->hiddenInput(['id' => 'signature3']) ->label(false)?>
    	</div>
    </div>

    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-primary', 'onclick' => 'saveImages()' ]) ?>
    </div>

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

</div>
<script>
x = 1;
canvass = $('canvas').length;

function saveImages() {
    while (x <= canvass) {
        id = "signature" + x;
        cavid = "canvas_" + x;
        canvas = document.getElementById(cavid);
        if (canvas)
            document.getElementById(id).value = canvas.toDataURL();
        x++
    }
}
</script>