<?php

namespace App\Http\Controllers;

use App\Models\Member;
use App\Models\Transaction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use DateTime;


class MemberController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
     public $file_path = "uploads/Member_files";

    public function index()
    {
        //
    }

    /**
     * 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)
    // {
    //     //  dd($request->all());
    //     // $request->validate([
    //     //                 'title' => 'required',
    //     //                 'first_name' => 'required',
    //     //                 'middle_name' => 'required',
    //     //                 'last_name' => 'required',
    //     //                 'date_of_birth' => 'required',
    //     //                 'upload_member_photo'  => 'required',
    //     //                 'highest_qualification'  => 'required',
    //     //                 'upload_qualification_certificate' => 'required',
    //     //                 'speciality'  => 'required',
    //     //                 'medical_council_registraion_number'  => 'required',
    //     //                 'address'  => 'required',
    //     //                 'city'  => 'required',
    //     //                 'state'  => 'required',
    //     //                 'postal_code'  => 'required',
    //     //                 'country'  => 'required',
    //     //                 'upload_resident_photo'  => 'required',
    //     //                 'mobile_no'  => 'required',
    //     //                 'alt_mobile_no'  => 'required',
    //     //                 'email'  => 'required',
    //     //                 'alt_email' => 'required',
    //     //                 'other_contact_no' => 'required'
    //     //             ]);
        
    //     // Member::create($request->all());
    //        $member = new Member;
    //     $member->title = $request->title;
    //     $member->first_name = $request->first_name;
    //     $member->middle_name = $request->middle_name;
    //     $member->last_name = $request->last_name;
    //     $member->date_of_birth = $request->date_of_birth;
    //     $member->upload_member_photo = $request->upload_member_photo;
    //     $member->highest_qualification = $request->highest_qualification;
    //     $member->upload_qualification_certificate = $request->upload_qualification_certificate;
    //     $member->speciality = $request->speciality;
    //     $member->medical_council_registraion_number = $request->medical_council_registraion_number;
    //     $member->address = $request->address;
    //     $member->city = $request->city;
    //     $member->state = $request->state;
    //     $member->postal_code = $request->postal_code;
    //     $member->country = $request->country;
    //     $member->upload_resident_photo = $request->upload_resident_photo;
    //     $member->mobile_no = $request->mobile_no;
    //     $member->alt_mobile_no = $request->alt_mobile_no;
    //     $member->email = $request->email;
    //     $member->alt_email = $request->alt_email;
    //     $member->other_contact_no = $request->other_contact_no;
    //     $member->save();

    //     return redirect()->route('pss_membership_form')->with('success','Member has been created successfully.');
    // }

    // public function addMember(Request $request)
    // {
    //     //  dd($request->all());
       
    //     $title = $request->input('title');
    //     // dd($title);
    //     $first_name = $request->input('first_name');
    //     $middle_name = $request->input('middle_name');
    //     $last_name = $request->input('last_name');
    //     $date_of_birth = $request->input('date_of_birth');
    //     $upload_member_photo = '';
    //     if($request->file('upload_member_photo'))
    //     {
    //         $upload_member_photo = $this->uploadFile($request->file('upload_member_photo'), $this->file_path);
    //     }
    //     $highest_qualification = $request->input('highest_qualification');
    //     $upload_qualification_certificate = '';
    //     if($request->file('upload_qualification_certificate'))
    //     {
    //         $upload_qualification_certificate = $this->uploadFile($request->file('upload_qualification_certificate'), $this->file_path);
    //     }
    //     $speciality = $request->input('speciality');
    //     $medical_council_registraion_number = $request->input('medical_council_registraion_number');
    //     $address = $request->input('address');
    //     $city = $request->input('city');
    //     $state = $request->input('state');
    //     $postal_code = $request->input('postal_code');
    //     $country = $request->input('country');
    //     $upload_resident_photo = '';
    //     if($request->file('upload_resident_photo'))
    //     {
    //         $upload_resident_photo = $this->uploadFile($request->file('upload_resident_photo'), $this->file_path);
    //     }
    //     $mobile_no = $request->input('mobile_no');
    //     $alt_mobile_no = $request->input('alt_mobile_no');
    //     $email = $request->input('email');
    //     $alt_email = $request->input('alt_email');
    //     $other_contact_no = $request->input('other_contact_no');
    //     $mode_of_payment = $request->input('mode_of_payment');
    //     $instrument_no = $request->input('instrument_no');
    //     $amount = $request->input('amount');
    //     $date = $request->input('date');
    //     $bank_name = $request->input('bank_name');
    //     $upload_payment_proof = $request->input('upload_payment_proof');
    //     $now = (new DateTime())->format('Y-m-d H:i:s');
        
    //     $member_id = DB::table('members')
    //     ->insertGetId([ 'title' => $title, 'first_name' => $first_name,
    //     'middle_name' => $middle_name,
    //     'last_name'   => $last_name,
    //     'date_of_birth' => $date_of_birth,
    //     'upload_member_photo'   =>  $upload_member_photo,
    //     'highest_qualification' => $highest_qualification,
    //     'upload_qualification_certificate'  => $upload_qualification_certificate,
    //     'speciality' => $speciality,
    //     'medical_council_registraion_number' => $medical_council_registraion_number,
    //     'address'  => $address,
    //     'city' => $city,
    //     'state' =>  $state,
    //     'postal_code' => $postal_code,
    //     'country'   => $country,
    //     'upload_resident_photo' => $upload_resident_photo,
    //     'mobile_no' => $mobile_no,
    //     'alt_mobile_no' => $alt_mobile_no,
    //     'email'  => $email,
    //     'alt_email'  =>  $alt_email,
    //     'other_contact_no' => $other_contact_no,
    //     'mode_of_payment' => $mode_of_payment,
    //     'instrument_no'  => $instrument_no,
    //     'amount'  => $amount,
    //     'date'  => $date,
    //     'bank_name'  => $bank_name,
    //     'upload_payment_proof' => $upload_payment_proof,
    //     'created_at' => $now]);
       
    //     return Redirect::back()->with('success-message',"Members added successfully");
        
    // }

    public function addMember(Request $request)
    {
        // dd($request->all());
        $member = new Member;
        $member->title = $request->title;
        $member->first_name = $request->first_name;
        $member->middle_name = $request->middle_name;
        $member->last_name = $request->last_name;
        $member->date_of_birth = $request->date_of_birth;
        $member->upload_member_photo = $request->upload_member_photo;
       
        if($request->file('upload_member_photo'))
        {
            $upload_member_photo = $this->uploadFile($request->file('upload_member_photo'), $this->file_path);
        }
        $member->highest_qualification = $request->highest_qualification;
        $member->upload_qualification_certificate = $request->upload_qualification_certificate;
        if($request->file('upload_qualification_certificate'))
        {
            $upload_qualification_certificate = $this->uploadFile($request->file('upload_qualification_certificate'), $this->file_path);
        }
        $member->speciality = $request->speciality;
        $member->medical_council_registraion_number = $request->medical_council_registraion_number;
        $member->address = $request->address;
        $member->city = $request->city;
        $member->state = $request->state;
        $member->postal_code = $request->postal_code;
        $member->country = $request->country;
        $member->upload_resident_photo = $request->upload_resident_photo;
        if($request->file('upload_resident_photo'))
        {
            $upload_resident_photo = $this->uploadFile($request->file('upload_resident_photo'), $this->file_path);
        }
        $member->mobile_no = $request->mobile_no;
        $member->alt_mobile_no = $request->alt_mobile_no;
        $member->email = $request->email;
        $member->alt_email = $request->alt_email;
        $member->other_contact_no = $request->other_contact_no;
        $member->save();

        $transaction = new Transaction;

        $transaction->member_id = $member->id;
        $transaction->mode_of_payment = $request->mode_of_payment;
        $transaction->instrument_no = $request->instrument_no;
        $transaction->amount = $request->amount;
        $transaction->date = $request->date;
        $transaction->bank_name = $request->bank_name;
        $transaction->upload_payment_proof = $request->upload_payment_proof;
        if($request->file('upload_payment_proof'))
        {
            $upload_payment_proof = $this->uploadFile($request->file('upload_payment_proof'), $this->file_path);
        }
        $transaction->save();
        return redirect::back()->with('success-message','Member has been created successfully.');
       
        
    }



    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Member  $member
     * @return \Illuminate\Http\Response
     */
    public function show(Member $member)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Member  $member
     * @return \Illuminate\Http\Response
     */
    public function edit(Member $member)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Member  $member
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Member $member)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Member  $member
     * @return \Illuminate\Http\Response
     */
    public function destroy(Member $member)
    {
        //
    }

    public function uploadFile($file, $target_dir)
    { 
        $image_name = "Member_".date('YmdHis').".".$file->getClientOriginalExtension();
        if($file->move($target_dir, $image_name)){
            return $image_name;
        }
        else{
            return '';
        }
    }
}
