<?php

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

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

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

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

//        dump($oneToOnePage);

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

        return view('views.etc.1to1-list', compact('oneToOnePage'));
    }

    public function store()
    {
        $validator = Validator::make(request()->all(), [
            'post_title' => 'required',
            'post_contents' => 'required',
            'pc1' => 'required',
            'pc2' => 'required|email'
        ]);

        if ($validator->fails()) {
            notify()->error(_e('Action failed'), 'Error', 'bottomRight');
            return redirect()->back()
                ->withErrors($validator)
                ->withInput();
        }

        $response =  $this->callApiService->callApi([
            'url' => 'post-act',
            'data' => [
                'Page' => [
                    [
                        'Id' => 0,
                        'PostTitle' => request('post_title'),
                        'PostContents' => request('post_contents'),
                        'PostTypeId' => 6,
                        'Pc1' => request('pc1'),
                        'Pc2' => request('pc2'),
                        'Status' => '2',
                        'MemberId' => session('member')['MemberId'],
                    ]
                ]
            ],
        ]);

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

        notify()->success(_e('Action completed'), 'Success', 'bottomRight');
        return redirect()->route('1to1.list');
    }

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

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

//        dump($listType1Book);
        return view('views.etc.1to1-details', compact('listType1Book'));
    }
}
