<?php
/**
 * ${CARET}
 * @file:NoticesController.php
 * @Created by gnitif
 * @Date: 2023/3/27
 * @Time: 11:18
 */


namespace Modules\System\Http\Controllers;


use Catch\Base\CatchController as Controller;
use Catch\Exceptions\FailedException;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
use Modules\System\Http\Requests\NoticeRequest;
use Modules\System\Models\NoticeTypes;
use Modules\System\Services\Notice\NoticesService;

class NoticesController extends Controller
{
    public function list(Request $request)
    {
        return NoticesService::list();

    }

    /**
     *  添加通知
     * name: addNotice
     * @param NoticeRequest $request
     * @return mixed
     * date 2023/03/29 14:47
     */
    public function addNotice(NoticeRequest $request)
    {
        return NoticesService::addNotice($request->all());
    }

    /**
     *  删除通知
     * name: delete
     * @param $id
     * date 2023/03/29 14:48
     */
    public function delete($id)
    {
        return NoticesService::delete($id);
    }

    /**
     *  获取通知详情
     * name: info
     * @param $id
     * @return mixed
     * date 2023/03/29 14:59
     */
    public function info($id)
    {
        return NoticesService::getDetail($id);
    }

    public function edit($id, Request $request)
    {
        $title = $request->input('title', '');
        $content = $request->input('content', '');
        $notice_type_id = $request->input('notice_type_id', 0);

        if (empty($title)) {
            throw new FailedException('通知标题不能为空！');
        }
        if (empty($content)) {
            throw new FailedException('通知内容不能为空！');
        }
        if (empty($notice_type_id)) {
            throw new FailedException('通知类型必填！');
        }
        $tye = NoticeTypes::where('id', $notice_type_id)->where('is_deleted', 0)->value('id');
        if (empty($tye)) {
            throw new FailedException('通知类型不正确！');
        }
        $param = [
            'title' => $title,
            'content' => $content,
            'notice_type_id' => $notice_type_id,
            'sort' => $request->input('sort',0),
        ];
        NoticesService::update($id, $param);
    }

    /**
     *  我的通知
     * name: myNotices
     * @param Request $request
     * @return LengthAwarePaginator
     * date 2023/03/29 23:48
     */
    public function myNotices(Request $request)
    {
        return NoticesService::myNoticesList($request->all());
    }

    /**
     *  已读
     * name: setRead
     * @param $id
     * date 2023/03/29 23:51
     */
    public function setRead($id): mixed
    {
         NoticesService::setRead($id);
         return response()->json(['code' => 10000, "message" => "操作成功",'data' => []]);
    }

    /**
     *  用户删除
     * name: userDel
     * @param $id
     * @return mixed
     * date 2023/03/29 23:55
     */
    public function userDel($id): mixed
    {
        return NoticesService::userDel($id);
    }

    /**
     *  阅读详情
     * name: detail
     * @param $id
     * date 2023/03/30 00:06
     */
    public function detail($id)
    {
        return NoticesService::detail($id);
    }

    /**
     *  获取通知人群选项
     * name: objOption
     * @param Request $request
     * date 2023/03/30 10:20
     */
    public function objOption(Request $request)
    {
        $type = $request->input('type', '2');
        $name = $request->input('name', '');
        $type = $type == "3" ? "role" : "user";
        return NoticesService::objOption($type, $name);
    }

    /**
     *  home页弹窗公告
     * name: getPopup
     * date 2023/03/30 16:41
     */
    public function getPopup(){
        return  NoticesService:: getPopup();
    }

}
