<?php
include '../config/database.php';
function _Query(){
	$conn = _connectDB();
	$sql = "SELECT groupline_name,groupline_api_key,groupline_keycode,groupline_alldate,groupline_countdown,groupline_encode,mode_kclub,mode_arawan,mode_newyeekee FROM groupline WHERE groupline_api_key!=''";
	$result = $conn->query($sql);
	$res = [];
	if ($result->num_rows > 0) {
	    while($row = $result->fetch_assoc()) {
				$count = 0;
				if($row['mode_kclub'] == 'ON'){ $count+=1; }
				if($row['mode_arawan'] == 'ON'){ $count+=1; }
				if($row['mode_newyeekee'] == 'ON'){ $count+=1; }
				$mid = $row['groupline_api_key'];
				if($count != 0){
					$a = $row['groupline_countdown']; // วันคงเหลือ
					$b = $count;// วันที่จะต้องหัก
					$c = $a-$b;
					if($c < 0){ // จะต้องน้้อยกว่า 0 วัน
						$c = 0;
					}
					$sql_update = "UPDATE groupline SET groupline_countdown=".$c." WHERE groupline_api_key='".$mid."'";
					$conn->query($sql_update);
					array_push($res,$row);
				}
			}
	}
	return $res;
}
echo json_encode(_Query());
