<?php 
if(!defined('IN_DEMOSOSO')) {
	exit('this is wrong page,please back to homepage');
}
$res = array();
 
$act = @htmlentitdm($_GET['act']);
$staffid = @htmlentitdm($_GET['staffid']);


if($act=='') $act='list';


if($act=='list') {	

// $sql = "SELECT * from wu_staff a 
// left join wu_arrange b on a.staffid = b.staff 
// left join wu_squad c on b.squad = c.squadid 
// group by a.staffid 
//  order by a.staffid desc limit 100";
$sql = "SELECT * from wu_staff a order by a.staffid desc limit 100";

   if(getnum($sql)>0){
		 $res['data'] = getall($sql); 
		 $res['code'] =  ['code'=>200];
	 
		 
	 }
	 else {
		$res['code'] =  ['code'=>401];
	 }



}

if($act=='detail') {	

$sql = "SELECT * from wu_arrange a
left join wu_staff b on a.staff  = b.staffid 
left join wu_squad c on a.squad = c.squadid 
 where a.staff = $staffid 
 order by a.date desc limit 100";


   if(getnum($sql)>0){
		 $res['data'] = getall($sql); 
		 $res['code'] =  ['code'=>200];
	 
		 
	 }
	 else {
		$res['code'] =  ['code'=>401];
	 }

}

 if($act=='insert') {	
		 	$postData = file_get_contents('php://input');
			$postData = json_decode($postData, true);
			//pre($postData);

			$staff = $postData['staff']['staffid'];
			$date =  substr($postData['date'],0,10);
			$squad = $postData['squad'];

			$sql = "SELECT * from wu_arrange where staff='$staff' and date='$date' ";
			if(getnum($sql)>0){
				$res['code'] = ['code'=>401];

			}
		   else{
		   	$ss = "insert into wu_arrange  (staff,squad,date) values ('$staff','$squad','$date')"; 
			iquery($ss);

		    $res['data'] = $postData;
		    $res['code'] = ['code'=>200];


		   }	
 
}

 if($act=='update') {	
		 	$postData = file_get_contents('php://input');
			$postData = json_decode($postData, true);
			//pre($postData);

			$staff = $postData['staff']['staffid'];
			$date =  substr($postData['date'],0,10);
			$squad = $postData['squad'];

			 $ss = "update wu_arrange set  squad='$squad'   where staff='$staff' and date='$date' ";
		       //echo $ss;exit;
		      iquery($ss);  

 

		    $res['data'] = $postData;
		    $res['code'] = ['code'=>200];

 
 
}
 
 
	 
	 if($test=='y') pre($res);

	 		// pre($_SERVER['HTTP_ACCEPT']);
			 $jsonv = json_encode($res,JSON_UNESCAPED_UNICODE);
			 //echo $jsonv;
			  echo api_jsonp_encode($jsonv); 

 

		
?>