<div class="content-wrapper">
    <?php
    $form = $this->form;
    $form->prepare();

    $form->get('appName')->setAttributes([
        'placeholder' => $this->translate('商城名称')
    ]);

    $form->get('appAccessId')->setAttributes([
        'placeholder' => $this->translate('appId')
    ]);

    $form->get('appAccessSecret')->setAttributes([
        'placeholder' => $this->translate('appSecret')
    ]);

    $form->get('appUrl')->setAttributes([
        'placeholder' => $this->translate('以https://或http://开头')
    ]);

    $form->get('appUrlPort')->setAttributes([
        'placeholder' => $this->translate('端口')
    ]);

    $form->get('appType')->setEmptyOption('选择商城类别');
    ?>
    <?php echo $this->form()->openTag($form); ?>
    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right">
            <a href="<?php echo $this->url('app');?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> <?php echo $this->translate('返回商城列表'); ?></a>
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-check"></i> <?php echo $this->translate('保存商城信息'); ?></button>
            <?php echo $this->HelpUrl('appAdd'); ?>
        </h1>
    </section>

    <section class="content">

        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"><?php echo (isset($this->appInfo) ? $this->translate('编辑商城') : $this->translate('添加商城')); ?></h3>
                    </div>

                    <div class="box-body">
                        <?php echo $this->formElementErrors($form->get('dberp_csrf'), ['class'=>'error-message']); ?>

                        <div class="form-group">
                            <label  class="col-sm-2 control-label"><?php echo $this->translate('商城类别'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-2">
                                <?php echo $this->formElement($form->get('appType')); ?>
                                <?php echo $this->formElementErrors($form->get('appType'), ['class'=>'error-message']); ?>
                            </div>
                        </div>

                        <div class="form-group">
                            <label  class="col-sm-2 control-label"><?php echo $this->translate('商城名称'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-6">
                                <?php echo $this->formElement($form->get('appName')); ?>
                                <?php echo $this->formElementErrors($form->get('appName'), ['class'=>'error-message']); ?>
                            </div>
                        </div>

                        <div class="form-group">
                            <label  class="col-sm-2 control-label"><?php echo $this->translate('appId'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-6">
                                <div class="col-xs-9" style="padding-left: 0px;padding-right: 3px;">
                                    <?php echo $this->formElement($form->get('appAccessId')); ?>
                                    <?php echo $this->formElementErrors($form->get('appAccessId'), ['class'=>'error-message']); ?>
                                </div>
                                <div class="col-xs-3" style="padding-right: 0px;">
                                    <button type="button" onclick="createAccessId();" class="btn btn-sm bg-olive"><i class="fa fa-home"></i> <?php echo $this->translate('点击系统生成'); ?></button>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label  class="col-sm-2 control-label"><?php echo $this->translate('appSecret'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-6">
                                <div class="col-xs-9" style="padding-left: 0px;padding-right: 3px;">
                                    <?php echo $this->formElement($form->get('appAccessSecret')); ?>
                                    <?php echo $this->formElementErrors($form->get('appAccessSecret'), ['class'=>'error-message']); ?>
                                </div>
                                <div class="col-xs-3" style="padding-right: 0px;">
                                    <button type="button" onclick="createAccessSecret();" class="btn btn-sm bg-olive"><i class="fa fa-key"></i> <?php echo $this->translate('点击系统生成'); ?></button>
                                </div>
                            </div>
                        </div>

                        <div class="form-group" style="padding-right: ">
                            <label  class="col-sm-2 control-label"><?php echo $this->translate('商城地址'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-6">
                                <div class="col-xs-10" style="padding-left: 0px;padding-right: 3px;">
                                    <?php echo $this->formElement($form->get('appUrl')); ?>
                                    <?php echo $this->formElementErrors($form->get('appUrl'), ['class'=>'error-message']); ?>
                                </div>
                                <div class="col-xs-2" style="padding-left: 3px;padding-right: 0px;">
                                    <?php echo $this->formElement($form->get('appUrlPort')); ?>
                                    <?php echo $this->formElementErrors($form->get('appUrlPort'), ['class'=>'error-message']); ?>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label  class="col-sm-2 control-label"><?php echo $this->translate('商品绑定'); ?></label>
                            <div class="col-sm-2">
                                <?php echo $this->formElement($form->get('appGoodsBindType')); ?>
                                <?php echo $this->formElementErrors($form->get('appGoodsBindType'), ['class'=>'error-message']); ?>

                                <?php echo $this->formElement($form->get('appGoodsBind')); ?>
                                <?php echo $this->formElementErrors($form->get('appGoodsBind'), ['class'=>'error-message']); ?>
                            </div>
                        </div>

                        <div class="form-group">
                            <label  class="col-sm-2 control-label"><?php echo $this->translate('商品发货仓库'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-6">
                                <?php echo $this->formElement($form->get('appGoodsWarehouse')); ?>
                                <?php echo $this->formElementErrors($form->get('appGoodsWarehouse'), ['class'=>'error-message']); ?>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('状态'); ?></label>
                            <div class="col-sm-6">
                                <div class="checkbox">
                                    <label>
                                        <?php echo $this->formElement($form->get('appState')); ?> <?php echo $this->translate('启用'); ?>
                                        <?php echo $this->formElementErrors($form->get('appState'), ['class'=>'error-message']); ?>
                                    </label>
                                </div>
                            </div>
                        </div>

                    </div>

                    <div>
                        <div class="box-footer">
                            <?php echo $this->formElement($form->get('dberp_csrf')); ?>
                            <button type="submit" class="btn btn-primary col-md-offset-2"><i class="fa fa-check"></i> <?php echo $this->translate('保存商城信息'); ?></button>
                        </div>
                    </div>

                </div>
            </div>
        </div>

    </section>
    <?php echo $this->form()->closeTag(); ?>

</div>
<script type="text/javascript">
    $(document).ready(function() {
        $("#<?php echo $form->getName(); ?>").validate({
            rules: {
                appName: {
                    required: true
                },
                appAccessId: {
                    required: true
                },
                appAccessSecret: {
                    required: true
                },
                appUrl: {
                    required: true
                },
                appUrlPort: {
                    required: true,
                    number: true,
                    min: 1
                },
                appType: {
                    required: true
                }
            },
            messages: {
                appName: {
                    required: "<?php echo $this->translate('商城名称不能为空！'); ?>"
                },
                appAccessId: {
                    required: "<?php echo $this->translate('appId不能为空！'); ?>"
                },
                appAccessSecret: {
                    required: "<?php echo $this->translate('APPSecret不能为空！'); ?>"
                },
                appUrl: {
                    required: "<?php echo $this->translate('商城url地址不能为空！'); ?>"
                },
                appUrlPort: {
                    required: "<?php echo $this->translate('端口不能为空！'); ?>",
                    number: "<?php echo $this->translate('必须为数字！'); ?>",
                    min: "<?php echo $this->translate('最小为1！'); ?>"
                },
                appType: {
                    required: "<?php echo $this->translate('商城类别不能为空！'); ?>"
                }
            }
        });
    });
    
    function createAccessId() {
        $.get('<?php echo $this->url('app', array('action'=>'createAccessId')); ?>',{}, function(data){
            if(data.content != '') {
                $('#appAccessId').val(data.content);
            }
        }, 'json');
    }
    function createAccessSecret() {
        $.get('<?php echo $this->url('app', array('action'=>'createAccessSecret')); ?>',{}, function(data){
            if(data.content != '') {
                $('#appAccessSecret').val(data.content);
            }
        }, 'json');
    }
</script>
