<?php

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

use App\Http\Controllers\Controller;
use App\Services\CallApiService;
use Carbon\Carbon;
use Unirest\Request;

class CheckoutOkController extends Controller
{
    private $callApiService;

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

    public function index()
    {
        $id = session('sorder_id');

        if (empty($id)) {
            notify()->error('잘 못 된 접근 입니다', 'Error', 'bottomRight');
            return redirect()->to('/');
        }

        $listType1Book = $this->callApiService->callApi([
            'url' => 'list-type1-book',
            'data' => [
                'Book' => [
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:shop/sorder',
                            'IsntPagination' => true,
                            'SimpleFilter' => "mx.id = $id"
                        ],
                        'PageVars' => [
                            'Limit' => 100000
                        ]
                    ],
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:shop/sorder-bd',
                            'IsntPagination' => true,
                            'SimpleFilter' => "mb.sorder_id = $id",
                            'SubSimpleFilter' => "image_type = 'thumb'",
                        ],
                        'PageVars' => [
                            'Limit' => 100000
                        ]
                    ],
                ]
            ]
        ]);

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

        $sorderCharge['sub_total'] = collect($listType1Book['Book'][1]['Page'])->sum('C8');
        $checkoutCharge['charge_amt'] = (int)$listType1Book['Book'][0]['Page'][0]['D7'];
        $sorderCharge['total'] = $sorderCharge['sub_total'] + $checkoutCharge['charge_amt'];
        $sorderCharge['count'] = collect($listType1Book['Book'][1]['Page'])->count();

//        dump($listType1Book['Book']);
        return view('views.shop.checkout-ok', compact('listType1Book', 'sorderCharge'))->with('codeTitle', [
            "paymethod('sorder')",
        ]);
    }

    public function store()
    {

    }
}
