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

namespace home\controllers;

use Yii;
use home\models\Message;
use home\models\MessageLink;
use common\models\Common;
use home\models\Schedule;
use yii\data\ActiveDataProvider;
use common\components\Tool;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use common\models\User;
use yii\helpers\Url;

/**
 * <日程管理控制器>
 * 
 * @author zealxd http://blog.u-bo.com
 * @since 0.1
 */
class ScheduleController extends Controller
{
    public function actions()
    {
        $actions = parent::actions();
        $actions['create']['layout'] = '//modal';
        $actions['update']['layout'] = '//modal';
        $actions['view']['layout'] = '//modal';
        return $actions;
    }

    /**
     * ajax获取关联日志
     */
    public function actionAjaxGetLinkSchedule()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        $request = Yii::$app->request;
        $linkId = $request->get('link_id');
        $linkType = $request->get('link_type');
        $userId = $request->get('user_id');
        $scopeUser = '';

        $linkData = Common::getLinkData($linkType, $linkId);

        if (empty($linkData)) {
            return ['status' => 0, 'info' => '关联信息不存在'];
        }

        $types = Common::getLinkTypes([$linkType]);
        if (empty($types)) {
            return ['status' => 0, 'info' => '关联类型不支持'];
        }
        $linkData['link_type'] = $types[$linkType];


        $linkIds = $linkData[$linkData['link_type']['link_field']];

        $query = Schedule::find()
            ->with('user', 'contacter', 'category')
            ->where(['link_type' => $linkType, 'link_id' => $linkIds])
            ->orderBy(['schedule_time' => SORT_DESC, 'create_time' => SORT_DESC])
            ->asArray();

        if (!empty($scopeUser) && empty($userId)) {
            $query->andWhere(['user_id' => $scopeUser]);
        }

        if (!empty($userId)) {
            $query->andWhere(['user_id' => $userId]);
        }

        $provider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);
        $models = $provider->getModels();
        foreach ($models as &$model) {
            $model['schedule_time'] = date('Y-m-d ', $model['schedule_time']);
            $model['create_time'] = date('Y-m-d H:i', $model['create_time']);
            $model['avatar'] = (!empty($model['user']['avatar'])) ? $model['user']['avatar'] : Yii::getAlias('@web') . '/static/images/headpic.jpg';
            $model['join_names'] = implode(',', ArrayHelper::getColumn(User::getUserByDot($model['join_ids']), 'realname'));
            $model['url'] =  Url::to(['/schedule/view', 'id' =>$model['schedule_id'], 'from' => Url::to('')]) ;
            if (empty($model['contacter'])) {
                $model['contacter']['name'] = '无';
            }
            $model['is_self'] = ($model['user_id'] == Yii::$app->user->id) ? 1 : 0;

            //附件
            $attachHtml = '';
            if ($model['attach_ids'] != '' && $attachs = Attachment::getAttachByDot($model['attach_ids'])) {
                foreach ($attachs as $attach) {
                    $attachHtml .= '<p><a href="' . Url::to(['/attachment/download', 'id' => $attach['attach_id']]) . '" title="点击下载" >' . $attach['file_name'] . '</a> ' . ($attach['is_image'] == 1 ? '<a href="javascript:imagePriview(\'' . $attach['file_path'] . '\')" >[预览]</a>'
                                : '' ) . '</p>';
                }
            }
            $model['attach_html'] = $attachHtml;


        }

        $data = [
            'models' => $models,
            'pages' => \yii\widgets\LinkPager::widget(['pagination' => $provider->pagination, 'prevPageLabel' => '上一页', 'nextPageLabel' => '下一页', 'firstPageLabel' => '首页', 'lastPageLabel' => '末页']),
            'count' => $query->count(),
        ];

        return $data;
    }

}
