<?php

namespace Themes\Pro\modunawa\app\Http\Controllers\MyPage;

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

class EventController 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);

        $eventPage = $this->callApiService->callApi([
            'url' => 'list-type1-page',
            'data' => [
                'QueryVars' => [
                    'QueryName' => 'pro:my-page/event-list',
                    'SimpleFilter' => "mx.status != '0'",
                    'SubSimpleFilter' => "image_type = 'middle'",
                    'IsntPagination' => false
                ],
                'ListType1Vars' => [
                    'OrderBy' => request('sort', 'mx.status asc')
                ],
                'PageVars' => [
                    'Limit' => $limit,
                    'Offset' => ($page - 1) * $limit
                ]
            ]
        ]);

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

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

        return view('views.my-page.event-list', compact('eventPage'))->with('codeTitle', [
            "status('post-event')",
        ]);
    }

    public function show($id)
    {
        $listType1Book = $this->callApiService->callApi([
            'url' => 'list-type1-book',
            'data' => [
                'Book' => [
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:my-page/event-details',
                            'SimpleFilter' => "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 = 3) 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 = 3) 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.event-details', compact('listType1Book'))->with('codeTitle', [
            "status('post-event')",
        ]);
    }
}
