<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\grid\GridView;
use yii\helpers\ArrayHelper;
use kartik\editable\Editable;

/* @var $this yii\web\View */
/* @var $searchModel app\models\ItemsSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$lang = Yii::$app->language;
$this->title = Yii::t('app', 'Items');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="items-index">

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

    <p>
        <?= Html::a(Yii::t('app', 'Create Item'), ['create'], ['class' => 'btn btn-success']) ?>
        <?= Html::a(Yii::t('app', 'Update Prices'), ['update-variations'], ['class' => 'btn btn-primary']) ?>
        <?= Html::a(Yii::t('app', 'Uncategorized Items'), ['categorize'], ['class' => 'btn btn-warning']) ?>
    </p>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<div class="table-responsive">
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'tableOptions' => [
            'class' => 'table table-striped table-bordered table-responsive',
        ],
        'columns' => [
            $lang == 'ar' ?
                [
                    'attribute' => 'main_category_id',
                    'filter' => ArrayHelper::map($main_categories, 'id', 'arabic_name'),
                    'value' => function($model) {
                        if ($model->main_category_id) {
                            return $model->mainCategory->arabic_name;
                        }
                    }
                ]
            :
                [
                    'attribute' => 'main_category_id',
                    'filter' => ArrayHelper::map($main_categories, 'id', 'english_name'),
                    'value' => function($model) {
                        if ($model->main_category_id) {
                            return $model->mainCategory->english_name;
                        }
                    }
                ],
            $lang == 'ar' ?
                [
                    'attribute' => 'sub_category_id',
                    'filter' => ArrayHelper::map($sub_categories, 'id', 'arabic_name'),
                    'value' => function($model) {
                        if ($model->sub_category_id) {
                            return $model->subCategory->arabic_name;
                        }
                    }
                ] 
            :
                [
                    'attribute' => 'sub_category_id',
                    'filter' => ArrayHelper::map($sub_categories, 'id', 'english_name'),
                    'value' => function($model) {
                        if ($model->sub_category_id) {
                            return $model->subCategory->english_name;
                        }
                    }
                ],
            
            $lang == 'ar' ? 'arabic_name' : 'english_name',
            $lang == 'ar' ? 
                [
                    'attribute' => 'arabic_description',
                    'headerOptions' => ['style' => 'width:40%'],
                    'contentOptions' => ['style' => 'word-break:break-word;']
                ]
            : 
                [
                    'attribute' => 'english_description',
                    'headerOptions' => ['style' => 'width:40%'],
                    'contentOptions' => ['style' => 'word-break:break-word;']
                ],

            [
                'attribute' => 'preparation_time',
                'headerOptions' => ['style' => 'width:10%'],
            ],
            'calories',
            $lang == 'ar' ? 'arabic_additional_sauces' : 'english_additional_sauces',
            [
                'attribute' => 'status',
                'filter'    => ["Active" => Yii::t('app', 'Active'), "Inactive" => Yii::t('app', 'Inactive'), "Uncategorized" => Yii::t('app', 'Uncategorized')],
                'value' => function($model) {
					if ($model->status != "Uncategorized") {
						return  Editable::widget([
							'model' => $model,
							'attribute' => 'status',
							'header' => Yii::t('app', 'Status'),
							// 'asPopover' => true,
							'size'=>'md',
							'inputType' => Editable::INPUT_DROPDOWN_LIST,    
							'value' => $model->status,
							'options'=>[
								'options'=>['placeholder' => Yii::t('app', 'Status')]
							],
							'formOptions' => [
								'action' => Url::base() . '/items/change-status?id=' . $model->id,
								'method' => 'get'
							],
							'options' => ['class'=>'form-control', 'id' => $model->id],
							'preHeader' => Yii::t('app', 'Edit') . " ",
							'data' => ["Active" => Yii::t('app', 'Active'), "Inactive" => Yii::t('app', 'Inactive')],
							'displayValueConfig'=> [
								'Active' => Yii::t('app', 'Active'),
								'Inactive' => Yii::t('app', 'Inactive'),
							],
						]);
					}
					else {
						return "Uncategorized";
					}
                },
                'format' => 'raw'
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{view} {update} {images} {delete}',
                'visibleButtons' => [
                    'images' => function ($model){
                        return true;
                    },
                ],
                'buttons' => [
                    'delete' => function ($url, $model) {
                        return Html::a('<span class="glyphicon glyphicon-trash"></span>',
                            ['delete', 'id' => $model->id],
                            ['title' => Yii::t('app', 'Delete'),
                            'data' => [
                                'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'),
                                'method' => 'post',
                            ]]
                        );
                    },
                    'images' => function ($url, $model) {
                        return Html::a('<span class="glyphicon glyphicon-picture"></span>', 
                            ['update-images', 'id' => $model->id], 
                            ['title' => Yii::t('app', 'Images')]
                        );
                    },
                ],
                // 'headerOptions' => ['style' => 'width:8%'],
            ],
        ],
    ]); ?>
</div>

</div>
