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

namespace home\models;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * 组件模型
 *
 * @property string $id 组件ID
 * @property string $title 默认名称
 * @property string $description 描述
 * @property integer $system 是否是系统组件,系统组件默认显示
 * @property integer $x 组件横坐标
 * @property integer $y 组件纵坐标
 *
 * @author legendjw http://blog.u-bo.com
 * @since 0.1
 */
class Portlet extends \yii\base\Model
{
    const IS_SYSTEM = 1; //是系统组件
    const NOT_SYSTEM = 0; //非系统组件

    /**
     * 获取当前可用的组件
     */
    public static function getPortlet()
    {
        $data = [
            'announcement' => [
                'id' => 'announcement',
                'title' => '系统通知',
                'description' => '用于显示系统发送的最新系统通知',
                'system' => self::IS_SYSTEM,
                'x' => 0,
                'y' => 0,
            ],
            'message' => [
                'id' => 'message',
                'title' => '消息通知',
                'description' => '用于显示用户发送给您的最新通知公告和短消息',
                'system' => self::IS_SYSTEM,
                'x' => 0,
                'y' => 1,
            ],
            'weather' => [
                'id' => 'weather',
                'title' => '天气预报',
                'description' => '用于显示最近的天气预报',
                'system' => self::NOT_SYSTEM,
                'x' => 0,
                'y' => 1,
            ],
//            'lead-task' => [
//                'id' => 'lead-task',
//                'title' => '我负责的任务',
//                'description' => '用于显示任务模块里您负责的最新任务',
//                'system' => self::NOT_SYSTEM,
//                'x' => 0,
//                'y' => 0,
//            ],
            'reimbursement' => [
                'id' => 'reimbursement',
                'title' => '我的报销',
                'description' => '用于显示您最新申请的财务报销',
                'system' => self::NOT_SYSTEM,
                'x' => 0,
                'y' => 0,
            ],
            'customer' => [
                'id' => 'customer',
                'title' => '我的客户',
                'description' => '用于显示您负责的客户',
                'system' => self::NOT_SYSTEM,
                'x' => 0,
                'y' => 0,
            ],
            'opportunity' => [
                'id' => 'opportunity',
                'title' => '销售机会',
                'description' => '用于显示您负责的销售机会',
                'system' => self::NOT_SYSTEM,
                'x' => 0,
                'y' => 0,
            ],
            'contract' => [
                'id' => 'contract',
                'title' => '我的合同',
                'description' => '用于显示您负责的区域所在客户的合同',
                'system' => self::NOT_SYSTEM,
                'x' => 0,
                'y' => 0,
            ],
        ];

        return $data;
    }

    /**
     * 获取组件下拉列表数据
     */
    public static function getDropDownList() 
    {
        $portlets = static::getPortlet();
        
        return ArrayHelper::map($portlets, 'id', 'title');
    }

    /**
     * 获取组件下拉列表选项的说明
     */
    public static function getDropDownOptionDesc()
    {
        $portlets = static::getPortlet();
        $options = [];

        foreach ($portlets as $portlet) {
            $options[$portlet['id']] = ['data-desc' => $portlet['description']];
        }

        return $options;
    }

    /**
     * 获取组件插件的类名
     *
     * @param string $portlet 组件ID
     */
    public static function getPortletWidgetName($portlet)
    {
        $name = '\home\widgets\portlet\\';
        if (false !== strpos($portlet, '-')) {
            $wordArr = explode('-', $portlet);
            foreach ($wordArr as $word) {
                $name .= ucfirst($word);
            }
        }
        else {
            $name .= ucfirst($portlet);
        }
        
        return $name . 'Portlet';
    }

}
