<div class="content-wrapper">
    <?php
    $form = $this->form;
    $form->prepare();
    $form->get('deliveryMode')->setAttributes(['value' => 2]);
    ?>
    <?php echo $this->form()->openTag($form); ?>
    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right" id="dbshop-menu-top">
            <a href="<?php echo isset($this->queryData['return']) ? $this->url('sales', ['action' => 'detail', 'id' => $this->orderInfo->getOrderId()]) : $this->url('sales', [], ['query' => $this->queryData]);?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> <?php echo $this->translate('返回'); ?></a>
            <?php if (empty($this->vGoodsStockFew)) { ?>
                <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-check"></i> <?php echo $this->translate('点击发货'); ?></button>
            <?php } ?>
            <?php echo $this->HelpUrl('shipOrder'); ?>
        </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 $this->translate('订单发货'); ?></h3>
                    </div>
                    <div class="box-body">
                        <?php echo $this->partial('layout/messages'); ?>

                        <?php if (!empty($this->vGoodsStockFew)) { ?>
                            <div class="col-sm-12">
                                <div class="alert alert-danger alert-dismissible">
                                    <i class="icon fa fa-exclamation"></i><?php echo $this->translate('将虚拟商品库存补全后，再进行发货处理!'); ?>
                                </div>
                            </div>
                        <?php } ?>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('订单编号'); ?></label>
                            <div class="col-sm-3">
                                <p class="form-control-static"><?php echo $this->escapeHtml($this->orderInfo->getOrderSn()); ?></p>
                            </div>
                        </div>
                        <?php if($this->orderDeliveryAddress && !empty($this->orderDeliveryAddress->getDeliveryTimeInfo())) { ?>
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><?php echo $this->translate('送货时间'); ?></label>
                                <div class="col-sm-3">
                                    <p class="form-control-static"><?php echo $this->escapeHtml($this->orderDeliveryAddress->getDeliveryTimeInfo()); ?></p>
                                </div>
                            </div>
                        <?php } ?>
                        <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('deliveryMode')); ?>
                                <?php echo $this->formElementErrors($form->get('deliveryMode'), ['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('statusInfo')); ?>
                                <?php echo $this->formElementErrors($form->get('statusInfo'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <?php echo $this->formElementErrors($form->get('goodsId'), ['class'=>'error-message']); ?>
                        <?php echo $this->formElementErrors($form->get('deliveryId'), ['class'=>'error-message']); ?>
                        <?php echo $this->formElementErrors($form->get('expressNumber'), ['class'=>'error-message']); ?>
                        <table class="table shop-table table-bordered">
                            <thead>
                            <tr class="shop-bg-thead">
                                <th width="30%"><?php echo $this->translate('商品名称'); ?></th>
                                <th width="15%"><?php echo $this->translate('商品货号'); ?></th>
                                <th width="15%"><?php echo $this->translate('商品规格'); ?></th>
                                <th width="8%"><?php echo $this->translate('购买数量'); ?></th>
                                <th width="16%"><?php echo $this->translate('快递信息'); ?><span class="must_add_value">*</span></th>
                                <th width="16%"><?php echo $this->translate('快递单号'); ?><span class="must_add_value">*</span></th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach($this->orderInfo->getOrderGoods() as $goodsValue) { ?>
                                <tr>
                                    <td>
                                        <input type="hidden" name="goodsId[]" value="<?php echo $goodsValue->getGoodsId(); ?>">
                                        <img src="<?php echo $this->goodsHelper()->goodsImage($goodsValue->getGoodsImage()); ?>" class="img-thumbnail" style="width: 50px;height: 50px;">
                                        <?php echo $this->escapeHtml($goodsValue->getGoodsName()); ?>
                                        <?php if (isset($this->vGoodsStockFew[$goodsValue->getGoodsId()])) echo '(<span class="text-red">'.$this->translate('需补货数量').'：'.$this->vGoodsStockFew[$goodsValue->getGoodsId()]['num'].'</span>)'; ?>
                                    </td>
                                    <td><?php echo $this->escapeHtml($goodsValue->getGoodsItem()); ?></td>
                                    <td>
                                        <?php if(!empty($goodsValue->getInSpecGoodsStr())) { echo $this->escapeHtml($goodsValue->getInSpecGoodsStr()); } else echo $this->translate('无'); ?>
                                    </td>
                                    <td><?php echo $goodsValue->getBuyNum(); ?></td>
                                    <?php if (isset($this->vGoodsStockFew[$goodsValue->getGoodsId()])) { ?>
                                        <td></td>
                                        <td></td>
                                    <?php } else { ?>
                                        <td>
                                            <select name="deliveryId[]" class="form-control">
                                                <?php foreach ($this->deliveryArray as $dKey => $dValue) { ?>
                                                    <option value="<?php echo $dKey ?>"><?php echo $this->escapeHtml($dValue); ?></option>
                                                <?php } ?>
                                            </select>
                                        </td>
                                        <td>
                                            <input type="text" name="expressNumber[]" class="form-control">
                                        </td>
                                    <?php } ?>
                                </tr>
                            <?php } ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <?php echo $this->form()->closeTag(); ?>

</div>
<script type="text/javascript">
    $(document).ready(function() {
        $("#<?php echo $form->getName(); ?>").validate({
            rules: {
                statusInfo: {
                    required: true
                }
            },
            messages: {
                statusInfo: {
                    required: "<?php echo $this->translate('操作备注不能为空!'); ?>"
                }
            },
            submitHandler: function(form) {
                let deliveryIdStatus = true;
                $("select[name='deliveryId[]']").each(function() {
                    if(isNaN(this.value) || this.value <= 0) {
                        shopMessage('<?php echo $this->translate('请选择快递信息!'); ?>');
                        deliveryIdStatus = false;
                        return false;
                    }
                });
                if(!deliveryIdStatus) return false;

                let expressNumberStatus = true;
                $("input[name='expressNumber[]']").each(function() {
                    if(this.value.length <= 0) {
                        shopMessage('<?php echo $this->translate('请填写快递单号!'); ?>');
                        expressNumberStatus = false;
                        return false;
                    }
                });
                if(!expressNumberStatus) return false;

                form.submit();
            }
        });
    });

    $('#deliveryMode').change(function () {
        if(this.value == 1) {
            window.location.href = "<?php echo $this->url('sales', ['action' => 'shipOrder', 'id' => $this->orderInfo->getOrderId()], ['query' => array_merge($this->queryData, ['mode' => 1])]);?>";
        }
    });
</script>
