<?php

namespace App\Http\Controllers\backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\MembershipRecordUpdate;
use App\MembershipType;
use App\MemberRegistration;
use Validator;
use Alert;
use Auth;
use Carbon\Carbon;

class MembershipRecordUpdateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $result = Auth::check();

        if($result){
            $members = MembershipRecordUpdate::latest()->get();
            return view('backend.membership_record_update',compact(['members']));
        }
        else{
            $membership_types = MembershipType::latest()->get();
            return view('frontend.online_service.membership_record_update',compact(['membership_types']));
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //validating data to ensure all the fields have been filled
        $rules = [
            'title' => 'required',
            'first_name' => 'required',
            'last_name' => 'required',
            'address' => 'required',
            'email' => 'required',
            'gender' => 'required',
            'phone' => 'required',
            'membership_number' => 'required',
            'membership_type' => 'required',
            'annual_fee_year' => 'required',
            'qualification' => 'required',
        ];

        $error = Validator::make($request->all(),$rules);

        if($error->fails()){
            return response()->json(['info'=>"Please ensure that all the required fields have been filled (fields marked with *)."]);
        }
        else{
            //getting the membership number first
            $reg_number = $request->membership_number;
            
            if(!(MembershipRecordUpdate::where('membership_number',$reg_number)->exists()) && ($reg_number != 0)){
                //returning the notification
                return response()->json(['errors'=>"The Membership number $reg_number has already submitted the details"]);
            }
            
             elseif(!(MembershipRecordUpdate::where('membership_number',$reg_number)->exists()) && ($reg_number != 00)){
                //returning the notification
                return response()->json(['errors'=>"The Membership number $reg_number has already submitted the details"]);
            }
            
             elseif(!(MembershipRecordUpdate::where('membership_number',$reg_number)->exists()) && ($reg_number != 000)){
                //returning the notification
                return response()->json(['errors'=>"The Membership number $reg_number has already submitted the details"]);
            }
            
             elseif(!(MembershipRecordUpdate::where('membership_number',$reg_number)->exists()) && ($reg_number != 0000)){
                //returning the notification
                return response()->json(['errors'=>"The Membership number $reg_number has already submitted the details"]);
            }
            
             elseif(!(MembershipRecordUpdate::where('membership_number',$reg_number)->exists()) && ($reg_number != 00000)){
                //returning the notification
                return response()->json(['errors'=>"The Membership number $reg_number has already submitted the details"]);
            }
            
             elseif(!(MembershipRecordUpdate::where('membership_number',$reg_number)->exists()) && ($reg_number != 000000)){
                //returning the notification
                return response()->json(['errors'=>"The Membership number $reg_number has already submitted the details"]);
            }
            
             elseif(!(MembershipRecordUpdate::where('membership_number',$reg_number)->exists()) && ($reg_number != 0000000)){
                //returning the notification
                return response()->json(['errors'=>"The Membership number $reg_number has already submitted the details"]);
            }
           
            else{
                //getting data to save 
                $data_save = [
                    'title' => $request->title,
                    'first_name' => $request->first_name,
                    'middle_name' => $request->middle_name,
                    'last_name' => $request->last_name,
                    'gender' => $request->gender,
                    'membership_number' => $request->membership_number,
                    'membership_type' => $request->membership_type,
                    'annual_fee_year' => $request->annual_fee_year,
                    'qualification' => $request->qualification,
                    'occupation' => $request->occupation,
                    'experience' => $request->experience,
                    'address' => $request->address,
                    'phone' => $request->phone,
                    'email' => $request->email,
                ];

                //saving data
                $data = MembershipRecordUpdate::create($data_save);

                if($data){
                    return response()->json(['success'=>"Details have been submitted successully."]);
                }
                else{
                    return response()->json(['errors'=>"Unable to submit the details please try again later."]);
                }
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $data = MembershipRecordUpdate::findOrFail($id);
        return response()->json(['result'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, MembershipRecordUpdate $model)
    {
        //
        //validating data to ensure all the fields have been filled
        $rules = [
            'title' => 'required',
            'first_name' => 'required',
            'last_name' => 'required',
            'address' => 'required',
            'email' => 'required',
            'gender' => 'required',
            'phone' => 'required',
            'membership_number' => 'required',
            'membership_type' => 'required',
            'annual_fee_year' => 'required',
            'qualification' => 'required',
        ];

        $error = Validator::make($request->all(),$rules);

        if($error->fails()){
            return response()->json(['info'=>"Please ensure that all the required fields have been filled (fields marked with *)."]);
        }
        else{
            //getting data to update 
            $data_update = [
                'title' => $request->title,
                'first_name' => $request->first_name,
                'middle_name' => $request->middle_name,
                'last_name' => $request->last_name,
                'gender' => $request->gender,
                'membership_number' => $request->membership_number,
                'membership_type' => $request->membership_type,
                'annual_fee_year' => $request->annual_fee_year,
                'qualification' => $request->qualification,
                'occupation' => $request->occupation,
                'experience' => $request->experience,
                'address' => $request->address,
                'phone' => $request->phone,
                'email' => $request->email,
            ];

            //saving data
            $data = MembershipRecordUpdate::where('id',$request->hidden_data)->update($data_update);

            if($data){
                return response()->json(['success'=>"Details have been updated successully."]);
            }
            else{
                return response()->json(['errors'=>"Unable to update the details please try again later."]);
            }


        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $data = MembershipRecordUpdate::findOrFail($id);

        if($data->delete()){
            return response()->json(['success'=>"Record deleted successfully."]);
        }
        else{
            return response()->json(['errors'=>"Unable to delete record."]);
        }
    }

    public function check_status(Request $request){
        //getting the data first
        $email = $request->email_address;
        $number = $request->member_number;

        if($number == ""){
            return response()->json(['info'=>"Please membership number is required."]);
        }

        if($email == ""){
            return response()->json(['info'=>"Please enter the email address."]);
        }

        //checking in member registration table first
        if(MemberRegistration::where('membership_number',$number)->where('email',$email)->exists()){
            $data = MemberRegistration::where('membership_number',$number)->get();
            if(!($data->isEmpty())){
                foreach($data  as $row){
                    $title = $row->title;
                    $tpha_number = $row->membership_number;
                    $date = Carbon::parse($row->created_at)->format('d-m-Y');
                    $name = $row->first_name." ".$row->middle_name." ".$row->last_name;
                }

                return response()->json(['success'=>"Dear $title $name your details have been submitted to online database since $date with membership number $tpha_number."]);  
            }
        }

        //checking in newly collected data
        elseif(MembershipRecordUpdate::where('email',$email)->where('membership_number',$number)->exists()){
            $data = MembershipRecordUpdate::where('membership_number',$number)->get();

            if(!($data->isEmpty())){
                foreach($data  as $row){
                    $title = $row->title;
                    $tpha_number = $row->membership_number;
                    $date = Carbon::parse($row->created_at)->format('d-m-Y');
                    $name = $row->first_name." ".$row->middle_name." ".$row->last_name;
                }

                return response()->json(['info'=>"Dear $title $name, TPHA #.($tpha_number) your details are being processed and will be saved to online database once the process completed ."]);  
            }
        }
        else{
            return response()->json(['errors'=>"Your details are not available to online database, please consider to submit again using the online form as announced."]);
        }


    }
}
