<?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;
use yii\helpers\Url;
use kartik\datetime\DateTimePicker;
use app\widgets\ButtonGroupInput;
use yii\widgets\DetailView;
use yii\helpers\Json;

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

$unserializeJson = Json::encode(json_decode($model->serialize_data, true)); 

$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();
    });

    $('input[name="type"]').on('change', function(e){        
        $(".circle_down").hide();
        if($(this).val() == '2'){
            $(".circle_down").show();
        }
    });

    var concat_val = '';
    var value1 = value2 = value3 = 0;

    $("#button-save2").on("click",function(e){
        step = $("#input-save0").val();
        val1 = $("#input-save1").val();
        val2 = $("#input-save2").val();
        val3 = $("#input-save3").val();
        value1 = 0;
        if(val1 > 0){
            value1 = val1;
        }
        value2 = 0;
        if(val2 > 0){
            value2 = val2;
        }
        value3 = 0;
        if(val3 > 0){
            value3 = val3;
        }     
        concat_val = value1 + ":"+ value2 + ":" + value3;
        // if(concat_val.length > 0){
        //     alert(concat_val);
        // }
        $("#lead").html('кроки:'+step+', час:'+concat_val);
    });    
    
    $(document).ready(function () {
        var settings = {$unserializeJson};    
        if (settings) {
            $('input[name=\"time_cont\"][value=\"'+settings.time_cont+'\"]').prop('checked', true);
            $('input[name=\"type\"][value=\"'+settings.type+'\"]').prop('checked', true).change();

            $("#input-save0").val(settings.step_change);
            $("#input-save1").val(settings.time_step_change.split(':')[0]);
            $("#input-save2").val(settings.time_step_change.split(':')[1]);
            $("#input-save3").val(settings.time_step_change.split(':')[2]);

            $("#button-save2").click();
        }        
    });

    //====

    function serializeData() {
        return {
            time_cont: $('input[name="time_cont"]:checked').val(),
            type: $('input[name="type"]:checked').val(),
            step_change: step,
            time_step_change: concat_val,
            date_stop: $("#date_stop").val(),
        };
    }

    $('form').on('submit', function () {
        var data = serializeData();        
        $('#serialize_data').val(JSON.stringify(data));
    });
JS
);

//=========================

$time_change = Html::button(
    Yii::t('app', 'Enter value'), 
    [
        'style' => 'font-weight: bold; text-transform: uppercase; color: black', 
        'class' => 'btn btn-info btn-sm',
        'data-toggle' => 'modal', 
        'data-target' => '#myModal2'
    ]
);
?>

<!-- Modal -->
<div class="modal fade" id="myModal2" role="dialog">
    <div class="modal-dialog modal-sm">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title"><?= Yii::t('app', 'Circle ID') ?></h4>
            </div>
            <div class="modal-body">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="form-group">
                                <label for=""><?= Yii::t('app', 'Кроки'); ?></label>
                                <input type="number" class="form-control" placeholder="Кроки" min="0" max="100" value="3" id="input-save0">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-xs-4">
                            <div class="form-group">
                                <label for=""><?= Yii::t('app', 'Години'); ?></label>
                                <input type="number" class="form-control" placeholder="Години" min="0" max="23" value="0" id="input-save1">
                            </div>
                        </div>
                        <div class="col-xs-4">
                            <div class="form-group">
                                <label for=""><?= Yii::t('app', 'Хвилини'); ?></label>
                                <input type="number" class="form-control" placeholder="Хвилини" min="0" max="59" value="10" id="input-save2">
                            </div>
                        </div>
                        <div class="col-xs-4">
                            <div class="form-group">
                                <label for=""><?= Yii::t('app', 'Секунди'); ?></label>
                                <input type="number" class="form-control" placeholder="Секунди" min="0" max="59" value="0" id="input-save3">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <?= Html::button(Yii::t('app', 'Save'), ['class' => 'btn btn-default', 'id' => 'button-save2', 'data-dismiss' => 'modal']) ?>
                <button type="button" class="btn btn-default" data-dismiss="modal"><?= Yii::t('app', 'Close') ?></button>
            </div>
        </div>
    </div>
</div>

<div class="lots-form">
    <?php
    $serialize_data = json_decode($model->serialize_data);
    $files = new \app\models\Files();

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

    <?= $form->field($model, 'serialize_data')->hiddenInput(['id' => 'serialize_data'])->label(false) ?>
    
    <?= $form->field($model, 'user_id')->hiddenInput(['value' => Yii::$app->user->identity->id])->label(false) ?>
    
    <div class="auclots-form">
        <div class="row">
            <div class="col-sm-3">
                <label for=""><?= Yii::t('app', 'Час продовження, хв'); ?></label>
                <br>
                <?= ButtonGroupInput::widget([
                    'name' => 'time_cont',
                    'items' => [
                        '00:05' => Yii::t('app', '5 хвилин'), 
                        '00:10' => Yii::t('app', '10 хвилин')
                    ],
                    'value' => isset($serialize_data->time_cont) ? $serialize_data->time_cont : '00:05',
                ]); ?>
            </div>
            <div class="col-sm-5">
                <label for="type"><?= Yii::t('app', 'Тип аукціону'); ?></label>
                <br>
                <?= ButtonGroupInput::widget([
                    'name' => 'type',
                    'items' => [
                        '1' => Yii::t('app', 'Підвищення'),
                        '2' => Yii::t('app', 'Голландський'),
                        '3' => Yii::t('app', 'Банкрутство'),
                    ],
                    'value' => isset($serialize_data->type) ? $serialize_data->type : 1,
                ]); ?>
            </div>
            <div class="col-sm-4">
                <label for="date_stop"><?= Yii::t('app', 'Дата закінчення'); ?></label>                                
                <br>
                <?= DateTimePicker::widget([
                    'layout' => '{picker}{input}',
                    'value' => isset($serialize_data->date_stop) ? $serialize_data->date_stop : date('Y-m-d H:i', time() + 360),                    
                    'name' => 'date_stop',
                    'id' => 'date_stop',
                    'options' => ['placeholder' => Yii::t('app', 'DateStop ID')],
                    'pluginOptions' => [
                        'autoclose' => true,
                        'format' => 'yyyy-mm-dd hh:ii',
                    ],
                ]); ?>
            </div>
        </div>

        <style>.circle_down{display:none;margin:0px}</style>
        <?= DetailView::widget([
            'options' => [
                'class' => 'table table-striped table-bordered detail-view circle_down'
            ],
            'model' => $model,
            'attributes' =>
                [
                    [
                        'label' => Yii::t('app', 'Circle ID'),
                        'value' => Html::tag('span', 'кроки 0, час 00:00:00', ['class' => 'lead','id'=>'lead']) . " " . $time_change,
                        'format' => 'raw',
                    ],
                ],
        ]); ?>        

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

        <?php $requisites = $model->allRequisites; ?>
        <?php if (!$requisites) { ?>
        <div class="alert alert-danger alert-dismissible fade in">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <?= Yii::t('app', 'noRequisites ID')?>
        </div>
        <?php } ?>
        
        <div class="row">
            <div class='col-md-8'>
                <?= $form->field($model, 'requisites_id')->dropdownList($requisites) ?>
            </div>
            <div class='col-md-3' id="addReq">
                <label>|</label>
                <?= Html::button(Yii::t('app', 'Create Requisites'), ['value' => Url::to('/requisites/create-ajax'), 'class' => 'btn btn-success btn-block', 'id' => 'modalButton']) ;?>
            </div>
        </div>

        <?= $form->field($files, 'project_dogovor')->widget(FileInput::classname(), [
            'model' => $files,
            'pluginOptions' => [
                //'allowedFileExtensions'=>['doc','docx','pdf', 'png', ],
                'initialCaption'=>'*.doc *.docx *.pdf *.png *.jpg *.tiff, *.zip *rar',
                'showPreview' => false,
                'showCaption' => true,
                'showRemove' => true,
                'showUpload' => false
            ],
            'options' => ['multiple' => false,],
        ])->label(Yii::t('app','ProjectDogovor ID')); ?>

        <?= $form->field($model, 'bidding_date')->widget(DateTimePicker::className(),[
            'name' => 'bidding_date',
            'type' => DateTimePicker::TYPE_COMPONENT_PREPEND,
            'value' => date("dd-M-yyyy hh:ii"),
            //'options' => ['placeholder' => Yii::t('app','')],
            'pluginOptions' => [
                'autoclose'=>true,
                'format' => 'yyyy-mm-dd hh:ii'
            ]
        ]) ?>

        <?= $form->field($model, 'auction_date')->widget(DateTimePicker::className(),[
            'name' => 'auction_date',
            'type' => DateTimePicker::TYPE_COMPONENT_PREPEND,
            'value' => date("dd-M-yyyy hh:ii"),
            'options' => ['placeholder' => Yii::t('app','CreateAukDate ID')],
            'pluginOptions' => [
                'autoclose'=>true,
                'format' => 'yyyy-mm-dd hh:ii'
            ]
        ]) ?>

    </div>

    <hr>

    

    <?php /*echo $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, 'member_require')->textarea(/*['maxlength' => 5000, 'rows' => 50, 'cols' => 100]*/) ?>    

    <?= $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); ?>

    <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>
