<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Item</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <style>
        .price-box {
            border: 1px solid #ccc;
            padding: 10px;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            background-color: #f8f9fa;
        }
        .price-box img {
            width: 30px;
            margin-right: 10px;
        }
        .price-box span {
            font-size: 1.2em;
            font-weight: bold;
        }
        .price-box small {
            display: block;
            font-size: 0.8em;
            color: #888;
        }
        .card {
            margin-bottom: 15px;
        }
        .card-header img {
            width: 45px;
            margin-right: 10px;
        }
        .parts-tree {
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
        }
        .parts-tree > li {
            margin-right: 10px;
        }
        .part-item {
            display: flex;
            flex-direction: row;
            align-items: center;
            background-color: #f8f9fa;
            padding: 5px;
            border-radius: 5px;
            margin-bottom: 5px;
        }
        .part-item img {
            width: 30px;
            margin-right: 5px;
        }
        .search-result-item {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }
        .search-result-item img {
            width: 30px;
            margin-right: 10px;
        }
        .search-result-item button {
            margin-left: auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Edit {{ item.details.name }}</h1>
        <div class="form-group">
            <label for="main-item">Main Item</label>
            <div class="input-group">
                <div class="input-group-prepend">
                    <span class="input-group-text">
                        <img src="{{ item.details.image512pxLink }}" alt="{{ item.details.name }}" style="width: 50px;">
                    </span>
                </div>
                <input type="text" class="form-control" id="main-item" name="main-item" value="{{ item.details.name }}" readonly>
            </div>
        </div>
        <div class="price-box">
            <img src="{{ ruble_image.image512pxLink }}" alt="RUB">
            <div>
                <span>{{ item.details.basePrice }} RUB</span>
                <small>Base Price (for reference)</small>
            </div>
        </div>
        {% if item.parts|length > 0 %}
        <h2>
            <button class="btn btn-outline-primary-custom" type="button" data-toggle="collapse" data-target="#parts-{{ item._id }}" aria-expanded="false" aria-controls="parts-{{ item._id }}">
              Parts
            </button>
        </h2>
        <div id="parts-{{ item._id }}" class="collapse">
            <ul class="parts-tree">
                {% include 'parts.html' with context %}
            </ul>
        </div>
        {% endif %}
        <h2>Barter Scheme</h2>
        <form method="POST" id="barter-form">
            <div id="barter-scheme">
                {% for scheme in item.barter_scheme %}
                <div class="form-group barter-item" data-index="{{ loop.index0 }}">
                    {% for req in scheme %}
                    <div class="input-group">
                        <div class="input-group-prepend">
                            <span class="input-group-text">
                                <img src="{{ req.details.image512pxLink }}" alt="{{ req.details.name }}" style="width: 30px;" id="barter_img_{{ loop.index0 }}">
                            </span>
                        </div>
                        <input type="text" class="form-control barter_tpl" id="barter_tpl_{{ loop.index0 }}" name="barter_tpl" value="{{ req._tpl }}" required>
                        <input type="number" class="form-control barter_count" id="barter_count_{{ loop.index0 }}" name="barter_count" value="{{ req.count }}" required>
                        <div class="input-group-append">
                            <button class="btn btn-danger remove-barter-item" type="button">Remove</button>
                        </div>
                    </div>
                    {% endfor %}
                </div>
                {% endfor %}
            </div>
            <button type="button" class="btn btn-primary" id="add-barter-item">Add Barter Item</button>
            <button type="submit" class="btn btn-success">Save</button>
            <a href="{{ url_for('index') }}" class="btn btn-secondary">Cancel</a>
        </form>
        {% if item.quest_requirement %}
        <h2>Quest Requirement</h2>
        <p>
            <strong>Quest Requirement:</strong> 
            {{ item.quest_requirement.quest }} 
            {% if item.quest_requirement.type == 'success' %}
            <i class="fas fa-check-circle text-success"></i>
            {% elif item.quest_requirement.type == 'fail' %}
            <i class="fas fa-times-circle text-danger"></i>
            {% elif item.quest_requirement.type == 'started' %}
            <i class="fas fa-question-circle text-primary"></i>
            {% endif %}
        </p>
        {% endif %}
        
        <!-- Add the search form and results section -->
        <h2>Search Items</h2>
        <div class="form-group">
            <input type="text" class="form-control" id="item-search" placeholder="Search for items...">
        </div>
        <div id="search-results" class="mt-3"></div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <script>
        $(document).ready(function() {
            let barterIndex = {{ item.barter_scheme|length }};
            
            $('#add-barter-item').click(function() {
                const newItem = `
                <div class="form-group barter-item" data-index="${barterIndex}">
                    <div class="input-group">
                        <div class="input-group-prepend">
                            <span class="input-group-text">
                                <img src="" alt="Barter Item" style="width: 30px;" id="barter_img_${barterIndex}">
                            </span>
                        </div>
                        <input type="text" class="form-control barter_tpl" id="barter_tpl_${barterIndex}" name="barter_tpl" required>
                        <input type="number" class="form-control barter_count" id="barter_count_${barterIndex}" name="barter_count" required>
                        <div class="input-group-append">
                            <button class="btn btn-danger remove-barter-item" type="button">Remove</button>
                        </div>
                    </div>`;
                $('#barter-scheme').append(newItem);
                barterIndex++;
            });

            $(document).on('click', '.remove-barter-item', function() {
                if ($('.barter-item').length > 1) {
                    $(this).closest('.barter-item').remove();
                } else {
                    alert("You must have at least one barter item.");
                }
            });

            $(document).on('change', '.barter_tpl', function() {
                const index = $(this).closest('.barter-item').data('index');
                const tpl = $(this).val();
                $.get(`/item_image/${tpl}`, function(data) {
                    $(`#barter_img_${index}`).attr('src', data.image512pxLink);
                });
            });

            $('#item-search').on('input', function() {
                const query = $(this).val();
                if (query.length > 2) {
                    $.get('/search_items', { query: query }, function(data) {
                        let resultsHtml = '';
                        data.forEach(item => {
                            resultsHtml += `
                            <div class="search-result-item" data-id="${item.id}" data-name="${item.name}" data-img="${item.gridImageLink}">
                                <img src="${item.gridImageLink}" alt="${item.name}" style="width: 30px;">
                                <span>${item.name}</span>
                                <button class="btn btn-primary btn-sm add-search-item" type="button">Add</button>
                            </div>`;
                        });
                        $('#search-results').html(resultsHtml);
                    });
                } else {
                    $('#search-results').empty();
                }
            });

            $(document).on('click', '.add-search-item', function() {
                const itemId = $(this).closest('.search-result-item').data('id');
                const itemName = $(this).closest('.search-result-item').data('name');
                const itemImg = $(this).closest('.search-result-item').data('img');
                
                const newItem = `
                <div class="form-group barter-item" data-index="${barterIndex}">
                    <div class="input-group">
                        <div class="input-group-prepend">
                            <span class="input-group-text">
                                <img src="${itemImg}" alt="${itemName}" style="width: 30px;" id="barter_img_${barterIndex}">
                            </span>
                        </div>
                        <input type="text" class="form-control barter_tpl" id="barter_tpl_${barterIndex}" name="barter_tpl" value="${itemId}" required>
                        <input type="number" class="form-control barter_count" id="barter_count_${barterIndex}" name="barter_count" value="1" required>
                        <div class="input-group-append">
                            <button class="btn btn-danger remove-barter-item" type="button">Remove</button>
                        </div>
                    </div>
                </div>`;
                $('#barter-scheme').append(newItem);
                barterIndex++;
            });
        });
    </script>
</body>
</html>
