<?php
$page = "prettify-json-online";
include_once('includes/header.php');
include __DIR__ . '/vendor/autoload.php';
use Seld\JsonLint\JsonParser;
$normal_string = "";
if(isset($_POST['unformatted_json'])){
    $old = $_POST['unformatted_json'];
    $parser = new JsonParser();
    
    try {
        $parser->parse($old, JsonParser::DETECT_KEY_CONFLICTS);
        $new = json_encode(json_decode($old), JSON_PRETTY_PRINT);
    } catch (Exception $e) {
        $line = $e->getDetails();
        $line = $line['line'];
        $details = $e->getMessage();
        $new = $details;
    }
}else{
    $old = "";
    $new = "";
}

?>
<link href="https://geekyhumans.com/tools/assets/css/jquery-linedtextarea.css" type="text/css" rel="stylesheet" />

    <div class="container">
        <div class="row">
            <div class="col-md-12 mt-2 mb-2">
                <h1><?php echo $title; ?></h1>
                <form action="" method="POST">
                    <div class="row">
                        <div class="col-md-6">
                            
                            <label for="exampleFormControlTextarea1" class="form-label">Original JSON</label>
                            <textarea class="lined" rows="30" cols="70" name="unformatted_json"><?php echo $old; ?></textarea>
                        </div>

                        <div class=" col-md-6">
                            <label for="exampleFormControlTextarea1" class="form-label">Updated JSON</label>
                            
                            <textarea class="lined" rows="30" cols="70" name="formatted_json"><?php echo $new; ?></textarea>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12 mt-2">
                            <button type="submit" class="btn btn-dark float-end">Submit</button>
                        </div>
                    </div>
                </form>
            </div>  
            <div class="alert alert-primary" role="alert">
              Want to checkout our <a href="https://geekyhumans.com/tools/string-diff-viewer"> Free Code Diff Viewer</a>?. Use it to find out changes in your code or text.
            </div>
        </div>
    </div>

<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>
<link href='http://fonts.googleapis.com/css?family=Roboto+Condensed' rel='stylesheet' type='text/css'>
<script src="https://geekyhumans.com/tools/assets/js/jquery-linedtextarea.js"></script>

<script>
    $(function() {
        $(".lined").linedtextarea(
            <?php if(isset($line)){ ?>
                {selectedLine: <?php echo (int)$line + 1; selectedClass: 'lineselect';?>}
            <?php } ?>
        );
    });
</script>

<?php
include_once('includes/footer.php');
