<?php

namespace App\Http\Controllers;

use App\Device;
use App\FamilyMemberDetails;
use App\FireBase;
use App\NativeAddress;
use App\OverseasAddress;
use App\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class RegistrationController extends Controller
{
    public function index($id = null)
    {
        if ($id == null) {
//with(['overseas_address','overseas_address.user_id','assignedEmployee','assignedEmployee.designation'])
            //   $users = Users::with(['NativeAddress'])->get();
            $users = DB::table('users as us')
                ->leftjoin('native_address as na', 'us.id', '=', 'na.user_id')
                ->leftjoin('overseas_address as oa', 'us.id', '=', 'oa.user_id')
                ->select('us.*', 'na.*', 'oa.*', 'us.id as id')
                ->orderBy('us.id', 'desc')
                ->get();
            // print_r($users);
            //return response($users,200);
            return view("registered_users", compact('users'));
        } else {
            $data['registration'] = $this->show($id);
            return response($data, 200);
        }

    }


    public function view($id)
    {
        $user = DB::table('users as us')
            ->leftjoin('native_address as na', 'us.id', '=', 'na.user_id')
            ->leftjoin('overseas_address as oa', 'us.id', '=', 'oa.user_id')
            ->select('us.*', 'na.*', 'oa.*', 'us.id as id')
            ->where('us.id', '=', $id)
            ->get();
        $familydetails = DB::table('users as us')
            ->join('family_member_details as fmd', 'us.id', '=', 'fmd.relative_of')
            ->select('fmd.*')
            ->where('us.id', '=', $id)
            ->get();
        return view("registered_users_details", compact('user', 'familydetails'));
    }

    public function verify($id)
    {
        $user = DB::table('users as us')
            ->leftjoin('native_address as na', 'us.id', '=', 'na.user_id')
            ->leftjoin('overseas_address as oa', 'us.id', '=', 'oa.user_id')
            ->select('us.*', 'na.*', 'oa.*', 'us.id as id')
            ->where('us.id', '=', $id)
            ->get();
        $familydetails = DB::table('users as us')
            ->join('family_member_details as fmd', 'us.id', '=', 'fmd.relative_of')
            ->select('fmd.*')
            ->where('us.id', '=', $id)
            ->get();
        return view("verify_users", compact('user', 'familydetails'));
    }

    public function verifyUser(Request $request)
    {
        $id = $request->id;
        $user = Users::find($id)->first();
        $toUpdate = array('registration_status' => 'verified');
        $result = Users::where('id', $id)->update($toUpdate);
        if ($result) {
            $data['status'] = 1;
        } else {
            $data['status'] = 0;
        }
        return $data;

    }

    public function GenerateNorkaId($id)
    {
        $randNum = rand(10000, 99999);
        $users = Users::find($id);
        $users->norka_id = $randNum;
        $toUpdate = array('norka_id' => $users->norka_id);
        Users::where('id', $id)->update($toUpdate);
//        FireBase::pushToSingleUser('users', [
//            'notification' => [
//                'title' => substr($result->content, 0, 100),
//                'text' => substr($result->description, 0, 200)
//            ],
//            'data' => [
//                'type' => 'news'
//            ]
//        ]);
    }
//    public function show($id)
//    {
//        return Users::with(['postedEmployee','postedEmployeeDesignation.designation','assignedEmployee','assignedEmployeeDesignation.designation'])->find($id);
//    }

    public function store(Request $request)
    {
        $input = $request->all();

        $rules = [
            'user_type_id' => 'required',
            'sur_name' => 'required',
            'given_name' => 'required',
            'city' => 'required',
            'state' => 'required',
            'country' => 'required',
            'zipcode' => 'required',
            'mobile' => 'required',
            'landline' => 'required',
            'native_state' => 'required',
            'district' => 'required',
            'pincode' => 'required',
            'kerala_address' => 'required',
            'native_phone' => 'required',
            'gender' => 'required',
            'age' => 'required',
            'fullname' => 'required',
            'email' => 'required',
            'occupation' => 'required',
            'passport_no' => 'required',
            'marital_status' => 'required',

//            'relative_fullname' => 'required',
//            'relation' => 'required',
//            'relative_age' => 'required',
//            'relative_passport_no' => 'required',
//            'relative_address' => 'required'
        ];

        $messages = [
            'user_type_id.required' => 'Applicant type required',
            'sur_name.required' => 'Surname required',
            'given_name.required' => 'Name required',
            'city.required' => 'City required',
            'state.required' => 'State required',
            'country' => 'Country required',
            'zipcode' => 'Zipcode required',
            'mobile' => 'Mobile required',
            'landline' => 'Landline required',
            'native_state' => 'Native state required',
            'district' => 'District required',
            'pincode' => 'Pincode required',
            'kerala_address' => 'Kerala address required',
            'native_phone' => 'Native phone required',
            'gender' => 'Gender required',
            'age' => 'Age required',
            'fullname' => 'Fullname required',
            'email' => 'Email required',
            'occupation' => 'Occupation required',
            'passport_no' => 'Passport number required',
            'marital_status' => 'Marital status required',
//            'relative_fullname' => 'Relative fullname required',
//            'relation' => 'Relation required',
//            'relative_age' => 'Relative age required',
//            'relative_passport_no' => 'Relative passport number required',
//            'relative_address' => 'Relative address required'

        ];
        $validator = Validator::make($input, $rules, $messages);

        if ($validator->passes()) {
            $users = new Users();
            $users->user_type_id = $input['user_type_id'];
            $users->sur_name = $input['sur_name'];
            $users->given_name = $input['given_name'];
            $users->mobile = $input['mobile'];
            $users->landline = $input['landline'];
            $users->gender = $input['gender'];
            $users->age = $input['age'];
            $users->fullname = $input['fullname'];
            $users->email = $input['email'];
            $users->occupation = $input['occupation'];
            $users->passport_no = $input['passport_no'];
            $users->marital_status = $input['marital_status'];
            $users->qr_code = $input['qrcode'];
            if ($request->old_passport_no) {
                $users->old_passport_no = $input['old_passport_no'];
            } else {
                $users->old_passport_no = '';
            }
            if ($request->reference_code) {
                $users->reference_code = $input['reference_code'];
            } else {
                $users->reference_code = '';
            }
            if ($request->points) {
                $users->points = $input['points'];
            } else {
                $users->points = '';
            }
            if ($request->registration_status) {
                $users->registration_status = $input['registration_status'];
            } else {
                $users->registration_status = '';
            }
            if ($request->local_contact_no) {
                $users->local_contact_no = $input['local_contact_no'];
            } else {
                $users->local_contact_no = '';
            }
            if ($request->norka_id) {
                $users->norka_id = $input['norka_id'];
            } else {
                $users->norka_id = '';
            }
            $randNum = rand(10000, 99999);
            $users->pre_norka_id = $randNum;
            $users->save();
            if ($users->id) {
                if ($request->device_token) {
                    $device_token = $input['device_token'];
                } else {
                    $device_token = '';
                }
                $userId = $users->id;
                $device = new Device();
                $device->user_id = $userId;
                $device->device_token = $device_token;
                $device->save();
                $overseasAddress = new OverseasAddress();
                $overseasAddress->user_id = $userId;
                $overseasAddress->city = $input['city'];
                $overseasAddress->state = $input['state'];
                $overseasAddress->country = $input['country'];
                $overseasAddress->zipcode = $input['zipcode'];
                $overseasAddress->save();
                $nativeAddress = new NativeAddress();
                $nativeAddress->user_id = $userId;
                $nativeAddress->state = $input['native_state'];
                $nativeAddress->district = $input['district'];
                $nativeAddress->pincode = $input['pincode'];
                $nativeAddress->kerala_address = $input['kerala_address'];
                $nativeAddress->phone = $input['native_phone'];
                $nativeAddress->save();
                // dd($request->memberdetails);
                $family_array = json_decode($request->memberdetails);
//                dd($input['memberdetails']);
//                dd($family_array);

                if (is_array($family_array)) {

                    foreach ($family_array as $mem) {
// dd($mem);
                        $familyMember = new FamilyMemberDetails();
                        $familyMember->relative_of = $userId;
                        $familyMember->fullname = $mem->fullname;
                        $familyMember->relation = $mem->relation;
                        $familyMember->age = $mem->age;
                        $familyMember->passport_number = $mem->passport_number;
                        $familyMember->address = $mem->address;
                        $familyMember->is_nominee = $mem->is_nominee;
                        $familyMember->save();
                    }
                } else {
                    dd("not array");
                }

                $data['id'] = strval($users->id);
                $data['status'] = 1;
                $data['pre_norka_id'] = strval($users->pre_norka_id);
                $data['message'] = "User registered successfully";
                return response($data, 200);
            }

        } else {
            $data['message'] = $validator->errors(0);
            $data['status'] = 0;
            return response($data, 400);
        }

    }

    public function update(Request $request)
    {
        $input = $request->all();

        $rules = [
            'user_type_id' => 'required',
            'sur_name' => 'required',
            'given_name' => 'required',
            'city' => 'required',
            'state' => 'required',
            'country' => 'required',
            'zipcode' => 'required',
            'mobile' => 'required',
            'landline' => 'required',
            'native_state' => 'required',
            'district' => 'required',
            'pincode' => 'required',
            'kerala_address' => 'required',
            'native_phone' => 'required',
            'gender' => 'required',
            'age' => 'required',
            'fullname' => 'required',
            'email' => 'required',
            'occupation' => 'required',
            'passport_no' => 'required',
            'marital_status' => 'required',

//            'relative_fullname' => 'required',
//            'relation' => 'required',
//            'relative_age' => 'required',
//            'relative_passport_no' => 'required',
//            'relative_address' => 'required'
        ];

        $messages = [
            'user_type_id.required' => 'Applicant type required',
            'sur_name.required' => 'Surname required',
            'given_name.required' => 'Name required',
            'city.required' => 'City required',
            'state.required' => 'State required',
            'country' => 'Country required',
            'zipcode' => 'Zipcode required',
            'mobile' => 'Mobile required',
            'landline' => 'Landline required',
            'native_state' => 'Native state required',
            'district' => 'District required',
            'pincode' => 'Pincode required',
            'kerala_address' => 'Kerala address required',
            'native_phone' => 'Native phone required',
            'gender' => 'Gender required',
            'age' => 'Age required',
            'fullname' => 'Fullname required',
            'email' => 'Email required',
            'occupation' => 'Occupation required',
            'passport_no' => 'Passport number required',
            'marital_status' => 'Marital status required',
//            'relative_fullname' => 'Relative fullname required',
//            'relation' => 'Relation required',
//            'relative_age' => 'Relative age required',
//            'relative_passport_no' => 'Relative passport number required',
//            'relative_address' => 'Relative address required'

        ];
        $validator = Validator::make($input, $rules, $messages);

        if ($validator->passes()) {
            $users = new Users();
            $users->user_type_id = $input['user_type_id'];
            $users->sur_name = $input['sur_name'];
            $users->given_name = $input['given_name'];
            $users->mobile = $input['mobile'];
            $users->landline = $input['landline'];
            $users->gender = $input['gender'];
            $users->age = $input['age'];
            $users->fullname = $input['fullname'];
            $users->email = $input['email'];
            $users->occupation = $input['occupation'];
            $users->passport_no = $input['passport_no'];
            $users->marital_status = $input['marital_status'];
            $users->qr_code = $input['qrcode'];
            if ($request->old_passport_no) {
                $users->old_passport_no = $input['old_passport_no'];
            } else {
                $users->old_passport_no = '';
            }
            if ($request->reference_code) {
                $users->reference_code = $input['reference_code'];
            } else {
                $users->reference_code = '';
            }
            if ($request->points) {
                $users->points = $input['points'];
            } else {
                $users->points = '';
            }
            if ($request->registration_status) {
                $users->registration_status = $input['registration_status'];
            } else {
                $users->registration_status = '';
            }
            if ($request->local_contact_no) {
                $users->local_contact_no = $input['local_contact_no'];
            } else {
                $users->local_contact_no = '';
            }
            if ($request->norka_id) {
                $users->norka_id = $input['norka_id'];
            } else {
                $users->norka_id = '';
            }

            $randNum = rand(10000, 99999);
            $users->pre_norka_id = $randNum;
            $toUpdate = array('user_type_id' => $users->user_type_id,
                'sur_name' => $users->sur_name,
                'given_name' => $users->given_name,
                'mobile' => $users->mobile,
                'landline' => $users->landline,
                'gender' => $users->gender,
                'age' => $users->age,
                'fullname' => $users->fullname,
                'email' => $users->email,
                'occupation' => $users->occupation,
                'passport_no' => $users->passport_no,
                'reference_code' => $users->reference_code,
                'points' => $users->points,
                'registration_status' => $users->registration_status,
                'local_contact_no' => $users->local_contact_no,
                'norka_id' => $users->norka_id,
                'pre_norka_id' => $users->pre_norka_id);
            $id = $input['id'];
            $isUpdated = Users::where('id', $id)->update($toUpdate);
            if ($isUpdated) {
                $userId = $id;
                if ($request->device_token) {
                    $device_token = $input['device_token'];
                } else {
                    $device_token = '';
                }
                $toUpdate = array('device_token' => $device_token);
                Device::where('user_id', $userId)->update($toUpdate);
                $overseasAddress = new OverseasAddress();
                $overseasAddress->city = $input['city'];
                $overseasAddress->state = $input['state'];
                $overseasAddress->country = $input['country'];
                $overseasAddress->zipcode = $input['zipcode'];
                $toUpdate = array('city' => $overseasAddress->city,
                    'state' => $overseasAddress->state,
                    'country' => $overseasAddress->country,
                    'zipcode' => $overseasAddress->zipcode);
                OverseasAddress::where('user_id', $userId)->update($toUpdate);
                $nativeAddress = new NativeAddress();
                $nativeAddress->state = $input['native_state'];
                $nativeAddress->district = $input['district'];
                $nativeAddress->pincode = $input['pincode'];
                $nativeAddress->kerala_address = $input['kerala_address'];
                $nativeAddress->phone = $input['native_phone'];
                $toUpdate = array('city' => $overseasAddress->city,
                    'state' => $nativeAddress->state,
                    'district' => $nativeAddress->district,
                    'pincode' => $nativeAddress->pincode,
                    'kerala_address' => $nativeAddress->kerala_address,
                    'phone' => $nativeAddress->phone
                );
                NativeAddress::where('user_id', $userId)->update($toUpdate);
                $family_array = json_decode($request->memberdetails);
                if (is_array($family_array)) {
                    foreach ($family_array as $mem) {
                        $familyMember = new FamilyMemberDetails();
                        $familyMember->relative_of = $userId;
                        $familyMember->fullname = $mem->fullname;
                        $familyMember->relation = $mem->relation;
                        $familyMember->age = $mem->age;
                        $familyMember->passport_number = $mem->passport_number;
                        $familyMember->address = $mem->address;
                        $familyMember->is_nominee = $mem->is_nominee;
                        $toUpdate = array('city' => $overseasAddress->city,
                            'fullname' => $familyMember->fullname,
                            'relation' => $familyMember->relation,
                            'age' => $familyMember->age,
                            'passport_number' => $familyMember->passport_number,
                            'address' => $familyMember->address,
                            'is_nominee' => $familyMember->is_nominee,
                        );
                    }
                } else {
                    dd("not array");
                }

                $data['id'] = strval($users->id);
                $data['status'] = 1;
                $data['norka_id'] = strval($users->pre_norka_id);
                $data['message'] = "User registered successfully";
                return response($data, 200);
            }

        } else {
            $data['message'] = $validator->errors(0);
            $data['status'] = 0;
            return response($data, 400);
        }

    }

}
