<?php

use common\models\Order;
use common\models\OrderSku;
use yii\grid\CheckboxColumn;
use yii\grid\GridView;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model common\models\Order */

$this->title = '订单号：' . $model->out_trade_no;
$this->params['breadcrumbs'][] = ['label' => Yii::t('common', 'Orders'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>

    <div class="nav-tabs-custom">
        <ul class="nav nav-tabs">
            <li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="true">订单详情</a></li>
            <li class=""><a href="#tab_2" data-toggle="tab" aria-expanded="true">订单商品</a></li>
            <li class=""><a href="#tab_3" data-toggle="tab" aria-expanded="true">物流包裹</a></li>
        </ul>
        <div class="tab-content">
            <div class="tab-pane active" id="tab_1">

                <?= $this->render('../site/view_menu', ['model' => $model]) ?>

                <?= DetailView::widget([
                    'model' => $model,
                    'attributes' => [
                        'id',
                        [
                            'attribute' => 'user_id',
                            'format' => 'raw',
                            'value' => function ($model) {
                                if (!empty($model->user)) {
                                    return Html::a($model->user->nickname, \yii\helpers\Url::to(['/wechat-user/view', 'id' => $model->user_id]), ['title' => '查看用户', 'data-toggle' => 'tooltip', 'target' => '_blank']);
                                } else {
                                    return '无';
                                }
                            },
                        ],
                        'out_trade_no',
                        'total_amount',
                        'pay_amount',
                        'pay_time:datetime',
                        'remark',
                        'receiver_name',
                        'receiver_tel',
                        'receiver_address',
                        'created_at:datetime',
                        'updated_at:datetime',
                        [
                            'attribute' => 'status',
                            'value' => function ($model) {
                                return \common\models\Order::getStatusList($model->status);
                            },
                        ],
                    ],
                ]) ?>


            </div>

            <!--        管理商品列表管理-->
            <div class="tab-pane " id="tab_2">
                <?php if ($model->status >= \common\models\Order::STATUS_FREIGHT): ?>
                    <p>
                        <a href="javascript:;"
                           data-href="<?= Url::to(["/package/create", 'order_id' => $model->id, 'origin' => Yii::$app->request->url]) ?>"
                           class="btn btn-success js-shipped" target="_blank">订单发货</a>
                    </p>
                <?php endif; ?>
                <?= GridView::widget([
                    'dataProvider' => $orderSku['data'],
                    //'filterModel' => $searchModel,
//                    'showFooter' => true,
                    'id' => 'order_sku_list',
                    'columns' => [
                        [
                            'class' => \yii\grid\CheckboxColumn::className(),
                            'name' => 'ids',
                            'checkboxOptions' => function ($model, $key, $index, $column) {
                                return ['value' => $model['id'], 'disabled' => $model->status == OrderSku::STATUS_NORMAL && $model->order->status >= Order::STATUS_FREIGHT ? false : true];
                            }
                        ],
                        'id',
                        [
                            'attribute' => 'goods_id',
                            'format' => 'raw',
                            'value' => function ($model) {
                                return $model->goods->name;
                            },
                        ],
                        [
                            'attribute' => 'sku_id',
                            'format' => 'raw',
                            'value' => function ($model) {
                                return $model->sku ? $model->sku->name : '';
                            },
                        ],

                        'quantity',
                        'unit_price',

                        [
                            'label' => '商品总价',
                            'format' => 'raw',
                            'value' => function ($model) {
                                return $model->quantity * $model->unit_price . '元';
                            },
                        ],
                        [
                            'label' => '物流单号',
                            'format' => 'raw',
                            'value' => function ($model) {
                                if ($model->package) {
                                    return $model->package->com . '：' . $model->package->no;
                                } else if ($model->packageFixed) {
                                    return $model->packageFixed->com . '：' . $model->packageFixed->no;
                                } else {
                                    return '--';
                                }
                            },
                        ],
                        'created_at:datetime',
                        'updated_at:datetime',
                        [
                            'attribute' => 'status',
                            'value' => function ($model) {
                                return \common\models\OrderSku::getStatusList($model->status);
                            },
                        ],

                    ],
                ]); ?>
            </div>
            <!--            物流包裹-->
            <div class="tab-pane " id="tab_3">

                <?= GridView::widget([
                    'dataProvider' => $package['data'],

                    'columns' => [
                        'id',
                        'no',
                        'com',
                        [
                            'label' => '商品规格',
                            'format' => 'raw',
                            'value' => function ($model) {
                                return $model->orderSkuName;
                            },
                        ],
                        [
                            'attribute' => 'status',
                            'value' => function ($model) {
                                return \common\models\Package::getStatusList($model->status);
                            },
                        ],
                        ['class' => 'common\helpers\DiyActionColumn',
                            'template' => '{update}',
                            'buttons' => [
                                'update' => function ($url, $model, $key) {
                                    return Html::a('更新', Url::to(['/package/update', 'id' => $model->id,  'order_id' => $model->order->id, 'origin' => Yii::$app->request->url]), [
                                        'class' => 'btn btn-success btn-xs',
                                        'title'=>Yii::t('common', 'Update Package'),
                                        'target'=>'_blank'
                                    ]);
                                },
                            ]
                        ],
                    ],
                ]); ?>
            </div>
        </div>
    </div>
<?php

$js = <<<JS

$(function() {
  $('body').delegate('.js-shipped', 'click', function(e) {
    var ids = $("#order_sku_list").yiiGridView("getSelectedRows");
    var ids_str = ids.join(",");
    if(ids_str ==''){
         $.modal.error('请选择要发货的订单商品');
         $(this).attr('href', 'javascript:;');
         e.preventDefault();
         return false;
    } else {
        var href = $(this).attr('data-href') + '&order_sku_ids=' + ids_str;
        $(this).attr('href', href);
    }
  });
});

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