<?php

use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\widgets\DetailView;
use yii\bootstrap\Modal;
use app\models\SubCategories;
use app\models\SpecialItems;
use app\models\Items;
use kartik\editable\Editable;

$lang = Yii::$app->language;
$this->title = Yii::$app->language == 'ar' ? $model->arabic_name : $model->english_name;
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Main Categories'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>

<div class="main-categories-view">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a(Yii::t('app', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
        <?php if ( !$model->allItems && !$model->subCategories ) : ?>
            <?= Html::a(Yii::t('app', 'Delete'), ['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; ?>
    </p>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'arabic_name',
            'english_name',
            [
                'attribute' => 'status',
                'value' => function($model) {
                    return Yii::t('app', $model->status);
                }
            ],
            [
                'attribute' => 'image_path',
                'format' => 'raw',
                'value' => function($model) {
                    if($model->image_path){
                        return '<a href="#" class="image-link"><div class="thumbnail-image">' . Html::img('@web/' . $model->image_path, ['class' => 'image-view']) . '</div></a>';
                    }
                }
            ],
            [
                'attribute' => 'Special Item',
                'label' => Yii::t('app', 'Special Item In Category'),
                'visible' => ! $model->subCategories,
                'value' => function($model) use($items, $special_item) {
                    return  Editable::widget([
                        'name' => 'special-item',
                        'header' => Yii::t('app', 'Special Item'),
                        'asPopover' => true,
                        'size'=>'lg',
                        'inputType' => Editable::INPUT_DROPDOWN_LIST,    
                        'value' => $special_item != null ? $special_item->item_id : null,
                        'options' => ['placeholder' => Yii::t('app', 'Special Item')],
                        'formOptions' => [
                            'action' => Url::base() . '/main-categories/change-special-item?id=' . $model->id,
                            'method' => 'get'
                        ],
                        'preHeader' => Yii::t('app', 'Edit') . " ",
                        'data' => ArrayHelper::merge(['' => Yii::t('app', 'There is no special item')], ArrayHelper::map($items, 'id', Yii::$app->language == 'ar' ? 'arabic_name' : 'english_name')),
                        'displayValueConfig'=> ArrayHelper::map($items, 'id', Yii::$app->language == 'ar' ? 'arabic_name' : 'english_name'),
                    ]);
                },
                'format' => 'raw'
            ],
        ],
    ]) ?>
    <h3><?= Yii::t('app', 'Sub Categories')?>:</h3>
    <?php if($sub_categories) : ?>
        <table class="table table-bordered table-striped">
        <thead>
            <tr>
                <th>
                    <?= Yii::t('app', 'Name') ?>
                </th>
                <th>
                    <?= Yii::t('app', 'Status') ?>
                </th>
                <th>
                    <?= Yii::t('app', 'Special Item') ?>
                </th>
                <th>
                    
                </th>
            </tr>
        </thead>
    
            <?php foreach ($sub_categories as $index => $sub_category) : ?>
                <tr>
                    <td style="width: 30%"> <?= $lang == 'en' ? $sub_category->english_name : $sub_category->arabic_name ?> </td>
                    <td style="width: 30%"> <?= Yii::t('app', $sub_category->status) ?> </td>
                    <td style="width: 30%">
                        <?php 
                         $items = Items::find()->where(['sub_category_id' => $sub_category->id])->all();
                         $special_item = SpecialItems::find()->where(['sub_category_id' => $sub_category->id])->one();
                        ?>
                        <?= Editable::widget([
                            'name' => 'special-item',
                            'header' => Yii::t('app', 'Special Item'),
                            'asPopover' => true,
                            'size' => 'lg',
                            'inputType' => Editable::INPUT_DROPDOWN_LIST,    
                            'value' => $special_item != null ? $special_item->item_id : null,
                            'options' => [
                                'placeholder' => Yii::t('app', 'Special Item'),
                                'id' => 'special-item-' . $sub_category->id,
                            ],
                            'formOptions' => [
                                'action' => Url::base() . '/sub-categories/change-special-item?id=' . $sub_category->id,
                                'method' => 'get'
                            ],
                            'preHeader' => Yii::t('app', 'Edit') . " ",
                            'data' => ArrayHelper::merge(['' => Yii::t('app', 'There is no special item')], ArrayHelper::map($items, 'id', Yii::$app->language == 'ar' ? 'arabic_name' : 'english_name')),
                            'displayValueConfig' => ArrayHelper::map($items, 'id', Yii::$app->language == 'ar' ? 'arabic_name' : 'english_name'),
                        ]);?>
                    </td>
                    <td style="width: 10%">
                        <div style="display:inline">
                            <?= Html::a('<span class="glyphicon glyphicon-eye-open"></span>', ['sub-categories/view', 'id' => $sub_category->id]) ?>
                            <?= Html::a('<span class="glyphicon glyphicon-pencil"></span>', ['sub-categories/update', 'id' => $sub_category->id]) ?>
                            <?php if (SubCategories::canDeleteSubCategory($sub_category->id)) : ?>
                                <?= Html::a('<span class="glyphicon glyphicon-trash"></span>', ['sub-categories/delete', 'id' => $sub_category->id], [
                                    'data' => [
                                        'method' => 'POST',
                                        'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'),
                                        ]]) ?>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
            <?php endforeach; ?>
        </table>
    <?php else : ?>

        <div class="alert alert-info" role="alert">
            <?= Yii::t('app', 'This main category don\'t have any sub-categories, you can add sub categories if you want.')?>
        </div>

    <?php endif; ?>

    <p>
        <?= Html::a(Yii::t('app', 'Add Sub Category'), ['sub-categories/create', 'main_cate_id' => $model->id], ['class' => 'btn btn-primary']) ?>
    </p>


</div>

<?php
	Modal::begin([
		'header' => '<h3>' . Yii::t('app', 'View Image') . '</h3>',
		'id' => 'image-preview',
		'size' => 'modal-lg'
    ]);

    echo '<div id="view-img">' . Html::img('@web/' . $model->image_path, ['style' => 'width: 100%']) . '</div>';

    Modal::end();
?>
