<?php

use home\assets\MainAsset;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\helpers\StringHelper;
use home\modules\contract\models\Contract;
use common\assets\My97DatePickerAsset;
use common\assets\ZTreeAsset;

/* @var $this yii\web\View */
$this->title = '合同列表';
$assetUrl = $this->getAssetManager()->getBundle(MainAsset::className())->baseUrl;
$this->registerJsFile($assetUrl . '/js/contract.status.js', ['depends' => [MainAsset::className(), My97DatePickerAsset::className()]]);
MainAsset::register($this);
?>

<?= \home\widgets\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="70"><label style="font-weight:lighter" ><input type="checkbox" name="select_all" class="box-select-all" style="position:relative;top:2px;" /> 全选</label></th>-->
                <th width="6%">合同编号</th>
                <th width="10%">合同名称</th>
                <th width="13%">真实客户</th>
                <th width="8%">签约客户</th>
                <th width="8%">所有人</th>
                <th width="8%">签订日期</th>
                <th width="9%">金额(元)</th>
                <th width="8%">已回款</th>
                <th width="8%">已开票</th>
                <th width="6%">状态</th>
                <th width="8%">跟进日期</th>
                <th width="2%">开票</th>
                <th width="2%">回款</th>
                <th width="2%">核对</th>
                <!--<th width="9%">操作</th>-->
            </tr>
            <?php
            $totalMoneyPage = 0;
            $totalReceivableMoneyPage = 0;
            $totalBillingMoneyPage = 0;
            ?>
            <?php if (empty($models)) : ?>
                <tr><td colspan="14" class="tac red">还没有任何数据...</td></tr>
            <?php else : ?>

                <?php foreach ($models as $key => $model) : ?>

                    <tr>
                        <!-- 全选框 -->
        <!--                        <td><input type="checkbox" name="delete[]" class="box-delete" value="" /></td>-->
                        <!-- 合同编号  合同名称-->
                        <?php if (Contract::readAccess('contract/contract/detail')) : ?>
                            <td class='theleft-min'><a target="_blank" href="<?= Url::to(['detail', 'id' => $model['contract_id']]) ?>"><?= $model['contract_no'] ?></a></td>
                            <td class='theleft-min' title="<?= $model['name']; ?>"><a target="_blank"  href="<?= Url::to(['detail', 'id' => $model['contract_id']]) ?>"><?=
                                    StringHelper::truncate($model['name'], 5, '')
                                    ?></a>
                            </td>
                        <?php else : ?>
                            <td class='theleft-min'><?= $model['contract_no'] ?></td>
                            <td class='theleft-min' title="<?= $model['name']; ?>"><?= StringHelper::truncate($model['name'], 5, '') ?></td>
                        <?php endif; ?>
                        <!-- 真实客户 -->
                        <?php if (Contract::readAccess('customer/customer/detail')) : ?>
                            <td class='theleft-min' title="<?= $model['realCustomer']['name'] ?>" >
                                <a target="_blank" href="<?= Url::to(['/customer/customer/detail', 'id' => $model['realCustomer']['customer_id'], 'from' => Url::to('')]) ?>  ">
                                    <?=
                                    StringHelper::truncate($model['realCustomer']['name'], 8, '')
                                    ?>
                                </a>
                            </td>
                        <?php else : ?>
                            <td class='theleft-min' title="<?= $model['realCustomer']['name'] ?>" >
                                <?=
                                StringHelper::truncate(!empty($model['realCustomer']['simple_name']) ? $model['realCustomer']['simple_name'] : $model['realCustomer']['name'], 4, '')
                                ?>
                            </td>
                        <?php endif; ?>
                        <!-- 签约客户 -->
                        <?php if (Contract::readAccess('customer/customer/detail')) : ?>
                            <td class='theleft-min' title="<?= $model['customer']['name'] ?>" >
                                <a target="_blank" href="<?= Url::to(['/customer/customer/detail', 'id' => $model['customer']['customer_id'], 'from' => Url::to('')]) ?>  ">
                                    <?=
                                    StringHelper::truncate(!empty($model['customer']['simple_name']) ? $model['customer']['simple_name'] : $model['customer']['name'], 4, '')
                                    ?>
                                </a>
                            </td>
                        <?php else : ?>
                            <td class='theleft-min' title="<?= $model['customer']['name'] ?>" >
                                <?=
                                StringHelper::truncate(!empty($model['customer']['simple_name']) ? $model['customer']['simple_name'] : $model['customer']['name'], 4, '')
                                ?>
                            </td>
                        <?php endif; ?>

                        <!-- 商务负责人 -->
                        <td title="<?= $model['user']['realname'] ?>"><?= StringHelper::truncate($model['user']['realname'], 5, '') ?></td>
                        <!-- 签订日期 -->
                        <td><?= date('Y/m/d', $model['signing_time']); ?></td>
                        <!-- 金额(元) -->
                        <td>
                            <?= number_format($model['money'], 2, '.', ',') ?>
                        </td>
                        <!-- 已回款 -->
                        <td class='theright-min'><?= number_format($model['receivableMoney'], 2, '.', ',') ?></td>
                        <!-- 已开票 -->
                        <td class='theright-min'><?= number_format($model['billingMoney'], 2, '.', ',') ?></td>

                        <?php
                        $totalMoneyPage += $model['money'];
                        $totalReceivableMoneyPage += $model['receivableMoney'];
                        $totalBillingMoneyPage += $model['billingMoney'];
                        ?>
                        <!-- 合同状态 -->
                        <td class='theleft-min'><?= $model['info']['name'] ?></td>
                        <!-- 待跟进日期 -->
                        <td><?= Contract::judgeFollow($model['contract_id']) ?></td>
                        <!--开票-->
                        <td>
                            <?php if (!Yii::$app->session->get('isSuperAdmin') && !Contract::readAccess('contract/contract/set-check')) : ?>
                                <?= ($model['bill_collate_status'] == Contract::CHECK_ENABLE) ? '<font color="green">已</font>' : '<font color="red">未</font>' ?>
                            <?php else : ?>
                                <?php if ($model['bill_collate_status'] == Contract::CHECK_ENABLE) : ?>
                                    <a href="javascript:;" class="set-status"  data-url="<?= Url::to(['set-check', 'id' => $model['contract_id'], 'check' => Contract::CHECK_DISABLE, 'status_name' => 'bill_collate_status']) ?>"><font color="green">已</font></a>
                                <?php else : ?>
                                    <a href="javascript:;"    class="set-status"  data-url="<?= Url::to(['set-check', 'id' => $model['contract_id'], 'check' => Contract::CHECK_ENABLE, 'status_name' => 'bill_collate_status']) ?>"><font color="red">未</font></a>
                                <?php endif; ?>
                            <?php endif; ?>
                        </td>
                        <!--回款-->
                        <td>
                            <?php if (!Yii::$app->session->get('isSuperAdmin') && !Contract::readAccess('contract/contract/set-check')) : ?>
                                <?= ($model['money_collate_status'] == Contract::CHECK_ENABLE) ? '<font color="green">已</font>' : '<font color="red">未</font>' ?>
                            <?php else : ?>
                                <?php if ($model['money_collate_status'] == Contract::CHECK_ENABLE) : ?>
                                    <a href="javascript:;" class="set-status"  data-url="<?= Url::to(['set-check', 'id' => $model['contract_id'], 'check' => Contract::CHECK_DISABLE, 'status_name' => 'money_collate_status']) ?>"><font color="green">已</font></a>
                                <?php else : ?>
                                    <a href="javascript:;"    class="set-status"  data-url="<?= Url::to(['set-check', 'id' => $model['contract_id'], 'check' => Contract::CHECK_ENABLE, 'status_name' => 'money_collate_status']) ?>"><font color="red">未</font></a>
                                <?php endif; ?>
                            <?php endif; ?>
                        </td>
                        <!-- 核对状态 -->
                        <td>
                            <?php if (!Yii::$app->session->get('isSuperAdmin') && !Contract::readAccess('contract/contract/set-check')) : ?>
                                <?= ($model['collate_status'] == Contract::CHECK_ENABLE) ? '<font color="green">已</font>' : '<font color="red">未</font>' ?>
                            <?php else : ?>
                                <?php if ($model['collate_status'] == Contract::CHECK_ENABLE) : ?>
                                    <a href="javascript:;" class="set-status"  data-url="<?= Url::to(['set-check', 'id' => $model['contract_id'], 'check' => Contract::CHECK_DISABLE, 'status_name' => 'collate_status']) ?>"><font color="green">已</font></a>
                                <?php else : ?>
                                    <a href="javascript:;"    class="set-status"  data-url="<?= Url::to(['set-check', 'id' => $model['contract_id'], 'check' => Contract::CHECK_ENABLE, 'status_name' => 'collate_status']) ?>"><font color="red">未</font></a>
                                <?php endif; ?>
                            <?php endif; ?>
                        </td>
                        <!-- 操作 -->
                    </tr>
                <?php endforeach; ?>
            <?php endif; ?>
            <?php if (Yii::$app->session->get('isSuperAdmin') || Contract::readAccess('contract/contract/amount')) : ?>
                <tr>
                    <td>本页∑</td>
                    <td colspan="13">
                        <div><span>金额&nbsp;<i class="icon-yen"></i>&nbsp;<?= number_format($totalMoneyPage, 2, '.', ',') ?></span>
                            <span>已回款&nbsp;<i class="icon-yen"></i>&nbsp;<?= number_format($totalReceivableMoneyPage, 2, '.', ',') ?></span>
                            <span>已开票&nbsp;<i class="icon-yen"></i>&nbsp;<?= number_format($totalBillingMoneyPage, 2, '.', ',') ?></span>
                    </td>
                </tr>
                <tr>
                    <td>全部∑</td>
                    <td colspan="13">
                        <span>金额&nbsp;<i class="icon-yen"></i>&nbsp;<?= number_format($total['money'], 2, '.', ',') ?></span>
                        <span>已回款&nbsp;<i class="icon-yen"></i>&nbsp;<?= number_format($total['rece_money'], 2, '.', ',') ?></span>
                        <span>已开票&nbsp;<i class="icon-yen"></i>&nbsp;<?= number_format($total['invoice_money'], 2, '.', ',') ?></span>
                    </td>
                </tr>
            <?php endif; ?>
        </table>

        <!-- <div class="tab-column tab-manage">
            <button type="button" class="btn btn-default btn-task batch-delete" data-url="<?= Url::to([$this->context->id . '/delete']) ?>" >
                <a href="javascript:;">删除</a>
            </button>
        </div> -->

        <?php ActiveForm::end(); ?>

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