<?php

use home\assets\MainAsset;
use yii\helpers\Url;
use home\modules\customer\models\Category;
use home\modules\customer\models\Customer;
//use home\modules\blog\models\Blog;
use common\models\User;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use yii\helpers\StringHelper;
use yii\helpers\Html;
use home\modules\schedule\models\Schedule;

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

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

$this->title = '客户详情';
?>

<script>
    var assetUrl = "<?= $assetUrl ?>";
</script>

<!--container-->
<div class="container manage-project fix">
    <?= $this->render('@home/views/site/_left-menu') ?>
    <!--manage-left -->

    <div class="manage-right" >
        <div class="tab-column tab-manage">
            <div class="tab-right">
                <?php if(Customer::readAccess('/sale/opportunity/create')) : ?>
                    <button title="添加销售机会" 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"  type="button" class="btn btn-default btn-task open-dialog">
                        <i class="icon-user"></i>
                    </button>
                <?php endif; ?>
                <?php if(Customer::readAccess('schedule/schedule/link-customer')) : ?>
                    <button title="写日程" type="button" data-url='<?= Url::to(['/schedule/schedule/create', 'link_id' => $model['customer_no'], 'link_type' => 'customer']) ?>'data-title='添加日程'  data-width="600" data-height="380" class="btn btn-default btn-task open-dialog">
                        <i class="icon-calendar"></i>
                    </button>
                <?php endif; ?>
                <?php if (Customer::readAccess('reimbursement/reimbursement/create')) : ?>
                    <button title="报销" type="button" data-url='<?= Url::to(['/reimbursement/reimbursement/create', 'link_id' => $model['customer_no'], 'link_type' => 'customer']) ?>' data-title="我要报销" data-width="600" data-height="380" class="btn btn-default btn-task open-dialog">
                       <i class="icon-dollar"></i>
                    </button>
                <?php endif; ?>
                <?php if (Customer::readAccess('customer/customer/view')) : ?>
                    <?php if (Yii::$app->session->get('isSuperAdmin') || $model['leader_id'] == Yii::$app->user->id) : ?>
                        <button title="编辑" type="button" data-title='编辑客户' data-url='<?= Url::to([$this->context->id . '/view', 'id' => $model['customer_id'], 'from' => Url::to('')]) ?>'  data-width="800" data-height="550" class="btn btn-default btn-task open-dialog">
                        <i class="icon-edit"></i>
                        </button>
                    <?php endif; ?>
                <?php endif; ?>
                <?php if (Customer::readAccess('customer/customer/delete')) : ?>
                    <?php if (Yii::$app->session->get('isSuperAdmin') || $model['leader_id'] == Yii::$app->user->id) : ?>
                        <button title="删除" type="button" onclick="if (!confirm('确认删除?'))
                                    return false;
                                javascript:window.location.href = '<?= Url::to([$this->context->id . '/delete', 'id' => $model['customer_id'], 'from' => (isset($_GET['from'])) ? $_GET['from'] : '']) ?>'" class="btn btn-default btn-task">
                       <i class="icon-trash"></i>
                        </button>
                    <?php endif; ?>
                <?php endif; ?>
            </div>
            <p class="caption">客户详情</p>
        </div>
        <div class="task-title">
            <p class="lefttitle"><span>客户名称：</span><?= StringHelper::truncate($model['name'], 26) ?><span>&nbsp;&nbsp; 客户类别：</span><?= empty($model['category']['name']) ? '' : $model['category']['name'] ?>&nbsp;&nbsp; <span>客户等级：</span><?= $model['level']['name'] ?></p>
        </div>
        <div class="mgt10">
            <table class="table task-table taskdetail-table">
                <tr>
                    <td width="10%" class="table-blue">客户编号</td>
                    <td width="10%"><?= empty($model['customer_no']) ? '' : $model['customer_no'] ?></td>
                    <td width="10%" class="table-blue">负责人</td>
                    <td width="10%"><?= empty($model['leader']['realname']) ? '' : $model['leader']['realname'] ?></td>
                    <td width="8%" class="table-blue" >共享人</td>
                    <td width="8%"><?= empty($model['share_user_ids']) ? '' : implode(',', ArrayHelper::getColumn(User::getUserByDot($model['share_user_ids']), 'realname')) ?></td>
                    <td width="10%" class="table-blue">谷歌坐标</td>
                    <td width="10%"><?= empty($model['google_coordinate']) ? '' : '(' . $model['google_coordinate'] . ')' ?></td>
                </tr>
                <tr>
                    <td   class="table-blue">客户简称</td>
                    <td  ><?= $model['simple_name'] ?></td>
                    <td  class="table-blue">所在区域</td>
                    <td  ><?= empty($model['district']['name']) ? '' : $model['district']['name'] ?></td>
                    <td  class="table-blue">创建人</td>
                    <td title="<?= empty($model['create_time']) ? '' : date('Y-m-d', $model['create_time']) ?>"><?= $model['user']['realname'] ?></td>
                     <td  class="table-blue">百度坐标</td>
                    <td ><?= empty($model['baidu_coordinate']) ? '' : '(' . $model['baidu_coordinate'] . ')' ?></td>
                </tr>
                <tr>
                    <td  class="table-blue">信息科</td>
                    <td ><?= $model['department1'] ?></td>
                    <td  class="table-blue">信息科电话</td>
                    <td  ><?= $model['dept_phone1'] ?></td>
                     <td  class="table-blue">科室</td>
                    <td><?= empty($model['department2']) ? '' : $model['department2'] ?></td>
                    <td  class="table-blue">科室电话</td>
                    <td><?= empty($model['dept_phone2']) ? '' : $model['dept_phone2'] ?></td>
                </tr>
                <tr>
                    <td  class="table-blue">客户地址</td>
                    <td  colspan="7"><?= empty($model['address']) ? '' : $model['address'] ?></td>
                </tr>
                <tr>
                    <td class="table-blue">客户信息</td>
                    <td  colspan="7"><?= empty($model['info']) ? '' : $model['info'] ?></td>
                </tr>
                <tr>
                    <td class="table-blue">备注信息</td>
                    <td  colspan="7"><?= empty($model['remark']) ? '' : $model['remark'] ?></td>
                </tr>
            </table>
        </div>

        <?php $contacter = new Customer; ?>
        <?php $contacter = $contacter->getContacters($model['customer_no']); ?>
        <?php if (!empty($contacter)) : ?>
            <?php $contacter = new Customer; ?>
            <?php $contacter = $contacter->getContacters($model['customer_no']); ?>
            <table class="table task-table taskdetail-table">
                <tr>
                    <td class="table-blue" rowspan="<?= count($contacter) + 1 ?>"  width="10%">
                        联系人
                        <?php if (Customer::readAccess('customer/contacter/create')) : ?>
                            <a  href="javascript:void(0);" data-url='<?= Url::to(['/customer/contacter/create', 'customer_no' => $model['customer_no']]) ?>' data-title='新增联系人' data-width="600" data-height="350" class="open-dialog">
                                <i class="icon-plus" title="添加联系人"></i>
                            </a>
                        <?php endif; ?>
                    </td>
                    <td class="table-blue" width="10%">姓名</td>
                    <td class="table-blue" width="10%">职位</td>
                    <td class="table-blue" width="10%">联系方式</td>
                    <td class="table-blue">备注</td>
                </tr>
                <?php foreach ($contacter as $con) : ?>
                    <tr>
                        <td><?= $con['name'] ?></td>
                        <td><?= $con['job'] ?></td>
                        <td><?= $con['phone'] ?></td>
                        <td><?= $con['remark'] ?></td>
                    </tr>
                <?php endforeach; ?>
            </table>
        <?php else: ?>
            <table class="table task-table taskdetail-table">
                <tr>
                    <td class="table-blue" rowspan="<?= count($contacter) + 1 ?>"  width="10%">
                        联系人
                        <?php if (Customer::readAccess('customer/contacter/create')) : ?>
                            <a  href="javascript:void(0);" data-url='<?= Url::to(['/customer/contacter/create', 'customer_no' => $model['customer_no']]) ?>' data-title='新增联系人' data-width="600" data-height="350" class="open-dialog">
                                <i class="icon-plus" title="添加联系人"></i>
                            </a>
                        <?php endif; ?>
                    </td>
                </tr>
            </table>
        <?php endif; ?>
        <!--loglist-->

        <!--  下面包含项目列表/合同列表/销售机会列表     start  -->
        <div class="fix customer-all">
            <!--loglist-->

            <!--loglist-->
            <?php if (Customer::readAccess('customer/customer/view-contract')) : ?>
                 <div class="sale-list sale-list1 fl">
                    <div class="task-title rel">
                        合同<span class="customer-count"><?= $model['contractNumber'] ?></span>
                    </div>
                    <div class="salelists">
                        <?php if (!empty($model['contractAll'])) : ?>
                            <ul>
                                <?php if (Customer::readAccess('contract/contract/detail')) : ?>
                                    <?php foreach ($model['contractAll'] as $v) : ?>
                                        <li>
                                            <a title="<?= $v['name'] ?>" target="_blank" href="<?= Url::to(['/contract/contract/detail', 'id' => $v['contract_id']]) ?>"><?= StringHelper::truncate($v['name'], 20) ?></a>
                                        </li>
                                    <?php endforeach; ?>
                                <?php else : ?>
                                    <?php foreach ($model['contractAll'] as $v) : ?>
                                        <li>
                                            <a title="<?= $v['name'] ?>" ><?= StringHelper::truncate($v['name'], 20) ?></a>
                                        </li>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </ul>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endif; ?>

            <!--loglist-->
            <?php if (Customer::readAccess('customer/customer/view-opportunity')) : ?>
               <div class="sale-list sale-list2 fl">
                    <div class="task-title rel">
                        销售机会<span class="customer-count"><?= $model['opportunityNumber'] ?></span>
                    </div>
                    <div class="salelists">
                        <?php if (!empty($model['opportunityAll'])) : ?>
                            <ul>
                                <?php if (Customer::readAccess('sale/opportunity/detail')) : ?>
                                    <?php foreach ($model['opportunityAll'] as $v) : ?>
                                        <li>
                                            <a title="<?= $v['name'] ?>" target="_blank" href="<?= Url::to(['/sale/opportunity/detail', 'id' => $v['opportunity_id']]) ?>"><?= StringHelper::truncate($v['name'], 20) ?></a>
                                        </li>
                                    <?php endforeach; ?>
                                <?php else : ?>
                                    <?php foreach ($model['opportunityAll'] as $v) : ?>
                                        <li>
                                            <a title="<?= $v['name'] ?>" ><?= StringHelper::truncate($v['name'], 20) ?></a>
                                        </li>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </ul>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endif; ?>

        </div>
        <!--  上面包含项目列表/合同列表/销售机会列表     end  -->

        <div class="task-title task-title2">
            <p class="lefttitle">客户日程(<b id="schedule-count" data-get-schedule-url="<?= Url::to(['/schedule/schedule/ajax-get-link-schedule', 'link_id' => $model['customer_no'], 'link_type' => 'customer']) ?>" >0</b>)：
                <?php $blogUser = Schedule::getLinkScheduleUser('customer', $model['customer_no']); ?>
                <?php if (!empty($blogUser)) : ?>
                    <button type="button" class="btn btn-default btn-sm btn-task schedule-user" data-get-schedule-url="<?= Url::to(['/schedule/schedule/ajax-get-link-schedule', 'link_id' => $model['customer_no'], 'link_type' => 'customer', 'user_id' => '']) ?>" >
                        <a href="javascript:;" >全部</a>
                    </button>
                    <?php foreach ($blogUser as $user) : ?>
                        <button type="button" class="btn btn-default btn-sm btn-task schedule-user" data-get-schedule-url="<?= Url::to(['/schedule/schedule/ajax-get-link-schedule', 'link_id' => $model['customer_no'], 'link_type' => 'customer', 'user_id' => $user['user_id']]) ?>" >
                            <a href="#" ><?= $user['realname'] ?></a>
                        </button>
                    <?php endforeach; ?>
                    
                <?php endif; ?>
            </p>
        </div>

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