<?php

namespace App\Http\Controllers\Channel\Notice;

use App\Http\Controllers\Channel\BaseController;
use App\Http\Controllers\Channel\Notice\Transformers\NoticeTransformer;
use App\Http\Controllers\Channel\Notice\Transformers\SimpleNoticeTransformer;
use App\Modules\Channel\Services\ChannelService;
use App\Modules\Notice\Services\fansLimitNoticeService;
use App\Modules\Notice\Services\NoticeService;
use Illuminate\Http\Request;

class NoticesController extends BaseController
{

    /**
     * @apiDefine Notice 公告
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取公告列表
     * @api {GET} notices 获取公告列表
     * @apiGroup Notice
     * @apiName notices
     * @apiParam   {Number}  [notice_type_id] 公告分类id.(可不传，不传获取所有分类公告)
     * @apiParam   {Number}  [status] 状态.0:正常状态; -1:删除   (可不传，不传获取所有分类公告)
     * @apiSuccess {Number}  id 公告ID.
     * @apiSuccess {String}  is_popup 是否弹出. 0:普通；1:弹框；
     * @apiSuccess {String}  title 标题.
     * @apiSuccess {String}  content 内容.
     * @apiSuccess {status}  status 状态.0:正常状态; -1:删除
     * @apiSuccess {String}  updated_time 更新时间.
     * @apiSuccess {String}  type_name 类型名称
     * @apiSuccess {Number}  type_id 类型ID
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *         {
     *             "list":[
     *                 {
     *                     "id": "121",
     *                     "is_popup": "1",
     *                     "title": "1212-12-12-12",
     *                     "content": "对不起，我爱你",
     *                     "status": 0,
     *                     "updated_time": "2017-12-12 12:12:12"
     *                     "type_name": "测试类型",
     *                     "type_id": 1
     *                 }
     *             ],
     *             "meta":{
     *                 "total"=>224,
     *                 "per_page"=>15,
     *                 "current_page"=>1,
     *                 "last_page"=>15,
     *                 "next_page_url"=>"",
     *                 "prev_page_url"=>""
     *             }
     *         }
     *     }
     */
    function get_list(Request $request)
    {
        $notice_type_id = $request->has('notice_type_id') && $request->input('notice_type_id') ? $request->input('notice_type_id') : null;
        $status = $request->has('status') && $request->input('status') ? $request->input('status') : null;
        $status = 0;

        $notices = NoticeService::getAllNoticeList($notice_type_id, $status);
        return response()->pagination(new NoticeTransformer(), $notices);
    }

    //获取简易的通知列表
    function get_simple_list(Request $request)
    {
        $notice_type_id = $request->has('notice_type_id') && $request->input('notice_type_id') ? $request->input('notice_type_id') : null;
        $status = $request->has('status') && $request->input('status') ? $request->input('status') : null;
        $status = 0;

        $notices = NoticeService::getSimpleNoticeList($notice_type_id, $status);
        return response()->pagination(new SimpleNoticeTransformer(), $notices);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取一个公告
     * @api {get} getNotice 获取一个公告
     * @apiGroup Notice
     * @apiName getNotice
     * @apiParam   {Number}  id 公告ID.
     * @apiSuccess {String}  is_popup 是否弹出. 0:普通；1:弹框；
     * @apiSuccess {String}  title 标题.
     * @apiSuccess {String}  content 内容.
     * @apiSuccess {status}  status 状态.0:正常状态; -1:删除
     * @apiSuccess {String}  updated_time 更新时间.
     * @apiSuccess {String}  type_name 类型名称
     * @apiSuccess {Number}  type_id 类型ID
     * @apiSuccessExample {json} Success-Response:
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{
     *                     "id": "121",
     *                     "is_popup": "1",
     *                     "title": "1212-12-12-12",
     *                     "content": "对不起，我爱你",
     *                     "status": 0,
     *                     "updated_time": "2017-12-12 12:12:12"
     *                     "type_name": "测试类型",
     *                     "type_id": 1
     *                 }
     *     }
     */
    function get_notice(Request $request) {
        $id = $request->has('id') ? $request->input('id') : '';
        if(!is_numeric($id)) {
            return response()->error("PARAM_ERROR");
        }
        $notice = NoticeService::getNotice($id);
        if(empty($notice)) {
            return response()->error("PARAM_ERROR");
        }
        return response()->item(new NoticeTransformer(), $notice);
    }

    public function getfansLimitNotice(){
        $channel_user_id = $this->getChannelUserId();
        if(!$channel_user_id) return response()->error('PARAM_ERROR');
        $distribution_channel_ids = ChannelService::getUserChannelIds($channel_user_id);
        $notice = fansLimitNoticeService::getAllOfficialAccountsLimit($distribution_channel_ids);
        $data=[];
        if(!$notice->isEmpty()){
            $official_accounts = [];
            foreach ($notice as $item){
                $official_accounts[] = $item->account_nickname;
                $item->is_read=1;
                $item->save();
            }
            $official_accounts = implode(',',$official_accounts);
            $data = [
                'title'=>'阈值提醒',
                'contents'=>"站点服务号{$official_accounts}今日增粉阈值0，请关注服务号解封情况！
解封后请前往系统设置-服务号设置-编辑，调高日关注上限！"
            ];
            return response()->success($data);
        }
        return response()->success($data);
    }
}
