{#
/**
 * This file is part of FacturaScripts
 * Copyright (C) 2018-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */
#}
{% set currentView = fsc.getCurrentView() %}
{% set firstView = fsc.views | first %}

<script>
    function refundAll() {
        const refundableQty = document.getElementsByClassName("refundable");
        const inputToBeRefunded = document.getElementsByClassName("to_refund");
        for (let i = 0; i < inputToBeRefunded.length; i++) {
            inputToBeRefunded.item(i).value = refundableQty.item(i).value;
        }
        return false;
    }

    function refundNone() {
        const inputToBeRefunded = document.getElementsByClassName("to_refund");
        for (let i = 0; i < inputToBeRefunded.length; i++) {
            inputToBeRefunded.item(i).value = 0;
        }
        return false;
    }
</script>

{% if currentView.count > 0 %}
    <div class="table-responsive">
        <table class="table table-hover mb-5">
            <thead>
            <tr>
                <th>{{ trans('rectified-invoice') }}</th>
                <th>{{ trans('number2') }}</th>
                <th>{{ trans('observations') }}</th>
                <th class="text-end">{{ trans('total') }}</th>
                <th class="text-end">{{ trans('date') }}</th>
            </tr>
            </thead>
            <tbody>
            {% for rectified in currentView.cursor %}
                <tr class="table-danger clickableRow" data-href="{{ rectified.url() }}">
                    <td>{{ rectified.codigo }}</td>
                    <td>{{ rectified.numero2 | raw }}</td>
                    <td>{{ rectified.observaciones | raw }}</td>
                    <td class="text-end">{{ money(rectified.total) }}</td>
                    <td class="text-end">{{ rectified.fecha }}</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
{% endif %}

<form action="{{ firstView.model.url() }}" method="post" onsubmit="animateSpinner('add')">
    {{ formToken() }}
    <input type="hidden" name="action" value="new-refund"/>
    <input type="hidden" name="activetab" value="{{ currentView.getViewName() }}"/>
    <input type="hidden" name="idfactura" value="{{ firstView.model.primaryColumnValue() }}"/>
    <div class="container-fluid">
        <div class="row">
            <div class="col">
                <div class="card border-warning shadow mb-4">
                    <div class="card-header bg-warning">
                        <i class="fa-solid fa-share me-1" aria-hidden="true"></i> {{ trans('new-refund') }}
                    </div>
                    <div class="card-body">
                        <p>{{ trans('rectified-invoice-p') }}</p>
                        <div class="row g-2 align-items-end">
                            <div class="col-sm-6 col-md-4 col-lg">
                                <div class="mb-3">
                                    {{ trans('serie') }}
                                    <select name="codserie" class="form-select">
                                        {% set series = fsc.series('R') %}
                                        {% if series is empty %}
                                            {% set series = fsc.series() %}
                                        {% endif %}
                                        {% for serie in series %}
                                            {% if serie.codserie == settings('default','codserierec','R') %}
                                                <option value="{{ serie.codserie }}" selected>
                                                    {{ serie.descripcion }}
                                                </option>
                                            {% else %}
                                                <option value="{{ serie.codserie }}">{{ serie.descripcion }}</option>
                                            {% endif %}
                                        {% endfor %}
                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-6 col-md-4 col-lg">
                                <div class="mb-3">
                                    {{ trans('date') }}
                                    <input type="date" name="fecha" value="{{ task.end | date('Y-m-d') }}"
                                           class="form-control" required>
                                </div>
                            </div>
                            <div class="col-sm-6 col-md-4 col-lg">
                                <div class="mb-3">
                                    {{ trans('status') }}
                                    <select name="idestado" class="form-select">
                                        {% for status in firstView.model.getAvailableStatus() %}
                                            {% if status.idestado is same as(11) %}
                                                <option value="{{ status.idestado }}" selected>
                                                    {{ status.nombre }}
                                                </option>
                                            {% else %}
                                                <option value="{{ status.idestado }}">
                                                    {{ status.nombre }}
                                                </option>
                                            {% endif %}
                                        {% endfor %}
                                    </select>
                                </div>
                            </div>
                            <div class="col-auto">
                                <div class="btn-group mb-3">
                                    <div class="dropdown">
                                        <button class="btn btn-secondary dropdown-toggle" type="button"
                                                data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                            <i class="fa-solid fa-check-square me-1"></i> {{ trans('select') }}
                                        </button>
                                        <div class="dropdown-menu dropdown-menu-end">
                                            <a class="dropdown-item" href="#" onClick="return refundNone();">
                                                <i class="fa-regular fa-square fa-fw me-1"></i> {{ trans('select-none') }}
                                            </a>
                                            <a class="dropdown-item" href="#" onClick="return refundAll();">
                                                <i class="fa-solid fa-square fa-fw me-1"></i> {{ trans('select-all') }}
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-auto">
                                <div class="btn-group mb-3">
                                    <button type="submit" class="btn btn-spin-action btn-warning">
                                        <i class="fa-solid fa-save me-1" aria-hidden="true"></i>
                                        {{ trans('save') }}
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-sm table-hover mb-0">
                            <thead>
                            <tr>
                                <th>{{ trans('description') }}</th>
                                <th class="text-end">{{ trans('quantity') }}</th>
                                <th class="text-end">{{ trans('to-refund') }}</th>
                                <th class="text-end">{{ trans('refund-amount') }}</th>
                            </tr>
                            </thead>
                            <tbody>
                            {% for line in firstView.model.getLines() %}
                                {% set refunded = line.refundedQuantity() %}
                                <tr>
                                    <td class="align-middle">
                                        <a href="{{ line.getProducto().url() }}">{{ line.referencia }}</a>
                                        {{ line.descripcion | raw }}
                                    </td>
                                    <td class="align-middle text-end">
                                        {{ number(line.cantidad) }}
                                    </td>
                                    <td class="table-warning">
                                        <input type="hidden" value="{{ line.cantidad }}" class="refundable">
                                        {% if line.cantidad > 0 %}
                                            <input type="number" name="refund_{{ line.primaryColumnValue() }}"
                                                   value="0" min="0" max="{{ line.cantidad - refunded }}" step="any"
                                                   class="form-control text-end to_refund" autocomplete="off"/>
                                        {% else %}
                                            <input type="number" name="refund_{{ line.primaryColumnValue() }}"
                                                   value="0" step="any" class="form-control text-end to_refund"
                                                   autocomplete="off"/>
                                        {% endif %}
                                    </td>
                                    <td class="align-middle table-warning text-end">
                                        {% if refunded == 0 %}
                                            -
                                        {% else %}
                                            {{ number(refunded) }}
                                        {% endif %}
                                    </td>
                                </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <textarea name="observaciones" class="form-control"
                                      placeholder="{{ trans('observations') }}"></textarea>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
