<?php

namespace App\Console\Commands;

use GuzzleHttp\Client;
use Illuminate\Console\Command;

class Crawler extends Command {
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'crawl:data';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Command description';

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct() {
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function getDetail($id)
	{
		$client = new Client();
		$jar = new \GuzzleHttp\Cookie\CookieJar();
		$url    = 'http://www.nhatrosinhvien.vn/loadmarker.sam?q='.$id;
		$response = $client->request( 'GET', $url, [
			'cookies'=>$jar,
			'headers' => [
				'Host'             => 'www.nhatrosinhvien.vn',
				'Connection'       => 'keep-alive',
				'Accept'           => 'application/json, text/javascript, */*; q=0.01',
				'X-Requested-With' => 'XMLHttpRequest',
				'User-Agent'       => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
				'Referer'          => 'http://www.nhatrosinhvien.vn/tim-kiem.html?z=11&lat=20.97001590213822&lng=105.8926574252664',
				'Accept-Encoding'  => 'gzip, deflate',
				'Accept-Language'  => 'en,vi-VN;q=0.9,vi;q=0.8,fr-FR;q=0.7,fr;q=0.6,en-US;q=0.5',
				'Cookie'           => 'ga=GA1.2.341313529.1527517890; _gid=GA1.2.74525919.1527517890; _gat=1; __dtsu=2DE7B66B582DB95A7A573B6C02BE3A52; _local=1'

			]
		] );

		$jsonData = $response->getBody()->getContents();

		$item = json_decode($jsonData, true);

		$item = $item['data'];

		return $item;
	}

	public function handle() {
		//
		$client = new Client();
		$jar = new \GuzzleHttp\Cookie\CookieJar();
		$url    = 'http://nhatrosinhvien.vn/marker.sam?q=1322101120303%2C1322101120321%2C1322101120323%2C1322101120312%2C1322101120330%2C1322101120332%2C1322101120313%2C1322101120331%2C1322101120333%2C1322101121202%2C1322101121220%2C1322101121222%2C1322101121203%2C1322101121221%2C1322101121223%2C1322101121212%2C1322101121230%2C1322101121232%2C1322101121213%2C1322101121231%2C1322101121233%2C1322101121302%2C1322101121320%2C1322101121322';
		$response = $client->request( 'GET', $url, [
			'cookies'=>$jar,
			'headers' => [
				'Host'             => 'batdongsan.com.vn',
				'Connection'       => 'keep-alive',
				'Accept'           => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
				'User-Agent'       => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36',
				'Accept-Encoding'  => 'gzip, deflate, br',
				'Accept-Language'  => 'en,vi;q=0.9,vi-VN;q=0.8,fr-FR;q=0.7,fr;q=0.6,en-US;q=0.5',
				'Cookie'           => 'SERVERID=H; __cfduid=dd4eb38764847c21713e193fb76a0c20a1560749931; _ga=GA1.3.1734287481.1560749934; _gid=GA1.3.1585520473.1560749934; scs=%7B%22t%22%3A1%7D; _fbp=fb.2.1560749934306.181658147; sidtb=1AvWDl8O7BA0wsYbi9vkynXUBEaUUFkO; usidtb=Usjdyrip60rUnyWgsA7R3f3q1oXxtim8; USER_SEARCH_PRODUCT_CONTEXT=38%7C324%7CHN%7C718%7C11382%7C0%7C2454%2C20945404; ASP.NET_SessionId=skyn1lkxlfu2y53nthxhhvys; ins-mig-done=1; ins-product-id=pr20945404; current-currency=VND; ins-gaSSId=d6558367-e153-c8b7-b8bd-a85590574a68_1560749935; __asc=136a0eef16b63f208a464d02d08; __auc=136a0eef16b63f208a464d02d08; insdrSV=2; _gat=1; __zlcmid=sqiJmsjR6TSaHi'

			]
		] );

		$jsonData = $response->getBody()->getContents();

		$items = json_decode($jsonData, true);

		$items = $items['data'];


		foreach ($items as $key => $item)
		{
			foreach ( $item as $dt ) {



				if(isset($dt['id'])) {
					$hostel = $this->getDetail( $dt['id'] );
					dd(json_encode($hostel));
					$this->line('Phone: '.$hostel['phone'].'- Address: '.$hostel['address']);
				}

			}
		}
	}
}
