<?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\helpers\ArrayHelper;
use yii\base\InvalidConfigException;
use home\modules\sale\models\Opportunity;
use home\modules\customer\models\Customer;
use common\models\PermissionScope;

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

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

            $userId = Yii::$app->user->id;
            $district = Customer::find()->select('customer_no')->where(['in', 'leader_id', $scopeUser])->asArray()->all();
            $customerArray = arrayHelper::getColumn($district, 'customer_no');
            $query->where(['in', 'customer_no', $customerArray]);
        }

        $opportunitys = $query->limit(5)->all();

        return $this->render('opportunity', [
            'opportunitys'=> $opportunitys,
        ]);
    }
}
