{% extends 'core/base.html' %}
{% load static %}

{% block title %}Reduce Stock - SwiftPOS{% endblock %}

{% block topbar_title %}
Reduce Stock
{% endblock %}

{% block content %}
<div class="container-fluid px-0">

    <!-- Toast Messages -->
    <div class="swiftpos-toast-container">
        {% if messages %}
            {% for message in messages %}
                <div class="swiftpos-toast swiftpos-toast-{{ message.tags }}">
                    <div class="toast-icon">
                        {% if message.tags == 'success' %}
                            <i class="fas fa-check-circle"></i>
                        {% elif message.tags == 'error' or message.tags == 'danger' %}
                            <i class="fas fa-exclamation-circle"></i>
                        {% elif message.tags == 'warning' %}
                            <i class="fas fa-exclamation-triangle"></i>
                        {% else %}
                            <i class="fas fa-info-circle"></i>
                        {% endif %}
                    </div>
                    <div class="toast-text">
                        {{ message }}
                    </div>
                </div>
            {% endfor %}
        {% endif %}
    </div>

    <div class="d-flex flex-wrap justify-content-between align-items-center mb-3 gap-2">
        <div>
            <h5 class="mb-0">Reduce Stock</h5>
            <div class="text-muted small">
                {{ product.name }} (SKU: {{ product.sku }})
            </div>
        </div>
        <a href="{% url 'inventory:product_detail' product.id %}"
           class="btn btn-outline-secondary btn-sm d-flex align-items-center gap-2">
            <i class="fas fa-arrow-left"></i> Back to product
        </a>
    </div>

    <div class="row g-3">
        <div class="col-lg-4">
            <div class="card mb-3">
                <div class="card-body">
                    <div class="fw-semibold small mb-2">Current stock</div>
                    <div class="h4 mb-0">{{ product.stock_quantity }}</div>
                    <div class="text-muted small">
                        Minimum: {{ product.minimum_stock }}
                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-8">
            <div class="card">
                <div class="card-header">
                    <h6 class="card-title mb-0 d-flex align-items-center gap-2">
                        <i class="fas fa-minus-circle"></i> Reduction details
                    </h6>
                </div>
                <div class="card-body">
                    <form method="post">
                        {% csrf_token %}

                        <div class="row g-3">
                            <div class="col-md-12 mb-2">
                                <label for="branch_id" class="form-label small fw-semibold text-danger">
                                    <i class="fas fa-store"></i> Target Branch / Location
                                </label>
                                <select name="branch_id" id="branch_id" class="form-select" required>
                                    <option value="">-- Select Branch to Reduce Stock From --</option>
                                    {% for br in branches %}
                                        <option value="{{ br.id }}">{{ br.name }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>

                        <div class="row g-3">
                            <div class="col-md-6">
                                <label for="quantity" class="form-label small fw-semibold">
                                    Quantity to Reduce
                                </label>
                                <input type="number"
                                       name="quantity"
                                       id="quantity"
                                       class="form-control"
                                       placeholder="Enter quantity"
                                       min="1"
                                       max="{{ product.stock_quantity }}"
                                       required>
                            </div>
                            <div class="col-md-6">
                                <label for="reference_number" class="form-label small fw-semibold">
                                    Reference Number (optional)
                                </label>
                                <input type="text"
                                       name="reference_number"
                                       id="reference_number"
                                       class="form-control"
                                       placeholder="Report or reference number">
                            </div>
                        </div>

                        <div class="mt-3">
                            <label for="reason" class="form-label small fw-semibold">
                                Reason / Note
                            </label>
                            <textarea name="reason"
                                      id="reason"
                                      rows="3"
                                      class="form-control"
                                      placeholder="Why is this stock being reduced? (damaged, expired, correction, etc.)"
                                      required></textarea>
                        </div>

                        <div class="mt-4 d-flex justify-content-end gap-2">
                            <a href="{% url 'inventory:product_detail' product.id %}"
                               class="btn btn-outline-secondary btn-sm">
                                Cancel
                            </a>
                            <button type="submit"
                                    class="btn btn-outline-danger btn-sm d-flex align-items-center gap-2">
                                <i class="fas fa-check-circle"></i> Confirm reduction
                            </button>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}