<?php

include("config.php");
include('countrycodes.php');

date_default_timezone_set('Asia/Jakarta');

define('GX_PATH', realpath(__DIR__.'/'));
define('GX_LIB', GX_PATH.'/lib/');

require 'autoload.php';
new Db();

$page = "kelola";

include('header.php');


if (!User::isLoggedin()) {
    header("Location: login.php");
} else {

if( isset($_POST['submit']) ) {
    $str = file_get_contents("config.php");
    $str = str_replace($teamName, $_POST['teamName'], $str);
    $str = str_replace($passWd, $_POST['passWd'], $str);
    $str = preg_replace('/logo = "(.*)"; #END LOGO/', "logo = \"".$_POST['logo']."\"; #END LOGO", $str);
    $str = preg_replace('/channelTele = "(.*)"; # END Channel/', "channelTele = \"".$_POST['channelTele']."\"; # END Channel", $str);
    $str = preg_replace('/beforetext = "(.*)"; # END beforetext/', "beforetext = \"".$_POST['beforetext']."\"; # END beforetext", $str);
    $str = preg_replace('/aftertext = "(.*)"; # END aftertext/', "aftertext = \"".$_POST['aftertext']."\"; # END aftertext", $str);
    // echo $str;
    
    file_put_contents('config.php', $str);
    $teamName = $_POST['teamName'];
    $passWd = $_POST['passWd'];
    $logo = $_POST['logo'];
    $channelTele = $_POST['channelTele'];
    $beforetext = $_POST['beforetext'];
    $aftertext = $_POST['aftertext'];
}

if( isset($_POST['sandiKelola']) ) {
    $newpass = User::randpass($_POST['passKelola']);
    echo Db::query("UPDATE `user` SET `pass` = '{$newpass}' WHERE `userid` = 'admin'  ");
}

?>
<div class="container">
    <div class="row">
        <div class="col-12">
        <h3>Kelola Report </h3><a href="logout.php" class="badge badge-danger">Logout</a>
        </div>
        <form action="" method="post">
        <div class="form-group row">
            <label for="teamName" class="col-4 col-form-label">Nama Team</label> 
            <div class="col-12">
            <div class="input-group">
                <div class="input-group-prepend">
                <div class="input-group-text">
                    <i class="fa fa-users"></i>
                </div>
                </div> 
                <input id="teamName" name="teamName" type="text" class="form-control" required="required" value="<?=$teamName;?>">
            </div>
            </div>
        </div>
        <div class="form-group row">
            <label for="passWd" class="col-4 col-form-label">Sandi Report</label> 
            <div class="col-12">
            <div class="input-group">
                <div class="input-group-prepend">
                <div class="input-group-text">
                    <i class="fa fa-key"></i>
                </div>
                </div> 
                <input id="passWd" name="passWd" type="text" class="form-control" required="required" value="<?=$passWd;?>">
            </div>
            </div>
        </div>
        <div class="form-group row">
            <label for="logo" class="col-4 col-form-label">URL Logo</label> 
            <div class="col-12">
            <div class="input-group">
                <div class="input-group-prepend">
                <div class="input-group-text">
                    <i class="fa fa-image"></i>
                </div>
                </div> 
                <input id="logo" name="logo" type="text" class="form-control" value="<?=$logo;?>">
            </div>
            </div>
        </div> 
        <div class="form-group row">
            <label for="channelTele" class="col-4 col-form-label">Channel Tele</label> 
            <div class="col-12">
            <div class="input-group">
                <div class="input-group-prepend">
                <div class="input-group-text">
                    <i class="fa fa-image"></i>
                </div>
                </div> 
                <input id="channelTele" name="channelTele" type="text" class="form-control" value="<?=$channelTele;?>">
            </div>
            </div>
        </div> 
        <div class="form-group row">
            <label for="beforetext" class="col-4 col-form-label">Text Before Tele</label> 
            <div class="col-12">
            <div class="input-group">
                <div class="input-group-prepend">
                <div class="input-group-text">
                    <i class="fa fa-image"></i>
                </div>
                </div> 
                <input id="beforetext" name="beforetext" type="text" class="form-control" value="<?=$beforetext;?>">
            </div>
            </div>
        </div> 
        <div class="form-group row">
            <label for="aftertext" class="col-4 col-form-label">Text After Tele</label> 
            <div class="col-12">
            <div class="input-group">
                <div class="input-group-prepend">
                <div class="input-group-text">
                    <i class="fa fa-image"></i>
                </div>
                </div> 
                <input id="aftertext" name="aftertext" type="text" class="form-control" value="<?=$aftertext;?>">
            </div>
            </div>
        </div> 
        <div class="form-group row">
            <div class="offset-4 col-8">
            <button name="submit" type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>
        </form>
    </div>

</div>

<div class="container mt-5">
    <div class="row">
        <form action="" method="post">
        <div class="form-group row">
            <label for="passKelola" class="col-4 col-form-label">Sandi KELOLA</label> 
            <div class="col-8">
            <div class="input-group">
                <div class="input-group-prepend">
                <div class="input-group-text">
                    <i class="fa fa-key"></i>
                </div>
                </div> 
                <input id="passKelola" name="passKelola" type="text" class="form-control" required="required">
            </div>
            </div>
        </div> 
        <div class="form-group row">
            <div class="offset-4 col-8">
            <button name="sandiKelola" type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>
        </form>
    </div>
</div>
<?php
}
include('footer.php');