<?php

use home\assets\MainAsset;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\helpers\StringHelper;
use home\modules\customer\models\Customer;
use home\modules\customer\models\District;
use home\assets\ModalAsset;
use home\modules\contract\models\Contract;

/* @var $this yii\web\View */
$this->title = '客户列表';

$assetUrl = $this->getAssetManager()->getBundle(ModalAsset::className())->baseUrl;
$this->registerJsFile($assetUrl . '/js/district.link.js', ['depends' => [ModalAsset::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="60"><label style="font-weight:lighter" ><input type="checkbox" name="select_all" class="box-select-all" style="position:relative;top:2px;" /> 全选</label></th>
        <th width="15%">客户名称</th>
        <th width="10%">客户类别</th>
        <th width="15%">合同</th>
        <th width="10%" colspan="2">合同/销售</th>
        <th width="10%">客户区域</th>
        <th width="10%">负责人</th>
        <th width="5%">状态</th>
        <th align="center">操作</th>
    </tr>

    <?php if(empty($models)) : ?>
        <tr><td colspan="12" class="tac red">还没有任何数据...</td></tr>
    <?php else : ?>
        <?php foreach ($models as $key => $model) : ?>
            <tr>
                <!-- 多选框 -->
                <td><input type="checkbox" name="delete[]" class="box-delete" value="<?= $model['customer_id'] ?>" /></td>
                <!-- 客户名称 -->
                <?php if(Customer::readAccess('customer/customer/detail')) : ?>
                    <td class='theleft-min' title="<?= $model['name']; ?>" ><a target="_blank" href="<?= Url::to([$this->context->id . '/detail', 'id' => $model['customer_id'], 'from' => Url::to('')]) ?>  ">
                            <?= StringHelper::truncate($model['name'], 10, '') ?>
                            <?= ($model['attention'] == Customer::STATUS_ENABLE) ? '<font color="green" class="icon-star"></font>' : '' ?>
                        </a>
                    </td>
                <?php else : ?>
                    <td class='theleft-min' title="<?= $model['name']; ?>" ><?= StringHelper::truncate($model['name'], 10, '') ?></td>
                <?php endif; ?>
                <!-- 客户类别 -->
                <td><?= empty($model['category']['name']) ? '' : $model['category']['name'] ?></td>
                <!-- 星标 -->
                <!-- 合同 -->
                <?php if(Contract::readAccess('contract/contract/detail') && !empty($model['newestCustomer']['name'])) : ?>
                    <td class='theleft-min' title="<?= $model['newestCustomer']['name'] ?>"><a target="_blank" href="<?= Url::to(['/contract/contract/detail', 'id' => $model['newestCustomer']['contract_id']]) ?>"><?= empty($model['newestCustomer']['name']) ? '' : StringHelper::truncate($model['newestCustomer']['name'], 9, '')
                    ?></a></td>
                <?php else : ?>
                    <td class='theleft-min' title="<?= $model['newestCustomer']['name'] ?>"><?= empty($model['newestCustomer']['name']) ? '' : StringHelper::truncate($model['newestCustomer']['name'], 9, '')
                    ?></td>
                <?php endif; ?>
                <!-- 项目/合同/销售 -->
                <td> <?= $model['contractNumber'] ?></td>
                <td> <?= $model['opportunityNumber'] ?></td>
                <!-- 客户区域 -->
                <td> <?= empty($model['district']['name']) ? '' : $model['district']['name'] ?></td>
                <!-- 负责人 -->
                <td> <?= empty($model['leader']['realname']) ? '' : $model['leader']['realname'] ?></td>
                <!-- 状态 -->
                <td><?= ($model->status == Customer::STATUS_ENABLE) ? '<font color="green">启用</font>' : '<font color="red">禁用</font>' ?></td>
                <!-- 操作 -->
                <td align="center" >
                    <?php if(Customer::readAccess('sale/opportunity/create')) : ?>
                        <a href="javascript:;" data-url="<?= Url::to(['/sale/opportunity/create', 'customer_no' => $model['customer_no'], 'customer_name' => $model['name'], 'from' => Url::to('')]) ?>" data-title="销售机会" data-width="650" data-height="400" class="open-dialog" >销售机会</a>
                    <?php else : ?>
                        <a href="javascript:;" class="gray" title="当前没有权限添加销售机会">销售机会</a>
                    <?php endif; ?>

                    <?php if(Customer::readAccess('schedule/schedule/link-customer')) : ?>
                        <a href="javascript:;" data-url='<?= Url::to(['/schedule/schedule/create','edit'=>true, 'link_id' => $model['customer_no'], 'link_type' => 'customer']) ?>' data-title='添加日程'  data-width="600" data-height="380" class="open-dialog" >日程</a>
                    <?php else : ?>
                        <a href="javascript:;" class="gray" title="当前没有权限添加日程">日程</a>
                    <?php endif; ?>

                    <?php if(Customer::readAccess('reimbursement/reimbursement/create')) : ?>
                        <a href="javascript:;" data-url='<?= Url::to(['/reimbursement/reimbursement/create', 'link_id' => $model['customer_no'], 'link_type' => 'customer']) ?>' data-title="我要报销" data-width="600" data-height="380" class="open-dialog">报销</a>
                    <?php else : ?>
                        <a href="javascript:;" class="gray" title="当前没有权限添加报销">报销</a>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach; ?>
    <?php endif; ?>
</table>


<style>
    .search-form1{
        width:25%;    
        position:relative;
        top:-1px;    
        margin-right: 10px;
        display: inline;
        font-size: 12px;            
    }
</style>

<?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>
<!--分页标签-->
<script>
    var projectParams = {
        selectDistrictUrl: "<?= Url::to(['district/select-customer']) ?>",
        cityUrl: "<?= Url::to(['district/select-city']) ?>",
        areaUrl: "<?= Url::to(['district/select-area']) ?>",
        dateType: "1"
    }
</script>
