<?php
$namaku=$this->session->userdata('nama');
$username=$this->session->userdata('user_id');
$result=$this->db->query("SELECT * FROM web_bot WHERE id='1'");
foreach ($result->result() as $baris)  {
$userbot=$baris->username;
$token=$baris->token;
}

$getwebhhok=getWebhookInfo($token);
$jsonget=json_decode($getwebhhok,true);
$result=$jsonget['result'];
$statusurl=$result['url'];
//echo $result['pending_update_count']."<br>";
//unssetwebhook($token);
//$urlsaldo=base_url()."absenbot";
//$set=setwebhook($urlsaldo,$token);
?>
<section class="content-header">
	<h1>
		Setting Bot Telegram
	</h1>
	<ol class="breadcrumb">
		<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
		<li class="active">bot</li>
	</ol>
</section>

<section class="content">
	<div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class='box-header with-border'>
					  <h3 class="box-title">Edit Setting</h3>
					</div>
					<div class="box-body">
						<form action="" method="post" enctype="multipart/form-data" class="form-horizontal">
							<div class="form-body">
								<div class="form-group">
									<label class="control-label col-md-3">Username</label>
									<div class="col-md-9">
										<input name="userbot" placeholder="Nama Username Bot" class="form-control" type="text" value="<?php echo $userbot;?>">
										<span class="help-block"></span>
									</div>
								</div>
								<div class="form-group">
									<label class="control-label col-md-3">Token Bot</label>
									<div class="col-md-9">
										<input name="token" placeholder="Token Bot" class="form-control" type="text" value="<?php echo $token;?>">
										<span class="help-block"></span>
									</div>
								</div>
								
								
							</div>
							<div class="form-group">
							<div class="col-md-3">
							</div>
							<div class="col-md-9">
								<button type="submit" name="btnsave" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> &nbsp; Simpan</button>
								<?php
								if(empty($statusurl)) {
									?>
									<button type="submit" name="btnset" class="btn btn-info"><i class="fa fa-cog fa-lg"></i> &nbsp; Set Webhook</button>
									<?php
								} else {
									?>
									<button type="submit" name="btnunset" class="btn btn-danger"><i class="fa fa-cog fa-lg"></i> &nbsp; UnSet Webhook</button>
									<?php
								}
								?>
							</div>
							</div>
						</form>
						Status bot telegram<br>
						<?php
						if(empty($statusurl)) {
							echo "Status = Nonaktif <br>";
						} else {
							echo "Status = Aktif <br>";
							echo "Jumlah pesan pending = ".$result['pending_update_count']."<br>";
						}
						
						?>
					</div>
				</div>

			</div>
		</div>
</section>
<?php
	if(isset($_POST['btnsave']))
 {
  $userbot = $_POST['userbot'];
  $token = $_POST['token'];
  
  if(empty($userbot)){
   $errMSG = "Please Enter Username.";
  }
  else if(empty($token)){
   $errMSG = "Please Enter Token.";
  }
 
  
  if(!isset($errMSG))
  {
	  $result=$this->db->query("UPDATE web_bot SET username ='$userbot', token='$token' WHERE id='1'");
	  
	  header("refresh:1;bot");
  }
 } elseif(isset($_POST['btnset']))
 {
	$urlsaldo=base_url()."absenbot";
	$set=setwebhook($urlsaldo,$token);
	header("refresh:1;bot");
 } elseif(isset($_POST['btnunset']))
 {
	unssetwebhook($token);
	header("refresh:1;bot");
 }
 
?>