<?php

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

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

class ContactAddController extends Controller
{
    private $callApiService;

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

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

    public function store()
    {
        $response = $this->callApiService->callApi([
            'url' => 'company-contact-bd-act',
            'data' => [
                'Page' => [
                    [
                        'Id' => 0,
                        'ContactName' => request('contact_name'),
                        'MobileNo' => formatPhone(request('mobile_no')),
                        '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-contact-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;
        }

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

//        dump($contact);

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

    public function update($id)
    {
        $response = $this->callApiService->callApi([
            'url' => 'company-contact-bd-act',
            'data' => [
                'Page' => [
                    [
                        'Id' => (int)$id,
                        'ContactName' => request('contact_name'),
                        'MobileNo' => formatPhone(request('mobile_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 destroy($id){
        $response = $this->callApiService->callApi([
            'url' => 'company-contact-bd-act',
            'data' => [
                'Page' => [
                    [
                        'Id' => (int)-$id,
                    ]
                ]
            ]
        ]);

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

        return response($response, 200);
    }
}
