<?php 

session_start();
// ============
include("../../asset/theme/css/myfunc.php");
include("../../php_crud/my_class.php");
$obj=new my_class();

extract($_REQUEST);

$branch = $_SESSION["LOGIN_BRANCHES"];
$today = date('Y-m-d');

	function send_sms($obj,$branch,$module_id,$send_mobile,$message){
		if($send_mobile && $send_mobile !='' ){
			$priority = 'N';
			$entry2=$obj->Sms_Send($_SESSION["LOGIN_INSTITUTE_ID"],$module_id,'Detention',$send_mobile,$message, $priority, $_SESSION['LOGIN_USERID'],  date("Y:m:d h:i:s") );
	
	
		$smsstatus = true; 
			
			// return $_SESSION["LOGIN_INSTITUTE_ID"];
	   }
	
	   
	   
	}
	
	function send_sms_by_guardian($obj,$branch,$module_id,$full_name, $guardian,$gender,$mobile,$message,$class_name,$section_roll){
		
		if($gender=='Male'){
			$gender = 'son';
		}else if($gender=='Female'){
			$gender = 'daughter';
		}else{
			$gender= 'child' ;
		}
		
		if($guardian=='F'){
			$send_mobile = $mobile['father_phone'];
			$guardian = 'Parents';	
		}
		if($guardian=='M'){
			$send_mobile = $mobile['mother_phone'];	
			$guardian = 'Parents';
		}
		if($guardian=='O'){
			$send_mobile = $mobile['guardian_phone'];	
			$guardian = 'Guardian';
		}
		
		$message  = "Dear Guardian, Your child ".$full_name.", Class : ".$class_name.", Class Roll : ".$section_roll." got detention. Detention Details : ".$message." . Thank you! ";
		
		
		 return send_sms($obj,$branch,$module_id,$send_mobile,$message);
		 
	}

	$section = str_replace('\\','',$p);
	$data= json_decode($section);
	$error = 0;
	$row = 0;
	if(!empty($data)){
		$module_id = $data->module_id ;
		$AllSection = isset($data->AllSection)?$data->AllSection:'';
		$i = 0;
		
		foreach($AllSection as $product)
		{
			//print_r($product); 
			if(!empty($product)){
			$section_id =  $product->section_id;
			
			$table_name =  "_detention_daily_status_class ";
			$COLUMN = '_approve_satus';
			$where_cond = '	_section_id='.$section_id.'  &&  DATE(_entry_date) ="'.$today.'"'  ;
			$SEND_STATUS=$obj->View_colmn_By_Cond($table_name,$COLUMN,$where_cond);
			
		//	 print_r($SEND_STATUS);
			if( $SEND_STATUS[0]['_approve_satus']==0){
				foreach($obj->View_Student_List_with_topics_detention('sc._id="'.$section_id.'" && pf_std._status!="D" && hmc._detention_message_status = 1 AND DATE(hmc._entry_date) ="'.$today.'" ORDER BY _section_roll ASC') as $value){
					
					/*echo '<pre>';
						print_r($value);		
					echo '</pre>';*/
					
					$mobile = array();
						$mobile['guardian_phone'] = $value['guardian_phone'];
						$mobile['mother_phone'] = $value['mother_phone'];
						$mobile['father_phone'] = $value['father_phone'];	
						$message = $value['_detention_topics'] ;
					send_sms_by_guardian($obj, $branch,$module_id,$value['_full_name'], $value['_current_guardian'],$value['_gender'], $mobile,$message,$value['_class_name'],$value['_section_roll']);			
				
				}
			}
			
			$table_name=" _detention_daily_status_class";
			$column="_approve_satus = 1";
			$where_cond='_detention_status = 1 AND  DATE(_entry_date) = "'.$today.'" AND _section_id = '.$section_id.' ';
			$row=$obj->Update_Data2($table_name,$column,$where_cond);		
			}
		} 
	}
	//echo $i;
	$MSG = array('msg' => 'Added Successfuly');
	
	echo json_encode($MSG);	