<?php

$jsondata = file_get_contents("./api/connv2.json");
$json = json_decode($jsondata, true);
$message = '<div class="has-sidebar-left has-sidebar-tabs"><div class="container-fluid relative animatedParent animateOnce p-lg-5"><div class="alert alert-success" id="flash-msg"><center><h4 style="color:white!important"><i class="icon fa fa-check"></i>Updated!</h4></center></div></div></div>';

if (isset($_POST['submit']))
{
    $jsonData = file_get_contents("./api/connv2.json");
    if ($_POST["msg_status"] == 'ACTIVE')
    {
        $success = '1';
    }
    else
    {
        $success = '0';
    }
    if (empty($_POST["message"]))
    {
        $message = '';
    }
    else
    {
        $message = $_POST["message"];
    }
    if (empty($_POST["msg_status"]))
    {
        $msg_status = 'INACTIVE';
    }
    else
    {
        $msg_status = $_POST["msg_status"];
    }
    if (empty($_POST["msg_expire"]))
    {
        $msg_expire = '';
    }
    else
    {
        $msg_expire = $_POST["msg_expire"];
    }
    $arrayData = json_decode($jsonData, true);
    $replacementData = array(
        'success' => $success,
        "message" => $message,
        "msg_status" => $msg_status,
        "msg_expire" => $msg_expire,
    );
    $newArrayData = array_replace_recursive($arrayData, $replacementData);
    $newJsonData = json_encode($newArrayData, JSON_PRETTY_PRINT);
    file_put_contents("./api/connv2.json", $newJsonData);
    header("Location: messageall.php?message=$message");
}

include ('includes/header.php');
?>
<div class="has-sidebar-left has-sidebar-tabs">
    <div class="container-fluid relative animatedParent animateOnce p-lg-5">
        <div class="col-md-8 mx-auto">
            <div class="card-body">
                <div class="card bg-primary text-white">
                    <div class="card-header card-header-warning">
                        <center>
                            <h2><i class="icon icon-bullhorn"></i> In-app Message</h2>
                        </center>
                    </div>
                    
                    <div class="card-body">
                            <div class="col-12">
                                <h3> Edit Message</h3>
                            </div>
                            <form method="post">
                                <div class="form-group ">
                                    <label class="form-label" for="message">Message For Everyone</label>
                                    <input class="form-control" id="message" name="message" value="<?=$json['message']?>" type="text">
                                </div>

                                <div class="form-group ">
                                    <label class="form-label" for="msg_status">Status</label>
                                    <select class="form-control" id="select" name="msg_status">
                                        <option value="INACTIVE" <?php echo $json['msg_status']=='INACTIVE'?'selected':'' ?> >INACTIVE</option>
                                        <option value="ACTIVE" <?php echo $json['msg_status']=='ACTIVE'?'selected':'' ?> >ACTIVE</option>
                                    </select>
                                </div>

                                <div class="form-group ">
                                    <label class="form-label" for="datetimepicker">Expiration</label>
                                    <input type="text" class="form-control" autocomplete="false" name="msg_expire" data-toggle="datetimepicker" id="datetimepicker" value="<?=$json['msg_expire'] ?>">
                                </div>

                                <hr>

                                <div class="form-group">
                                  <center>
                                      <button class="btn btn-info" name="submit" type="submit">
                                          <i class="icon icon-check"></i>Submit
                                      </button>
                                  </center>
                                </div>
                            </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include ('includes/footer.php');?>

<script>
    $('.datetimepicker').datetimepicker({
      //value:dateTime, 
      step:30,
      format:'Y-m-d H:i:s',
    });
</script>

</body>
</html>