<?php
class ModelExtensionModuleHotels24 extends Model {
	public function post($data) {
		$this->load->model('setting/setting');
		$this->load->model('tool/sinc');
		$login             = $this->config->get('hotel24_login');
		$pass              = $this->config->get('hotel24_pass');
		$login_url         = 'https://partner.hotels24.ua/auth/hotel?lastUri=/hotel/5029/room-management/'; 
		$set_url           = '';		
		$login_var_name    = 'email'; 
		$password_var_name = 'password';
		$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; (R1 1.5))"; 
		$user_cookie_file  = DIR_CACHE . '/cookie_ext_h.txt'; 		
		$request = [];

		if (!$ch = curl_init()) 
		{ 
			echo curl_error($ch); 
			exit; 
		}   

		curl_setopt($ch, CURLOPT_URL, $login_url);    
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($ch, CURLOPT_VERBOSE, 1); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
		curl_setopt($ch, CURLOPT_REFERER, $login_url); 
		curl_setopt($ch, CURLOPT_COOKIEJAR,  $user_cookie_file);  
		curl_setopt($ch, CURLINFO_HEADER_OUT, true);
		$content = curl_exec($ch); 
		

		preg_match('<input type="hidden" name="(\w*)" value="(\w*)"/>', $content, $c);
		$stranniy_var = $c[1];
		$stranniy_value = $c[2];
		
		curl_close($ch); 

		if (!$ch = curl_init()) 
		{ 
			echo curl_error($ch); 
			exit; 
		}   
		$post_fields = "$stranniy_var=$stranniy_value&lastUri=&$login_var_name=$login&$password_var_name=$pass"; 
		$login_url = 'https://partner.hotels24.ua/auth/hotel';
		
		curl_setopt($ch, CURLOPT_URL, $login_url);    
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($ch, CURLOPT_VERBOSE, 1); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
		curl_setopt($ch, CURLOPT_REFERER, $login_url); 
		curl_setopt($ch, CURLOPT_POST, 1); 
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 
		curl_setopt($ch, CURLOPT_COOKIEJAR,  $user_cookie_file);  
		curl_setopt($ch, CURLOPT_COOKIEFILE, $user_cookie_file); 
		curl_setopt($ch, CURLINFO_HEADER_OUT, true);
		curl_exec($ch); 
		curl_close($ch); 
		$log = new Log('sync_to_hotels24.log');
		$message = '--------------HOTELS24-------------------';
		$log->write($message);

		
		foreach ($data['apartamets'] as $rooms) {
			if ($rooms['send2_ok']==0){
				if (!$ch = curl_init()) 
				{ 
					echo curl_error($ch);
					exit; 
				}   
				$post_fields = "cdd-l-dateStart=" . $rooms['date'] . "&cdd-l-dateEnd=" . $rooms['date'] . "&cdd-l-pr=" . $rooms['price'] . "&cdd-l-pla=" . $rooms['room_to_sell'] . "&period_days=1&cc-l-booking-type_old=0&hotel_online=0&cdd-l-com=15&cdd-l-min_nights=1&cdd-l-unavailable=0&cdd-l-sell-half-price=0&cc-l-booking-type=0&dates[0]=" . $rooms['date'] . "&event=changeCalendarData&useSpinner=true&spinnerTarget=%5Bobject%20HTMLDivElement%5D&format=json&year=2016&month=01&room_id=" . $rooms['room_id']['hotel24'] . "&tariff_id=" . $rooms['tariff']; 
				$set_url = 'https://partner.hotels24.ua/hotel/5029/room-management/calendar/' . $rooms['room_id']['hotel24'];
				$message = 'room_id = ' . $rooms['room_id']['hotel24'] . " dateStart=" . $rooms['date'] . " dateEnd=" . $rooms['date'] . " price=" . $rooms['price'] . " place=" . $rooms['room_to_sell'] . " dates[0]=" . $rooms['date'];
				$log->write($message);

				curl_setopt($ch, CURLOPT_URL, $set_url);    
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
				curl_setopt($ch, CURLOPT_HEADER, true);
				curl_setopt($ch, CURLOPT_VERBOSE, 2); 
				curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
				curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
				curl_setopt($ch, CURLOPT_REFERER, $set_url); 
				curl_setopt($ch, CURLOPT_COOKIEFILE, $user_cookie_file); 
				curl_setopt($ch, CURLOPT_HEADER, true);
				curl_setopt($ch, CURLINFO_HEADER_OUT, true);
				curl_setopt($ch, CURLOPT_POST, 1); 
				curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest", "Content-Type: application/x-www-form-urlencoded; charset=UTF-8"));
				curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 

				curl_exec($ch); 

				$request[] = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
				curl_close($ch); 

				$this->model_tool_sinc->send_ok($rooms['product_id'], $rooms['date'], 2);
				
				echo '.';

			}
		}
		
		
		return $request;
	}
	
	public function login() {
		$this->load->model('setting/setting');
		$this->load->model('tool/sinc');
		$login             = $this->config->get('hotel24_login');
		$pass              = $this->config->get('hotel24_pass');
		$login_url         = 'https://partner.hotels24.ua/auth/hotel?lastUri=/hotel/5029/room-management/'; 
		$set_url           = '';		
		$login_var_name    = 'email'; 
		$password_var_name = 'password';
		$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; (R1 1.5))"; 
		$user_cookie_file  = DIR_CACHE . '/cookie_ext_h.txt'; 		
		$request = [];

		if (!$ch = curl_init()) 
		{ 
			echo curl_error($ch); 
			exit; 
		}   

		curl_setopt($ch, CURLOPT_URL, $login_url);    
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($ch, CURLOPT_VERBOSE, 1); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
		curl_setopt($ch, CURLOPT_REFERER, $login_url); 
		curl_setopt($ch, CURLOPT_COOKIEJAR,  $user_cookie_file);  
		curl_setopt($ch, CURLINFO_HEADER_OUT, true);
		$content = curl_exec($ch); 
		

		preg_match('<input type="hidden" name="(\w*)" value="(\w*)"/>', $content, $c);
		$stranniy_var = $c[1];
		$stranniy_value = $c[2];
		
		curl_close($ch); 

		if (!$ch = curl_init()) 
		{ 
			echo curl_error($ch); 
			exit; 
		}   
		$post_fields = "$stranniy_var=$stranniy_value&lastUri=&$login_var_name=$login&$password_var_name=$pass"; 
		$login_url = 'https://partner.hotels24.ua/auth/hotel';
		
		curl_setopt($ch, CURLOPT_URL, $login_url);    
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($ch, CURLOPT_VERBOSE, 1); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
		curl_setopt($ch, CURLOPT_REFERER, $login_url); 
		curl_setopt($ch, CURLOPT_POST, 1); 
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 
		curl_setopt($ch, CURLOPT_COOKIEJAR,  $user_cookie_file);  
		curl_setopt($ch, CURLOPT_COOKIEFILE, $user_cookie_file); 
		curl_setopt($ch, CURLINFO_HEADER_OUT, true);
		curl_exec($ch); 
		
		curl_close($ch); 

		if (!$ch = curl_init()) 
		{ 
			echo curl_error($ch);
			exit; 
		}   
	
		$set_url = 'https://partner.hotels24.ua/hotel/5029/booking-list/search?page=1&perpage=100&sortby=DESC&filter=&urlParams[cdd-l-dateStart]=&urlParams[cdd-l-dateEnd]=&urlParams[search_by_id_and_name]=&urlParams[status_booking]=&urlParams[booking_type]=&urlParams[prepay]=';

		curl_setopt($ch, CURLOPT_URL, $set_url);    
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_VERBOSE, 2); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
		curl_setopt($ch, CURLOPT_REFERER, $set_url); 
		curl_setopt($ch, CURLOPT_COOKIEFILE, $user_cookie_file); 
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLINFO_HEADER_OUT, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest", "Content-Type: application/json; charset=UTF-8", "Accept-Encoding:gzip, deflate, sdch, br", "Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,uk;q=0.2"));

		$a = (curl_exec($ch)); 
		$a = json_decode(substr($a, strpos($a, 'includeSubDomains') + 18));
	
		curl_close($ch); 
		
		return $a->data;
	}
}