<?php

use yii\helpers\Html;
use yii\helpers\Arrayhelper;
use modules\custom\models\Accounts;
use modules\users\models\User;
use yii\widgets\ActiveForm;
use kartik\datetime\DateTimePicker;
use kartik\date\DatePicker;


/* @var $this yii\web\View */
/* @var $model modules\forms\models\Dt197 */
/* @var $form yii\widgets\ActiveForm */

//$this->registerJsFile('/js/touch.js'); 
$this->registerJsFile('/js/touch_0.js'); 
//$this->registerJsFile('/js/touch_1.js'); 
//$this->registerJsFile('/js/touch_2.js'); 
//$this->registerJsFile('/js/touch_3.js');
$users = User::Employeesu();
$clients = Accounts::getActive();
if ($this->context->action->id == 'update') {$this->registerJsFile('/js/updatesignatures.js');} ?>


<script>
window.onload = function() {
    //init(); //Drawing
    init0(); //canvas_0
    //init1(); //canvas_1
    //init2(); //canvas_2
    //init3(); //canvas_3
}


function update(id, field, content) {
    $.ajax({
            type: "POST",
            url: "/dt197/updaterow",
            data: {
                id: id,
                field: field,
                content: content
            },
        })
        .done(function(result) {
            if (result == 'Updated') {
                data = "<div>Updated</div>"
            } else {
                data = result;
            }
            $('#message').html(data);
            $('#flashmessage').delay(500).fadeIn('normal', function() {
                $(this).delay(2500).fadeOut();



            });
        });
}
</script>

<div class="dt197-form">
    <?php $form = ActiveForm::begin(); ?>
    <table class="table table-borderless">
        <thead>

        </thead>
        <tbody>

            <tr>
                <th class="DarkerBlue">Client</th>
                <td><?= $form->field($model, 'client')->dropdownlist($clients, ['prompt' => 'Please Select'])->label(false) ?>
                </td>
                <th class="DarkerBlue">Date &amp; Time</th>
                <td><?= $form->field($model, 'insdate')->widget(DateTimePicker::class, [
		'options' => ['placeholder' => 'Enter event time ...'],
			'pluginOptions' => [
				'autoclose' => true
			]
		])->label(false) ; ?></td>
                <th class="DarkerBlue">Location</th>
                <td><?= $form->field($model, 'location')->textInput()->label(false) ?></td>
                <th class="DarkerBlue">Job No:</th>
                <td><?= $form->field($model, 'jobno')->textInput()->label(false) ?></td>
            </tr>
        </tbody>
        <tr>
            <td colspan="4" style="vertical-align:top; padding-right: 15px;">
                <table class="Table">
                    <tr>
                        <th class="DarkerBlue">Pre-Welding Description</th>
                        <th class="DarkerBlue">Y/N</th>
                        <th class="DarkerBlue">Comments</th>
					</tr>
					<?php $x = 21; ?>
					<?php while($x <= 40 ){ ?>
						<?php if($model->getAttributelabel("response$x".'a') !== "response$x".'a'){ ?>
							<tr>
								<th>
									<?= $model->getAttributelabel("response$x".'a') ?>
								</th>
								<th>
									<?= $form->field($model, "response$x".'a')->dropdownlist(['NO' => 'NO', 'YES' => 'YES'], ['prompt' => 'Please Select', 'id' => "yes$x",])->label(false)  ?>
								</th>
								<td>
									<?= $form->field($model, "response$x".'b')->textArea(['id' => "comment10$x", ])->label(false)  ?>
								</td>
							</tr>
						<?php } ?>
					<?php  $x++ ?>
					<?php  } ?>
                </table>
            </td>
            <td colspan="4" style="vertical-align:top; padding-left: 15px;">
                <table class="Table">
                    <tr>
                        <th class="DarkerBlue">Post-Welding Description </th>
                        <th class="DarkerBlue">Y/N</th>
                        <th class="DarkerBlue">Comments</th>
                    </tr>
					<?php $x = 1; ?>
					<?php while($x <= 20 ){ ?>
						<?php if($model->getAttributelabel("response$x".'a') !== "response$x".'a'){ ?>
							<tr>
								<th>
									<?= $model->getAttributelabel("response$x".'a') ?>
								</th>
								<th>
									<?= $form->field($model, "response$x".'a')->dropdownlist(['NO' => 'NO', 'YES' => 'YES'], ['prompt' => 'Please Select', 'id' => "yes$x",])->label(false)  ?>
								</th>
								<td>
									<?= $form->field($model, "response$x".'b')->textArea(['id' => "comment10$x", ])->label(false)  ?>
								</td>
							</tr>
						<?php } ?>
					<?php  $x++ ?>
					<?php  } ?>
					<tr>
                        <th>Has welding production target been achieved?</th>
                        <td><?= $form->field($model, 'target')->textInput()->label(false) ?></td>
                    </tr>
                    <tr>
                        <td colspan="3">
                            <?= $form->field($model, 'comments')->textArea()->label(false) ?>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tfoot>
            <tr>
                <th class="DarkerBlue" class="font-italic">I confirm that all checks have been carried out:</th>
                <th><?= $form->field($model, "confirmation")->dropdownlist([0 => 'NO', 1 => 'YES'], ['prompt' => 'Please Select'])->label(false)  ?>
                </th>

                <th class="DarkerBlue">Print</th>
                <td><?= $form->field($model, 'signprint')->dropdownlist($users, ['prompt' => 'Please Select'])->label(false) ?>
                </td>
                <th class="DarkerBlue">Signed</th>
                <td>
                    <canvas id="canvas_0" class="canvas"></canvas>
                    <?= $form->field($model, 'signed')->hiddenInput(['id' => 'signature0'])->label(false) ?>
                </td>
                <th class="DarkerBlue">Date</th>

                <td>
                    <?= $form->field($model, 'signeddate')->widget(DatePicker::class, [
					'options' => ['placeholder' => 'Enter event time ...'],
						'pluginOptions' => [
							'autoclose' => true
						]
					])->label(false) ; ?>
                </td>
            </tr>
        </tfoot>
    </table>
    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-primary', 'onclick' => 'saveImages()']) ?>
    </div>

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

</div>
<script>
x = 0;

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