<!DOCTYPE html>
<html>

<head>
    <!-- Meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Title -->
    <title> Inventory </title>
    <!-- CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
    <link rel="stylesheet" href="/styles.css">
</head>

<body>
    
    <div class="container">

        <h1 class="text-center">Inventory</h1>

        <% if (message) { %>
            <p class="text-center"><%- message %></p>
        <% } %>

        <!-- Add form -->
        <form class="form-group row" action="/create" method="post">
            <div class="col-sm-10">
                <input type="text" class="form-control" name="item_name" id="item_name"  placeholder="Add an item">
            </div>
            <div class="col-sm">
                <button type="submit" class="btn btn-primary btn-block">Add</button>
            </div>
        </form>

        <!-- Export -->
        <div class="container text-center">
            <button type="button" onclick="exportCSV()" class="btn btn-sm btn-success">Export CSV</button>
        </div>

        <!-- Display in a table format -->
        <div class="container">
            
            <% if (items) {
                var rows = '';
                for (var i = 0; i < items.length; i++) {
                    rows += '<form class="form-group inventory-item row" action="/edit" method="post"><div class="col-sm-1">';
                    rows += '<input type="text" readonly class="form-control-plaintext id" name="id" value="' + items[i].id + '">';
                    rows += '</div><div class="input-group col">';
                    rows += '<input type="text" readonly class="form-control-plaintext form-display-editable item" name="item"';
                    rows += 'value="' + items[i].item + '">';
                    rows += '<div class="col-3 select-div"><select disabled class="custom-select stock" name="stock">';

                    // console.log(items[i].stock);
                    // console.log(typeof items[i].stock);

                    for (j = 1; j < 11; j++) {
                        rows += '<option value=' + j + '';
                        if (items[i].stock == j) {
                            rows += ' selected';
                        }
                        rows += '>' + j + '</option>';
                    }
                    rows += '</select></div>';
                    rows += '<button type="button" class="btn btn-sm btn-primary btn-edit">Edit</button>';
                    rows += '<button name="type" value="delete" class="btn btn-sm btn-danger">Delete</button>';
                    rows += '</div></form>';
                }
                %>
                
                <%- rows %>

            <% } %>

            <!-- <h3> static example of item </h3>

            <form class="form-group inventory-item row" action="/edit" method="post">
                <div class="col-sm-1">
                    <input type="text" readonly class="form-control-plaintext id" name="id" value="f">
                </div>
                <div class="input-group col">
                    <input type="text" readonly class="form-control-plaintext form-display-editable item" name="item" value="f">
                    <div class="col-3 select-div">
                        <select disabled class="custom-select stock" name="stock">
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                        </select>
                    </div>
                    <button type="button" class="btn btn-sm btn-primary btn-edit">Edit</button>
                    <button name="type" value="delete" class="btn btn-sm btn-danger">Delete</button>
                </div>
            </form> -->

        </div>
        
    </div>
    
    <!-- Scripts -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <script src="./resources/script.js""></script>

</body>

</html>