<?php

namespace App\Http\Controllers;

use App\Scheme;
use App\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class LoginController extends Controller
{
    public function index()
    {
        $regUserCount=Users::all()->count();
        $verUserCount=Users::where('norka_id','<>',null)->count();
        $nonVerUserCount=$regUserCount-$verUserCount;
        $schemeCount=Scheme::all()->count();
        return view("index",compact('regUserCount','verUserCount','nonVerUserCount','schemeCount'));
    }
    public function countryWiseList(Request $request)
    {
        $client = new \GuzzleHttp\Client();
        $response = $client->request('GET', 'http://www.ifm.co.in/webservice/listcountry');
        $country = json_decode($response->getBody()->getContents())->countryList;
        $index = 1;
        $resultArray=[];
        $resultArray[0]=['Country','Verified','Non-verified'];
        foreach ($country as $cy) {

//            $countryCount[$index]['Country'] = $cy->name;
            $verifiedCount = DB::table('users as us')
                ->leftjoin('overseas_address as oa', 'us.id', '=', 'oa.user_id')
                ->select('us.*')
                ->where('us.registration_status', 'verified')
                ->where('oa.country',$cy->name)
                ->get()->count();

            $notVerifiedCount = DB::table('users as us')
                ->leftjoin('overseas_address as oa', 'us.id', '=', 'oa.user_id')
                ->select('us.*')
                ->where('us.registration_status','=',null)
                ->where('oa.country', $cy->name)
                ->get()->count();
            $resultArray[$index]=[$cy->name,$verifiedCount,$notVerifiedCount];
//            $countryCount[$index]['Verified'] = $verifiedCount;
//            $countryCount[$index]['Not Verified'] = $notVerifiedCount;
            $index++;
        }
        $data['resultArray']=$resultArray;

       // dd($resultArray);
        return $resultArray;
    }

    public function view($norkaid)
    {
        $user = DB::table('users as us')

            ->join('native_address as na', 'us.id', '=', 'na.user_id')
            ->join('overseas_address as oa', 'us.id', '=', 'oa.user_id')
            ->select('us.*', 'oa.*','na.user_id', 'na.state as native_state','na.district','na.pincode','na.kerala_address','na.phone as native_phone')
            ->where('us.pre_norka_id', '=', $norkaid)
            ->get();
       // dd($user);

        if ($user->isEmpty()) {
            $data['message'] = "User not found";
            $data['status'] =0;
            return response($data, 400);
        }
        else
        {
            $id = $user[0]->user_id;
            $familydetails = DB::table('users as us')
                ->join('family_member_details as fmd', 'us.id', '=', 'fmd.relative_of')
                ->join('relation as rel', 'fmd.relation', '=', 'rel.id')
                ->select('fmd.*','fmd.relation','rel.relation as relation_name')
                ->where('us.id', '=', $id)
                ->get();

            $data['user']= $user[0];
            $data['familydetails']= $familydetails;
            $data['status'] = 1;
//        $data['user'] = $user;
            $data['message'] = "User details fetched successfully";
            return response($data, 200);
        }
    }

}
