<?php
date_default_timezone_set("Asia/Bangkok");
define('APP', 'mybot');


function _connectDB(){
	// $host = "public.05z3l.bkk1.database-hosting.webstarterz.com";
	// $username = "05z3l_linebot2";
	// $password = "@Anont0811045107";
	// $database = '05z3l_linebot';
	$host = "public.05z3l.bkk1.database-hosting.webstarterz.com";
	$username = "05z3l_linebot_demo";
	$password = "@Ubonsoft132755";
	$database = '05z3l_linebot_demo';
	$connect = mysqli_connect($host,$username,$password,$database);
	return $connect;
}

// SN 16.01.63 AP
function get_unregister(){
	$con = _connectDB();
	if ($con->connect_error) {
	    die("Connection failed: " . $con->connect_error);
	}
	$sql = "SELECT * FROM groupline WHERE groupline_access ='padding' Order By groupline_id desc;";
	$result = $con->query($sql);
	$json = [];
	if ($result->num_rows > 0) {
	    while($row = $result->fetch_assoc()) {
	       array_push($json, $row);
	    }
	} else {
	    echo "ยังไม่มีห้อง";
	}
	$con->close();
	return $json;
}
// EN 16.01.63 AP

// SN 01.04.63 AP
function get_missing(){
	$con = _connectDB();
	if ($con->connect_error) {
	    die("Connection failed: " . $con->connect_error);
	}
	$sql = "SELECT * FROM groupline WHERE groupline_missing='YES'";
	$result = $con->query($sql);
	$json = [];
	if ($result->num_rows > 0) {
	    while($row = $result->fetch_assoc()) {
	       array_push($json, $row);
	    }
	} else {
	    echo "ยังไม่มีห้อง";
	}
	$con->close();
	return $json;
}
// EN 01.04.63 AP


// EN 22.01.63 AP
function _users_register($id,$register){
	$con = _connectDB();
	date_default_timezone_set("Asia/Bangkok");
	$today = date('Y-m-d');
	$date1 = str_replace('-', '/', $today);
	$endpoint = date('Y-m-d',strtotime($date1 . "+".$register." days"));

	// Check connection
	if ($con->connect_error) {
	    die("Connection failed: " . $con->connect_error);
	}
	$sql = "UPDATE groupline SET groupline_alldate='".$register."',groupline_status='run',groupline_access='active',groupline_buy_date='".$today."',groupline_buy_end='".$endpoint."' WHERE groupline_id='".$id."'";
	$result = $con->query($sql);
	$res = ['status'=>true];
	return $res;
}
// EN 22.01.63 AP

// SN 22.01.63 AP list_detail.php | require [GET id]
function get_last_item_id($id){
	$con = _connectDB();
	if ($con->connect_error) {
	    die("Connection failed: " . $con->connect_error);
	}
	$sql = "SELECT * FROM groupline  WHERE groupline_id = '".$id."'";
	$result = $con->query($sql);
	$json = [];
	if ($result->num_rows > 0) {
	    while($row = $result->fetch_assoc()) {
	       array_push($json, $row);
	    }
	}
	$con->close();
	return $json;
}
// EN 22.01.63 AP

// SN 21.01.63 AP
function get_last_item_all(){
	$con = _connectDB();
	if ($con->connect_error) {
	    die("Connection failed: " . $con->connect_error);
	}
	$sql = "SELECT * FROM groupline WHERE groupline_key != '' ORDER BY groupline_id DESC LIMIT 20";
	$result = $con->query($sql);
	$json = [];
	if ($result->num_rows > 0) {
	    while($row = $result->fetch_assoc()) {
	       array_push($json, $row);
	    }
	} else {
	    echo "ยังไม่มีห้อง";
	}
	$con->close();
	return $json;
}
// EN 21.01.63 AP


// SN 27.03.63 AP Remove.php | require [GET id]
function remove_id($id){
	$con = _connectDB();
	if ($con->connect_error) {
	    die("Connection failed: " . $con->connect_error);
	}
	$sql = "DELETE FROM groupline WHERE groupline_id = ".$id;
	$result = $con->query($sql);
	$con->close();
	return true;
}
// EN 27.03.63 AP