<?php

use backend\widgets\ActiveForm;
use common\behaviors\GoodsTagBehavior;
use common\models\Cat;
use common\models\Store;
use common\modules\attachment\widgets\MultipleWidget;
use common\modules\attachment\widgets\SingleWidget;
use common\widgets\tag\TagsInput;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $model common\models\Goods */
/* @var $sku common\models\GoodsSku */
/* @var $form backend\widgets\ActiveForm */
/* @var $goodsSkuProvider ActiveDataProvider */
/* @var $goodsAttrProvider ActiveDataProvider */
/* @var $goodsShareRules \common\models\GoodsShareRule[] */
/* @var $goodsShareRule \common\models\GoodsShareRule */
?>

<?php $form = ActiveForm::begin([
    'enableClientValidation' => false,
    'enableAjaxValidation' => true, // 需要使用 backend\widgets\ActiveForm 后台自定义表单
    'options' => ['class' => 'form-horizontal'],
    'fieldConfig' => [  //统一修改字段的模板
//            'template' => "{label}\n{input}\n{hint}\n{error}",
        'template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-7'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{hint}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>",
    ],
]); ?>
<style>
    body {
        position: relative;
    }
</style>

<div class="row">
    <!--表单-->
    <div class="col-md-9">

        <div class="box box-success" id="base">
            <div class="box-header">
                <span class="box-title">基本信息</span>
            </div>
            <div class="box-body">
                <?= $form->field($model, 'cat_id')->dropDownList(Cat::getDropDownList(Cat::lists())) ?>
<!--                --><?//= $form->field($model, 'store_id')->dropDownList(ArrayHelper::map(Store::lists(), 'id', 'name')) ?>

                <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<!--                --><?//= $form->field($model, 'newness')->textInput(['maxlength' => true, 'type' => 'number'])->hint('数字1~10，代表几成新') ?>
                <?= $form->field($model, 'desc')->textarea(['maxlength' => true]) ?>


<!--                --><?//= $form->field($model, GoodsTagBehavior::$formName)->label(GoodsTagBehavior::$formLable)->widget(TagsInput::className()) ?>

            </div>

        </div>

        <div class="box box-success" id="price">
            <div class="box-header">
                <span class="box-title">价格销量</span>
            </div>
            <div class="box-body">
                <?= $form->field($model, 'origin_price')->textInput(['maxlength' => true])->inputSuffix('元') ?>

                <?= $form->field($model, 'price')->textInput(['maxlength' => true])->inputSuffix('元') ?>

                <?= $form->field($model, 'actual_sales')->textInput(['maxlength' => true, 'type' => 'number'])->inputSuffix('件') ?>

                <?= $form->field($model, 'virtual_sales')->textInput(['maxlength' => true, 'type' => 'number'])->inputSuffix('件') ?>

                <?= $form->field($model, 'actual_views')->textInput(['maxlength' => true, 'type' => 'number'])->inputSuffix('次') ?>

                <?= $form->field($model, 'virtual_views')->textInput(['maxlength' => true, 'type' => 'number'])->inputSuffix('次') ?>
            </div>

        </div>

        <div class="box box-success" id="sku">
            <div class="box-header">
                <span class="box-title">库存规格</span>
            </div>
            <div class="box-body">

                <?= $form->field($model, 'stock')->textInput(['maxlength' => true, 'type' => 'number'])->inputSuffix('件') ?>

                <!--编辑商品才显示规格-->
                <?php if (!$model->isNewRecord): ?>

                    <!--是否使用规格-->
                    <?= $form->field($model, 'is_sku')->radioList(\common\enums\StatusEnum::$list) ?>

                    <div id="goodsSku" class="form-group" style="display: none;">
                        <div class="col-xs-3 col-sm-2 text-right">
                            <label class="control-label">规格列表</label>
                        </div>
                        <div class="col-xs-9 col-sm-10">
                            <?= $this->render('../goods-sku/_index', [
                                'dataProvider' => $goodsSkuProvider,
                                'goods' => $model,
                            ]) ?>
                        </div>
                    </div>
                <?php endif; ?>
            </div>

        </div>

        <div class="box box-success" id="detail">
            <div class="box-header">
                <span class="box-title">商品图片</span>
            </div>
            <div class="box-body">
                <?= $form->field($model, 'cover')->widget(SingleWidget::className()) ?>
                <?= $form->field($model, 'banner', ['template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-10'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-12 col-sm-offset-2'>{hint}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>"])->widget(MultipleWidget::className()) ?>
                <?= $form->field($model, 'picture', ['template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-10'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-12 col-sm-offset-2'>{hint}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>"])->widget(MultipleWidget::className()) ?>
            </div>
        </div>

        <?php if (!$model->isNewRecord): ?>
            <div class="box box-success" id="attr">
                <div class="box-header">
                    <span class="box-title">商品属性</span>
                </div>
                <div class="box-body">
                    <div class="col-xs-3 col-sm-2 text-right">
                        <label class="control-label">属性列表</label>
                    </div>
                    <div class="col-xs-9 col-sm-10">
                        <?= $this->render('../goods-attr/_index', [
                            'dataProvider' => $goodsAttrProvider,
                            'goods' => $model,
                        ]) ?>
                    </div>
                </div>
            </div>
        <?php endif; ?>


        <div class="box box-success" id="share-rule" style="display: none;">
            <div class="box-header">
                <span class="box-title">分销设置</span>
            </div>
            <div class="box-body">


                <!--是否使用规格-->
                <?= $form->field($model, 'is_share_diy')->radioList(\common\enums\StatusEnum::$list) ?>

                <div id="goodsShareDiy" style="display: none;">

                    <table class="table table-bordered table-striped">

                        <thead>
                        <tr>
                            <th>等级名称</th>
                            <th>销售分润</th>
                            <th>直推分润</th>
                            <th>上架分润</th>
                        </tr>
                        </thead>

                        <tbody>
                        <?php
                        /* @var \common\models\GoodsShare $goodsShare */
                        foreach ($goodsShares

                        as $index => $goodsShare): ?>
                        <tr>
                            <td>
                                <?= $goodsShare->shareLevel->title ?>
                            </td>
                            <td>
                                <?= $form->field($goodsShare, "[$index]sale_rate", ['template' => "{input}\n{hint}\n{error}"])->textInput(['maxlength' => true])->hint('请填写小数，0.1 = 10%') ?>
                            </td>
                            <td>
                                <?= $form->field($goodsShare, "[$index]direct_rate", ['template' => "{input}\n{hint}\n{error}"])->textInput(['maxlength' => true])->hint('请填写小数，0.1 = 10%') ?>
                            </td>
                            <td>
                                <?= $form->field($goodsShare, "[$index]put_on_rate", ['template' => "{input}\n{hint}\n{error}"])->textInput(['maxlength' => true])->hint('请填写小数，0.1 = 10%') ?>

                            </td>
                            <div style="display: none;">
                                <!--隐藏域，等级id-->
                                <?= $form->field($goodsShare, "[$index]level_id", ['template' => "{input}"])->label(false)->hiddenInput(['value' => $goodsShare->shareLevel->id]) ?>
                            </div>
                            <?php endforeach; ?>
                        </tr>
                        </tbody>
                    </table>

                </div>

            </div>
        </div>


    </div>

    <!--定位导航-->
    <div class="col-md-3" style="position: fixed; right: 0;">
        <div class="box box-success">
            <div class="box-header">
                <span class="box-title">导航操作</span>
            </div>
            <div class="box-body" id="formNavs">
                <ul class="nav nav-pills nav-stacked">
                    <li class="active"><a href="#base">基本信息</a></li>
                    <li><a href="#price">价格销量</a></li>
                    <li><a href="#sku">库存规格</a></li>
                    <li><a href="#detail">商品图片</a></li>
                    <?php if (!$model->isNewRecord): ?>
                        <li><a href="#attr">商品属性</a></li>
                    <?php endif; ?>
<!--                    <li><a href="#share-rule">分销设置</a></li>-->
                </ul>
            </div>

            <div class="box-footer">
                <?= Html::submitButton($model->isNewRecord ? Yii::t('common', 'Create') : Yii::t('common', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-success']) ?>
                <?= Html::a(Yii::t('common', 'back'), Yii::$app->request->referrer, ['class' => 'btn btn-default']) ?>
            </div>
        </div>
    </div>

</div>

<?php ActiveForm::end(); ?>

<?php
$js = <<<JS
    //nav导航切换
    $('#formNavs li').click(function (e) {
        $('#formNavs li').removeClass('active');
        $(this).addClass('active');
    });

    function skuToggle() {
      if ($('input[name="Goods[is_sku]"]:checked').val() == 1) {
            $('#goods-stock').prop('readonly', true).val(0);
            $('#goodsSku').slideDown();
        } else {
            $('#goods-stock').prop('readonly', false);
             $('#goodsSku').slideUp();
        }
    }
    function shareDiyToggle() {
      if ($('input[name="Goods[is_share_diy]"]:checked').val() == 1) {
            $('#goodsShareDiy').slideDown();
        } else {
             $('#goodsShareDiy').slideUp();
        }
    }
    
    $('#goods-is_sku').change(function(e) {
        skuToggle();
    });
    
    $('#goods-is_share_diy').change(function(e) {
        shareDiyToggle();
    });
       
    //初始化调用
    skuToggle();
    shareDiyToggle();
    
    //滚动监听
    $('body').scrollspy({ target: '#formNavs' });
    $('#formNavs').on('activate.bs.scrollspy', function (e) {
        location.hash = $(e.target).find('a').attr('href');
    })
JS;
$this->registerJs($js);
?>



