<?php
/**
 * Created by PhpStorm.
 * User: varun_2
 * Date: 30-12-2015
 * Time: 17:03
 */

namespace App\Http\Controllers\Action;
use DB;

class Common
{
    public function getUserPortfolio($user_id, $type)
	{
		$temp = DB::table('tbl_user_portfolio')
            ->select('type','category', 'country', 'lead_type','row_id','prices_col')
			->where('user_id', '=' , $user_id)
			->where('type', '=' , $type)
            ->get();
		/* return $temp ;  */
		$res=array();
		if($type!='prices'){	
			foreach( $temp as $tm)
			{
				$res['category'] = trim($tm['category'],',');
				$res['country'] = str_replace(',',';',trim($tm['country'],','));
				$res['lead_type'] = trim($tm['lead_type'],',');
			}
		}		
		
		if($type=='prices'){	
			foreach( $temp as $tm)
			{
				if(!in_array($tm['row_id'], $res[strtolower($tm['prices_col'])]) && $tm['row_id']!='')
					$res[strtolower($tm['prices_col'])][] = $tm['row_id']; 
			}
		}
		return $res ;
	}
	
    public function getCountryState($country_id)
    {
    	if($country_id){
       		$country_list = DB::connection('mysql')
            ->select(DB::raw('SELECT  `city_id`, `city_name`, state_id, state_name, country_id,  country FROM tbl_state_city state INNER JOIN tbl_country country ON state.`country_id` = country.id Where country_id ="'.$country_id.'"')) ;
		}else{
			  $country_list = DB::connection('mysql')
            ->select(DB::raw('SELECT  `city_id`, `city_name`, state_id, state_name, country_id,  country FROM tbl_state_city state INNER JOIN tbl_country country ON state.`country_id` = country.id ORDER BY `state`.`country_id` ASC')) ;
		
		}	
        $list = array();
        foreach($country_list as $country )
        {
            if(empty( $list[$country['country_id']] ))
                $list[$country['country_id']] = array("name" => $country["country"] , "id" => strval($country['country_id']) ) ;
            if(empty($list[$country['country_id']]['state'][$country["state_id"]]))
                $list[$country['country_id']]['state'][$country["state_id"]] = array( 'name' => $country["state_name"], 'id' => strval($country["state_id"])) ;

            $list[$country['country_id']]['state'][$country["state_id"]]['city'][] = array( 'name' => $country["city_name"], 'id' => strval( $country["city_id"]) ) ;

        }

//        $list = array_values ($list) ;
//        for($i = 0 ; $i< count($list) ; $i++ )
//            $list[$i]["state"] = array_values ($list[$i]["state"]) ;



        return $list ;
    }
	
	public function getNewCategories($filter_type='')
    {
		if($filter_type=='news_arr'){
			return $finalCategoryArr = array( 
										"Iron Ore"=> "Raw Material",
										"Coal"=>"Raw Material",										 
										"Ferrous Scrap"=>"Raw Material",										 
										"Ship Breaking"=>"Raw Material",										 
										"Semi Finished"=>"Semi Finished",									 
										"Finish Long"=>"Finish Long",
										"Finish Flat"=>"Finish Flat",
										"Ferro Alloy"=>"Ferro Alloy"
										/* "Iron Ore"=> "13,14,15",
										"Coal"=>"16",										 
										"Ferrous Scrap"=>"17",										 
										"Ship Breaking"=>"114",										 
										"Semi Finish"=>"18,19,20,21",									 
										"Finish Long"=>"4,25,26,2427",
										"Finish Flat"=>"24",
										"Ferro Alloy"=>"22,23,3659,3660,3661,3662" */
									);
		}elseif($filter_type=='tenders'){
		
			$finalCategoryArr = array( 
										array("Iron Ore"=> "Raw Material"),
										array("Coal"=>"Raw Material"),										 
										array("Ferrous Scrap"=>"Raw Material"),										 
										array("Ship Breaking"=>"Raw Material"),										 
										array("Semi Finished"=>"Semi Finished"),									 
										array("Finish Long"=>"Finish Long"),
										array("Finish Flat"=>"Finish Flat"),
										array("Ferro Alloy"=>"Ferro Alloy")
									);
			
			
			return array('filter' => "category", "sub_filter" => $finalCategoryArr);
			//return $finalCategoryArr;
		}elseif($filter_type=='news'){
		
			$finalCategoryArr = array( 
										array("Iron Ore"=> "13,14,15"),
										array("Coal"=>"16"),										 
										array("Ferrous Scrap"=>"17"),										 
										array("Ship Breaking"=>"114"),										 
										array("Semi Finish"=>"18,19,20,21"),									 
										array("Finish Long"=>"4,25,26,2427"),
										array("Finish Flat"=>"24"),
										array("Ferro Alloy"=>"22,23,3659,3660,3661,3662")
									);
			
			
			//return array('filter' => "category", "sub_filter" => $finalCategoryArr);
			return $finalCategoryArr;
		}else{
		
			return array(
				"categories" => array(
					array(
						"cat_id"	=>	"13,14,15",
						"cat_name"	=>	"Iron Ore",
						"sub_cat"	=>	array(
							array(
								"cat_id"	=>	"13,14",
								"cat_name"	=>	"Fines/Lumps"
							),
							array(
								"cat_id"	=>	"15",
								"cat_name"	=>	"Pellets"
							)
						)
					),
					array(
						"cat_id"	=>	"16",
						"cat_name"	=>	"Coal",
						"sub_cat"	=>	array(
							array(
								"cat_id"	=>	"16",
								"cat_name"	=>	"Non Coking"
							),
							array(
								"cat_id"	=>	"16",
								"cat_name"	=>	"Coking"
							),
							array(
								"cat_id"	=>	"16",
								"cat_name"	=>	"Met Coke"
							),
							array(
								"cat_id"	=>	"16",
								"cat_name"	=>	"Anthracite"
							)
						)
					),
					array(
						"cat_id"	=>	"17",
						"cat_name"	=>	"Ferrous Scrap",
						"sub_cat"	=>	array(
							array(
								"cat_id"	=>	"17",
								"cat_name"	=>	"Melting Scrap"
							),
							array(
								"cat_id"	=>	"17",
								"cat_name"	=>	"Mill Scale"
							)
						)
					),
					array(
						"cat_id"	=>	"114",
						"cat_name"	=>	"Ship Breaking",
						"sub_cat"	=>	array()
					),
					array(
						"cat_id"	=>	"18,19,20,21",
						"cat_name"	=>	"Semi Finish",
						"sub_cat"	=>	array(
							array(
								"cat_id"	=>	"21",
								"cat_name"	=>	"Sponge Iron"
							),
							array(
								"cat_id"	=>	"20",
								"cat_name"	=>	"Pig Iron"
							),
							array(
								"cat_id"	=>	"18,19",
								"cat_name"	=>	"Billet/Ingot"
							)
						)
					),
					array(
						"cat_id"	=>	"4,25,26,2427",
						"cat_name"	=>	"Finish Long",
						"sub_cat"	=>	array(
							array(
								"cat_id"	=>	"25",
								"cat_name"	=>	"Re bar"
							),
							array(
								"cat_id"	=>	"26",
								"cat_name"	=>	"Wire rod"
							),
							array(
								"cat_id"	=>	"4",
								"cat_name"	=>	"Structures"
							),
							array(
								"cat_id"	=>	"2427",
								"cat_name"	=>	"Pipes"
							)
						)
					),
					array(
						"cat_id"	=>	"793,794,3663",
						"cat_name"	=>	"Finish Flat",
						"sub_cat"	=>	array(
							array(
								"cat_id"	=>	"793,794",
								"cat_name"	=>	"HRC/CRC"
							),
							array(
								"cat_id"	=>	"3663",
								"cat_name"	=>	"Plates"
							),
							array(
								"cat_id"	=>	"3663",
								"cat_name"	=>	"GP"
							)
						)
					),
					array(
						"cat_id"	=>	"22,23,3659,3660,3661,3662",
						"cat_name"	=>	"Ferro Alloy",
						"sub_cat"	=>	array(
							array(
								"cat_id"	=>	"3659",
								"cat_name"	=>	"Silico Manganese"
							),
							array(
								"cat_id"	=>	"3660",
								"cat_name"	=>	"Ferro Manganese"
							),
							array(
								"cat_id"	=>	"3662",
								"cat_name"	=>	"Ferro Chrome"
							),
							array(
								"cat_id"	=>	"3661",
								"cat_name"	=>	"Ferro Silicon"
							),
							array(
								"cat_id"	=>	"23",
								"cat_name"	=>	"Manganese Ore"
							),
							array(
								"cat_id"	=>	"22",
								"cat_name"	=>	"Chrome Ore"
							)
						)
					)
				)
			);
		}
    	
    }
	
	public function getRegions($filter_type='')
	{
		
		if($filter_type=='news' || $filter_type=='tenders'){
			
			$finalCountryArr = array( 
										
										array("Asia & Asia Pacific"=> "India,China,Japan,Korea,Pakistan, Bangladesh, Nepal,Australia",
											"sub_filter" =>array(
													'India'=>'India',
													'China'=>'China',
													'Japan,Korea'=>'Japan,Korea',
													'Pakistan, Bangladesh, Nepal'=>'Pakistan, Bangladesh, Nepal',
													'Australia'=>'Australia',
												)
											),
										array("South East Asia"=>"South East Asia"),										 
										array("Middle East Africa"=>"Middle East Africa"),										 
										array("CIS Nations"=>"CIS Nations"),										 
										array("Europe"=>"Europe"),									 
										array("America"=> "US/Canada,Brazil",
											"sub_filter" =>array(
													'US/Canada'=>'US/Canada',
													'Brazil'=>'Brazil'
												)
											),
										array("Global"=>"Global")
									);
			return array('filter' => "country", "sub_filter" => $finalCountryArr);
		}else{
			return $countryArr = array(
				'Asia & Asia Pacific'	=>	array(
					'India'=>'India',
					'China'=>'China',
					'Japan, Korea'=>'Japan, Korea',
					'Pakistan, Bangladesh, Nepal'=>'Pakistan, Bangladesh, Nepal',
					'Australia'=>'Australia'
				),
				'South East Asia'=>'South East Asia',
				'Middle East Africa'=>'Middle East Africa',
				'CIS Nations'=>'CIS Nations',
				'Europe'=>'Europe',
				'America'	=>	array(
					'US/Canada'=>'US/Canada',
					'Brazil'=>'Brazil'
				),
				'Global'=>'Global'
			);
		}
	}
}