<?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 Share Rule'), 'javascript:;', [
        'url' => Url::to([
            '/goods-share-rule/create',
            'goods_id' => $goods->id,
        ]),
        'class' => 'btn btn-success js-layer-share-rule-link',
        'title' => Yii::t('common', 'Create Goods Share Rule'),
    ]) ?>
</p>

<div class="box box-primary">
    <div class="box-body">
        <?php Pjax::begin(['id' => 'goods-share-rule-list']) ?>
        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'export' => false,
            'options' => ['id' => 'share_rule_list'],
            'columns' => [
                [
                    'class' => 'kartik\grid\CheckboxColumn',
                    'name' => 'id',
                ],//复选框列
                'level',
                'rate',
//                'created_at:datetime',
//                'updated_at:datetime',
                [
                    '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-share-rule/update', 'id' => $model->id]),
                                'class' => 'btn btn-success btn-xs js-layer-share-rule-link',
                                'title' => Yii::t('common', 'Update Goods Share Rule'),
                            ]);
                        },
                        'delete' => function ($url, $model, $key) {
                            return Html::a('删除', 'javascript:;', [
                                'url' => Url::to(['/goods-share-rule/delete', 'id' => $model->id]),
                                'class' => 'btn btn-success btn-xs js-share-rule-del',
                                'title' => Yii::t('common', '删除商品分润'),
                            ]);
                        },
                    ],
                ],
            ],
        ]); ?>
        <?php Pjax::end() ?>
    </div>
</div>


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


$(document).on('click', '.js-share-rule-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 = '#share-rule';
          $.pjax({
            url: location.href,
            container: '#goods-share-rule-list',
            timeout: 0
          });
}

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