<?php

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

/* @var $this yii\web\View */
/* @var $searchModel app\models\MainCategoriesSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Main Categories');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="main-categories-index">

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

    <p>
        <?= Html::a(Yii::t('app', 'Create Main Category'), ['create'], ['class' => 'btn btn-success']) ?>
    </p>

<div class="table-responsive">
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        // 'tableOptions' => [
        //     'class' => 'table table-striped table-bordered table-responsive',
        // ],
        'columns' => [
            [
                'label' => Yii::t('app', 'Image'),
                'format' => 'html',
                'value' => function ($model) {
                    if ($model['image_path']) {
                       return Html::img(Url::base() . '/' . $model['image_path'],
                        ['width' => '120px', 'height' => '120px']); 
                    }
                    else return Yii::t('app', '(No Image)');
                    
                },
                'contentOptions' => ['style'=>'text-align: center'],
            ],
            'arabic_name',
            'english_name',
            [
                'attribute' => 'status',
                'filter'    => ["Active" => Yii::t('app', 'Active'), "Inactive" => Yii::t('app', 'Inactive')],
                'value' => function($model) {
                    return  Editable::widget([
                        'model' => $model,
                        'attribute' => 'status',
                        'header' => Yii::t('app', 'Status'),
                        'asPopover' => true,
                        'size'=>'sm',
                        'inputType' => Editable::INPUT_DROPDOWN_LIST,    
                        'value' => $model->status,
                        'options'=>[
                            'options'=>['placeholder'=>Yii::t('app', 'Status')]
                        ],
                        'formOptions' => [
                            'action' => Url::base() . '/main-categories/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'),
                        ],
                    ]);
                },
                'format' => 'raw'
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{view} {update} {delete} {sub-cat}',
                'visibleButtons' => [
                    'delete' => function ($model){
                        if ($model->subCategories || $model->items) {
                            return false;
                        }
                        else { return true; } 
                    },
                    'sub-cat' => function ($model){
                        return true;
                    },
                ],
                'buttons' => [
                    'delete' => function ($url, $model) {
                        if ($model->items) {
                            return ;
                        }
                        else {
                            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 category?'),
                                    'method' => 'post',
                                ]]
                            );
                        }
                    },
                    'sub-cat' => function ($url, $model) {
                        return Html::a('<span class="glyphicon glyphicon-plus"></span>',
                            ['sub-categories/create', 'main_cate_id' => $model->id],
                            ['title' => Yii::t('app', 'Create Sub Category')]
                        );
                    },
                ],
            ],
        ],
    ]); ?>
</div>

</div>
