<?php

namespace App\Http\Controllers;

use App\Models\Member;
use App\Models\Membership;
use App\Models\MembershipPayment;
use App\Models\Notification;
use Stripe;
use Illuminate\Http\Request;
use Srmklive\PayPal\Services\PayPal as PayPalClient;


class MembershipPaymentController extends Controller
{

    public function index()
    {
        if (\Auth::user()->can('manage membership payment')) {
            $membershipPayments = MembershipPayment::where('parent_id', parentId())->get();
            return view('membership_payment.index', compact('membershipPayments'));
        } else {
            return redirect()->back()->with('error', __('Permission Denied.'));
        }
    }


    public function create()
    {
        //
    }


    public function store(Request $request)
    {
        //
    }


    public function show($id)
    {
        if (\Auth::user()->can('show membership payment')) {
            $id = \Illuminate\Support\Facades\Crypt::decrypt($id);
            $payment = MembershipPayment::where('id', $id)->where('parent_id', parentId())->first();
            $membership = Membership::where('member_id', $payment->member_id)->where('plan_id', $payment->plan_id)->first();
            $invoicePaymentSettings = invoicePaymentSettings($payment->parent_id);
            return view('membership_payment.show', compact('payment', 'membership', 'invoicePaymentSettings'));
        } else {
            return redirect()->back()->with('error', __('Permission Denied.'));
        }
    }


    public function edit(MembershipPayment $membershipPayment)
    {
        $payment_methods = MembershipPayment::$payment_method;
        return view('membership_payment.payment', compact('membershipPayment', 'payment_methods'));
    }


    public function update(Request $request, MembershipPayment $membershipPayment)
    {

        // dd($request->all());
        $validetor = \Validator::make(
            $request->all(),
            [
                'payment_date' => 'required',
                'payment_method' => 'required',
            ]
        );

        if ($validetor->fails()) {
            $messages = $validetor->getMessageBag();
            return redirect()->back()->with('error', $messages->first());
        }
        $membershipPayment->payment_date = $request->payment_date;
        $membershipPayment->payment_method = $request->payment_method;
        $membershipPayment->notes = !empty($request->notes) ? $request->notes : '';
        $membershipPayment->status = 'Paid';
        $membershipPayment->save();

        $module = 'payment_create';
        $notification = Notification::where('parent_id', parentId())->where('module', $module)->first();
        $setting = settings();
        $errorMessage = '';

        if (!empty($notification) && $notification->enabled_email == 1) {
            $notificationResponse = MessageReplace($notification, $membershipPayment->id);

            $data['subject'] = $notificationResponse['subject'];
            $data['message'] = $notificationResponse['message'];
            $data['module'] = $module;
            $data['logo'] = $setting['company_logo'];
            $to = $membershipPayment->members->email;

            $response = commonEmailSend($to, $data);

            if ($response['status'] == 'error') {
                $errorMessage = $response['message'];
            }
        }


        return redirect()->back()->with('success', __('Membership Payment successfully.') . '' . $errorMessage);
    }


    public function destroy(MembershipPayment $membershipPayment)
    {
        if (\Auth::user()->can('delete membership payment')) {
            $payment = MembershipPayment::where('id', $membershipPayment->id)->where('parent_id', parentId())->first();
            if ($payment) {

                $payment->delete();
                // $payment->payment_date = null;
                // $payment->payment_method = null;
                // $payment->notes = null;
                // $payment->status = 'Unpaid';
                // $payment->save();

                return redirect()->back()->with('success', __('Membership Payment successfully deleted.'));
            }
        } else {
            return redirect()->back()->with('error', __('Permission Denied.'));
        }
    }
}

