<?php

namespace Modules\Internal\Http\Controllers\Api\Linksehat;

use App\Helpers\Helper;
use App\Models\OLDLMS\Appointment;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Internal\Transformers\LinksehatPaymentResource;

class PaymentController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index(Request $request)
    {
        // return $request->toArray();
        $appointments = Appointment::query()
            ->where('sPaymentStatus', 'settlement')
            ->with(['healthCare', 'healthCare.commission', 'detail', 'user', 'doctor', 'doctor.user']);

        if ($request->has('search')) {
            $appointments->where(function ($query) use ($request) {
                $query->where('nID', $request->search)
                    ->orWhere('sBookingCode', $request->search)
                    ->orWhereHas('detail', function (Builder $detail) use ($request) {
                        $detail->where('sPaymentDetails', 'LIKE', '%' . $request->search . "%");
                    });
            });
        }

        if (($request->has('appointment_start') || $request->has('appointment_end'))
            && !empty($request->appointment_start)
            && !empty($request->appointment_end)
        ) {
            // $appointments = $appointments->whereHas('detail', function (Builder $detail) use ($request) {

            //     // Appointment Start
            //     // if ($request->has('appointment_start')) {
            //     $detail->where('dTanggalAppointment', '>=', $request->appointment_start);
            //     // } else {
            //     //     $detail->where('dTanggalAppointment', '>', now()->format('Y-m-d'));
            //     // }

            //     // if ($request->has('appointment_end')) {
            //     $detail->where('dTanggalAppointment', '<=', $request->appointment_end);
            //     // } else {
            //     //     $detail->where('dTanggalAppointment', '<', now()->addDay(1)->format('Y-m-d'));
            //     // }

            // });

            $appointments = $appointments->where(function($q) use ($request) {
                $q->where('dCreateOn', '>=', $request->appointment_start)
                    ->where('dCreateOn', '<=', $request->appointment_end);
            });
        }

        if ($request->has('payment_status') && $request->payment_status != 'semua') {
            $appointments->where('sPaymentStatus', $request->payment_status);
        }

        if ($request->has('healthcare_id') && !empty($request->healthcare_id)) {
            $appointments->where('nIDHealthCare', $request->healthcare_id);
        }

        $appointments = $appointments->orderBy('dUpdateOn', 'DESC')
            ->paginate();

        return Helper::responseJson(Helper::paginateResources(LinksehatPaymentResource::collection($appointments)));
    }

    /**
     * Show the form for creating a new resource.
     * @return Renderable
     */
    public function create()
    {
        return view('internal::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param Request $request
     * @return Renderable
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Show the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function show($id)
    {
        return view('internal::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function edit($id)
    {
        return view('internal::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return Renderable
     */
    public function destroy($id)
    {
        //
    }
}
