<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use kartik\file\FileInput;
use app\models\Units;
use app\models\Category;

/* @var $this yii\web\View */
/* @var $model app\models\Lots */
/* @var $model app\models\Category */
/* @var $form yii\widgets\ActiveForm */

$this->registerJs(<<<JS
    function recalculateSum(){
        $('#count-input').val($('#count-input').val());
        $('#lots-price_per_one').val($('#lots-price_per_one').val());
        var value = $('#lots-price_per_one').val() * $('#count-input').val();
        $('#lots-start_price').val(value || '');
    }

    $('input[name="price_type"]').on('change', function(e){
        var type = $('input[name="price_type"]:checked').val();
        $('.per_one input, .per_one select').prop('readonly', type === 'total' ? 'readonly': false);
        $('#lots-start_price').prop('readonly', type === 'total' ? false : 'readonly');
        $('.per_one input, .per_one select').val('');
    });
    $('#count-input, #lots-price_per_one').on('keyup', function(e){
        recalculateSum();
    });
JS
);
?>
<div class="lots-form">

    <?php
    $files = new \app\models\Files();

    $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?>

    <?= $form->field($model, 'user_id')->hiddenInput(['value' => Yii::$app->user->identity->id]) ?>

    <?= $form->field($model, 'aukname')->dropDownList(ArrayHelper::map(\app\models\Auclots::find()
        ->where(['user_id' => Yii::$app->user->identity->id])->orderBy('id DESC')->all(), 'name', 'name')) ?>

    <!--  ivakhnov  -->
    <?= $form->field($model, 'num')->textInput(['maxlength' => 255])->label(Yii::t('app','AucNumber ID')) ?>

    <?= $form->field($model, 'category_id')->dropDownList(ArrayHelper::map(Category::find()->all(), 'id', 'name')) ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => 255])->label(Yii::t('app','LotName ID')) ?>

    <?= $form->field($model, 'description')->textarea(['maxlength' => 15000, 'rows' => 6, 'cols' => 50]) ?>

    <div class="row">
        <div class="col-sm-12">
            <div class="row">
                <div class="col-sm-3">
                    <label class="price_type-input">
                        <?= Yii::t('app', 'За одиницю'); ?>
                        <?= Html::radio('price_type', true, ['value' => 'per_one']); ?>
                    </label>
                </div>
                <div class="col-sm-9 per_one">
                    <div class="row">
                        <div class="col-sm-4">
                            <div class="form-group">
                                <label for=""><?= Yii::t('app', 'Кількість'); ?></label>
                                <?= Html::activeInput('number', $model, 'count', [
                                    'class' => 'form-control',
                                    'id' => 'count-input',
                                    'min' => 1,
                                    'max' => 99999999999999999999999,
                                    'step' => 1,
                                    'readonly' => 'readonly',
                                ]); ?>
                            </div>
                        </div>
                        <div class="col-sm-4">
                            <?= $form->field($model, 'price_per_one', ['inputOptions' => ['readonly' => 'readonly', 'class' => 'form-control']])->label(Yii::t('app', 'Ціна за одиницю')); ?>
                        </div>
                        <div class="col-sm-4">
                            <div class="form-group">
                                <label><?= Yii::t('app', 'Одиниці виміру'); ?></label>
                                <?= Html::activeDropDownList($model, 'unitId', ArrayHelper::merge(['' => Yii::t('app', 'Оберіть')], ArrayHelper::map(Units::find()->all(), 'id', 'name')), [
                                    'class' => 'form-control',
                                    'readonly' => 'readonly',
                                ]); ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-12">
            <div class="row">
                <div class="col-sm-3">
                    <label class="price_type-input">
                        <?= Yii::t('app', 'Загальна вартість'); ?>
                        <?= Html::radio('price_type', true, ['value' => 'total']); ?>
                    </label>
                </div>
                <div class="col-sm-9 total">
                    <?= $form->field($model, 'start_price')->textInput()->label(Yii::t('app', 'Загальна віртість')) ?>
                </div>

                <div class="col-sm-12 total">
                    <?= Html::tag('label', Yii::t('app', 'Download'), ['class' => 'control-label']);

                    echo FileInput::widget([
                        'model' => new \app\models\Lots,
                        'attribute' => 'files[]',
                        'options' => [
                            'multiple' => true,
                        ],
                        'pluginOptions' => [
                            'showPreview' => true,
                            'showRemove' => true,
                            'showUpload' => false,
                            'showCaption' => true,
                        ],
                    ]);?>
                </div>

            </div>
        </div>
    </div>


    <?= $form->field($model, 'nds')->checkbox(['checked ' => true])  ?>

    <?= $form->field($model, 'step')->textInput(['maxlength' => true]) ?>



    <?= $form->field($files, 'file[]')->widget(FileInput::classname(), [
        'model' => $files,

        'pluginOptions' => [
            //'allowedFileExtensions'=>['doc','docx','pdf'],
            'initialCaption'=>'*.doc, *.docx, *.pdf *.png *.jpg *.tiff *.zip *.rar',
            'showPreview' => true,
            'showCaption' => true,
            'showRemove' => true,
            'showUpload' => false,
            'maxFileCount' => 10,
        ],
        'options' => [
            'multiple' => true,
        ],
    ])->label(Yii::t('app','DopDocumentation ID')); ?>

    <?= $form->field($model, 'payment_term')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'term_procedure')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'address')->textInput(['maxlength' => true]) ?>

    <!--

    <?= $form->field($model, 'delivery_time')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'delivery_term')->textarea(['maxlength' => 800, 'rows' => 6, 'cols' => 50]) ?>

    <?= $form->field($model, 'requires')->textarea(['maxlength' => 800, 'rows' => 6, 'cols' => 50]) ?>

    <?= $form->field($model, 'member_docs')->textarea(['maxlength' => 800, 'rows' => 6, 'cols' => 50]) ?> --!>

    <?= $form->field($model, 'notes')->textarea(['maxlength' => 15000, 'rows' => 6, 'cols' => 50]) ?>

    <?= $form->field($model, 'date')->hiddenInput(['value' => date('Y-m-d H:i:s')])->label(false); ?>

<!--    <?= $form->field($model, 'status')->textInput() ?> --!>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create ID') : Yii::t('app', 'Update ID'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

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



</div>
