<?php

use modules\forms\models\Dt127rows;
use modules\users\models\User;
use backend\models\Attributesvalues;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use common\widgets\TinyMce;

$model->heldby == null ? $model->heldby = 1 : false;
$model->date == null ? $model->date = date('d/m/Y', strtotime('now')) : false;

$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}';
$tinyInput = '<div class="input-group">
	<div class="input-group-append w-100 my-auto">
        <span class="input-group-text DarkerBlue w-100">
            {label}
        </span>
	</div>
	{input}
	</div>{hint}{error}';

$users = User::Employeesu();
/* @var $this yii\web\View */
/* @var $model modules\forms\models\Dt127 */
/* @var $form yii\widgets\ActiveForm */
//load canvas drawing
?>

<script>
function addRow(row) {
$.ajax({
    url:'/forms/addsignature',
    type:'get',
    data:{id: '<?= $model->id ?>'},
    success: function(result, b, c){
        $('#signatures').append(result.body);
    }
})
}

function removeRow(id, row) {
    $.ajax({
    url:'/forms/removesignature',
    type:'get',
    data:{id: id},
    success: function(result, b, c){
        $('#signature-'+row).remove();
    }
})

}
</script>


<div class="dt127-form">
    <?php $form = ActiveForm::begin();?>

    <table class="Table">
        <tr>

            <td><img src="/images/logo.jpg" alt="logo"></img></td>
            <td><?= $formModel->formname ?></td>
            <td class="pb-2">Inline Track Welding Ltd<br />
                Ashmill Business Park<br />
                Ashford Rd Lenham<br />
                Maidstone Kent<br />
                ME17 2GQ</td>

        </tr>
        <tr>
            <td colspan=""><?=$form->field($model, 'date', ['template' => $textInput])->textInput()->label()?></td>
            <td colspan=""><?=$form->field($model, 'location', ['template' => $textInput])->textInput()->label()?></td>
            <td colspan="">
                <?=$form->field($model, 'heldby', ['template' => $textInput])->dropdownlist($users, ['prompt' => 'Please Select'])->label()?>
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <?=$form->field($model, 'title', ['template' => $textInput])->textInput()->label()?>
            </td>
        </tr>

        <tr>
            <td colspan="3">
                <?=$form->field($model, 'subjects', ['template' => $tinyInput])->widget(TinyMce::class, [])->label();?>
            </td>
        </tr>
    </table>
    <div id="signatures">
        <?php foreach($signatures as $i => $signature) { ?>
			<?php if(empty($signature->name) == false){ ?>			
				<?= $this->render('signature', ['signature' => $signature, 'form' => $form, 'model' => $model, 'i' => $i, 'users' => $users]); ?>
			<?php } ?>
		<?php } ?>
    </div> 
    <div class="row mb-5">
        <div class="col-12 text-left">

            <?=  Html::button('<i class="fa fa-plus"></i> Add Another', ['onClick' => "addRow(1)", 'class' => "btn btn-info", 'id' => 'btnadd']); ?>
        </div>
    </div>

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

    <?php ActiveForm::end();?>
</div>
<script>
x = 0;
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>
<style>
.dt127-form .tox.tox-tinymce {
    width: 100%;
}

.dt127-form .control-label {
    background-color: #8DB4E2;
}
</style>