<?php
class drtelegram extends CI_Controller {

	public function index(){
		$this->load->model("telegram_model");
		$this->load->model("general_model");
		$this->load->model("nobat_model");
		$this->load->model("articles_model");
		$params   = $this->uri->segment(2);
		$update = json_decode(file_get_contents('php://input'));


if(isset($update->message->chat->username)){
$username = $update->message->chat->username;
}else{
$username = "مشخص نیست";
}
$token = $this->telegram_model->get_token($params);
$uid = $this->telegram_model->get_groupuid_byurl($params);
$callback = $update->callback_query->from->id;

if($callback){
$id = $callback;
$message = $update->callback_query->data;
}else{
$id = $update->message->chat->id;
$message=$update->message->text;
}

if($message == "/start" || $message == "بازگشت" || $message == "انصراف"){
	if($message == "انصراف"){
		$this->telegram_model->clear_telegram_data($uid,$id);
	}
$keyboard = array("keyboard" => array(array("گالری تصاویر"),array("نوبت دهی آنلاین"),array("مقالات"),array("تماس با ما"),array("مشاوره ی پزشکی")),"resize_keyboard"=>true);
$text = "لطفا انتخاب کنید";
$this->telegram_model->send_message($token,$id,$text,$keyboard);
}
//for galery tasavir
else if($message == "گالری تصاویر"){
	$images = directory_map('./assets/data_images/drs/dr_galery/'.$uid);
		foreach($images as $value){
		$this->telegram_model->send_photo($token,$id,'./assets/data_images/drs/dr_galery/'.$uid."/".$value);
		}
}//nobat dehi
else if($message == "مشاوره ی پزشکی"){
	$text = "لطفا نام و نام خانوادگی خود را وارد کنید";
		$keyboard = array("keyboard" => array(array("انصراف")),"resize_keyboard"=>true);
			$this->telegram_model->send_message($token,$id,$text,$keyboard);	
		$data = array("uid"=>$uid,"type"=>6,"parentt"=>"null","telegramid"=>$id,"data"=>"null");
			$this->telegram_model->set_telegram_data($data);
}
else if($message == "نوبت دهی آنلاین"){
	$text = "لطفا ماه خود را انتخاب کنید.";
	$array_mon = array("فروردین","اردیبهشت","خرداد","تیر","مرداد","شهریور","مهر","آبان","آذر","دی","بهمن","اسفند");
	$arraydata = array(array(),array(),array(),array());
	$number = $this->persiandate->jdate('m',"","",'Asia/Tehran',"en");
	$number = (int)$number-1;
	$arraynumber = 0;
	$jw = 1;
	for($i=$number;$i<=11;$i++){
		array_push($arraydata[$arraynumber],array(text=>$array_mon[$i],callback_data=>"mon_".$i));
		if($jw % 3 == 0){
	$arraynumber++;
	}
	$jw++;
}
	
	
	$keyboard =  array("inline_keyboard" => $arraydata);
	$this->telegram_model->send_message($token,$id,$text,$keyboard);
}//roz
else if(preg_match('/mon_*/',$message)){
	$general_setting = $this->general_model->get_setting($uid);
	$daywork = $general_setting[0]->day_work;
	$daywork = explode(",",$daywork);
	$select_mon = explode("_", $message);
	$select_mon = $select_mon[1];
	$year = $this->persiandate->jdate('Y',"","",'Asia/Tehran',"en");
	$mon = $this->persiandate->jdate('m',"","",'Asia/Tehran',"en");
	$mon = (int)$mon-1;
	$checkstring = "mon_".$mon;
	$text = "لطفا روز خود را انتخاب کنید";
	//$this->telegram_model->send_message_only($token,$id,"dsds342erwaads");
	 if($checkstring == $message){
		$sday =  (int)$this->persiandate->jdate('d',"","",'Asia/Tehran',"en");
	}else{
		$sday =  1;
	 }
	 if($select_mon>5){
		$eday = 30;
	 }else{
		$eday = 31;
	 }
	 if($select_mon == 11){
		$eday = 29;
	 }
	$array1 = array(array(),array(),array(),array(),array(),array(),array());
	$arraynumber = 0;

	//$this->telegram_model->send_message_only($token,$id,$array1[0][0]["text"]);
	for($i=$sday;$i<=$eday;$i++){
		$isholiday = true;
	$gregorian =  $this->persiandate->jalali_to_gregorian( $year , $select_mon+1 , $i);
	$timestamp = strtotime($gregorian[1]."/".$gregorian[2]."/".$gregorian[0]);
	if($gregorian[1] <10){
	$gregorian[1] = "0".$gregorian[1];	
	}
	if($gregorian[2]<10){
	$gregorian[2] = "0".$gregorian[2];	
	}
	$whatday = date("w",$timestamp);
	//$this->telegram_model->send_message_only($token,$id,$year."/".($select_mon+1)."/".$i);
	//$this->telegram_model->send_message_only($token,$id,$gregorian[1]."/".$gregorian[2]."/".$gregorian[0]);
	//$this->telegram_model->send_message_only($token,$id,$whatday);
	foreach($daywork as $value){
	if($value == $whatday){
		$isholiday = false;
		break;
	}
	}
	if(!$isholiday){
		array_push($array1[$arraynumber],array(text=>"".$i,callback_data=>"day_".$i."_".$gregorian[0]."-".$gregorian[1]."-".$gregorian[2]));
	}
	
		if($i % 6 == 0){
		$arraynumber++;
	}
	}
	$keyboard =  array("inline_keyboard" => $array1);
	$this->telegram_model->send_message($token,$id,$text,$keyboard);
	}
else if(preg_match('/day_*/',$message)){
	$general_setting = $this->general_model->get_setting($uid);
	$text = "لطفا ساعت را انتخاب کنید.";
	$array1 = array(array(),array(),array(),array(),array(),array(),array());
	$arraynumber = 0;
	$date = explode("_", $message);
	$date = $date[2];
	$start_time = $general_setting[0]->time_work_s;
	$end_time =  $general_setting[0]->time_work_e;
	$start_time = explode(":",$start_time);
	$end_time = explode(":",$end_time);
	$start_time_ho = $start_time[0];
	$start_time_min = (int)$start_time[1];
	
	$end_time_ho = $end_time[0];
	$end_time_min = (int)$end_time[1];
	$endtimeco = ($end_time_ho*100)+ $end_time_min;
	$timeho = $start_time_ho;
	$timemin =  $start_time_min;
	$timeco = ($timeho*100)+$timemin;
	$step = $general_setting[0]->time_visit_avg;
	$i = 1;
	while($timeco <= $endtimeco ){
		if($timemin < 10){
			$timemin = "0".$timemin;
		}
		$comparedate = $date."T".$timeho.":".$timemin.":00";
		//$this->telegram_model->send_message_only($token,$id,$date);
		if(!$this->nobat_model->is_nobat_exist($uid,$comparedate)){
		array_push($array1[$arraynumber],array(text=>$timeho.":".$timemin,callback_data=>"hour_".$date."T".$timeho.":".$timemin.":"."00"));
		}
		$timemin = (int)$timemin;
		$timemin = $timemin + $step;
		if($timemin >= 60){
			$numofhour = floor($timemin / 60);
			$timeho = $timeho + $numofhour;
			$timemin = $timemin - ($numofhour*60);
		}
		if($i % 6 == 0){
		$arraynumber++;
	}
	$i++;
	$timeco = ($timeho*100)+$timemin;
	}
	
	//$this->telegram_model->send_message_only($token,$id,$array1[0][0]["text"]);
	
	$keyboard =  array("inline_keyboard" => $array1);
	$this->telegram_model->send_message($token,$id,$text,$keyboard);
	}
	else if(preg_match('/hour_*/',$message)){
		$date = explode("_",$message);
		$date = $date[1];
		$data = array("uid"=>$uid,"type"=>5,"parentt"=>"null","telegramid"=>$id,"data"=>$date);
			$this->telegram_model->set_telegram_data($data);
			$this->telegram_model->send_message_only($token,$id,"لطفا نام و نام خانوادگی خود را وارد کنید.");
		
	}else if($message == "مقالات"){
		$articles = $this->articles_model->get_articles($uid);
		$array = array(array(),array(),array(),array(),array(),array(),array(),array(),array(),array(),array("بازگشت"));
		if(count($articles) == 0){
			
			$this->telegram_model->send_message_only($token,$id,"هیچ مقاله ای وجود ندارد");
		}else{
			$text = "لطفا انتخاب کنید";
			$count = 1;
			$k = 0;
			foreach($articles as $value){
			array_push($array[$k],$value->title);
			$count++;
			if($count == 3){
				$k++;
			}
			}
		$keyboard = array("keyboard" =>$array,"resize_keyboard"=>true);
		$this->telegram_model->send_message($token,$id,$text,$keyboard);
		}
		
		}else if($message == "تماس با ما"){
		$this->load->model("contact_model");	
		$result = $this->contact_model->get_contact($uid);
		 $text =  "شماره ی تماس:".$result[0]->phone;
		 $this->telegram_model->send_message_only($token,$id,$text);
		 $text = "آدرس:".$result[0]->address;
		 $this->telegram_model->send_message_only($token,$id,$text);
		 $text = $result[0]->discription;
		 $this->telegram_model->send_message_only($token,$id,$text);
		}else{
		$count1 = count($this->telegram_model->get_telegram_data($uid,$id,5));
		$count2 = count($this->telegram_model->get_telegram_data($uid,$id,6));
		$count3 = count($this->telegram_model->get_telegram_data($uid,$id,7));
		$get_maghaleh = $this->articles_model->get_article_title($uid,$message);
		if($count1 >0 && $count2 == 0 && $count3 == 0){
			//$this->telegram_model->send_message_only($token,$id,"تا اینجا که حلاله");
			$time_iso = $this->telegram_model->get_telegram_data($uid,$id,5);
			$time_iso = $time_iso[0]->data;
			$fullname = $message;
			$time =  explode("T",$time_iso);
			$time = $time[1];
			$time = rtrim($time,":00");
			$date = explode("T",$time_iso);
			$date = $date[1];
			$date = explode("-",$date);
			  $persiandate = $this->persiandate->gregorian_to_jalali( $date[0] , $date[1] , $date[2]);
			$dateformat = $persiandate[0].$persiandate[1].$persiandate[2];
			$data = array("uid"=>$uid,"po_name"=>$fullname,"po_date_ISO8601"=>$time_iso,"po_date"=>$dateformat,"po_status"=>1,"po_time"=>$time);
			$last_id = $this->nobat_model->get_nobat_lastid();
			$last_id = $last_id +1;
			$this->nobat_model->set_nobat($data);
			$data2 = array("nobatid" => $last_id , "telegramid"=>$id);
			$this->nobat_model->telegram_infonobatdehi_add($data2);
			$this->telegram_model->clear_telegram_data($uid,$id);
			$this->telegram_model->send_message_only($token,$id,"نوبت شما با موفقیت ثبت شد با در ربات تلگرام تماس میگیرند");
		}else if(count($get_maghaleh) > 0){
			$this->telegram_model->send_message_only($token,$id,$get_maghaleh[0]->content);
			
			}else if($count2 > 0 && $count3 == 0){
				$text = "لطفا مورد پزشکی خود را بفرستید";
				$data = array("uid"=>$uid,"type"=>7,"parentt"=>"null","telegramid"=>$id,"data"=>$message);
			$this->telegram_model->set_telegram_data($data);
				$keyboard = array("keyboard" => array(array("انصراف")),"resize_keyboard"=>true);
			$this->telegram_model->send_message($token,$id,$text,$keyboard);	
				
				}else if($count2 > 0 && $count3 > 0){
					$text = "پیام شما با موفقیت ثبت شد.";
				$fullname = $this->telegram_model->get_telegram_data($uid,$id,7);
				$fullname = $fullname[0]->data;
			$data = array("uid"=>$uid,"tellid"=>$id,"fullname"=>$fullname,"phone"=>"null","message"=>$message);
			$this->telegram_model->set_telegram_message($data);
			$keyboard = array("keyboard" => array(array("انصراف")),"resize_keyboard"=>true);
			$this->telegram_model->send_message($token,$id,$text,$keyboard);	
					}else{	
			$this->telegram_model->send_message_only($token,$id,"ورودی معتبر نمیباشد");
		}
	}
}}
?>