<?php
header('Content-type: application/json');
include '../../config/database.php';
function _command($gid,$name,$products,$members,$version,$picture,$_json){
	$conn = _connectDB();
	$sql_get = "SELECT * FROM `line` WHERE line_gid='".$gid."' AND line_product='".$products."' AND line_version='".$version."'";
	$res_get = $conn->query($sql_get);
	if ($res_get->num_rows == 0) {
		$sql = 'INSERT INTO `line` (line_gid,line_name,line_product,line_members,line_version,line_picture,line_person) VALUES ("'.$gid.'", "'.$name.'","'.$products.'",'.$members.',"'.$version.'","'.$picture.'","'.$_json.'")';
		$result = $conn->query($sql);
		if($result){
			$res = ['status'=>true];
		}else{
			$sql = 'INSERT INTO `line` (line_gid,line_name,line_product,line_members,line_version,line_picture,line_person) VALUES ("'.$gid.'", "'.$name.'","'.$products.'",'.$members.',"'.$version.'","'.$picture.'","null")';
			$result = $conn->query($sql);
			if($result){
				$res = ['status'=>true,'step2' => true];
			}else{
				$res = ['status'=>false,'step2' => false];
			}
		}
	}else{
		$sql = 'UPDATE  `line` SET line_name="'.$name.'",line_members='.$members.',line_picture="'.$picture.'",line_person="'.$_json.'" WHERE line_gid="'.$gid.'" AND line_product="'.$products.'" AND line_version="'.$version.'"';
		$result = $conn->query($sql);
		if($result){
			$res = ['status'=>true];
		}else{
			$sql = 'UPDATE  `line` SET line_name="'.$name.'",line_members='.$members.',line_picture="'.$picture.'",line_person="null" WHERE line_gid="'.$gid.'" AND line_product="'.$products.'" AND line_version="'.$version.'"';
			$result = $conn->query($sql);
			if($result){
				$res = ['status'=>true,'step2' => true];
			}else{
				$res = ['status'=>false,'step2' => false];
			}
		}
	}
	return $res;
}
if(isset($_POST)){
	if(isset($_POST['gid'])  AND isset($_POST['products'])  AND isset($_POST['version'])){
		$gid = $_POST['gid'];
		$name = base64_encode($_POST['name']);
		$products = $_POST['products'];
		$members = $_POST['members'];
		$version = $_POST['version'];
		$picture = $_POST['picture'];
		$_json = base64_encode($_POST['_json']);
		echo json_encode(_command($gid,$name,$products,$members,$version,$picture,$_json));
	}else{echo json_encode(['status'=>false]);}
}else{echo json_encode(['status'=>false]);}




// _json	{'u9195ea31b3efda88e48be41fedbd0782': {'mid': 'u9195ea31b3efda88e48be41fedbd0782', 'name': 'มายบอท แอดมิน', 'picture': '0hNBZVBLuxEW1nLzpQd_puOltqHwAQARclH0FeA0Z7H19KTFRvDBwOAkt7SlQZS1M7DB0LWBcuSVRJ', 'status': 'บอทมีปัญหาติดต่อได้ตลอดเวลา'}, 'u52d0356f9b6d6d19a80e11795cafa6cc': {'mid': 'u52d0356f9b6d6d19a80e11795cafa6cc', 'name': 'บริการ Bot Line⚡', 'picture': '0hcTnccKuaPHtTGheU6KpDLG9fMhYkNDozK3hwSHcYZkwpIigqOnsmHX5Ka0oqKnsrZ3ghTSFKZRks', 'status': 'http://line.me/ti/p/~selfthai2'}, 'u69d97eff932100cd2a76375dbd527b98': {'mid': 'u69d97eff932100cd2a76375dbd527b98', 'name': 'บอทระบบ UI', 'picture': '0hbkKQZe6fPUR2KBW3SmdCE0ptMykBBjsMDkl6d1suNyMMEHgRSk9zcAYrM3JcGnwWHx0nJlAuMHxS', 'status': 'สนใจติดต่อ @mybot'}, 'u50d3b61180acf0d190e1808d7993570e': {'mid': 'u50d3b61180acf0d190e1808d7993570e', 'name': 'นำ ชุด', 'picture': '0hw7KauBM3KBh2GAD9Y3NXT0pdJnUBNi5QDnpmK1FLcShbIW4eGClmK1MQd3xbIW5HH3hvdlEccCkP', 'status': ''}, 'u40bfda8b36ffc202cd5474be0da7227b': {'mid': 'u40bfda8b36ffc202cd5474be0da7227b', 'name': 'หาราย นวด', 'picture': '0hKSXV3H7OFHxlLzyZcAZrK1lqGhESARI0HUxaSRd6GkUbHVR9DhtYG0N9H0hNGVZ5DUgOSkYsSUob', 'status': ''}, 'uba20a6a763749206ecc502ff626f9b02': {'mid': 'uba20a6a763749206ecc502ff626f9b02', 'name': 'Aj Aj', 'picture': '0hZdbZZ6yBBUtfIy2uSPN6HGNmCyYoDQMDJxYaLntzXCt0FEAbZk1JLH13CSl2EkFKNEJJKi0rCHpx', 'status': ''}, 'u1466ce33fbc9b0c06269688edde6a087': {'mid': 'u1466ce33fbc9b0c06269688edde6a087', 'name': 'มายบอท แอดมิน 2', 'picture': '0hjGgC362fNWhaOh5fgVVKP2Z_OwUtFDMgIlx_CXhoP1p1XSFsbg97XH49aF9_DXs3MgkvWnhqalFx', 'status': ''}, 'ud7d20ba86aa2a0de084bf268515b1f63': {'mid': 'ud7d20ba86aa2a0de084bf268515b1f63', 'name': 'Bos8.1', 'picture': '0hm90apnjmMhYNGBmcHotNQTFdPHt6NjRedSt7JSsZP3YpLyUQMi58JXwZZSNzf3ZHOHp9dHsYOyEi', 'status': ''}}
// gid	c1a57f1a7ed3ba828821e3458889789e4
// members	8
// name	บอทไลน์ใหม่ 10 ตัว
// picture	0hEUD1U3VOGnp5SDKaqwBlLUUNFBcOZgsyFy4FGFofRBpSLAguFn5cH1VBR0oAelgoTCdcFFpBTB5R
// products	LINGGUI
// version	V1