<?php

use home\assets\MainAsset;
use home\assets\MenuAsset;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\StringHelper;
use home\modules\contract\models\Contract;
use home\modules\contract\models\Category;
use home\models\Attachment;
use home\modules\receivable\models\Plan;

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

MenuAsset::register($this);

$this->title = '合同详情';
?>

<!--container-->
<div class="container manage-project fix">

    <?= $this->render('@home/views/site/_left-menu') ?>

    <div class="manage-right">

        <div class="tab-column tab-manage">
            <div class="tab-right">
                <?php if (Contract::checkAccess($model, 'contract/contract/view')) : ?>
                    <button title="编辑" type="button" data-url='<?= Url::to([$this->context->id . '/view', 'id' => $model['contract_id']]) ?>' data-height='510' data-width='800' data-title='编辑' class="btn btn-default btn-task open-contract-dialog">
                        <i class="icon-edit"></i>
                    </button>
                <?php endif; ?>
                <?php if (Contract::checkAccess($model, 'contract/contract/delete')) : ?>
                    <button title="删除" type="button" onclick="if (!confirm('确认删除?'))
                                    return false;
                                javascript:window.location.href = '<?= Url::to([$this->context->id . '/delete', 'id' => $model['contract_id'], 'from' => (isset($_GET['from'])) ? $_GET['from'] : '']) ?>'" class="btn btn-default btn-task">
                        <i class="icon-trash"></i>
                    </button>
                <?php endif; ?>
            </div>
            <p class="caption">合同详情</p>
        </div>

        <div class="task-title">
            <p class="lefttitle">
                <span>合同名称：</span><?= $model['name'] ?>&nbsp;&nbsp;
                <span>合同类别：</span><?= $model['category']['name'] ?>&nbsp;&nbsp;
                <span>任务状态：</span><font class="contract-status"><?= $model['status_name'] ?></font>
            </p>
        </div>

        <div class="mgt10">
            <table class="table task-table taskdetail-table">
                <tr>
                    <td width="20%" class="table-blue">合同编号</td>
                    <td width="30%"><?= $model['contract_no'] ?></td>
                    <td width="20%" class="table-blue"></td>
                    <td></td>
                </tr>
                <tr>
                    <td width="20%" class="table-blue">签约客户名称</td>
                    <td title="<?= $model['customer']['name'] ?>" width="30%"><a target="_blank" href="<?= Url::to(['/customer/customer/detail', 'id' => $model['customer']['customer_id']]) ?>"><?=
                            StringHelper::truncate($model['customer']['name'], 15)
                            ?></a></td>
                    <td width="20%" class="table-blue">真实客户名称</td>
                    <td title="<?= $model['realCustomer']['name'] ?>" width="30%"><a target="_blank" href="<?= Url::to(['/customer/customer/detail', 'id' => $model['realCustomer']['customer_id']]) ?>"><?=
                            StringHelper::truncate($model['realCustomer']['name'], 15)
                            ?></a></td>
                </tr>
                <tr>
                    <td width="20%" class="table-blue">签订日期</td>
                    <td width="30%"><?= date('Y-m-d', $model['signing_time']) ?></td>
                    <td width="20%" class="table-blue">到期日期</td>
                    <td width="30%"><?= date('Y-m-d', $model['due_time']) ?></td>
                </tr>

                <tr>
                    <td width="20%" class="table-blue">验收日期</td>
                    <td width="30%"><?= date('Y-m-d', $model['acceptance_time']) ?></td>
                    <td width="20%" class="table-blue">合同类别</td>
                    <td width="30%"><?= $model['category']['name'] ?></td>
                </tr>

                <tr>
                    <td width="20%" class="table-blue">维保期开始日期</td>
                    <td width="30%"><?= date('Y-m-d', $model['begin_time']) ?></td>
                    <td width="20%" class="table-blue">维保期结束日期</td>
                    <td width="30%"><?= date('Y-m-d', $model['end_time']) ?></td>
                </tr>


                <tr>
                    <td width="20%" class="table-blue">合同所有人</td>
                    <td width="30%"><?= $model['user']['realname'] ?></td>
                    <td width="20%" class="table-blue">合同金额(元)</td>
                    <td width="30%">
                        <?php if (Yii::$app->session->get('isSuperAdmin')||Yii::$app->user->can('contract/contract/amount')) : ?>
                            <?= number_format($model['money'], 2, '.', ','); ?>
                        <?php else: ?>
                            ###
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <td width="20%" class="table-blue">外采购金额(元)</td>
                    <td width="30%">
                        <?php if (Yii::$app->session->get('isSuperAdmin')||Yii::$app->user->can('contract/contract/amount')) : ?>
                            <?= number_format($model['purchase_amount'], 2, '.', ','); ?>
                        <?php else: ?>
                            ###
                        <?php endif; ?>
                    </td>
                    <td width="20%" class="table-blue">硬件金额(元)</td>
                    <td width="30%">
                        <?php if (Yii::$app->session->get('isSuperAdmin')||Yii::$app->user->can('contract/contract/amount')) : ?>
                            <?= number_format($model['hardware_amount'], 2, '.', ','); ?>
                        <?php else: ?>
                            ###
                        <?php endif; ?>
                    </td>
                </tr>
               <?php if (Yii::$app->session->get('isSuperAdmin')||Yii::$app->user->can('contract/contract/amount')) : ?>
                    <!-- 付款方式说明 begin -->
                    <tr>
                        <td  class="table-blue">付款方式说明</td>
                        <td colspan="3">
                            <?php if (empty($model->paymentInstruction)) : ?>
                        <tr><td colspan="3"></td></tr>
                    <?php else : ?>
                        <table class="table task-table">
                            <tr>
                                <th  width='10%'>付款批次</th>
                                <th  width='10%'>付款比例</th>
                                <th  width='10%'>付款金额</th>
                                <th>备注</th>
                            </tr>
                            <?php
                            $totalSum = 0;
                            foreach ($model->paymentInstruction as $instruction) :
                                ?>
                                <?php
                                $totalSum += $instruction['sum'];
                                ?>
                                <tr>
                                    <td  style="text-align:center"><?= $instruction['sort'] ?></td>
                                    <td  style="text-align:right"><?= $instruction['percentage'] ?>%</td>
                                    <td  style="text-align:right"><?= number_format($instruction['sum'], 2, '.', ',') ?></td>
                                    <td colspan="2"><?= $instruction['remark'] ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <tr >
                                <td colspan="6"><center>
                                总额：<?= number_format($totalSum, 2, '.', ',') ?> 元</center>
                            </td>
                            </tr>
                        </table>
                    <?php endif; ?>
                    </td>
                    </tr>

                    <!-- 付款方式说明 end -->
                <?php endif; ?>
                <tr>
                    <td width="20%" class="table-blue">备注</td>
                    <td colspan="3" style="text-align:left" style="padding-left: 6px;">
                        <!--<div style="width: 100%; height: 120px;overflow-y:scroll;" >-->
                        <?= !empty($model['remark']) ? $model['remark'] : '' ?>
                        <!--</div>-->
                    </td>
                </tr>
                <tr>
                    <td width="20%" class="table-blue">附件</td>
                    <td colspan="3" class="files"><?= !empty($model['attach_ids']) ? Attachment::getDownList($model['attach_ids']) : '' ?></td>
                </tr>
            </table>
        </div>

        <?php if (Yii::$app->session->get('isSuperAdmin')||Yii::$app->user->can('contract/contract/record')) : ?>
            <div class="fix sale-all">

                <!--loglist-->
                <div class="sale-list sale-list1 fl">
                    <div class="task-title rel">
                        回款计划
                        <?php if (Yii::$app->session->get('isSuperAdmin')||Yii::$app->user->can('receivable/plan/create')) : ?>
                            <button title="新增回款计划" type="button" data-url='<?= Url::to(['/receivable/plan/create', 'contract_no' => $model['contract_no']]) ?>' data-title='新增回款计划' data-width="550" data-height="320" class="btn btn-default open-dialog add-btn">
                                <i class="icon-plus"></i>
                            </button>
                        <?php endif; ?>
                    </div>
                    <div class="salelists">
                        <?php if (!empty($model['receivablePlan'])) : ?>
                            <ul>
                                <?php if (Yii::$app->session->get('isSuperAdmin') || Yii::$app->user->can('receivable/plan/detail')) : ?>
                                    <?php foreach ($model['receivablePlan'] as $v) : ?>
                                        <li>
                                            <a href="javascript:;" class="open-only-see-dialog"  data-url="<?= Url::to(['/receivable/plan/detail', 'id' => $v['plan_id'], 'type' => 'contract']) ?>" data-title='查看计划详情' data-width="800" data-height="200">
                                                <?= date('Y-m-d', $v['plan_time']) . ' 计划回款 ' . number_format($v['plan_money'], 2, '.', ' ,'); ?>  (<?=
                                                ($v['status'] == Plan::STATUS_ENABLE) ? '<font color="green">已完成</font>' : '<font color="red">未完成</font>'
                                                ?>)
                                            </a>
                                        </li>
                                    <?php endforeach; ?>
                                <?php else : ?>
                                    <?php foreach ($model['receivablePlan'] as $v) : ?>
                                        <li>
                                            <?= date('Y-m-d', $v['plan_time']) . ' 计划回款 ' . number_format($v['plan_money'], 2, '.', ' ,'); ?>  (<?=
                                            ($v['status'] == Plan::STATUS_ENABLE) ? '<font color="green">已完成</font>' : '<font color="red">未完成</font>'
                                            ?>)
                                        </li>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </ul>
                        <?php endif; ?>
                    </div>
                </div>
                <!--loglist-->
                <div class="sale-list sale-list2 fl">
                    <div class="task-title rel">
                        回款记录
                        <?php if (Yii::$app->session->get('isSuperAdmin')||Yii::$app->user->can('receivable/receivable/create')) : ?>
                            <button title="新增回款记录" type="button" data-url='<?= Url::to(['/receivable/receivable/create', 'contract_no' => $model['contract_no']]) ?>' data-title='新增回款记录' data-width="580" data-height="360" class="btn btn-default  open-dialog add-btn">
                                <i class="icon-plus"></i>
                            </button>
                        <?php endif; ?>
                    </div>
                    <div class="salelists">
                        <?php if (!empty($model['receivable'])) : ?>
                            <ul>
                                <?php if (Yii::$app->session->get('isSuperAdmin')||Yii::$app->user->can('receivable/receivable/detail')) : ?>
                                    <?php foreach ($model['receivable'] as $v) : ?>
                                        <li>
                                            <a href="javascript:;" class="open-only-see-dialog"  data-url="<?= Url::to(['/receivable/receivable/detail', 'id' => $v['rece_id'], 'type' => 'contract']) ?>" data-title='查看记录详情' data-width="750" data-height="200">
                                                <?= date('Y-m-d', $v['to_date']) . ' 回款 ' . number_format($v['rece_money'], 2, '.', ' ,') ?>
                                            </a>
                                        </li>
                                    <?php endforeach; ?>
                                <?php else : ?>
                                    <?php foreach ($model['receivable'] as $v) : ?>
                                        <li>
                                            <?= date('Y-m-d', $v['to_date']) . ' 回款 ' . number_format($v['rece_money'], 2, '.', ' ,') ?>
                                        </li>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </ul>
                        <?php endif; ?>
                    </div>
                    <?php if (!empty($model['receivable'])) : ?>
                        <div class="totalprice">
                            总金额：<span><?= $model['receivableMoney'] ?></span>
                        </div>
                    <?php endif; ?>
                </div>
                <!--loglist-->
                <div class="sale-list sale-list3 fr">
                    <div class="task-title rel">
                        开票记录
                        <?php if (Yii::$app->session->get('isSuperAdmin')||Yii::$app->user->can('receivable/billing/create')) : ?>
                            <button title="新增开票记录" type="button" data-url='<?= Url::to(['/receivable/billing/create', 'contract_no' => $model['contract_no']]) ?>'  data-title='新增开票记录' data-width="530" data-height="350" class="btn btn-default open-dialog add-btn">
                                <i class="icon-plus"></i>
                            </button>
                        <?php endif; ?>
                    </div>
                    <div class="salelists">
                        <?php if (!empty($model['billing'])) : ?>
                            <ul>
                                <?php if (Yii::$app->session->get('isSuperAdmin')||Yii::$app->user->can('receivable/billing/detail')) : ?>
                                    <?php foreach ($model['billing'] as $v) : ?>
                                        <li>
                                            <a href="javascript:;" class="open-only-see-dialog" data-url="<?= Url::to(['/receivable/billing/detail', 'id' => $v['record_id'], 'type' => 'contract']) ?>" data-title='查看开票详情' data-width="750" data-height="70">
                                                <?= date('Y-m-d', $v['invoice_time']) . ' 开票 ' . number_format($v['invoice_money'], 2, '.', ' ,'); ?>
                                            </a>
                                        </li>
                                    <?php endforeach; ?>
                                <?php else : ?>
                                    <?php foreach ($model['billing'] as $v) : ?>
                                        <li>
                                            <?= date('Y-m-d', $v['invoice_time']) . ' 开票 ' . number_format($v['invoice_money'], 2, '.', ' ,'); ?>
                                        </li>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </ul>
                        <?php endif; ?>
                    </div>
                    <?php if (!empty($model['billing'])) : ?>
                        <div class="totalprice">
                            总金额：<span><?= $model['billingMoney'] ?></span>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        <?php endif; ?>
    </div>
    <!--manage-right-->
</div>
<!--container-->