<?php

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

use App\Http\Controllers\Controller;
use App\Services\CallApiService;

class AddressAddController extends Controller
{
    private $callApiService;

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

    public function index()
    {
        return view('views.shop.checkout.address.index')->with('codeTitle', [
            "sort('company-destina-bd')",
        ]);
    }

    public function store()
    {
        $response = $this->callApiService->callApi([
            'url' => 'company-destina-bd-act',
            'data' => [
                'Page' => [
                    [
                        'Id' => 0,
                        'ZipCode' => request('zip_code'),
                        'Addr1' => request('addr1'),
                        'Addr2' => request('addr2'),
                        'SeqNo' => (int)request('seq_no'),
                        'Sort' => request('sort'),
                    ]
                ]
            ]
        ]);

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

        notify()->success(_e('Action completed'), 'Success', 'bottomRight');
        return redirect()->back();
    }

    public function show($id)
    {
        $response = $this->callApiService->callApi([
            'url' => 'company-destina-bd-pick',
            'data' => [
                'Page' => [
                    [
                        'Id' => (int)$id,
                    ]
                ]
            ]
        ]);

        if ($this->callApiService->verifyApiError($response)) {
            notify()->error($response['body'], 'Error', 'bottomRight');
            echo "<script>window.top.location.href = '" . url()->previous() . "'</script>";
            return false;
        }

        $address = reset($response['Page']);

        return view('views.shop.checkout.address.show', compact('address'))->with('codeTitle', [
            "sort('company-destina-bd')",
        ]);
    }

    public function update($id)
    {
        $response = $this->callApiService->callApi([
            'url' => 'company-destina-bd-act',
            'data' => [
                'Page' => [
                    [
                        'Id' => (int)$id,
                        'ZipCode' => request('zip_code'),
                        'Addr1' => request('addr1'),
                        'Addr2' => request('addr2'),
                        'Sort' => request('sort'),
                    ]
                ]
            ]
        ]);

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

        notify()->success(_e('Action completed'), 'Success', 'bottomRight');
        return redirect()->back();
    }

    public function destroy($id){
        $response = $this->callApiService->callApi([
            'url' => 'company-destina-bd-act',
            'data' => [
                'Page' => [
                    [
                        'Id' => (int)-$id,
                    ]
                ]
            ]
        ]);

        if ($this->callApiService->verifyApiError($response)) {
            return response($response, 200);
        }

        return response($response, 200);
    }
}
