<?php

namespace Modules\PaymentLog\Controllers\Admin;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\Canteen\Services\CanteenService;
use Modules\PaymentLog\Enums\PaymentLogAccStatusEnum;
use Modules\PaymentLog\Enums\PaymentLogPaidEnum;
use Modules\PaymentLog\Enums\PaymentLogStatusEnum;
use Modules\PaymentLog\Requests\Admin\PaymentLogRequest;
use Modules\PaymentLog\Services\PaymentLogService;
use Modules\Country\Services\CountryService;
use Modules\Region\Services\RegionService;
use Modules\School\Services\SchoolService;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Services\UserService;

class PaymentLogController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/payment_logs';
    /** View folder */
    private $viewDirectory= 'admin.payment_logs.';
    //

    public function __construct(
        private PaymentLogService $paymentLogService,
        private CanteenService $canteenService,
        private SchoolService $schoolService,
        private UserService $userService,
    )
    {}

    /**
     * Display a listing of the payment_logs.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $head= __('payment_logs.head');
        $action= route('payment_logs.store');
        $acc_statuses = PaymentLogAccStatusEnum::values();
        $statuses = PaymentLogStatusEnum::dashboard_operations();
        return view($this->viewDirectory.'index', get_defined_vars());
    }

    /**
     * return data of the payment_logs.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $request->merge(['paginate'=>true]);
        $canteens= $this->canteenService->get($request->all());

        return view($this->viewDirectory.'grid',get_defined_vars());
    }

    public function selectStatus(Request $request)
    {
        if ($request->acc_status == PaymentLogAccStatusEnum::CANTEEN->value)
        {
            $key = 'canteen_id';
            $accounts = $this->canteenService->get();
        }
        else
        {
            $key = 'user_id';
            $accounts = $this->userService->get([
                'role'=>UserRoleEnum::PARENT->value
            ]);
        }
        return view($this->viewDirectory.'status',get_defined_vars());
    }

    public function paymentInfo(Request $request)
    {
        $key = $request->acc_status == PaymentLogAccStatusEnum::CANTEEN->value ? 'canteen_id' : 'user_id';
        $data =
        [
            'acc_status'=>$request->acc_status,
            $key => $request->id
        ];
        $logs = $this->paymentLogService->get($data);
        return view($this->viewDirectory.'info',get_defined_vars());
    }

    /**
     * Show the form for creating a new PaymentLog.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('payment_logs.store');
        $head= __('payment_logs.new');
        $statuses = PaymentLogStatusEnum::dashboard_operations();
        $canteens = $this->canteenService->get();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Store a newly created PaymentLog in storage.
     * @param PaymentLogRequest $request
     * @return JsonResponse
     */
    public function store(PaymentLogRequest $request) :JsonResponse
    {
        $this->paymentLogService->create($request->validated());

        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('payment_logs.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Show the form for editing the specified PaymentLog.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $canteen = $this->canteenService->getItem($id);
        if (!$canteen)
        {
            abort(404);
        }
        $head= __('payment_logs.show'). ' : '.$canteen[getCurrentLocale()]->title;
        $payment_logs = $this->paymentLogService->get(['canteen_id'=>$id]);
        $total = $this->paymentLogService->getTotal($id);
        return view( $this->viewDirectory.'show', get_defined_vars() );
    }

    public function show($id)
    {
        $canteen = $this->canteenService->getItem($id);
        if (!$canteen)
        {
            abort(404);
        }
        $total = $this->paymentLogService->getTotal($id);
        if ($total == 0)
        {
            abort(404);
        }

        $this->paymentLogService->returnMoney($id);

        return redirect(route('payment_logs.edit',$id));
    }

    /**
     * Update the specified PaymentLog in storage.
     * @param PaymentLogRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(PaymentLogRequest $request, int $id) :JsonResponse
    {
        $paymentLog = $this->paymentLogService->checkItem($id);
        if (!$paymentLog)
        {
            abort(404);
        }
        $this->paymentLogService->update($request->validated(), $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('payment_logs.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified PaymentLog from storage.
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(int $id) :JsonResponse
    {
        $this->paymentLogService->destroy($id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('payment_logs.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove several payment_logs by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $this->paymentLogService->destroyAll([
            'ids' => $request->ids,
            'status' => 1
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('payment_logs.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore the specified PaymentLog from storage.
     *
     * @param int $id
     * @return JsonResponse
     */
    public function restore(int $id) :JsonResponse
    {
        $this->paymentLogService->destroy($id);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('payment_logs.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore several payment_logs by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $this->paymentLogService->destroyAll([
            'ids' => $request->ids,
            'status' => 0
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('payment_logs.index'),
        ];
        return ResponseService::response($response);
    }
}
