<?php

use kartik\grid\GridView;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\Pjax;

/* @var $goods \common\models\Goods */
/* @var $dataProvider \yii\data\ActiveDataProvider */


?>

<p>
    <?= Html::a(Yii::t('common', 'Create Goods Attr'), 'javascript:;', [
        'url' => Url::to([
            '/goods-attr/create',
            'goods_id' => $goods->id,
            'origin' => Yii::$app->request->url
        ]),
        'class' => 'btn btn-success js-layer-attr-link',
        'title' => Yii::t('common', 'Create Goods Attr'),
    ]) ?>
</p>

<div class="box box-primary">
    <div class="box-body">
        <?php Pjax::begin(['id' => 'goods-attr-list']) ?>
        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'export' => false,
            'options' => ['id' => 'attr_list'],
            'columns' => [
                [
                    'class' => 'kartik\grid\CheckboxColumn',
                    'name' => 'id',
                ],//复选框列
                'name',
                'value',
//                'created_at:datetime',
//                'updated_at:datetime',
                [
                    'class' => 'backend\widgets\grid\PositionColumn',
                    'attribute' => 'sort'
                ],
                [
                    'class' => 'common\helpers\DiyActionColumn',
                    'template' => '{update} {delete}',
                    'buttons' => [
                        'update' => function ($url, $model, $key) {
                            return Html::a(Yii::t('yii', 'Update'), 'javascript:;', [
                                'url' => Url::to(['/goods-attr/update', 'id' => $model->id]),
                                'class' => 'btn btn-success btn-xs js-layer-attr-link',
                                'title' => Yii::t('common', 'Update Goods Attr'),
                            ]);
                        },
                        'delete' => function ($url, $model, $key) {
                            return Html::a('删除', 'javascript:;', [
                                'url' => Url::to(['/goods-attr/delete', 'id' => $model->id]),
                                'class' => 'btn btn-success btn-xs js-attr-del',
                                'title' => Yii::t('common', '删除商品属性'),
                            ]);
                        },
                    ],
                ],
            ],
        ]); ?>
        <?php Pjax::end() ?>
    </div>
</div>


<?php
$js = <<<JS
//layer弹窗
$(document).on('click', '.js-layer-attr-link',function() {
    let _this =  $(this);
    layer.open({
        type: 2,
        title: _this.attr('title'),
        shadeClose: true,
        shade: 0.8,
        area: ['600px', '450px'],
        content: _this.attr('url'),
        end: function() {
           //关闭弹窗，刷新数据
           attrPjax();
        }
    });
});


$(document).on('click', '.js-attr-del', function(e) {
    e.preventDefault();
    let link = $(this);
    $.modal.confirm('确定删除“' + link.parents('tr').find('td:eq(1)').text() + '”吗？', () => {
        //表单提交 
        $.post(link.attr('url'), {}, (res) => {
            if (res.errcode == 0) {
                $.modal.notify(res.errmsg, 'success', () => {
                    //移除行
                    // link.parents('tr').slideUp().remove();
                    attrPjax();
                });
            } else {
                $.modal.error(res.errmsg);
            }
        });
    });
});

function attrPjax() {
  location.hash = '#attr';
          $.pjax({
            url: location.href,
            container: '#goods-attr-list',
            timeout: 0
          });
}

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