<?php

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

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

class MemberEditController extends Controller
{
    private $callApiService;

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

    public function index()
    {
        $memberId = session('member')['MemberId'];
        $listType1Book = $this->callApiService->callApi([
            'url' => 'list-type1-book',
            'data' => [
                'Book' => [
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:shop/company-contact-bd',
                            'IsntPagination' => true,
                        ],
                        'ListType1Vars' => [
                            'OrderBy' => 'sort asc'
                        ],
                        'PageVars' => [
                            'Limit' => 100000
                        ]
                    ],
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro:shop/company-destina-bd',
                            'IsntPagination' => true,
                        ],
                        'ListType1Vars' => [
                            'OrderBy' => 'sort asc'
                        ],
                        'PageVars' => [
                            'Limit' => 100000
                        ]
                    ],
                    [
                        'QueryVars' => [
                            'QueryName' => 'pro/member-company-single',
                            'SimpleFilter' => "mx.id = $memberId",
                            'IsntPagination' => true,
                        ],
                        'PageVars' => [
                            'Limit' => 100000
                        ]
                    ],
                ]
            ]
        ]);

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

        $member = $listType1Book['Book'][2]['Page'][0];
//        dump($member);

        return view('views.my-page.member-edit', compact('listType1Book', 'member'))
            ->with('codeTitle', [
                "sort('company-contact-bd')", "sort('company-destina-bd')", "paymethod('sorder')",
            ]);
    }

    public function show()
    {
        $memberId = session('member')['MemberId'];

        $response = $this->callApiService->callApi([
            'url' => 'list-type1-page',
            'data' => [
                'QueryVars' => [
                    'QueryName' => 'pro/member-company-single',
                    'SimpleFilter' => "mx.id = $memberId",
                    'IsntPagination' => true,
                ],
                'PageVars' => [
                    'Limit' => 100000
                ]
            ],
        ]);

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

        $member = reset($response['Page']);
//        dump($member);

        return view('views.my-page.member-edit.show', compact('member'));
    }

    public function update($id)
    {
        $response = $this->callApiService->callApi([
            'url' => 'member-act',
            'data' => [
                'Page' => [
                    [
                        'Id' => (int)$id,
                        'FirstName' => request('first_name'),
                    ]
                ]
            ]
        ]);

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

        if (request('van')) {
            $company_json = json_encode([
                'Van' => request('van'),
                'VanId' => request('van_id'),
                'VanPw' => request('van_pw'),
            ]);
        }

//        dd($company_json);

        $response = $this->callApiService->callApi([
            'url' => 'company-act',
            'data' => [
                'Page' => [
                    [
                        'Id' => (int)session('member')['MemberId'],
                        'TaxNo' => request('tax_no'),
                        'CompanyJson' => $company_json ?? ''
                    ]
                ]
            ]
        ]);

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

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