<?php

namespace Themes\kbgolf\pro\app\Http\Controllers\MyPage;

use App\Http\Controllers\Controller;
use App\Services\CallApiService;
use Illuminate\Pagination\LengthAwarePaginator;

class NoticeController extends Controller
{
    private $callApiService;

    public function __construct(CallApiService $callApiService)
    {
        $this->callApiService = $callApiService;
    }

    public function list()
    {
        $limit = (int)request('limit', 12);
        $page = (int)request('page', 1);

        $noticePage = $this->callApiService->callApi([
            'url' => 'list-type1-page',
            'data' => [
                'QueryVars' => [
                    'QueryName' => 'pro:my-page/post-list',
                    'SimpleFilter' => "post_code='notice' and mx.status != '0'",
                    'SubSimpleFilter' => "image_type = 'thumb'",
                    'IsntPagination' => false
                ],
                'ListType1Vars' => [
                    'OrderBy' => request('sort', 'mx.created_on desc')
                ],
                'PageVars' => [
                    'Limit' => $limit,
                    'Offset' => ($page - 1) * $limit
                ]
            ]
        ]);

        if ($this->callApiService->verifyApiError($noticePage)) {
            notify()->error($noticePage['body'], 'Error', 'bottomRight');
            return redirect()->back();
        }

        $noticePage['Page'] = new LengthAwarePaginator($noticePage['Page'], $noticePage['PageVars']['QueryCnt'],
            $limit, $page, ['path' => request()->url()]);

        return view('views.my-page.notice-list', compact('noticePage'));
    }

    public function show($id)
    {
        $listType1Book = $this->callApiService->callApi([
            'url' => 'list-type1-book',
            'data' => [
                'Book' => [
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:my-page/post-details',
                            'SimpleFilter' => "post_code='notice' and mx.id = $id",
                            'IsntPagination' => true,
                        ],
                        'PageVars' => [
                            'Limit' => 1
                        ]
                    ],
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:my-page/post-details-prenext',
                            'SimpleFilter' => "mx.id = (select max(id) from pro_post where id < ${id} and post_type_id = 1) and mx.status != '0'",
                            'SubSimpleFilter' => "",
                            'IsntPagination' => true,
                        ],
                        'PageVars' => [
                            'Limit' => 1
                        ]
                    ],
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:my-page/post-details-prenext',
                            'SimpleFilter' => "mx.id = (select min(id) from pro_post where id > ${id} and post_type_id = 1) and mx.status != '0'",
                            'SubSimpleFilter' => "",
                            'IsntPagination' => true,
                        ],
                        'PageVars' => [
                            'Limit' => 1
                        ]
                    ],
                ]
            ]
        ]);

        if ($this->callApiService->verifyApiError($listType1Book)) {
            notify()->error($listType1Book['body'], 'Error', 'bottomRight');
            return redirect()->back();
        }

//        dump($listType1Book);
        return view('views.my-page.notice-details', compact('listType1Book'));
    }
}
