<?php
use modules\custom\models\Accounts;
Use backend\models\Attributesvalues;
use kartik\date\DatePicker;
use kartik\daterange\DateRangePicker;
use kartik\time\TimePicker;
use yii\bootstrap4\Modal;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;

$range = $model::hoursRange( 0, 86400, 60 * 15 );

$this->registerJsFile('/js/moment.js');

$employees = \frontend\models\User::EmployeesU();
        
$trade = Attributesvalues::formarray('trade');

$this->registerJsFile('/js/touch.js');
$this->registerJsFile('/js/touch_2.js');
$this->registerJsFile('/js/touch_3.js');
if (Yii::$app->controller->action->id == 'update') {
    $this->registerJsFile('/js/touch_1.js');
    
$this->registerJs("$(function() {
    var canvas = document.getElementById('canvas');
    if (canvas) {
    var context = canvas.getContext('2d');    
    var imagesrc = document.getElementById('drawingloc').value;
    var image = new Image();
        image.src = imagesrc
        context.drawImage(image, 0, 0);	
    }
    var canvas0 = document.getElementById('canvas_0');
    if (canvas0) {
    var context0 = canvas0.getContext('2d');    
    var imagesrc0 = document.getElementById('signature0').value;
    var image0 = new Image();
        image0.src = imagesrc0
        context0.drawImage(image0, 0, 0);
        console.log('canvas0 loaded');	
    }
    
    canvas1 = document.getElementById('canvas_1');
    if (canvas1) {
        context1 = canvas1.getContext('2d');    
        imagesrc1 = document.getElementById('signatureloc').value;
        image1 = new Image();
        image1.onload = function() {
            context1.drawImage(image1, 0, 0);	
        };
        image1.src = imagesrc1
    }
    
    canvas2 = document.getElementById('canvas_2');
    if (canvas2) {
        context2 = canvas2.getContext('2d');    
        imagesrc2 = document.getElementById('wsignatureloc').value;
        image2 = new Image();
        image2.onload = function() {
            context2.drawImage(image2, 0, 0);	
        };
        image2.src = imagesrc2
    }
    canvas3 = document.getElementById('canvas_3');
    if (canvas3) {
        context3 = canvas3.getContext('2d');    
        imagesrc3 = document.getElementById('signcc').value;
        image3 = new Image();
        image3.onload = function() {
        context3.drawImage(image3, 0, 0);
            };
        image3.src = imagesrc3
    }
        console.log('C3 loaded');
    });
    ");
}
?>
<script>
function addRow(row) {
    i = row + 1;
    btnadd = '#btnadd' + row;
    btnrem = '#btnrem' + row;
    rowa = '.row-' + i;
    status = '#status' + i;
    $(rowa).toggleClass('d-none');
    $(btnadd).toggleClass('d-none');
    $(btnrem).toggleClass('d-none');
    $(status).val('Active');
}

function removeRow(row) {
    i = row;
    j = row - 1;
    btn = '#btn' + j;
    rowa = '.row-' + i;
    rowainputs = '.row-' + i + ' :input';
    status = '#status' + i;
    $(rowa).toggleClass('d-none');
    $(btn).toggleClass('d-none');
    $(status).val('Active');
    $(rowainputs).each(function(i, ev) {
        console.log($(this).val(''));
        $(this).val('');
    });

}
</script>


<div class="dt179-form">
    <?php $form = ActiveForm::begin(['options' => ['id' => 'dynamic-form', 'enctype' => 'multipart/form-data', 'enableClientValidation' => false]]);?>

    <div class="row">
        <div class="col-12 col-md-3">
            <?=$form->field($model, 'client')->dropDownList(Accounts::getActive(), ['id' => 'client', 'prompt' => 'Please Select'])->label()?>
        </div>
        <div class="col-12 col-md-3">
            <?=$form->field($model, 'siterepprint')->textInput([])->label()?>
        </div>

        <div class="col-12 col-md-3">
            <?= $form->field($model, 'date')->widget(DatePicker::class,[
							'type' => DatePicker::TYPE_INPUT, 
							'pluginOptions' => ['format' => 'dd/mm/yyyy', 'todayHighlight' => true]
						])->label('Weekending'); ?>
        </div>
        <div class="col-12 col-md-3">
            <?=$form->field($model, 'weekno')->textInput(['maxlength' => 100])->label('Week No')?>
        </div>

    </div>
    <div class="row">
        <div class="col-12 col-md-3">
            <?=$form->field($model, 'worksite')->textInput(['maxlength' => 100])->label()?>
        </div>
        <div class="col-12 col-md-3">
            <?=$form->field($model, 'clientpo')->textInput(['maxlength' => 100])->label()?>
        </div>
        <div class="col-12 col-md-3">
            <?=$form->field($model, 'supplierjobno')->textInput(['maxlength' => 100])->label()?>
        </div>
        <div class="col-12 col-md-3">
            <?=$form->field($model, 'welddepot')->textInput(['maxlength' => 100])->label()?>
        </div>
    </div>

    <div class="row">
        <div class="col border border-dark text-center">
            ACTUAL ON-SITE WORKING TIMES ONLY MUST BE RECORDED
        </div>
    </div>

    <div class="daterow">
        <div class="row">
            <div class="col-md-4">
                <div class="row">
                    <div class="col-2 col-md-2">
                    </div>
                    <div class="col-10 col-md-6">
                        <span class="float-right">
                            (Insert Date)
                        </span>
                    </div>
                    <div class="col-12 col-md-4">
                        <?= $form->field($model, 'date1')->widget(DatePicker::class, [
							'type' => DatePicker::TYPE_INPUT, 
							'pluginOptions' => ['format' => 'dd/mm/yyyy', 'todayHighlight' => true,
                            'daysOfWeekDisabled' => [0,1,2,3,4,5]]
						])->label('Saturday'); ?>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="row">
                    <div class="col-12 col-md-4">
                        <?= $form->field($model, 'date2')->widget(DatePicker::class, [
							'type' => DatePicker::TYPE_INPUT, 
                            'pluginOptions' => ['format' => 'dd/mm/yyyy', 'todayHighlight' => true,
                            'daysOfWeekDisabled' => [1,2,3,4,5,6],]
						])->label(); ?>
                    </div>
                    <div class="col-12 col-md-4">
                        <?= $form->field($model, 'date3')->widget(DatePicker::class, [
							'type' => DatePicker::TYPE_INPUT, 
							'pluginOptions' => ['format' => 'dd/mm/yyyy', 'todayHighlight' => true,
                            'daysOfWeekDisabled' => [0,2,3,4,5,6]]
						])->label(); ?>
                    </div>
                    <div class="col-12 col-md-4">
                        <?= $form->field($model, 'date4')->widget(DatePicker::class, [
							'type' => DatePicker::TYPE_INPUT, 
							'pluginOptions' => ['format' => 'dd/mm/yyyy', 'todayHighlight' => true,
                            'daysOfWeekDisabled' => [0,1,3,4,5,6]]
						])->label(); ?>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="row">
                    <div class="col-12 col-md-4">
                        <?= $form->field($model, 'date5')->widget(DatePicker::class, [	
							'type' => DatePicker::TYPE_INPUT, 
							'pluginOptions' => ['format' => 'dd/mm/yyyy', 'todayHighlight' => true,
                            'daysOfWeekDisabled' => [0,1,2,4,5,6]]
						])->label(); ?>
                    </div>
                    <div class="col-12 col-md-4">
                        <?= $form->field($model, 'date6')->widget(DatePicker::class, [
							'type' => DatePicker::TYPE_INPUT, 
							'pluginOptions' => ['format' => 'dd/mm/yyyy', 'todayHighlight' => true,
                            'daysOfWeekDisabled' => [0,1,2,3,5,6]]
						])->label(); ?>
                    </div>
                    <div class="col-12 col-md-4">
                        <?= $form->field($model, 'date7')->widget(DatePicker::class, [
							'type' => DatePicker::TYPE_INPUT, 
							'pluginOptions' => ['format' => 'dd/mm/yyyy', 'todayHighlight' => true,
                            'daysOfWeekDisabled' => [0, 1,2,3,4,6,]]
						])->label(); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>



    <?php yii\widgets\Pjax::begin(['id' => 'rows']);?>

    <?php $x = 0 ?>
    <?php while($x < 10){ ?>
    <?php $status = isset($model->rowstatus[$x]) ? $model->rowstatus[$x] : null; ?>
    <?php $y = $x +1; ?>
    <?php $ystatus = isset($model->rowstatus[$y]) ? $model->rowstatus[$y] : null; ?>
    <?php $last = $y < 30 && $ystatus == '' &&  $status !== 'Deleted' ? 'Last' : false; ?>

    <div class="datarows row-<?= $x ?> <?= $x == 0 || empty($status) == false ? '' : 'd-none' ?>">
        <div class="row">
            <div class="col-md-4">
                <div class="row">
                    <div class="col-2 col-md-2">
                        <?=  Html::button('<i class="fa fa-plus"></i>', ['onClick' => "addRow($x)", 'class' => $status == 'Active' && $last !== 'Last' ? "btn btn-info d-none" : "btn btn-info", 'id' => 'btnadd'.$x]); ?>
                        <?=  Html::button('<i class="fa fa-minus"></i>', ['onClick' => "removeRow('$x')", 'class' => $status  !== 'Active' || $last == 'Last' ?  'btn bg-danger text-white d-none' : 'btn bg-danger text-white', 'id' => 'btnrem'.$x ]) ?>
                        <?= $form->field($model, "rowstatus[$x]")->hiddenInput(['id' => 'status'.$x, 'value' => $x == 0 ? 'Active' : $status ])->label(false) ?>
                    </div>
                    <div class="col-10 col-md-6">
                        <div class="input-group">
                            <?=$form->field($model, "rowname[$x]", ['template' => 
                                            '<div class="input-group">{input}'.Html::Activedropdownlist($model, "rowtrade[$x]", $trade, ['prompt' => 'Trade', 'class' => 'form-control']).'
                                            </div>
                                            '])
                                            ->dropDownList($employees, ['prompt' => 'Please Select', 'class' => 'form-control' ])->label(false)?>
                        </div>
                    </div>
                    <div class="col-12 col-md-4">
                        <div class="input-group">
                            <?= $form->field($model, "rowdate1[$x]")->widget(DatePicker::class, [
							'type' => 1, 
							'options' => [
                                'placeholder' => 'Saturday', 
                                'class' => 'd-block d-md-none', 
                            ],
							'pluginOptions' => [
                                'format' => 'dd/mm/yyyy', 
                                'todayHighlight' => true,
                            'daysOfWeekDisabled' => [0,1,2,3,4,5,]]
						])->label(false); ?>
                            <?=$form->field($model, "rowstart1[$x]")->dropdownlist($range, ['prompt' => 'Start'])->label(false);?>
                            <?=$form->field($model, "rowfinish1[$x]")->dropdownlist($range, ['prompt' => 'Finish'])->label(false);?>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="row">
                    <div class="col-12 col-md-4">
                        <div class="input-group w-100">
                            <?= $form->field($model, "rowdate2[$x]")->widget(DatePicker::class, [
							'type' => 1, 
							'options' => [
                                'placeholder' => 'Sunday', 
                                'class' => 'd-block d-md-none', 
                            ],
							'pluginOptions' => [
                                'format' => 'dd/mm/yyyy', 
                                'todayHighlight' => true,
                            'daysOfWeekDisabled' => [1, 2,3,4,5,6,]]
						])->label(false); ?>
                            <?=$form->field($model, "rowstart2[$x]")->dropdownlist($range, ['prompt' => 'Start'])->label(false);?>
                            <?=$form->field($model, "rowfinish2[$x]")->dropdownlist($range, ['prompt' => 'Finish'])->label(false);?>
                        </div>
                    </div>
                    <div class="col-12 col-md-4">
                        <div class="input-group w-100">
                            <?= $form->field($model, "rowdate3[$x]")->widget(DatePicker::class, [
							'type' => 1, 
							'options' => [
                                'placeholder' => 'Monday', 
                                'class' => 'd-block d-md-none', 
                            ],
							'pluginOptions' => [
                                'format' => 'dd/mm/yyyy', 
                                'todayHighlight' => true,
                            'daysOfWeekDisabled' => [0,2,3,4,5,6,]]
						])->label(false); ?>
                            <?=$form->field($model, "rowstart3[$x]")->dropdownlist($range, ['prompt' => 'Start'])->label(false);?>
                            <?=$form->field($model, "rowfinish3[$x]")->dropdownlist($range, ['prompt' => 'Finish'])->label(false);?>
                        </div>
                    </div>
                    <div class="col-12 col-md-4">
                        <div class="input-group w-100">
                            <?= $form->field($model, "rowdate4[$x]")->widget(DatePicker::class, [
							'type' => 1, 
							'options' => [
                                'placeholder' => 'Tuesday', 
                                'class' => 'd-block d-md-none', 
                            ],
							'pluginOptions' => [
                                'format' => 'dd/mm/yyyy', 
                                'todayHighlight' => true,
                            'daysOfWeekDisabled' => [0, 1,3,4,5,6,]]
						])->label(false); ?>
                            <?=$form->field($model, "rowstart4[$x]")->dropdownlist($range, ['prompt' => 'Start'])->label(false);?>
                            <?=$form->field($model, "rowfinish4[$x]")->dropdownlist($range, ['prompt' => 'Finish'])->label(false);?>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="row">
                    <div class="col-12 col-md-4">
                        <div class="input-group w-100">
                            <?= $form->field($model, "rowdate5[$x]")->widget(DatePicker::class, [
							'type' => 1, 
							'options' => [
                                'placeholder' => 'Wednesday', 
                                'class' => 'd-block d-md-none', 
                            ],
							'pluginOptions' => [
                                'format' => 'dd/mm/yyyy', 
                                'todayHighlight' => true,
                            'daysOfWeekDisabled' => [0, 1,2,4,5,6,]]
						])->label(false); ?>
                            <?=$form->field($model, "rowstart5[$x]")->dropdownlist($range, ['prompt' => 'Start'])->label(false);?>
                            <?=$form->field($model, "rowfinish5[$x]")->dropdownlist($range, ['prompt' => 'Finish'])->label(false);?>
                        </div>
                    </div>
                    <div class="col-12 col-md-4">
                        <div class="input-group w-100">
                            <?= $form->field($model, "rowdate6[$x]")->widget(DatePicker::class, [
							'type' => 1, 
							'options' => [
                                'placeholder' => 'Thursday', 
                                'class' => 'd-block d-md-none', 
                            ],
							'pluginOptions' => [
                                'format' => 'dd/mm/yyyy', 
                                'todayHighlight' => true,
                            'daysOfWeekDisabled' => [0, 1,2,3,5,6,]]
						])->label(false); ?>
                            <?=$form->field($model, "rowstart6[$x]")->dropdownlist($range, ['prompt' => 'Start'])->label(false);?>
                            <?=$form->field($model, "rowfinish6[$x]")->dropdownlist($range, ['prompt' => 'Finish'])->label(false);?>
                        </div>
                    </div>
                    <div class="col-12 col-md-4">
                        <div class="input-group w-100">

                            <?= $form->field($model, "rowdate7[$x]")->widget(DatePicker::class, [
							'type' => 1, 
							'options' => [
                                'placeholder' => 'Friday', 
                                'class' => 'd-block d-md-none', 
                            ],
							'pluginOptions' => [
                                'format' => 'dd/mm/yyyy', 
                                'todayHighlight' => true,
                            'daysOfWeekDisabled' => [0, 1,2,3,4,6,]]
						])->label(false); ?>

                            <?=$form->field($model, "rowstart7[$x]")->dropdownlist($range, ['prompt' => 'Start'])->label(false);?>
                            <?=$form->field($model, "rowfinish7[$x]")->dropdownlist($range, ['prompt' => 'Finish'])->label(false);?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php $x++; ?>
    <?php } ?>



    <?php yii\widgets\Pjax::end();?>

    <div class="row">
        <div class="col">30 MINUTES MEAL BREAK TAKEN <br>Mandatory Requirement</div>
        <div class="col">
            <?=$form->field($model, 'break1')->dropdownList(['Yes' => 'Yes', 'No' => 'No'], ['prompt' => 'Please Select'])->label(false)?>
        </div>
        <div class="col">
            <?=$form->field($model, 'break2')->dropdownList(['Yes' => 'Yes', 'No' => 'No'], ['prompt' => 'Please Select'])->label(false)?>
        </div>
        <div class="col">
            <?=$form->field($model, 'break3')->dropdownList(['Yes' => 'Yes', 'No' => 'No'], ['prompt' => 'Please Select'])->label(false)?>
        </div>
        <div class="col">
            <?=$form->field($model, 'break4')->dropdownList(['Yes' => 'Yes', 'No' => 'No'], ['prompt' => 'Please Select'])->label(false)?>
        </div>
        <div class="col">
            <?=$form->field($model, 'break5')->dropdownList(['Yes' => 'Yes', 'No' => 'No'], ['prompt' => 'Please Select'])->label(false)?>
        </div>
        <div class="col">
            <?=$form->field($model, 'break6')->dropdownList(['Yes' => 'Yes', 'No' => 'No'], ['prompt' => 'Please Select'])->label(false)?>
        </div>
        <div class="col">
            <?=$form->field($model, 'break7')->dropdownList(['Yes' => 'Yes', 'No' => 'No'], ['prompt' => 'Please Select'])->label(false)?>
        </div>
    </div>
    <div class="row">
        <div class="col-12 col-md-4 border border-dark">
            <div class="row">
                <div class="col-12">
                    CLIENTS CONFIRMATION ON SITE BRIEFINGS UNDERTAKEN <br>
                    Rule Book Requirements <br>
                    I certify that the above persons have been <br>
                    briefed in accordance with the relevant sections of the Modular Rule Book <br>
                    (A Safe System of Work Briefing and the relevant Health, <br>
                    Safety and Welfare arrangements have been briefed)
                </div>
            </div>
            <div class="row">
                <div class="col-12">
                    <label class="control-label">SIGNED: </label>
                    <div id="sign2">
                        <canvas id="canvas_2" class="canvas1"></canvas>

                        <?=Html::button('Reset', ['class' => 'btn btn-primary', 'onclick' => 'ClearCanvasX(2);']);?>

                        <?=$form->field($model, 'signbc')->hiddenInput(['id' => 'wsignatureloc'])->label(false)?>

                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-12">

                    <?=$form->field($model, 'namebc')->textInput(['maxlength' => 100])->label('Print Name')?>
                    <?= $form->field($model, 'datebc')->widget(DatePicker::class,[
							'type' => DatePicker::TYPE_INPUT, 
							'pluginOptions' => ['format' => 'dd/mm/yyyy', 'todayHighlight' => true]
						])->label('Date'); ?>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-4 border border-dark">

            <div class="row">
                <div class="col-12">
                    RECORD OF HOURS ON SITE SUPPLIER’S CONFIRMATION

                </div>
            </div>

            <div class="row">
                <div class="col-12">
                    <label class="control-label">SIGNED: </label>
                    <div id="sign1" class="<?= $this->context->action->id == 'update' ? 'd-block' : 'd-none' ?>">
                        <canvas id="canvas_1" class="canvas1"></canvas>
                        <?=Html::button('Reset', ['class' => 'btn btn-primary', 'onclick' => 'ClearCanvasX(1);']);?>
                        <?=$form->field($model, 'signsc')->hiddenInput(['id' => 'signatureloc'])->label(false);?>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-12">
                    <?=$form->field($model, 'namesc')->textInput(['maxlength' => 100])->label('PRINT NAME')?>
                    <?= $form->field($model, 'datesc')->widget(DatePicker::class,[
							'type' => DatePicker::TYPE_INPUT, 
							'pluginOptions' => ['format' => 'dd/mm/yyyy', 'todayHighlight' => true]
						])->label('Date'); ?>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-4 border border-dark">
            <div class="row">
                <div class="col-12">
                    <label class="control-label">CLIENT CONFIRMATION SIGNED: </label>
                    <div id="sign3">
                        <canvas id="canvas_3" class="canvas1"></canvas>
                        <?=Html::button('Reset', ['class' => 'btn btn-primary', 'onclick' => 'ClearCanvasX(3);']);?>
                        <?=$form->field($model, 'signcc')->hiddenInput(['id' => 'signcc'])->label(false)?>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-12">
                    <?=$form->field($model, 'namecc')->textInput(['maxlength' => 100])->label('PRINT NAME')?>
                    <?= $form->field($model, 'datecc')->widget(DatePicker::class,[
						'type' => DatePicker::TYPE_INPUT, 
						'pluginOptions' => ['format' => 'dd/mm/yyyy', 'todayHighlight' => true]
					])->label('Date'); ?>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-12 col-md-3 border border-dark">
            <?=$form->field($model, 'comments')->textArea(['rows' => 4])->label('Comments')?>
        </div>
    </div>
    <div class="form-group">

        <?=Html::submitButton('Save', ['class' => 'btn btn-primary', 'onclick' => 'saveImage9()'])?>

    </div>

    <?php ActiveForm::end();?>
    <?php Modal::begin(['id' => 'modal', 'size' => 'modal-lg']);
echo "<div id='modalContent'></div>";
Modal::end();
?>
</div>
<?php if (Yii::$app->controller->action->id == 'update') {?>
<script>
function saveImage9() {
    $("#wsignatureloc").val(document.getElementById('canvas_2').toDataURL());
    $("#signcc").val(document.getElementById('canvas_3').toDataURL());
    $('#signatureloc').val(document.getElementById('canvas_1').toDataURL());
}

$(function() {
    init1();
    init2();
    init3();
});
</script>
<?php } else {?>
<script>
function saveImage9() {
    $("#wsignatureloc").val(document.getElementById('canvas_2').toDataURL());
    $("#signcc").val(document.getElementById('canvas_3').toDataURL());
}
$(function() {
    init2();
    init3();
});
</script>
<?php }?>

<style>
.sftime .form-group {
    width: 50%;
    display: flex;

}
</style>