<?php

use common\models\Goods;
use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\Url;
use yii\widgets\Pjax;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\search\GoodsSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('common', 'Goods');
$this->params['breadcrumbs'][] = $this->title;
?>
<?php $this->beginBlock('content-header') ?>
<?= $this->title . ' ' . Html::a(Yii::t('common', 'Create Goods'), ['create'], ['class' => 'btn btn-success']) ?>

<?= Html::a('批量删除', "javascript:void(0);", ['class' => 'btn btn-danger gridviewdelete', 'data-href' => Url::to(["batch-delete"])]) ?>

<?php $this->endBlock() ?>
<?php echo $this->render('_search', ['model' => $searchModel]); ?>

    <div class="box box-success">
        <div class="box-body">
            <?php Pjax::begin(['id' => 'goodsGridPjax']) ?>
            <?= GridView::widget([
                'dataProvider' => $dataProvider,
//            'filterModel' => $searchModel,
                'id' => 'goodsGrid',
                'columns' => [
                    [
                        'class' => 'yii\grid\CheckboxColumn',
                        'name' => 'id',
                    ],//复选框列
                    'id',
                    [
                        'attribute' => 'cat_id',
                        'value' => function ($model) {
                            /* @var  $model Goods */
                            if ($model->cat) {
                                return $model->cat->title;
                            } else {
                                return '分类已删除';
                            }
                        },
                    ],
//                    [
//                        'attribute' => 'store_id',
//                        'value' => function ($model) {
//                            /* @var  $model Goods */
//                            return $model->store->name;
//                        },
//                    ],
                    'name',
                    'price',
                    'origin_price',
//                'desc',
                    'actual_sales',
                    'virtual_sales',
                    'actual_views',
                    'virtual_views',
                    'created_at:datetime',
                    'updated_at:datetime',
                    [
                        'class' => 'backend\widgets\grid\SwitcherColumn',
                        'attribute' => 'status',
                        'onVal' => Goods::STATUS_ON,              //开启的值
                        'offVal' => Goods::STATUS_OFF,              //开启的值
                    ],
                    ['class' => 'common\helpers\DiyActionColumn',]
                    /*['class' => 'common\helpers\DiyActionColumn',
                        'template' => '{attr} {sku} {view} {update} {delete}',
                        'buttons' => [
                            'attr' => function ($url, $model, $key) {
                                return Html::a('添加属性', Url::to(['/goods-attr/create', 'goods_id' => $model->id,'origin'=>Yii::$app->request->url]), [
                                    'class' => 'btn btn-success btn-xs',
                                    'title'=>Yii::t('common', 'Create Goods Attr'),
                                    'target'=>'_blank'
                                ]);
                            },
                            'sku' => function ($url, $model, $key) {
                                return Html::a('添加规格', Url::to(['/goods-sku/create', 'goods_id' => $model->id,'origin'=>Yii::$app->request->url]), [
                                    'class' => 'btn btn-success btn-xs',
                                    'title'=>Yii::t('common', 'Create Goods Sku'),
    //                                'data-toggle' => 'tooltip',
                                    'target'=>'_blank'
                                ]);
                            },
                            'view' => function ($url, $model, $key) {
                                return Html::a('查看', Url::to(['/goods/view', 'id' => $model->id]), [
                                    'class' => 'btn btn-success btn-xs',
                                    'title'=>$model->name.'商品详情',
                                    'target'=>'_blank'
                                ]);
                            },
                        ]
                    ],*/
                ],
            ]); ?>
            <?php Pjax::end() ?>
        </div>
    </div>


<?php

$js = <<<JS

$(".gridviewdelete").on("click", function () {
    let _this = $(this);
    $.modal.confirm('您确定要删除吗？', () => {
        //表单提交 
        let keys = $("#goodsGrid").yiiGridView("getSelectedRows");
        if(keys.length == 0){
             $.modal.error('请选择要删除的数据');
             return false;
        }
         $.modal.loading();
        $.post(_this.attr('data-href'), {ids: keys}, (res) => {
            if (res.errcode == 0) {
                $.modal.notify(res.errmsg, 'success', () => {
                    //移除行
                    attrPjax();
                });
            } else {
                $.modal.error(res.errmsg);
            }
        });
    });
});


function attrPjax() {
          $.pjax({
            url: location.href,
            container: '#goodsGridPjax',
            timeout: 0
          });
}


JS;
$this->registerJs($js);


