<?php

namespace App\Http\Controllers\backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\MemberRegistration;
use App\MemberPayment;
use App\OnlineService;
use App\MembershipRecordUpdate;
use App\Exports\MemberRegistrationAll;
use App\Exports\MemberPaymentAll;
use App\Exports\MemberRegistrationSpecific;
use App\Exports\MemberPaymentSpecific;
use App\Exports\OnlineServiceAll;
use App\Exports\OnlineServiceSpecific;
use App\Exports\MembersRecordAll;
use App\Exports\MembersRecordSpecific;
use Excel;
use PDF;
use Carbon\Carbon;

class ReportController extends Controller
{
    //
    public function index(Request $request){
        return view('backend.report');
    }

    public function processing(Request $request){
         //getting parameters
         $type = $request->report_type;
         $criteria = $request->report_criteria; //all dates, specific
         $from_date = $request->from_date;
         $to_date = $request->to_date;
         $format = $request->report_format; //pdf, excel
         $date = date('d-m-Y');

         $from = Carbon::parse($from_date)->format('d-m-Y');
         $to = Carbon::parse($to_date)->format('d-m-Y');

         //PDF
         if($format == "pdf"){

            //switching the type
            switch ($type) {
                case 'member_registration':{

                    if($criteria == "all_dates"){
                        $members = MemberRegistration::latest()->get();
                        $title = "LIST OF ALL REGISTERED MEMBERS.";
                        $pdf = PDF::setPaper('a4', 'landscape')->loadView('backend.exports.members',compact(['members','title']));                        
                        return $pdf->stream('members.pdf');
                    }
                    else{
                        $members = MemberRegistration::latest()->whereBetween('created_at',[$from_date,$to_date])->get();
                        $title = "LIST OF ALL REGISTERED MEMBERS BETWEEN $from TO $to.";
                        $pdf = PDF::setPaper('a4', 'landscape')->loadView('backend.exports.members',compact(['members','title']));                        
                        return $pdf->stream('specific_members.pdf');
                    }
                }
                break;

                case 'payments':{
                    if($criteria == "all_dates"){
                        $members_payments = MemberPayment::leftJoin('member_registrations','member_registrations.id','=','member_payments.member_id')
                        ->latest()->get(['member_payments.*','member_registrations.title','member_registrations.first_name','member_registrations.middle_name','member_registrations.last_name','member_registrations.phone','member_registrations.email']);
                        $title = "LIST OF MEMBERS PAYMENTS.";
                        $pdf = PDF::setPaper('a4', 'landscape')->loadView('backend.exports.members_payments',compact(['members_payments','title']));                        
                        return $pdf->stream('members_payments.pdf');
                    }
                    else{
                        $members_payments = MemberPayment::leftJoin('member_registrations','member_registrations.id','=','member_payments.member_id')
                        ->whereBetween('member_payments.created_at',[$from_date,$to_date])
                        ->latest()
                        ->get(['member_payments.*','member_registrations.title','member_registrations.first_name','member_registrations.middle_name','member_registrations.last_name','member_registrations.phone','member_registrations.email']);
                        $title = "LIST OF MEMBERS PAYMENTS BETWEEN $from TO $to.";
                        $pdf = PDF::setPaper('a4', 'landscape')->loadView('backend.exports.members_payments',compact(['members_payments','title']));                        
                        return $pdf->stream('members_payments_specific.pdf');
                    }
                }
                break;

                case 'online_services':{

                    if($criteria == "all_dates"){
                        $online_services = OnlineService::latest()->get();
                        $title = "LIST OF ALL REGISTERED MEMBERS FOR 39TH CONFERENCE..";                        
                        $pdf = PDF::setPaper('a4', 'landscape')->loadView('backend.exports.online_service',compact(['online_services','title']));    
                        return $pdf->stream('online_conference_members.pdf');
                    }
                    else{
                        $online_services = OnlineService::latest()->whereBetween('created_at',[$from_date,$to_date])->get();
                        $title = "LIST OF ALL REGISTERED MEMBERS FOR 39TH CONFERENCE REGISTERED BETWEEN $from TO $to.";
                        $pdf = PDF::setPaper('a4', 'landscape')->loadView('backend.exports.online_service',compact(['online_services','title']));                        
                        return $pdf->stream('filtered_conference_registered_members.pdf');
                    }
                }
                break;

                case "membership_record_update":{
                    if($criteria == "all_dates"){
                        $members_records = MembershipRecordUpdate::latest()->get();
                        $title = "LIST OF ALL MEMBERS UPDATED RECORDS.";
                        $pdf = PDF::setPaper('a4', 'landscape')->loadView('backend.exports.members_records',compact(['members_records','title']));                        
                        return $pdf->stream('members_records.pdf');
                    }
                    else{
                        $members_records = MembershipRecordUpdate::latest()->whereBetween('created_at',[$from_date,$to_date])->get();
                        $title = "LIST OF ALL MEMBERS UPDATED RECORDS REGISTERED BETWEEN $from TO $to.";
                        $pdf = PDF::setPaper('a4', 'landscape')->loadView('backend.exports.members_records',compact(['members_records','title']));                        
                        return $pdf->stream('filtered_members_records.pdf');
                    }
                }
                break;
            }
            
         }
         else{
             switch ($type) {
                 case 'member_registration':{
                     if($criteria == "all_dates"){
                        return Excel::download (new MemberRegistrationAll(),'member_registration.xlsx');
                     }
                     else{
                        return Excel::download (new MemberRegistrationSpecific($from_date,$to_date,$from,$to),'member_registration_specific.xlsx');
                     }
                 }
                break;


                case 'online_services':{
                    if($criteria == "all_dates"){
                       return Excel::download (new OnlineServiceAll(),'online_conference_registration.xlsx');
                    }
                    else{
                       return Excel::download (new OnlineServiceSpecific($from_date,$to_date,$from,$to),'online_conference_registration_specific.xlsx');
                    }
                }

               break;

                case 'payments': {
                    if($criteria == "all_dates"){
                        return Excel::download (new MemberPaymentAll(),'member_payments_all.xlsx');
                    }
                    else{
                        return Excel::download (new MemberPaymentSpecific($from_date,$to_date,$from,$to),'member_payments_specific.xlsx');
                    }
                }
                break;

                case 'membership_record_update':{
                    if($criteria == "all_dates"){
                        return Excel::download (new MembersRecordAll(),'members_records_all.xlsx');
                    }
                    else{
                        return Excel::download (new MembersRecordSpecific($from_date,$to_date,$from,$to),'member_payments_specific.xlsx');
                    }
                }
             }
         }
    }
}
