<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Http\Request;

class OrgController extends Controller
{
    public function index(Request $request)
	{
		$filter = $users = $organizations = $list = [];
		
		if(isset($request->btn_merge))
		{
			if(isset($request->rep_id) && isset($request->org_ids) && count($request->org_ids) > 0)
			{
				DB::table('merge_organization')->where('org_id',$request->rep_id)->delete();

				$org_ids = array_diff($request->org_ids,[$request->rep_id]);
				
				$insert['org_id'] = $request->rep_id;
				$insert['merge_org_ids'] = implode(',',$org_ids);
				DB::table('merge_organization')->insert($insert);
			}
		}
		
		if(isset($request->country))
		{
			$country = explode(',',$request->country);
			$filter['country'] = $country[1];
			$filter['user_id'] = $request->user_id;
			$filter['org'] = $request->organization;
			
			$users = DB::table('users')->where('country',$country[0])->where('access_organization_id','<>',null)->get();
			$organizations = DB::table('user_access_organizations as uao')->join('users as u','u.access_organization_id','uao.access_organization_id')->where('u.country',$country[0])->get();
			
			$list = DB::table('user_access_organizations as uao')->select('u.display_name','u.email','c.name as country_name','uao.access_organization_id','uao.organization_name');
			$list = $list->join('users as u','u.access_organization_id','uao.access_organization_id');
			$list = $list->join('country as c','c.iso','u.country');
			$list = $list->leftjoin('merge_organization as mo','mo.org_id','uao.access_organization_id')->addSelect('merge_org_ids');
			
			$list = $list->where('u.country',$country[0]);
			$list = $list->where('uao.access_organization_id','<>',null);
			$list = $list->groupBy('uao.access_organization_id');
			$list = $list->orderBy('uao.organization_name');
			
			if(isset($request->organization))
			{
				$list = $list->whereRaw('uao.organization_name like "%'.$request->organization.'%" ');
				// $list = $list->where('uao.access_organization_id',$request->organization);
			}
			
			$list = $list->get();	
		}
		
		$countries = DB::table('country')->get();
		return view('organization_merge',compact(['countries','users','organizations','filter','list']));
	}
	
	function replace_user_org()
	{
		$merge_org = DB::table('merge_organization as m')->select('*');
		$merge_org = $merge_org->join('user_access_organizations as o','o.access_organization_id','m.org_id')->addSelect('o.organization_name');
		$merge_org = $merge_org->get();
		
		if(count($merge_org) > 0)
		{
			foreach($merge_org as $org)
			{
				DB::table('users')->whereIn('access_organization_id',explode(',',$org->merge_org_ids))->update(['access_organization_id'=>$org->org_id]);
				DB::table('user_access_info')->whereIn('access_org_id',explode(',',$org->merge_org_ids))->update(['access_org_id'=>$org->org_id]);
				DB::table('users')->where('access_organization_id',$org->org_id)->update(['org_name'=>$org->organization_name]);
			}
		}
		
		return 'Successfully Done';
	}
}
