<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use kartik\file\FileInput;
use yii\widgets\ActiveForm;
use kartik\datetime\DateTimePicker;
use yii\helpers\Url;
use app\models\Files;
use app\widgets\ButtonGroupInput;

/* @var $this yii\web\View */
/* @var $model app\models\Lots */

///$mid = isset($model->id) ? $model->id : 0;

$url = Url::to(['confirm', 'id' => $model->id]);
$this->registerJs(<<<JS

    $('input[name="status"]').on('change', function(e){
        var self = $(this);
        $.get('$url' + '&status=' + self.val(), function(data){
            if(data !== 1){
                window.location.reload();
            }
        });
    });

// $("#lot-form").on("beforeSubmit", function(e){
//     if($("input[name=\"status\"]:checked").val() != 2){
//         alert("' . Yii::t('app', 'Необхідно узгодити лот') . '");
//         return false;
//     }
// }); 
  
JS
);

$this->title = $model->name;
if(Yii::$app->user->can('member')) {
    $this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Auction ID'), 'url' => ['/auctions/index']];
} else {
    $this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Lots ID'), 'url' => ['index']];
}

$this->params['breadcrumbs'][] = $this->title;


$status = ButtonGroupInput::widget([
    'name' => 'status',
    'items' => [
        '1' => Yii::t('app', 'Обробляється'),
        '2' => Yii::t('app', 'Узгоджено'),
        '3' => Yii::t('app', 'Відхилено'),

    ],
    'value' => $model->status
]);


if($model->docs_id) {
    $docs = Html::a(Yii::t('app', 'Download ID'), ['/files/download', 'id' => $model->docs_id], ['class' => 'glyphicon glyphicon-circle-arrow-down']);
} else {
    $docs = NULL;
}

if($model->dogovor_id) {
    $dogovor = Html::a(Yii::t('app', 'Download ID'), ['/files/download', 'id' => $model->dogovor_id], ['class' => 'glyphicon glyphicon-circle-arrow-down']);
} else {
    $docs = NULL;
}
if($model->nds == 1) {
    $price_nds = $model->start_price . " " . Yii::t('app', 'zNDS ID');
} else {
    $price_nds = $model->start_price . " " . Yii::t('app', 'bNDS ID');
}
$registration_fee = 120.00 . 'грн ' . Yii::t('app', 'zNDS ID');//$model->start_price * 0.0015;
$guarantee_fee = $model->start_price * 0.015;

$serialize_data = json_decode($model->serialize_data);

$parts = array_pad(explode(':', $serialize_data->time_step_change), 3, 0);
$timeStepFormat = sprintf(
    "%02d:%02d:%02d.00000",
    (int)$parts[0],
    (int)$parts[1],
    (int)$parts[2]
);

?>

<!-- Modal by Ivakhnov -->
<div class="panel panel-primary">
    <div class="panel-heading">
        <span class="glyphicon glyphicon-stats"></span>
        <strong><?= Html::encode($this->title) ?></strong>
        
        <?php if(Yii::$app->user->id == $model->user_id || Yii::$app->user->can('admin')): ?>
            <?= Html::a(Yii::t('app', 'Update ID'), ['update', 'id' => $model->id], [
            'class' => 'btn btn-warning']); ?>

            <?= Html::a(Yii::t('app', 'Delete ID'), ['delete', 'id' => $model->id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'),
                'method' => 'post',
            ]]); ?>
        <?php endif; ?>

    </div>
    
    <div class="panel-body">
        <div class="lots-view">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <?= $this->render('_alert.php') ?>
                        <?php
                        if((Yii::$app->user->id == $model->user_id) || Yii::$app->user->can('admin'))
                        {
                        ActiveForm::begin([
                            'action' => ['/lots/auction', 'id' => $model->id, 'type' => 1],
                            'id' => 'lot-form',
                        ]); ?>

                        <?= DetailView::widget([
                            'options' => [
                                'class' => 'table table-striped table-bordered detail-view'
                            ],
                            'model' => $serialize_data,
                            
                            'attributes' =>
                                [
                                    [
                                        'label' => 'Тип аукціону',
                                        'value' => function ($model){
                                            if($model->type == 1){
                                                return 'Підвищення';
                                            }
                                            if($model->type == 2){
                                                return 'Голландський';
                                            }
                                            if($model->type == 3){
                                                return 'Банкрутство';
                                            }                                                                                        
                                        },
                                        'format' => 'raw',
                                    ],
                                    [
                                        'label' => 'Дата закінчення',
                                        'value' => $serialize_data->date_stop,
                                        'format' => 'raw',
                                    ],
                                    [
                                        'label' => 'Час продовження, хв',
                                        'value' => explode(":",$serialize_data->time_cont)[1]." хв",
                                        'format' => 'raw',
                                    ],
                                ],
                        ]); ?>

                        <?php if($serialize_data->type == 2): ?>
                        <?= DetailView::widget([
                            'options' => [
                                'class' => 'table table-striped table-bordered detail-view'
                            ],
                            'model' => $serialize_data,
                            
                            'attributes' =>
                                [
                                    [
                                        'label' => 'Крок',
                                        'value' => $serialize_data->step_change,
                                        'format' => 'raw',
                                    ],
                                    [
                                        'label' => 'Час',
                                        'value' => $timeStepFormat,
                                        'format' => 'raw',
                                    ],
                                ],
                        ]); ?>
                        <?php endif; ?>
                        
                        <div class="row">
                            <?php if(Yii::$app->user->can('admin')): ?>
                            <div class="col-sm-6">
                                <?= $status; ?>
                            </div>
                            
                            <div class="col-sm-6">
                                <?= Html::submitButton(Yii::t('app', 'Підтвердити / Створити'), ['class' => 'btn btn-success']); ?>
                            </div>
                            <?php endif;?>
                        </div>
                        <?php ActiveForm::end(); ?>
                        <br>
                    </div>

                    <hr>

                </div>
                <?php
                } else {
                    $files = new Files();
                    if(false != ($file = Files::findOne([
                            'user_id' => Yii::$app->user->id,
                            'type' => 'bid',
                            'lot_id' => $model->id,
                        ]))) {
                        $bidding = new \app\models\Bidding();
                        $form = ActiveForm::begin([
                            'action' => Url::to(['/auctions/bid', 'id' => $model->auction->id]),
                        ]); ?>
                        <?= $form->field($bidding, 'special_price'); ?>
                        <div class="form-group">
                            <?= Html::submitButton(Yii::t('app', 'Submit'), ['class' => 'btn btn-success']); ?>
                            <?= Html::a(Yii::t('app', 'Download file'), ["/files/download", 'id' => $file->id], ['class' => 'btn btn-warning']); ?>
                        </div>
                        <?php
                        ActiveForm::end();
                    } else {
                        $form = ActiveForm::begin([
                            'options' => ['enctype' => 'multipart/form-data'] // important
                        ]);
                        echo $form->field($files, 'file')->widget(FileInput::classname(), [
                            'options' => ['multiple' => true],
                            'pluginOptions' => [
                                'initialCaption' => 'файл типу: doc, docx, pdf, jpg, png, zip, rar (розмiр не бiльше 40Мб)',
                                'showPreview' => true,
                                'overwriteInitial' => false,
                                'pluginLoading' => true,
                                'initialPreviewAsData' => true,
                                'uploadClass' => 'btn btn-danger',
                                'removeIcon' => '<i class="glyphicon glyphicon-trash"></i>',
                            ],
                        ])->label(Yii::t('app', 'DownloadMemeberDoc ID'));
                        

                        ActiveForm::end();
                    }
                }
                ?>


                <?= DetailView::widget([
                    'model' => $model,
                    'attributes' =>
                        [
                            // [
                            //     'label' => Yii::t('app', 'Circle ID'),
                            //     'value' => $circle_down . Html::tag('span', $model->step_down, ['class' => 'lead']) . $circle_up . $circle_change . $time_change . $model->time_step_down,
                            //     'format' => 'raw',
                            // ],
                            'aukname',
                            'name',
                            'categoryName',
                            'description',
                            'auction_date',
                            'bidding_date',
                            [
                                'attribute' => 'start_price',
                                'value' => $price_nds,
                            ],
                            [
                                'attribute' => 'count',
                                'value' => $model->count,
                                'visible' => $model->count,
                            ],
                            [
                                'attribute' => 'price_per_one',
                                'value' => $model->unitId ? ($model->start_price / $model->count) : '',
                                'visible' => $model->unitId ? true : false,
                            ],
                            [
                                'attribute' => 'unitName',
                                'value' => $model->unit ? $model->unit->name : '',
                                'visible' => $model->unitId ? true : false,
                            ],
                            'step',
                            [
                                'attribute' => 'docs_id',
                                'value' => $docs,
                                'format' => 'raw',
                            ],
                            'address',
                            'member_require',
                            'payment_term',
                            'notes',
                            [
                                'attribute' => 'dogovor_id',
                                'value' => $dogovor,
                                'format' => 'raw',
                            ],
                            [
                                'attribute' => Yii::t('app', 'Registration fee'),
                                'value' =>  $registration_fee .
                                    Html::a('<i class="glyphicon glyphicon-circle-arrow-down"></i>',
                                        [
                                            '/bills/download-requisites',
                                            'id' => $model->billPayRegistration,
                                        ],
                                        [
                                            'target' => '_blank',
                                            'title' => Yii::t('app', 'Download bill')
                                        ]
                                    ),
                                'format' =>  'raw',
                            ],
                            [
                                'attribute' => Yii::t('app', 'Guaranteed payment'),
                                'value' =>  $guarantee_fee .
                                    Html::a('<i class="glyphicon glyphicon-circle-arrow-down"></i>',
                                        [
                                            '/bills/download-requisites',
                                            'id' => $model->billPayGuarantee,
                                        ],
                                        [
                                            'target' => '_blank',
                                            'title' => Yii::t('app', 'Download bill')
                                        ]
                                    ),
                                'format' =>  'raw',
                            ],
                        ],
                ]) ?>
            </div>
        </div>
    </div>
