<?php
/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace home\widgets\portlet;

use Yii;
use yii\helpers\Html;
use yii\base\InvalidConfigException;
use home\modules\customer\models\Customer;
use common\models\PermissionScope;

/**
 * 我的客戶组件
 * 
 * 调用方法如下：
 *
 * ```php
 * <?= CustomerPortlet::widget([
 *     'title' => '测试组件',
 *     'x' => 0,
 *     'y' => 0,
 * ]) ?>
 * ```
 * 
 * @author legendjw http://blog.u-bo.com
 * @since 0.1
 */
class CustomerPortlet extends BasePortlet 
{
	/**
     * @inheritdoc
     */
    public function getContent()
    {
        $query = Customer::find()
            //->where(['attention' => Customer::STATUS_ENABLE])
            ->orderBy(['attention' => SORT_DESC, 'customer_id' => SORT_DESC]);

        $scopeUser = PermissionScope::getScopeUser('customer/customer/index');
        $scopeUser = ($scopeUser === false) ? [Yii::$app->user->id] : $scopeUser;
        if (!Yii::$app->session->get('isSuperAdmin') && !empty($scopeUser)) {

            $query->where(['in', 'leader_id', $scopeUser]);
        }
        $customers = $query->limit(5)->all();

        return $this->render('customer', [
            'customers'=> $customers,
        ]);
    }
}
