<?php
    
    use common\modules\product\models\Brand;
    use common\modules\product\models\Category;
    use common\modules\product\models\Product;
    use common\modules\product\models\ProductSearch;
    use yii\data\ActiveDataProvider;
    use yii\helpers\Html;
    use yii\grid\GridView;
    use kartik\select2\Select2;
    use common\components\artboxtree\ArtboxTreeHelper;
    use common\modules\product\helpers\ProductHelper;
    use yii\web\View;
    
    /**
     * @var View               $this
     * @var ProductSearch      $searchModel
     * @var ActiveDataProvider $dataProvider
     */
    $this->title = Yii::t('product', 'Products');
    $this->params[ 'breadcrumbs' ][] = $this->title;
?>
<div class="product-index">
    
    <h1><?= Html::encode($this->title) ?></h1>
    
    <p>
        <?= Html::a(Yii::t('product', 'Create Product'), [ 'create' ], [ 'class' => 'btn btn-success' ]) ?>
    </p>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel'  => $searchModel,
        'columns'      => [
            'product_id',
            [
                'label'     => Yii::t('product', 'Product Name'),
                'attribute' => 'product_name',
                'value'     => 'lang.name',
            ],
            [
                'label'     => Yii::t('product', 'Brand'),
                'attribute' => 'brand_id',
                'value'     => 'brand.lang.name',
                'filter'    => Select2::widget([
                    'model'         => $searchModel,
                    'attribute'     => 'brand_id',
                    'data'          => Brand::find()
                                            ->joinWith('lang')
                                            ->select([
                                                'brand_lang.name',
                                                'brand.brand_id',
                                            ])
                                            ->asArray()
                                            ->indexBy('brand_id')
                                            ->column(),
                    'language'      => 'ru',
                    'options'       => [
                        'placeholder' => Yii::t('product', 'Select brand'),
                        'multiple'    => false,
                    ],
                    'pluginOptions' => [
                        'allowClear' => true,
                    ],
                ]),
            ],
            [
                'label'     => Yii::t('product', 'Category'),
                'attribute' => 'category_id',
                'value'     => function($model) {
                    /**
                     * @var Product $model
                     */
                    $categories = [];
                    foreach($model->getCategories()
                                  ->with('lang')
                                  ->all() as $category) {
                        /**
                         * @var Category $category
                         */
                        $categories[] = $category->lang->name;
                    }
                    return implode(", ", $categories);
                },
                'filter'    => Select2::widget([
                    'model'         => $searchModel,
                    'attribute'     => 'category_id',
                    'data'          => ArtboxTreeHelper::treeMap(ProductHelper::getCategories(), 'category_id', 'lang.name'),
                    'language'      => 'ru',
                    'options'       => [
                        'placeholder' => Yii::t('product', 'Select category'),
                        'multiple'    => false,
                    ],
                    'pluginOptions' => [
                        'allowClear' => true,
                    ],
                ]),
            ],
            [
                'label' => Yii::t('product', 'Variant Count'),
                'attribute' => 'variant_count',
                'value'     => function($model) {
                    /**
                     * @var Product $model
                     */
                    return count($model->variants);
                },
            ],
            [
                'class'      => 'yii\grid\ActionColumn',
                'template'   => '{items} {view} |{is_top} {is_new} {akciya}  | {update} {delete}',
                'buttons'    => [
                    'is_top' => function($url, $model) {
                        return Html::a('<span class="glyphicon glyphicon-star' . ( $model->is_top ? '' : '-empty' ) . '"></span>', $url, [
                            'title' => Yii::t('product', ( $model->is_top ? Yii::t('app', 'Set not is top') : Yii::t('app', 'Set is top') )),
                        ]);
                    },
                    'is_new' => function($url, $model) {
                        return Html::a('<span class="glyphicon glyphicon-heart' . ( $model->is_new ? '' : '-empty' ) . '"></span>', $url, [
                            'title' => Yii::t('product', ( $model->is_new ? Yii::t('app', 'Set not is new') : Yii::t('app', 'Set is new') )),
                        ]);
                    },
                    'akciya' => function($url, $model) {
                        return Html::a('<span class="glyphicon glyphicon-tag' . ( $model->akciya ? 's' : '' ) . '"></span>', $url, [
                            'title' => Yii::t('product', ( $model->akciya ? Yii::t('app', 'Set not is promotion') : Yii::t('app', 'Set is promotion') )),
                        ]);
                    },
                    'items'  => function($url, $model) {
                        return Html::a('<span class="glyphicon glyphicon-th-list"></span>', $url, [
                            'title' => Yii::t('product', 'Variants'),
                        ]);
                    },
                
                ],
                'urlCreator' => function($action, $model, $key, $index) {
                    switch($action) {
                        case 'items':
                            return \yii\helpers\Url::to([
                                '/product/variant',
                                'product_id' => $model->product_id,
                            ]);
                            break;
                        case 'is_top':
                            return \yii\helpers\Url::to([
                                'manage/is_top',
                                'id' => $model->product_id,
                            ]);
                            break;
                        case 'is_new':
                            return \yii\helpers\Url::to([
                                'manage/is_new',
                                'id' => $model->product_id,
                            ]);
                            break;
                        case 'akciya':
                            return \yii\helpers\Url::to([
                                'manage/akciya',
                                'id' => $model->product_id,
                            ]);
                            break;
                        case 'view':
                            return \yii\helpers\Url::to([
                                'manage/view',
                                'id' => $model->product_id,
                            ]);
                            break;
                        case 'update':
                            return \yii\helpers\Url::to([
                                'manage/update',
                                'id' => $model->product_id,
                            ]);
                            break;
                        case 'delete':
                            return \yii\helpers\Url::to([
                                'manage/delete',
                                'id' => $model->product_id,
                            ]);
                            break;
                        default:
                            return '';
                    }
                },
            ],
        ],
    ]); ?>
</div>
