<?php
/**
 * Created by PhpStorm.
 * User: jedi
 * Date: 12/27/18
 * Time: 4:46 AM
 */
?>


<style>
    #img-upload{
        width: 100%;
    }
</style>

    <div class="container-fluid">

        <!-- Breadcrumbs-->
        <ol class="breadcrumb">
            <li class="breadcrumb-item">
                <a href="#">Dashboard</a>
            </li>
            <li class="breadcrumb-item active">Found</li>
        </ol>

        <div id="alertmessage" class="collapse alert alert-danger" role="alert">
            This is a danger alert—check it out!
        </div>


        <div id="successmessage" class="collapse alert alert-success" role="alert">
            A simple success alert—check it out!
        </div>

        <!-- Area Chart Example-->
        <div class="card mb-3">
            <div class="card-header">
                <i class="fas fa-plus"></i>
                Add Found Item
            </div>
            <div class="card-body">
                <form method="POST" action="#" id="lost_form">

                    <!--div-- class="form-group">
                        <input type="text" class="form-control" placeholder="WANN" name="wann" value="<?php echo date("Y-m-d H:i:s");?>"></p>
                    </div-->

                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="WAS" name="was" autocomplete="off">
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="KISTE" name="container" autocomplete="off">
                    </div>

                    <div class="form-group">
                        <div class="input-group">
                            <div class="custom-file btn-file">
                                <input type="file" class="custom-file-input" id="inputGroupImage"  name="image" accept="image/*" capture="environment">
                                <label class="custom-file-label" for="inputGroupImage">Choose Image</label>
                                <input type="text" class="form-control" readonly>
                            </div>
                        </div>
                        <img id='img-upload'/>
                    </div>

                    <div class="form-group">
                        <button type="submit" class="form-control btn btn-primary" onclick="add_found_item(); return false;">Submit</button>
                    </div>
                </form>
            </div>
            <!--div class="card-footer small text-muted">Updated yesterday at 11:59 PM</div-->
        </div>


        <!-- DataTables Example -->
        <div class="card mb-3">
            <div class="card-header">
                <i class="fas fa-table"></i>
                Lost Items</div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>was</th>
                            <th>wann</th>
                            <th>wo</th>
                            <th>kontakt</th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th>ID</th>
                            <th>was</th>
                            <th>wann</th>
                            <th>wo</th>
                            <th>kontakt</th>
                        </tr>
                        </tfoot>
                        <tbody>
                        <?php
                        foreach( get_lostitems(true) as $item){
                            ?>
                            <tr>
                                <td><?php echo $item["id"]; ?></td>
                                <td><?php echo $item["was"]; ?></td>
                                <td><?php echo $item["wann"]; ?></td>
                                <td><?php echo $item["wo"]; ?></td>
                                <td><?php echo $item["contact"]; ?></td>
                                <!--td>
                                    <?php
                                foreach(explode(",",$item["tags"]) as $tag){
                                    ?>
                                                <span class="label label-default"><?php echo $tag; ?></span>
                                            <?php
                                }
                                ?>
                                </td>
                                <td><?php echo $item["id"]; ?></td-->
                            </tr>
                            <?php
                        }
                        ?>
                        </tbody>
                    </table>
                </div>
            </div>
            <!--div class="card-footer small text-muted">Updated yesterday at 11:59 PM</div-->
        </div>

    </div>
    <!-- /.container-fluid -->

<script>


    function add_found_item(){

        var fileInput = document.querySelector('#inputGroupImage');

        var xhr = new XMLHttpRequest();
        xhr.open('POST', '/ajax.php?action=add_found_item');

        xhr.upload.onprogress = function(e)
        {
            /*
            * values that indicate the progression
            * e.loaded
            * e.total
            */

            $("#alertmessage").collapse('hide');
            $("#successmessage").html((100*e.loaded/e.total)+"%").collapse('show');
            console.log("progress");
            console.log(e);

        };

        xhr.upload.onabort = function (e) {
            console.log("abort");
            console.log(e);
        };

        xhr.upload.onload = function (e) {
            console.log("load");
            console.log(e);
        };

        xhr.upload.ontimeout = function (e) {
            console.log("timeout");
            console.log(e);
        };

        xhr.upload.onerror = function (e) {
            console.log("error");
            console.log(e);
            $("#successmessage").collapse('hide');
            $("#alertmessage").html("error").collapse('show');
        };

        xhr.onload = function(e)
        {
            console.log("foo load");
            console.log(e);
            reply = JSON.parse(xhr.responseText);
            console.log(reply);

            if(reply.status == "ok"){
                $( "#lost_form" ).trigger("reset");
                $('#img-upload').attr('src', "");
                $("#alertmessage").collapse('hide');
                $("#successmessage").html(reply.message).collapse('show');
            }else{
                $("#successmessage").collapse('hide');
                $("#alertmessage").html(reply.message).collapse('show');
            }
        };

        // upload success
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (xhr.status != 200) {
                    reply = JSON.parse(xhr.responseText);
                    console.log(reply);
                    $("#successmessage").collapse('hide');
                    $("#alertmessage").html(reply.message).collapse('show');

                }

            }else{
                console.log(xhr.readyState);
            }
        }

        var form = new FormData(document.querySelector("#lost_form"));
        form.append('title', fileInput.files[0].name);
        form.append('image', fileInput.files[0]);

        xhr.send(form);


        /*
        $.post( "/ajax.php?action=add_found_item", $( "#lost_form" ).serialize() ).done(function( data ) {;
            reply = JSON.parse(data);
            if(reply.status == "ok"){
                $( "#lost_form" ).trigger("reset");
                $("#alertmessage").collapse('hide');
                $("#successmessage").html(reply.message).collapse('show');
            }else{
                $("#successmessage").collapse('hide');
                $("#alertmessage").html(reply.message).collapse('show');
            }
        }).fail(function() {
            $("#successmessage").collapse('hide');
            $("#alertmessage").html("error").collapse('show');
        });*/

    }

</script>

<!-- Page level plugin JavaScript-->
<script src="/vendor/chart.js/Chart.min.js"></script>

<!-- Custom scripts for all pages-->
<script src="/js/sb-admin.min.js"></script>

<!-- Demo scripts for this page-->
<script src="/js/demo/chart-area-demo.js"></script>
<script src="/js/demo/chart-bar-demo.js"></script>
<script src="/js/demo/chart-pie-demo.js"></script>


<script>

    $(document).ready( function() {
        $(document).on('change', '.btn-file :file', function() {
            var input = $(this),
                label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
            input.trigger('fileselect', [label]);
        });

        $('.btn-file :file').on('fileselect', function(event, label) {

            var input = $(this).parents('.input-group').find(':text'),
                log = label;

            if( input.length ) {
                input.val(log);
            } else {
                if( log ) alert(log);
            }

        });
        function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $('#img-upload').attr('src', e.target.result);
                }

                reader.readAsDataURL(input.files[0]);
            }
        }

        $("#inputGroupImage").change(function(){
            readURL(this);
        });
    });

</script>