<?php

use home\assets\MainAsset;
use yii\helpers\Url;
use home\widgets\Alert;
use home\modules\statement\models\StatementAccount;
use yii\widgets\ActiveForm;
use yii\helpers\StringHelper;
use yii\helpers\ArrayHelper;
use common\assets\My97DatePickerAsset;

/* @var $this \yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \home\modules\rreimbursement\models\Application */

MainAsset::register($this);
My97DatePickerAsset::register($this);
$this->title = '对账单管理';
?>

<!--container-->
<div class="container manage-project fix">
    <?= $this->render('@home/views/site/_left-menu') ?>
    <?php
    ?>
    <!--manage-left -->
    <div class="manage-right">
        <div id="info" style="display:none;"></div>
        <div class="tab-column tab-manage">
            <div class="tab-right">
                <?php if (Yii::$app->session->get('isSuperAdmin') || Yii::$app->user->can('statement/statement-account/create')) : ?>
                    <button title="新增进账" type="button" data-height="300" data-width='550' data-url='<?= Url::to([$this->context->id . '/create']) ?>' data-title='新增对账单' class="btn btn-default btn-task open-dialog">
                        <i class="icon-plus"></i>
                    </button>
                <?php endif; ?>
                <?php if (Yii::$app->session->get('isSuperAdmin') || Yii::$app->user->can('statement/statement-import/import')) : ?>
                    <button title="导入进账单" type="button" data-height="300" data-width='550' data-url='<?= Url::to(['statement-import/import']) ?>' data-title='导入进账单' class="btn btn-default btn-task open-dialog">
                        <i class="icon-upload-alt"></i>
                    </button>
                <?php endif; ?>
            </div>
            <p class="caption">进账管理</p>
        </div>
        <?= Alert::widget(); ?>
        <?= $this->render('_search', ['model' => $model, 'get' => $get]) ?>
        <?php $mainForm = ActiveForm::begin([ 'id' => 'main-form', 'action' => '']); ?>
        <table class="table task-table">
            <tr>
                <th width="6%">日期</th>
                <th width="6%">摘要</th>
                <th width="6%">收款银行</th>
                <!--<th width="8%">收款账号</th>-->
                <th width="8%">对方账号</th>
                <th width="8%">对方户名</th>
                <th width="8%">对方银行</th>
                <th width="6%">借方金额(元)</th>
                <th width="6%">贷方金额(元)</th>
                <th width="8%">银行流水号</th>
                <th width="4%">收/支</th>
                <th width="10%">客户</th>
                <th width="7%">关联记录</th>
                <th width="6%">余额（元）</th>
                <th width="8%">操作</th>
            </tr>
            <?php
            $totalDebitPage = 0;
            $totalCreditPage = 0;
            $totalAlocateAmount = 0;
            ?>
            <?php if (empty($models)) : ?>
                <tr><td colspan="16" class="tac red">还没有任何数据...</td></tr>
            <?php else : ?>
                <?php foreach ($models as $model) : ?>
                    <tr>
                        <td><?= date("y.m.d", $model->payment_time) ?></td>
                        <td><?= $model->remark ?></td>
                        <td title="<?= !empty($model->account_no) ? '收款账号:' . $model->account_no : '' ?>"><?= !empty($model->bank_name) ? $model->bank_name : $model->account_no ?></td>
                        <!--<td><?= $model->account_no ?></td>-->
                        <td><?= $model->remittance_no ?></td>
                        <td><?= $model->remittance_user ?></td>
                        <td><?= $model->remittance_bank ?></td>
                        <td style="text-align: right"><?= number_format($model->account_debit, 2, '.', ',') ?></td>
                        <td style="text-align: right"><?= number_format($model->account_credit, 2, '.', ',') ?></td>
                        <td><?= $model->serial_number ?></td>
                        <td>
                            <?php
                            if ($model->account_debit > 0) {
                                echo "收";
                            } elseif ($model->account_credit > 0) {
                                echo "支";
                            }
                            ?>
                        </td>
                        <td>
                            <?php if (Yii::$app->user->can("statement/statement-account/statement-link") || Yii::$app->session->get('isSuperAdmin')): ?>
                                <?php if (empty($model->customer->customer_id)): ?>
                                    <a class="icon-plus btn btn-default btn-task" href="<?= Url::to(['statement-link', 'id' => $model->statement_id, 'from' => Url::to('')]) ?>"></a>
                                <?php else : ?>
                                    <a  href="<?= Url::to(['statement-link', 'id' => $model->statement_id, 'from' => Url::to('')]) ?>"><?= $model->customer->name ?></a>
                                <?php endif; ?>
                            <?php else: ?>
                                <?= ArrayHelper::getValue($model->customer,'name')?>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php if (Yii::$app->user->can("statement/statement-account/create-link-contract") || Yii::$app->session->get('isSuperAdmin')): ?>
                                <?php if (!empty($model->customer->customer_id)): ?>
                                    <a href="<?= Url::to(['statement-link', 'id' => $model->statement_id, 'from' => Url::to(''), 'type' => 'contract']) ?>">关联合同</a>
                                <?php endif; ?>
                            <?php endif; ?>
                        </td>
                        <!--为分配金额-->
                        <td style="text-align: right"><?= number_format((max([$model->account_debit, $model->account_credit]) - $model->allocateAmount), 2, '.', ',') ?></td>
                        <td>

                            <?php if (Yii::$app->session->get('isSuperAdmin') || Yii::$app->user->can('statement/statement-account/view')) : ?>
                                <a href="javascript:;" class="open-dialog"  data-url="<?= Url::to([$this->context->id . '/view', 'id' => $model->statement_id]) ?>" data-title='编辑' data-height="300" data-width='550'>编辑</a>
                            <?php else : ?>
                                <a href="javascript:;" class="gray" title="当前没有编辑权限"><strike>编辑</strike></a>
                            <?php endif; ?>

                            <?php if (Yii::$app->session->get('isSuperAdmin') || Yii::$app->user->can('statement/statement-account/delete')) : ?>
                                <a class="link-delete-account" onclick="if (!confirm('确认删除?删除进账的同时会删除现有关联关系以及金额分配！'))
                                            return false;" href="<?= Url::to(['delete', 'id' => $model->statement_id, 'from' => Url::to('')]) ?>">删除</a>
                               <?php else : ?>
                                <a href="javascript:;" class="gray" title="当前没有删除权限">删除</a>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php
                    $totalDebitPage += $model->account_debit;
                    $totalCreditPage += $model->account_credit;
                    $totalAlocateAmount+=max([$model->account_debit, $model->account_credit]) - $model->allocateAmount;
                    ?>
                <?php endforeach; ?>
            <?php endif; ?>
            <tr>
                <td>本页∑</td>
                <td colspan="15">
                    <span>借方金额&nbsp;<i class="icon-yen"></i>&nbsp;<?= number_format($totalDebitPage, 2, '.', ',') ?></span>
                    <span>贷方金额&nbsp;<i class="icon-yen"></i>&nbsp;<?= number_format($totalCreditPage, 2, '.', ',') ?></span>
                    <span>未分配金额&nbsp;<i class="icon-yen"></i>&nbsp;<?= number_format($totalAlocateAmount, 2, '.', ',') ?></span>
                </td>
            </tr>
            <tr>
                <td>全部∑</td>
                <td colspan="15">
                    <span>借方金额&nbsp;<i class="icon-yen"></i>&nbsp;<?= number_format($total['total_debit'], 2, '.', ',') ?></span>
                    <span>贷方金额&nbsp;<i class="icon-yen"></i>&nbsp;<?= number_format($total['total_credit'], 2, '.', ',') ?></span>
                    <span>未分配金额&nbsp;<i class="icon-yen"></i>&nbsp;<?= number_format(StatementAccount::getTotalAmount(), 2, '.', ',') ?></span>
                </td>
            </tr>
        </table>

        <?php ActiveForm::end(); ?>
        <div class="pages">
            <span class="pagetotal">共<em><?= $pages->totalCount ?></em>条记录<em><?= $pages->page + 1 ?>/<?= $pages->pageCount ?></em>页 </span>
            <?= \yii\widgets\LinkPager::widget(['pagination' => $pages, 'prevPageLabel' => '上一页', 'nextPageLabel' => '下一页', 'firstPageLabel' => '首页', 'lastPageLabel' => '末页']); ?>
        </div>
    </div>
    <!--manage-right--> 
</div>
<!--container-->
