<?php
// header('Content-type: application/json');
include '../config/database.php';
function _command($product,$version){
	$conn = _connectDB();
	$sql = "SELECT * FROM line WHERE line_product = '".$product."' AND line_version = '".$version."' ";
	$result = $conn->query($sql);
	$res = [];
	if ($result->num_rows > 0) {
	    while($row = $result->fetch_assoc()) {
			array_push($res,$row);
	    }
	}
	return $res;
}


function _groupline($product,$version){
	$conn = _connectDB();
	$sql = "SELECT groupline_name,groupline_key,groupline_products,groupline_version,groupline_buy_date,groupline_buy_end,groupline_status,groupline_access FROM  groupline WHERE groupline_products = '".$product."' AND groupline_version = '".$version."' ";
	$result = $conn->query($sql);
	$res = [];
	if ($result->num_rows > 0) {
	    while($row = $result->fetch_assoc()) {
	    	$res[$row['groupline_key']] = $row;
	    }
	}
	return $res;
}



$groupline = [];
$data = [];
if(isset($_GET['product']) AND isset($_GET['version']))	{
	// MAP DATA
	$groupline = _groupline($_GET['product'],$_GET['version']);
	foreach(_command($_GET['product'],$_GET['version']) as $key => $val){
		array_push($data,$val);
	}

?>
<input type="hidden" id="product" value="<?php echo $_GET['product'] ?>">
<input type="hidden" id="version" value="<?php echo $_GET['version'] ?>">
<!DOCTYPE html>
<html>
<head>
	<title>GROUP | MONITOR</title>
	<link rel="stylesheet" type="text/css" href="assets/Semantic-UI-CSS/semantic.min.css">
	<script src="https://code.jquery.com/jquery-3.1.1.min.js"  integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="  crossorigin="anonymous"></script>
	<script src="assets/Semantic-UI-CSS/semantic.min.js"></script>
</head>
<body>
	<br><br>
	<div class="ui container">


		<div class="ui three column grid">
			<style>
			.ui.table td {
			    padding: 0em 0em !important;	
			    text-align: inherit;
			}
			.ui.grid>* {
			    padding-left: 0rem;
			    padding-right: 0rem;
			}
			</style>	
			<a href="http://150.95.89.148/api/py/monitor/index2.php"><< กลับ</a>
			<table class="ui celled table">
			  <thead>
 				<tr>
			      <th colspan="6"><center> [ <?php echo $_GET['product'].' - '.$_GET['version'] ?> ] </center></th>
			    </tr>
			    <tr>
			      <th></th>
			      <th><center>ชื่อกลุ่ม</center></th>
			      <th><center>สมาชิก</center></th>
			      <th><center>เช่า-หมดอายุ</center></th>
			      <th></th>
			    </tr>
			  </thead>
			  <tbody>
			  	<?php 
			  		$i = 1; 
			  		$time_green = 0;
			  		$time_red = 0;
					$time_gray = 0;
					$time_none = 0;

					$status_run = 0;
					$status_stop = 0;
					$status_padding = 0;
					$status_none = 0;
			  	?>
				<?php foreach ($data as $key => $val){ ?>
					<?php 

						//add maker
						$groupline[$val['line_gid']]['maker'] = true;
						$time_color = '#000';
						$date_value = date($groupline[$val['line_gid']]['groupline_buy_date']);
						$data_end = date($groupline[$val['line_gid']]['groupline_buy_end']);
						$date_now = date('Y-m-d');
						if($data_end < $date_now){
							$time_color = 'red';$time_red++;
						}else if($data_end > $date_now){
							$time_color = 'green';$time_green++;
						} else if($data_end == $date_now){
							$time_color = 'gray';$time_gray++;
						}else{
							$time_none++;
						}

						$status = $groupline[$val['line_gid']]['groupline_status'];
						if($status == ''){$status = 'none';}
						if($status == 'run'){
							$status_color = 'green';$status_run++;
						}else if($status == 'stop'){
							$status_color = 'red';$status_stop++;
						}else if($status == 'padding'){
							$status_color = '#ffa722';$status_padding++;
						}else{
							$status_color = '#000';$status_none++;
						}
					?>
					<?php $name = base64_decode($val['line_name']); ?>
				    <tr>
				      <td><img style="width: 30px;" src="http://dl.profile.line-cdn.net/<?php echo $val['line_picture']; ?>.jpg"> </td>
				      <td> [ <b><?php echo $i;?></b> ][ <b><i><u><?php echo $groupline[$val['line_gid']]['groupline_version'];?></u></i></b> ] <a href="members.php?gid=<?php echo $val['line_gid'];?>&product=<?php echo $val['line_product']; ?>&version=<?php echo $val['line_version']; ?>"><?php echo mb_substr($name, 0, 35, 'UTF-8'); ?></a> <br> <span><?php echo $val['line_gid']; ?></span></td>
				      <td><center><?php echo $val['line_members']; ?> </center> <center style="color:<?php echo $status_color; ?>">[ <?php echo $status; ?> ]</center></td>
				      <td><center><?php echo $date_value; ?></center><center><span style="color:<?php echo $time_color; ?>"><?php echo $data_end;?></span></center></td>
				      <td>

						<div class="ui buttons">
						  <button class="ui green icon button mini" onclick="_box_message('<?php echo $name;?>','<?php echo $val['line_gid'];?>','<?php echo $val['line_members'];?>','<?php echo $date_value;?>','<?php echo $data_end;?>')">ข้อความ</button>
						  <div class="or"></div>
						  <button class="ui red icon button mini" onclick="_box_alert('<?php echo $name;?>','<?php echo $val['line_gid'];?>')">ออกกลุ่ม</button>
						</div>

				      </td>
				    </tr>
				    <?php $i++;?>
				<?php } ?>
					<tr>
				      <td><br><br> </td>
				      <td colspan="2"><center> [run] = <?php echo $status_run; ?>  , [stop] = <?php echo $status_stop; ?> , [padding] = <?php echo $status_padding; ?> , [none] = <?php echo $status_none; ?> </center></td>
				      <td colspan="2"><center> [ทำงาน] = <?php echo $time_green; ?> , [หมดอายุ] = <?php echo $time_red; ?> , [อื่นๆ] = <?php echo $time_none; ?> </center></td>
				  	</tr>

			  </tbody>
			</table>

		</div>

		<br><br>
		<div class="ui three column grid">
			<br>
			<table class="ui celled table">
			  <thead>
			    <tr>
			      <th colspan="4"><center>[ กลุ่มที่ไม่มีตัวบอทอยู่ ]</center></th>
			    </tr>
			    <tr>
			      <th><center>ชื่อกลุ่ม</center></th>
			      <th><center>สถานะ</center></th>
			      <th><center>เช่า-หมดอายุ</center></th>
			      <th></th>
			    </tr>
			  </thead>
			  <tbody>

			<?php
			  		$time_green = 0;
			  		$time_red = 0;
					$time_gray = 0;
					$time_none = 0;

					$status_run = 0;
					$status_stop = 0;
					$status_padding = 0;
					$status_none = 0;
			?>
			<?php
			foreach ($groupline as $key2 => $val2){
				if(!isset($val2['maker'])){
			?>
					<?php 
						$time_color = '#000';
						$date_value = date($val2['groupline_buy_date']);
						$data_end = date($val2['groupline_buy_end']);
						$date_now = date('Y-m-d');
						if($data_end < $date_now){
							$time_color = 'red';$time_red++;
						}else if($data_end > $date_now){
							$time_color = 'green';$time_green++;
						} else if($data_end == $date_now){
							$time_color = 'gray';$time_gray++;
						}else{$time_none++;}

						$status = $val2['groupline_status'];
						$access = $val2['groupline_access'];
						if($status == ''){$status = 'none';}
						if($status == 'run'){
							if($access ==  'active'){
								$status_color = 'green';
							}else{
								$status_color = '#dc7a00';
							}
							$status_run++;
						}else if($status == 'stop'){
							$status_color = 'red';$status_stop++;
						}else if($status == 'padding'){
							$status_color = '#ffa722';$status_padding++;
						}else{
							$status_color = '#000';$status_none++;
						}		
					?>

	    		<tr>
			      <td><?php echo mb_substr($val2['groupline_name'], 0, 35, 'UTF-8'); ?><br><?php echo $val2['groupline_key']; ?></td> 
			      <td><center><span style="color:<?php echo $status_color; ?>"><?php echo $val2['groupline_status']; ?></span></center></td> 
			      <td><center><?php echo $val2['groupline_buy_date']; ?></center> <center><span style="color:<?php echo $time_color; ?>"><?php echo $val2['groupline_buy_end']; ?></span></center></td>
			      <td>
			      	
						<div class="ui buttons">
						  <button class="ui green icon button mini" onclick="">เริ่ม</button>
						  <div class="or"></div>
						  <button class="ui orange icon button mini" onclick="_box_alert_stop('<?php echo mb_substr($val2['groupline_name'], 0, 35, 'UTF-8'); ?>','<?php echo $val2['groupline_key'];?>')">หยุด</button>
						  <div class="or"></div>
						  <button class="ui red icon button mini" onclick="">ลบ</button>
						</div>

			      </td>
			    </tr>

			<?php
				}
			}
			?>
				
				<tr>

				  <td colspan="1"><center> [run] = <?php echo $status_run; ?>  , [stop] = <?php echo $status_stop; ?> , [padding] = <?php echo $status_padding; ?> , [none] = <?php echo $status_none; ?> </center></td>
				  <td colspan="3"><center> [ค้างทำงาน] = <?php echo $time_green; ?> , [หมดอายุ] = <?php echo $time_red; ?> , [อื่นๆ] = <?php echo $time_none; ?> </center></td>
			  	</tr>

			  </tbody>
			</table>
			<br><br>



		</div>

	</div>

</body>
</html>

<?php } ?>

<script>
	function _box_message(name,gid,total,ds,de){
		$('#name').html(name);
		$('#gid').html(gid);
		$('#total').html(total);
		$('#ds').html(ds);
		$('#de').html(de);
		$('.ui.modal.m1').modal('show');
		$('#modal_popup_success').addClass("hidden");
		// http://150.95.89.148/api/v3/push-system?msg=!_uc26465be087048ab9e5cccacf84a2c80_u1466ce33fbc9b0c06269688edde6a087_xxxx
	}

	function _send(){
		var param1 = '!';
		var param2 = $('#product').val()+''+$('#version').val();
		var param3 = $('#gid').html();
		var param4 = $('#msg').val();
		$.get( "notify.php", { param1: param1, param2: param2,param3: param3,param4: param4 } ).done(function( data ) {
			$('#msg').val('');
	    	$('#modal_popup_success').removeClass("hidden");
	  	});
	}


	function _box_alert(name,gid){
		$('#name2').html(name);
		$('#gid2').val(gid);
		$('.ui.basic.modal.m2').modal('show');
	}
	function _box_alert_confirm(){
		var param1 = '-';
		var param2 = $('#product').val()+''+$('#version').val();
		var param3 = $('#gid2').val();
		var param4 = $('#gid2').val();
		//for action delete
		var _p = $('#product').val();
		var _v = $('#version').val();
		$.get( "notify.php", { param1: param1, param2: param2,param3: param3,param4: param4 , _p:_p , _v:_v } ).done(function( data ) {
			$('.ui.basic.modal.m2').modal('hide');
	  	}).then(function(){
	  		location.reload();
	  	});
	  	//  http://150.95.89.148/api/py/monitor/notify.php?param1=-&param2=HUAYLIKEV1&param3=&param4=
	}

	//STOP
	function _box_alert_stop(name,gid){
		$('#name3').html(name);
		$('#gid3').val(gid);
		$('.ui.basic.modal.m3').modal('show');
	}
	function _box_alert_stop_confirm(){
		var param1 = '-';
		var param2 = $('#product').val()+''+$('#version').val();
		var param3 = $('#gid3').val();
		var param4 = $('#gid3').val();
		//for action delete
		var _p = $('#product').val();
		var _v = $('#version').val();
		$.get( "stop.php", { param1: param1, param2: param2,param3: param3,param4: param4 , _p:_p , _v:_v } ).done(function( data ) {
			$('.ui.basic.modal.m3').modal('hide');
	  	}).then(function(){
	  		location.reload();
	  	});
	}



	$( document ).ready(function() {

		$('.message .close').on('click', function() {
		    $(this).closest('.message').transition('fade');
		});

	});
</script>

<div class="ui modal m1">
  <i class="close icon"></i>
  <div class="header">
   	<span id="name"></span>
     [ <b id="total"></b> ]
  </div>
  <div class="content">

  	<div id="modal_popup_success" class="ui success message hidden">
	  <i class="close icon"></i>
	  <div class="header">
	    สำเร็จ
	  </div>
	  <p>ส่งข้อมูลไปยังกลุ่ม เรียบร้อยแล้ว</p>
	</div>

	<div class="ui form">
	  <div class="field">
	    <label>ข้อความที่จะส่ง</label>
	    <textarea autocomplete="off" id="msg" spellcheck="false"></textarea>
	  </div>
	</div>
  </div>
  <div class="actions">
  	<span class="">
  		[ <b id="gid"></b> ]
  		| เริ่ม <b id="ds"></b>
  		ถึง <b id="de"></b>
  	</span>
		<button class="ui icon red deny button">
		  <i class="x  icon"></i> ยกเลิก
		</button>
		<button class="ui green icon button" onclick="_send()">
		  <i class="play icon"></i> ส่งข้อความ
		</button> 
  </div>
</div>



<div class="ui basic modal m2">
  <div class="ui icon header">
    <i class="archive icon"></i>
    ! บอทออกจากกลุ่มไลน์
  </div>
  <div class="content">
  	<input type="hidden" id="gid2">
    <p><span id="name2"></span> หลังบอทออกจากกลุ่มแล้ว จะไม่สามารถเข้าได้อีก ยืนยันการออก?</p>
  </div>
  <div class="actions">
    <button class="ui red basic cancel inverted button">
      <i class="remove icon"></i>
      ไม่ออกแล้ว
    </button>
    <button class="ui green ok inverted button" onclick="_box_alert_confirm()">
      <i class="checkmark icon"></i>
      ออกเลย
    </button>
  </div>
</div>



<div class="ui basic modal m3">
  <div class="ui icon header">
    <i class="archive icon"></i>
    ! หยุดล่งผล
  </div>
  <div class="content">
  	<input type="hidden" id="gid3">
    <p><span id="name3"></span> stop?</p>
  </div>
  <div class="actions">
    <button class="ui red basic cancel inverted button">
      <i class="remove icon"></i>
      ไม่ออกแล้ว
    </button>
    <button class="ui green ok inverted button" onclick="_box_alert_stop_confirm()">
      <i class="checkmark icon"></i>
      ปิดล่ง
    </button>
  </div>
</div>