<?php

// error_reporting(0);
date_default_timezone_set("Asia/Bangkok");
define('APP', 'mybot');

function _connectDB_Demo(){
	$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;
}

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

// +++++++++++++++++++++++++++++
function get_id($id){
	$con = _connectDB_Demo();
	$sql = "SELECT * FROM groupline where groupline_id='".$id."'";
	$result = $con->query($sql);
	$json2 = [];
	if ($result->num_rows > 0) {
	    while($row = $result->fetch_assoc()) {
	       array_push($json2, $row);
	    }
	} else {
	    echo "ยังไม่มีห้อง";
	}
	$con->close();
	return ['json'=> $json2];
}
function target_update_time($ID,$START,$END,$CM){
	$con = _connectDB_Demo();
	if ($con->connect_error) {
	    die("Connection failed: " . $con->connect_error);
	}
	if(trim($CM) != ''){
		$sql = "UPDATE groupline SET groupline_buy_date='".$START."',groupline_buy_end='".$END."',groupline_text='".$CM."' WHERE groupline_id='".$ID."'";
	}else{
		$sql = "UPDATE groupline SET groupline_buy_date='".$START."',groupline_buy_end='".$END."',groupline_text=NULL WHERE groupline_id='".$ID."'";	
	}
	echo $sql;
	$result = $con->query($sql);
	$con->close();
	header( "location: view.php?id=".$ID );
 	exit(0);
}

function _action($ID,$EVENT){
	$con = _connectDB_Demo();
	if ($con->connect_error) {
	    die("Connection failed: " . $con->connect_error);
	}
	$sql = "UPDATE groupline SET groupline_status='".$EVENT."' WHERE groupline_id='".$ID."'";
	echo $sql;
	$result = $con->query($sql);
	$con->close();
	header( "location: view.php?id=".$ID );
 	exit(0);
}

function get_gid($gid){
	$con = _connectDB();
	if ($con->connect_error) {
	    die("Connection failed: " . $con->connect_error);
	}
	$sql = "SELECT * FROM groupline where groupline_key='".$gid."'";
	$result = $con->query($sql);
	$json = [];
	if ($result->num_rows > 0) {
	    while($row = $result->fetch_assoc()) {
	       array_push($json, $row);
	    }
	} else {
	    echo "ยังไม่มีห้อง";
	}
	$con->close();

	$con = _connectDB_Demo();

	$sql = "SELECT * FROM groupline where groupline_key='".$gid."'";
	$result = $con->query($sql);
	$json2 = [];
	if ($result->num_rows > 0) {
	    while($row = $result->fetch_assoc()) {
	       array_push($json2, $row);
	    }
	} else {
	    echo "ยังไม่มีห้อง";
	}
	$con->close();

	return ['json'=> $json,'to'=>$json2];
}

function update_gid($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);
	    }
	} else {
	    echo "ยังไม่มีห้อง";
	}
	$con->close();
	update_to($json);
}

function update_to($result){
	$con = _connectDB_Demo();
	if ($con->connect_error) {
	    die("Connection failed: " . $con->connect_error);
	}
	$sql = "UPDATE groupline SET groupline_buy_date='".$result[0]['groupline_buy_date']."',groupline_buy_end='".$result[0]['groupline_buy_end']."',groupline_keycode='".$result[0]['groupline_keycode']."'  WHERE groupline_products='".$result[0]['groupline_products']."' AND groupline_key='".$result[0]['groupline_key']."'";
	$result = $con->query($sql);
	$con->close();
	header( "location: index.php" );
 	exit(0);
}

