<?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;
use yii\helpers\Html;
use home\modules\customer\models\Category;
use home\modules\customer\models\District;
use home\modules\statement\models\StatementLinkContract;

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

My97DatePickerAsset::register($this);

$assetUrl = $this->getAssetManager()->getBundle(MainAsset::className())->baseUrl;
$this->registerJsFile($assetUrl . '/js/statement.contract.js', ['depends' => MainAsset::className()]);

$this->title = '对账单关联合同';
?>

<!--container-->
<div class="container manage-project fix">
    <?= $this->render('@home/views/site/_left-menu') ?>
    <?= Alert::widget(); ?>
    <!--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>

        <div class="task-title">
            <p class="lefttitle">
                进账详情
            </p>
        </div>

        <div class="mgt10">
            <table class="table task-table taskdetail-table">
                <tr>
                    <td width="10%" class="table-blue">银行流水号</td>
                    <td width="20%"><?= $model->serial_number ?></td>
                    <td width="10%" class="table-blue">日期</td>
                    <td width="20%"><?= date('Y-m-d', $model->payment_time) ?></td>
                    <td width="10%" class="table-blue">摘要</td>
                    <td><?= $model->remark ?></td>
                </tr>
                <tr>
                    <td width="10%" class="table-blue">收款银行</td>
                    <td width="20%"><?= $model->bank_name ?></td>
                    <td width="10%" class="table-blue">收款账号</td>
                    <td colspan="3"><?= $model->account_no ?></td>
                </tr>
                <tr>
                    <td width="10%" class="table-blue">对方银行</td>
                    <td width="20%"><?= $model->remittance_bank ?></td>
                    <td width="10%" class="table-blue">对方账号</td>
                    <td width="20%"><?= $model->remittance_no ?></td>
                    <td width="10%" class="table-blue">对方户名</td>
                    <td id="remittance_user"><?= $model->remittance_user ?></td>
                </tr>
                <tr>
                    <td width="10%" class="table-blue">借方金额(元)</td>
                    <td width="20%"><?= $model->account_debit ?></td>
                    <td width="10%" class="table-blue">贷方金额(元)</td>
                    <td width="20%"><?= $model->account_credit ?></td>
                    <td width="10%" class="table-blue">未关联金额(元)</td>
                    <td id="allocate-amount"> <?= number_format((max([$model->account_debit, $model->account_credit]) - $model->allocateAmount), 2, '.', ',') ?></td>
                </tr>
                <tr>
                    <td width="10%" class="table-blue">提交人</td>
                    <td width="20%"><?= $model->user->realname ?></td>
                    <td width="10%" class="table-blue">更新日期</td>
                    <td width="20%"><?= date('Y-m-d', $model->update_time) ?></td>
                    <td width="10%" class="table-blue">关联客户</td>
                    <td width="20%"> 
                        <?php if(!empty($model['customer'])): ?>
                            <?= $model['customer']['name']; ?>

                            <?php if(Yii::$app->session->get('isSuperAdmin') || Yii::$app->user->can('statement/statement-account/delete-link')) : ?>
                                <a href="javascript:void(0);" title="解除客户关联" id="delete_link_customer" date-customer_no="<?= $model['customer']['customer_no'] ?>" date-statement_id="<?= $model->statement_id ?>"><i class="icon-unlink" ></i></a>
                            <?php else : ?>
                                <a href="javascript:;" class="gray" title="当前没有删除权限"><i class="icon-unlink" ></i></a>
                            <?php endif; ?>
                            
                        <?php endif; ?>
                    </td>
                </tr>
            </table>
        </div>
        <div class="mgt10" id='data_list'>
            <table class="table task-table">
                <tr>
                    <th width="10%">关联金额(元)</th>
                    <th width="10%">合同编号</th>
                    <th >合同名称</th>
                    <th width="10%">所有人</th>
                    <th width="10%">添加人</th>
                    <th width="10%">添加时间</th>
                    <th width="10%">操作</th>
                </tr>
                <?php $linkContracts = StatementLinkContract::getLinkContracts(['statement_id' => $model->statement_id]) ?>
                <?php if(!empty($linkContracts)): ?>
                    <tbody>
                        <?php foreach ($linkContracts as $key => $contractItem): ?>
                            <tr>
                                <td>
                                    <?php if(Yii::$app->session->get('isSuperAdmin') || Yii::$app->user->can('statement/statement-link-contract/view')) : ?>
                                        <a href="javascript:;" class="open-dialog"  data-url="<?= Url::to(['statement-link-contract/view', 'id' => $contractItem->link_contract_id]) ?>" data-title='编辑' data-height="120" data-width='280'>
                                            <?= number_format($contractItem->money, 2, '.', ',') ?>
                                        </a>
                                    <?php else : ?>
                                        <?= number_format($contractItem->money, 2, '.', ',') ?>
                                    <?php endif; ?>
                                </td>
                                <td><?= $contractItem->contract_no ?></td>
                                <td><a title="<?= $contractItem->contractItems->name ?>" target="_blank" href="<?= Url::to(['/contract/contract/detail', 'id' => $contractItem->contractItems->contract_id]) ?>"><?= $contractItem->contractItems->name ?></a></td>
                                <td><?= $contractItem->contractItems->user->realname ?></td>
                                <td><?= $contractItem->user->realname ?></td>
                                <td><?= date('Y/m/d', $contractItem->create_time) ?></td>
                                <td>
                                    <?php if(Yii::$app->session->get('isSuperAdmin') || Yii::$app->user->can('statement/statement-link-contract/delete')) : ?>
                                        <a class="link-delete" href="<?= Url::to(['statement-link-contract/delete', 'id' => $contractItem->link_contract_id, 'from' => Url::to('')]) ?>">删除</a>
                                    <?php else : ?>
                                        <a href="javascript:;" class="gray" title="当前没有删除权限">删除</a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                <?php endif; ?>
            </table>
        </div>
        <div class="task-title">
            <p class="lefttitle">
                客户合同
            </p>
        </div>
        <div class="mgt10" id='data_list'>
            <table class="table task-table">
                <tr>
                    <th width="5%">选择</th>
                    <th width="10%">关联金额(元)</th>
                    <th width="10%">合同编号</th>
                    <th >合同名称</th>
                    <th width="10%">所有人</th>
                    <th width="10%">签订时间</th>
                    <th width="10%">金额(元)</th>
                    <th width="10%">已回款(元)</th>
                </tr>
                <?php $customerContracts = StatementAccount::getContracts($model->statement_id) ?>
                <?php if(!empty($customerContracts)): ?>
                    <tbody id="contract-info">
                        <?php foreach ($customerContracts as $key => $contract): ?>
                            <tr>
                                <td><input type='checkbox' name='link[]' class='box-link' value='<?= $contract->contract_no ?>'/></td>
                                <td><input type="text" class="link-money" name="<?= $contract->contract_no ?>" value=""/></td>
                                <td><?= $contract->contract_no ?></td>
                                <td><a title="<?= $contract->name ?>" target="_blank" href="<?= Url::to(['/contract/contract/detail', 'id' => $contract->contract_id]) ?>"><?= $contract->name ?></a></td>
                                <td><?= $contract->user->realname ?></td>
                                <td><?= date('Y/m/d', $contract->signing_time) ?></td>
                                <td style="text-align: right"><?= number_format($contract->money, 2, '.', ',') ?></td>
                                <td style="text-align: right"><?= number_format($contract->receivableMoney, 2, '.', ',') ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                <?php endif; ?>
            </table>
            <p style="text-align:right; padding-right:30px;">
                <button type="button" class="btn btn-primary btn-general save add-link-contract">提交</button>
            </p>
        </div>

    </div>
    <!--manage-right-->
</div>
<!--container-->

<script>
    var ajaxGetCustomerUrl = "<?= Url::to([$this->context->id . '/ajax-get-customer']) ?>";
    var linkContractUrl = "<?= Url::to([$this->context->id . '/create-link-contract']) ?>";
    var ajaxDeleteLinkUrl = "<?= Url::to([$this->context->id . '/delete-link']) ?>";
    var customerNo = "<?= empty($model->statementLink->link_id) ? '' : $model->statementLink->link_id ?>";
    var statementId =<?= $model->statement_id ?>;
    var remainingMoney =<?= max([$model->account_debit, $model->account_credit]) - $model->allocateAmount ?>;
</script>