<?php

namespace App\Http\Controllers\Manage\Notice;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Manage\Notice\Transformers\NoticeSmsTransformer;
use App\Http\Controllers\Manage\Notice\Transformers\NoticeTransformer;
use App\Libs\SMS;
use App\Modules\Channel\Services\ChannelUserService;
use App\Modules\Notice\Models\Notice;
use App\Modules\Notice\Services\NoticeService;
use App\Modules\Notice\Services\NoticeSmsService;
use Illuminate\Http\Request;

class NoticesController extends Controller
{
    /**
     * @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') : '';
        $status = $request->has('status') ? $request->input('status') : '';

        $notices = NoticeService::getAllNoticeList($notice_type_id, $status);
        return response()->pagination(new NoticeTransformer(), $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);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 添加一个公告
     * @api {POST} addNotice 添加公告
     * @apiGroup Notice
     * @apiName addNotice
     * @apiParam   {Number}  [notice_type_id] 公告分类id.
     * @apiParam   {String}  title 公告标题.
     * @apiParam   {String}  content 公告内容.
     * @apiParam   {Number}  is_popup 通知方式. 0:普通；1:弹框；
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{}
     *     }
     */
    function add_notice(Request $request)
    {
        $data = $request->all();

        $title = $request->has('title') ? $request->input('title') : '';
        if (empty($title)) {
            return response()->error("PARAM_EMPTY");
        }
        $content = $request->has('content') ? $request->input('content') : '';
        if (empty($content)) {
            return response()->error("PARAM_EMPTY");
        }
        $notice_type_id = $request->has('notice_type_id') ? $request->input('notice_type_id') : '';
        $data['is_popup'] = 0;

        $data = NoticeService::addNotice($data);
        return response()->success();
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 修改一个公告
     * @api {POST} updateNotice 修改一个公告
     * @apiGroup Notice
     * @apiName updateNotice
     * @apiParam   {Number}  id 公告id.
     * @apiParam   {Number}  [notice_type_id] 公告分类id.
     * @apiParam   {String}  title 公告标题.
     * @apiParam   {String}  content 公告内容.
     * @apiParam   {Number}  [is_popup] 通知方式.0:普通；1:弹框；
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{}
     *     }
     */
    function update_notice(Request $request)
    {
        $id = $request->has('id') ? $request->input('id') : '';
        if (!is_numeric($id)) {
            return response()->error("PARAM_ERROR");
        }
        $title = $request->has('title') ? $request->input('title') : '';
        if (empty($title)) {
            return response()->error("PARAM_EMPTY");
        }
        $content = $request->has('content') ? $request->input('content') : '';
        if (empty($content)) {
            return response()->error("PARAM_EMPTY");
        }
        $notice_type_id = $request->has('notice_type_id') ? $request->input('notice_type_id') : '';

        $is_popup = $request->has('is_popup') ? $request->input('is_popup') : '';
        if (!is_numeric($is_popup)) {
            $is_popup = 0;
        }
        $data['is_popup'] = $is_popup;

        $notice = NoticeService::getNotice($id);
        if (empty($notice)) {
            return response()->error("PARAM_ERROR");
        }

        $notice['title'] = $title;
        $notice['content'] = $content;
        $notice['notice_type_id'] = $notice_type_id;
        $notice['is_popup'] = $is_popup;

        $noticeUp = NoticeService::updateNotice($notice);

        return response()->success();
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 删除一个公告
     * @api {POST} rmNotice 删除一个公告
     * @apiGroup Notice
     * @apiName rmNotice
     * @apiParam   {Number}  id 公告id.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{}
     *     }
     */
    function rm_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");
        }
        $notice['status'] = -1;
        $noticeUp = NoticeService::updateNotice($notice);

        return response()->success();
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 发送短信通知
     * @api {POST} noticeSms 发送短信通知
     * @apiGroup Notice
     * @apiName noticeSms
     * @apiParam   {String}  phones 公告标题.多个用小写逗号分开 13245677654,13256778989,
     * @apiParam   {String}  content 公告内容.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{}
     *     }
     */
    function send_sms(Request $request)
    {
        $content = $request->has('content') ? $request->input('content') : '';
        if (empty($content)) {
            return response()->error("PARAM_ERROR");
        }
        $phoneInput = $request->has('phones') ? $request->input('phones') : '';
        if (empty($phoneInput)) {
            return response()->error("PARAM_ERROR");
        }
        if ($phoneInput == 'all') {
            $isEnabled = 1;
            $params = [
                'is_enabled' => $isEnabled,
            ];
            $result = ChannelUserService::getChannelList($params, true);
            foreach ($result as $rr) {
                $phone = $rr->phone;
                if (!empty($phone) && strlen($phone) > 10 && strlen($phone) < 15) {
                    $code = SMS::send($phone, $content);
                    if ($code && $code == 1) {
                        //成功
                    } else {
                    }
                }
            }
        } else {
            $phones = '';
            if ($phoneInput) {
                $phones = explode(',', $phoneInput);
            }
            if (empty($phones) || count($phones) == 0) {
                return response()->error("PARAM_ERROR");
            }

            foreach ($phones as $p) {
                if (!empty($p) && strlen($p) > 10 && strlen($p) < 15) {
                    $code = SMS::send($p, $content);
                    if ($code && $code == 1) {
                        //成功
                    } else {
                    }
                }
            }
        }

        $dataSms['phons'] = $phoneInput;
        $dataSms['content'] = $content;

        $data = NoticeSmsService::addNoticeSms($dataSms);

        return response()->success($data);
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 短信通知列表
     * @api {POST} noticeSmsList 短信通知列表
     * @apiGroup Notice
     * @apiName noticeSmsList
     * @apiParam   {String}  phones 公告标题.多个用小写逗号分开 13245677654,13256778989,
     * @apiParam   {String}  content 公告内容.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{}
     *     }
     */
    function send_sms_list(Request $request)
    {
        $data = NoticeSmsService::getNoticeSms();
        return response()->pagination(new NoticeSmsTransformer(), $data);
    }

    function getNoticeContent(Request $request)
    {
        $content = $request->has('content') ? $request->input('content') : '';
        if (empty($content)) {
            return response()->error("PARAM_EMPTY");
        }
        $content = Notice::getReplyConent($content);
        return response()->success(compact('content'));
    }
}
