<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use kartik\time\TimePicker;
use kartik\datetime\DateTimePicker;
use kartik\date\DatePicker;
Use backend\models\Attributesvalues;
use modules\custom\models\{Accounts, Contacts};

use yii\widgets\Pjax;
use yii\helpers\Url;
$model->company == '5' ? $model->siteinstructions = "Please log in with MAC RAIL on all COLAS RAIL  sites  - 01934 319 810<br>COSS contact:" : false;

$users = ArrayHelper::map(\frontend\models\User::find()->where(['not', ['in', 'username', ['Admin', 'HelenMoore']]])->andWhere(['is', 'blocked_at', null])->orderby(['username' => SORT_ASC])->all(), '_id', 'username');
$users2 = ArrayHelper::map(\frontend\models\User::find()->where(['=', "username", 'SebThomson'])->orderby('username')->all(), '_id', 'username');
$names1 = ArrayHelper::map(Contacts::find()
    ->filterwhere(['accountid' => $model->companyint])
    ->andWhere(['status' => 'Active'])
    ->orderby('firstname ASC')
    ->all(),'_id', 'fullname', 'contacttype');
$model->planneddate = $model->planneddate > 0 ? date('d/m/Y', $model->planneddate) : $model->planneddate;
$model->amendmentdate = $model->amendmentdate > 0 ? date('d/m/Y', $model->amendmentdate) : $model->amendmentdate;
$model->signdate = $model->signdate > 0 ? date('d/m/Y', $model->signdate) : $model->signdate;
    
//$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 ?>

<script>
window.onload = function() {
    //init(); //Drawing
    init1(); //canvas_1
    //init2(); //canvas_2
    //init3(); //canvas_3
}
</script>
<div class="row">
    <div class="col-md-12" id="form">
        <?php $form = ActiveForm::begin(); ?>
        <table class="table table-borderless">
            <tr>
                <td class="DarkerBlueC" colspan="8" align="center">KEY SITE CONTACT DETAILS</td>
            </tr>
            <tr>
                <td class="DarkerBlueC">COMPANY</td>
                <td colspan="3" class="DarkerBlueC">NAME</td>
                <td class="DarkerBlueC">GRADE</td>
                <td class="DarkerBlueC">CONTACT NO</td>
                <td class="DarkerBlueC">Email Address</td>
                <td class="DarkerBlueC" align="right">
                    <?= HTML::Button('Add Contact', ['id' => 'contactbutton', 'onclick' => 'contactsbuttonaction()', 'class' => 'btn btn-primary' ])?>
                </td>
            </tr>
            <tr>
                <td><?= $form->field($model, 'company')->dropDownList(Accounts::getActive(), ['id' => 'company', 'prompt' => 'Please Select...']) ->label(false)?>
                </td>
                <td colspan="3">
                    <?= $form->field($model, 'name1')->dropdownlist($names1, ['id'=>'name1', 'prompt' => 'Please Select...'])->label(false);?>


                </td>
                <td><?= $form->field($model, 'grade1')->textInput(['maxlength' => 45, 'id' => 'grade1']) ->label(false)?>
                </td>
                <td><?= $form->field($model, 'contactno1')->textInput(['maxlength' => 45, 'id' => 'contactno1' ]) ->label(false)?>
                </td>
                <td colspan="2">
                    <?= $form->field($model, 'email1')->textInput(['maxlength' => 1000, 'id' => 'email1']) ->label(false)?>
                </td>

            </tr>
            <tr>
                <td> &nbsp </td>
                <td colspan="3">
                    <?= $form->field($model, 'name2')->dropdownlist($names1, ['id'=>'name2', 'prompt' => 'Please Select...'])->label(false);?>
                <td><?= $form->field($model, 'grade2')->textInput(['maxlength' => 45,  'id' => 'grade2']) ->label(false)?>
                </td>
                <td><?= $form->field($model, 'contactno2')->textInput(['maxlength' => 45, 'id' => 'contactno2']) ->label(false)?>
                </td>
                <td colspan="2">
                    <?= $form->field($model, 'email2')->textInput(['maxlength' => 1000, 'id' => 'email2']) ->label(false)?>
                </td>
            </tr>

        </table>
        <div class="row">
            <div class="col-md-4">
                <?= $form->field($model, 'facilities_provider')->textInput([]) ->label()?>
            </div>
            <div class="col-md-8">
                <?= $form->field($model, 'facilities_notes')->textArea(['rows' => 3]) ->label()?>
            </div>
        </div>
        <table class="table table-borderless">
            <tr>
                <th class="DarkerBlue">Planned date agreed with customer:</th>
                <th class="DarkerBlue">Amendments agreed with customer:</th>
            </tr>
            <tr>
                <td><?= $form->field($model, 'planneddate')->widget(DatePicker::class, ['convertFormat' => true, 'pluginOptions' => [
        'format' => 'dd-MM-yyyy','autoclose'=>true,]])->label(false) ?></td>
                <td><?= $form->field($model, 'amendmentdate')->widget(DatePicker::class, ['convertFormat' => true, 'pluginOptions' => [
        'format' => 'dd-MM-yyyy','autoclose'=>true,]])->label(false) ?></td>
            </tr>
            <tr>
                <th class=DarkerBlue colspan="3">Site Instructions</th>
            </tr>
            <tr>
                <td colspan="3">
                    <?= $form->field($model, 'siteinstructions')->textArea(['rows' => '3','maxlength' => 1000]) ->label(false)?>
                </td>
            </tr>

            <tr class="DarkerBlueC">
                <th>ILT H&S Contact</th>
                <th>On Call Manager</th>
            </tr>
            <tr>
                <td><?= $form->field($model, 'hscontact')->dropdownlist($users, ['prompt' => 'Please Select'])->label(false)?></td>
                <td><?= $form->field($model, 'oncallman')->dropdownlist($users2, ['prompt' => 'Please Select'])->label(false)?></td>
            </tr>
            <tr>
                <th class="DarkerBlueC" colspan="3">Further Information</th>
            </tr>
            <tr>
                <td colspan="3"><?= $form->field($model, 'furtherinfo')->textArea(['maxlength' => 150]) ->label(false)?>
                </td>
            </tr>
            <tr class="DarkerBlue">
                <th>Work Instruction and Review approved by:</th>
                <th>Date</th>
            </tr>
            <tr>
                <td>
                    <div id="canvas1">
                        <canvas id="canvas_1" class="canvas">Signature</canvas>
                    </div>
                    <?= $form->field($model, 'sign')->hiddenInput(['id' => 'sign']) ->label(false)?>

                    <?= Html::button('Reset', [ 'class' => 'btn btn-primary', 'onclick' => 'ClearCanvasX(1);' ]); ?>
                </td>
                <td valign="top"><?= $form->field($model, 'signdate')->widget(DatePicker::class, ['convertFormat' => true, 'pluginOptions' => [
        'format' => 'dd-MM-yyyy','autoclose'=>true,]])->label(false) ?></td>
            </tr>
        </table>

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

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

    </div>
</div>
<script>
function getdetails(id) {
    var Id = $(this).val();
    $.get('/contacts/getdetails', {
        id: Id
    }, function(data) {
        var data = $.parseJSON(data);
        $('#contactno'.id).attr('value', data.mobileno);
        $('#grade'.id).attr('value', data.contacttype);
        $('#email'.id).attr('value', data.email);
    });
};

function refreshc2(val) {
    $.ajax({
            type: "POST",
            url: "/was/ajaxupdate",
            data: {
                id: "<?= $model->id ?>",
                value: val
            },
        })
        .done(function(result) {
            $.pjax.reload({
                container: '#namesgroup2'
            });
            console.log('triggered');
        });
}
$('#name1').change(function() {
    var Id = $(this).val();
    $.get('/contacts/getdetails', {
        id: Id
    }, function(data) {
        var data = $.parseJSON(data);
        console.log(data);
        $('#contactno1').attr('value', data.mobileno);
        $('#grade1').attr('value', data.contacttype);
        $('#email1').attr('value', data.email);
    });
});
$('#name2').change(function() {
    var Id = $(this).val();
    $.get('/contacts/getdetails', {
        id: Id
    }, function(data) {
        var data = $.parseJSON(data);
        $('#contactno2').attr('value', data.mobileno);
        $('#grade2').attr('value', data.contacttype);
        $('#email2').attr('value', data.email);
    });
});

function contactsbuttonaction() {
    window.open("/contacts/create", '', 'height=' + screen.height, 'width=' + screen.width, 'fullscreen=yes');
}

function saveImage9() {
    $("#sign").val(document.getElementById('canvas_1').toDataURL());
}
</script>