<?php
$form = $this->form;
$form->prepare();
?>
<div class="content-wrapper">
    <?= $this->form()->openTag($form); ?>
    <section class="content-header">
        <?=$this->partial('layout/breadcrumb')?>

        <h1 class="pull-right">
            <a href="<?=$this->url('p-order');?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> <?=$this->translate('返回采购订单列表')?></a>
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-check"></i> <?=$this->translate('点击退货')?></button>
            <?=$this->HelpUrl('orderReturnAdd')?>
        </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"><?=$this->translate('订单信息');?></h3>
                    </div>

                    <div class="box-body">
                        <?=$this->formElementErrors($form->get('dberp_csrf'), ['class'=>'error-message']); ?>
                        <div class="row">
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?=$this->translate('采购单号')?>：</b><?=$this->escapeHtml($this->orderInfo->getPOrderSn());?>
                                </div>
                            </div>

                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b class="text-blue"><?=$this->translate('订单金额')?>：</b><?=$this->erpCurrencyFormat($this->orderInfo->getPOrderAmount());?>
                                </div>
                            </div>

                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?=$this->translate('付款方式')?>：</b><?=$this->PurchaseHelper()->orderPayment($this->orderInfo->getPaymentCode());?>
                                </div>
                            </div>

                        </div>

                        <div class="row">
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?=$this->translate('供应商')?>：</b><?=$this->escapeHtml($this->orderInfo->getOneSupplier()->getSupplierName());?>
                                </div>
                            </div>

                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?=$this->translate('供应商联系人')?>：</b><?=$this->escapeHtml($this->orderInfo->getSupplierContacts());?>
                                </div>
                            </div>

                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?=$this->translate('供应商手机')?>：</b><?=$this->escapeHtml($this->orderInfo->getSupplierPhone());?>
                                </div>
                            </div>

                        </div>

                        <div class="row">
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?=$this->translate('供应商座机')?>：</b><?=$this->escapeHtml($this->orderInfo->getSupplierTelephone());?>
                                </div>
                            </div>
                            <div class="col-xs-8 form-group">
                                <div class="col-sm-12">
                                    <b><?=$this->translate('备注')?>：</b><?=$this->escapeHtml($this->orderInfo->getPOrderInfo());?>
                                </div>
                            </div>

                        </div>

                        <div class="box-header">
                            <h4 class="box-title"><?=$this->translate('选择退货商品')?></h4>
                            &nbsp;
                            <div>
                                <?= $this->formElementErrors($form->get('pGoodsId'), ['class'=>'error-message']); ?>
                                <?= $this->formElementErrors($form->get('goodsReturnNum'), ['class'=>'error-message']); ?>
                                <?= $this->formElementErrors($form->get('goodsReturnAmount'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <div class="box-body table-responsive no-padding">
                            <table class="table table-hover">
                                <thead>
                                <tr>
                                    <th width="4%"><?=$this->translate('选择')?></th>
                                    <th width="10%"><?=$this->translate('商品编号')?></th>
                                    <th width="20%"><?=$this->translate('商品名称')?></th>
                                    <th width="10%"><?=$this->translate('商品规格')?></th>
                                    <th width="10%"><?=$this->translate('采购单价')?></th>
                                    <th width="6%"><?=$this->translate('采购数量')?></th>
                                    <th width="8%"><?=$this->translate('税金')?></th>
                                    <th width="10%"><?=$this->translate('采购总价')?></th>
                                    <th width="8%"><?=$this->translate('退货数量')?></th>
                                    <th width="12%"><?=$this->translate('退货金额')?></th>
                                </tr>
                                </thead>
                                <?php
                                foreach ($this->orderGoods as $goodsValue) {
                                    ?>
                                    <tr>
                                        <td><input type="checkbox" title="<?=$this->translate('请选择退货商品')?>" required minlength="1" name="pGoodsId[]" value="<?=$goodsValue->getPGoodsId()?>" <?=(isset($this->formData['pGoodsId']) && in_array($goodsValue->getPGoodsId(), $this->formData['pGoodsId'])) ? 'checked' : ''?> class="minimal"></td>
                                        <td><?=$this->escapeHtml($goodsValue->getGoodsNumber());?></td>
                                        <td><?=$this->escapeHtml($goodsValue->getGoodsName());?></td>
                                        <td><?=$this->escapeHtml($goodsValue->getGoodsSpec());?></td>
                                        <td><?=$this->erpCurrencyFormat($goodsValue->getPGoodsPrice());?><input type="hidden" name="goods_price_<?=$goodsValue->getPGoodsId()?>" id="goods_price_<?=$goodsValue->getPGoodsId()?>" value="<?=$this->erpCurrencyFormat($goodsValue->getPGoodsPrice(), '');?>"></td>
                                        <td><?=$this->escapeHtml($goodsValue->getPGoodsBuyNum());?><?=$this->escapeHtml($goodsValue->getGoodsUnit());?></td>
                                        <td><?=$this->erpCurrencyFormat($goodsValue->getPGoodsTax());?></td>
                                        <td><?=$this->erpCurrencyFormat($goodsValue->getPGoodsAmount());?></td>
                                        <td><input type="number" min="0" max="<?=$this->escapeHtml($goodsValue->getPGoodsBuyNum());?>" oninput="returnGoodsPrice(<?=$goodsValue->getPGoodsId()?>);" name="goodsReturnNum[<?=$goodsValue->getPGoodsId()?>]" id="goods_return_num_<?=$goodsValue->getPGoodsId()?>" value="<?=isset($this->formData['goodsReturnNum'][$goodsValue->getPGoodsId()]) ? $this->escapeHtml($this->formData['goodsReturnNum'][$goodsValue->getPGoodsId()]) : '0'?>" class="form-control"></td>
                                        <td><input type="text" oninput="returnGoodsPrice(<?=$goodsValue->getPGoodsId()?>);" name="goodsReturnAmount[<?=$goodsValue->getPGoodsId()?>]" id="goods_return_amount_<?=$goodsValue->getPGoodsId()?>" value="<?=isset($this->formData['goodsReturnAmount'][$goodsValue->getPGoodsId()]) ? $this->escapeHtml($this->formData['goodsReturnAmount'][$goodsValue->getPGoodsId()]) : '0'?>" class="form-control"></td>
                                    </tr>
                                <?php } ?>
                            </table>
                        </div>
                        <div class="row">
                            <div class="col-xs-12 form-group">
                                <div class="col-sm-12">
                                    <?=$this->translate('退货原因')?>
                                    <?= $this->formElement($form->get('pOrderReturnInfo')); ?>
                                    <?= $this->formElementErrors($form->get('pOrderReturnInfo'), ['class'=>'error-message']); ?>
                                </div>
                            </div>
                        </div>

                    </div>
                    <div class="box-footer">
                        <?=$this->formElement($form->get('dberp_csrf')); ?>
                        <button type="submit" class="btn btn-primary col-md-offset-5"><i class="fa fa-check"></i> <?=$this->translate('点击退货')?></button>
                    </div>
                </div>
            </div>
        </div>

    </section>
    <?= $this->form()->closeTag(); ?>
</div>
<script>
    $(document).ready(function() {
        $("#<?=$form->getName()?>").validate();
    });

    function returnGoodsPrice(id) {
        var goodsPrice  = $('#goods_price_'+id).val();
        goodsPrice  = goodsPrice == '' ? 0 : parseFloat(goodsPrice);
        var goodsNum    = $('#goods_return_num_'+id).val();
        goodsNum    = goodsNum == '' ? 1 : parseInt(goodsNum);

        var goodsAmount = goodsPrice * goodsNum;

        $('#goods_return_amount_'+id).val(erpNumber(goodsAmount, 2));

        sumAmount();
    }
    function sumAmount() {
    }
</script>