<?php

use backend\widgets\ActiveForm;
use common\models\Warehouse;
use common\modules\attachment\widgets\SingleWidget;
use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $model common\models\Store */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="box box-success">
    <div class="box-body">
        <?php $form = ActiveForm::begin(); ?>
        <?= $form->field($model, 'user_id')->hint('还未添加商家账户？' . Html::a('点击去添加商家账户', ['/user/default/create']))->dropDownList(common\modules\user\models\User::lists()) ?>
        <?= $form->field($model, 'proxy_user_id')->hint('还未添加代理人？' . Html::a('点击去添加代理人', ['/user/default/create']))->dropDownList(common\modules\user\models\User::lists(), ['prompt' => '请选择']) ?>

        <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
        <?= $form->field($model, 'code')->textInput(['maxlength' => true]) ?>

        <?= $form->field($model, 'warehouse_id')->hint('添加新仓库' . Html::a('点击去添加仓库', ['/warehouse/create']))->dropDownList(\yii\helpers\ArrayHelper::map(Warehouse::lists(), 'id', 'name'), ['prompt' => '请选择']) ?>
        <!--地址选择-->
        <?= $form->field($model, 'address', [
            'template' => '{label}{input}{error}{hint}'
        ])->label('地址')->textInput(['maxlength' => true, 'placeholder' => '点击选择地址', 'data-toggle' => 'modal', 'data-target' => '#addressModal']) ?>

        <!--地址隐藏域-->
        <?= $form->field($model, 'latitude', ['template' => '{input}'])->hiddenInput() ?>
        <?= $form->field($model, 'longitude', ['template' => '{input}'])->hiddenInput() ?>
        <?= $form->field($model, 'address_name', ['template' => '{input}'])->hiddenInput() ?>
        <?= $form->field($model, 'city', ['template' => '{input}'])->hiddenInput() ?>

        <?= $form->field($model, 'rate')->textInput(['maxlength' => true, 'type' => 'number'])->inputSuffix('%')->hint("系统默认：" . Yii::$app->config->get('share_goods_profit_rate')) ?>

        <?= $form->field($model, 'logo')->widget(SingleWidget::className()) ?>

        <?= $form->field($model, 'license')->widget(SingleWidget::className()) ?>

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

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


        <?= $form->field($model, 'desc')->textarea(['maxlength' => true]) ?>

        <div class="form-group form-submit">
            <?= Html::submitButton($model->isNewRecord ? Yii::t('common', 'Create') : Yii::t('common', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-success']) ?>
        </div>

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

    <!--腾讯地址选择组件-->
    <div class="modal fade" id="addressModal" tabindex="-1" role="dialog" aria-labelledby="addressModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="addressModalLabel">选择地址</h4>
                </div>
                <div class="modal-body">
                    <!--接口文档：https://lbs.qq.com/webApi/component/componentGuide/componentPicker-->
                    <iframe id="mapIframe" width="100%" frameborder=0 scrolling="no" height="500"
                            src="<?= Yii::$app->config->get('map_url') ?>">
                    </iframe>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-success" data-dismiss="modal">确定</button>
                </div>
            </div>
        </div>
    </div>

</div>


<?php
$js = <<<JS
$(document).ready(function() {
        //腾讯地址选择组件弹窗
         window.addEventListener('message', function (event) {
            var loc = event.data;
             if (loc && loc.module == 'locationPicker') {
                $('input[name="Store[address_name]"]').val(loc.poiname);
                $('input[name="Store[city]"]').val(loc.cityname);
                $('input[name="Store[address]"]').val(loc.poiaddress);
                $('input[name="Store[latitude]"]').val(loc.latlng.lat);
                $('input[name="Store[longitude]"]').val(loc.latlng.lng);
             }
        }, false);
});
JS;
$this->registerJs($js);
?>

