{% extends '@RxConnexion/Admin/content.html.twig' %}
{% block content %}
    <form method="POST" action="{{ path('inventory_form', {action: action, id: id}) }}">
        <div class="row max-width-initial">
            <div class="large-6 columns">
                <h5>Basic information</h5>
                <p class="form-hint-text">Enter basic information below</p>
                <div class="row">
                    <div class="large-4 large-text-right columns">
                        {{ form_label(form.branch_description) }}
                    </div>
                    <div class="large-8 columns">
                        {{ form_widget(form.branch_description) }}
                        {{ form_widget(form.healthFacility) }}
                    </div>
                </div>
                <div class="row hide">
                    <div class="large-4 large-text-right columns">
                        {{ form_label(form.location_description) }}
                    </div>
                    <div class="large-8 columns">
                        {{ form_widget(form.location_description) }}
                        {{ form_widget(form.location) }}
                    </div>
                </div>
                <div class="row">
                    <div class="large-4 large-text-right columns">
                        {{ form_label(form.item_description) }}
                    </div>
                    <div class="large-8 columns">
                        {{ form_widget(form.item_description) }}
                        {{ form_widget(form.item) }}
                    </div>
                </div>
                <div class="row">
                    <div class="large-4 large-text-right columns">
                        {{ form_label(form.status) }}
                    </div>
                    <div class="large-8 columns">
                        {{ form_widget(form.status) }}
                    </div>
                </div>
                <div class="row">
                    <div class="large-4 large-text-right columns">
                        {{ form_label(form.quantity) }}
                    </div>
                    <div class="large-8 columns">
                        {{ form_widget(form.quantity) }}
                    </div>
                </div>
                {% if action == 'u' and (userData.type == 'Super Admin' or 'Admin Items Ave Cost' in userData.accesses) %}
                    <div class="row">
                        <div class="large-4 large-text-right columns">
                            <label for="item_ave_cost" class="middle">Ave Cost</label>
                        </div>
                        <div class="large-8 columns">
                            <input type="text" readonly id="item_ave_cost"
                                   value="{{ form.vars.value.item.aveCostByHealthFacility(form.vars.value.healthFacility) }}">
                        </div>
                    </div>
                {% endif %}
            </div>
            {% if action == 'u' and form.vars.value.item.isItemised %}
                <div class="large-6 columns">
                    <h5>Serial No/s</h5>
                    <p class="form-hint-text">See information of serial no/s below</p>
                    <select id="serial_no_datalist_filter-status">
                        <option value="">All</option>
                        <option value="Available" selected>Available</option>
                        <option value="Sold">Sold</option>
                        <option value="Transferred">Transferred</option>
                        <option value="Trial">Trial</option>
                    </select>
                    <table id="serial_no_datalist" width="100%">
                        <thead>
                        <tr>
                            <td>Serial No</td>
                            <td>Cost</td>
                            <td>Status</td>
                            <td></td>
                        </tr>
                        </thead>
                        <tbody>
                        {% for serialNo in form.vars.value.inventorySerialNos %}
                            <tr>
                                <td>{{ serialNo.serialNo }}</td>
                                <td>{{ serialNo.cost }}</td>
                                <td>{{ serialNo.status }}</td>
                                <td></td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% endif %}
            <div class="small-12 columns">
                <hr class="max-width-initial">
                <div class="button-container">
                    <button type="submit" class="button just-continue" name="action" value="{{ action }}"
                            data-message="Are you sure you want to save this information?"
                    >Save
                    </button>
                    {% if action == 'u' and (userData.type == 'Super Admin' or 'Admin Inventory Delete' in userData.accesses) %}
                        <button type="submit" class="ask-continue button alert" name="action" value="d"
                                data-message="Are you sure you want to delete this record?"
                        >Delete
                        </button>
                    {% endif %}
                    <a href="{{ path('inventory') }}" class="button secondary">Back</a>
                </div>
            </div>
        </div>
        <hr>
        {% if action == 'u' and inventory and (userData.type == 'Super Admin' or 'Admin Inventory History' in userData.accesses) %}
            <div class="row">
                <div class="columns small-12">
                    <h5>History</h5>
                    <table id="history">
                        <thead>
                        <tr>
                            <td>Date</td>
                            <td>Purchased</td>
                            <td>Sold</td>
                            <td>Transferred</td>
                            <td>Received</td>
                            <td>Quantity</td>
                        </tr>
                        </thead>
                        <tbody data-initialquantity="{{ inventory.initialQuantity }}">
                        {% set qty = inventory.initialQuantity %}
                        {% for inventoryMovement in inventory.inventoryMovements if inventoryMovement.createdAt >= historyStartDate %}
                            {% if inventoryMovement.purchaseOrderBreakdownItemDelivery %}
                                {% set qty = qty + inventoryMovement.quantity %}
                                <tr>
                                    <td>{{ inventoryMovement.createdAt|date('m/d/Y h:i a') }}</td>
                                    <td><a href="{{ path('material_requisition_purchase_order_breakdown_item_delivery_view', {purchaseOrderBreakdown_id: inventoryMovement.purchaseOrderBreakdownItemDelivery.purchaseOrderBreakdownItem.purchaseOrderBreakdown.idEncoded}) }}" target="_blank">{{ inventoryMovement.quantity|number_format(0) }}</a></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td>{{ qty }}</td>
                                </tr>
                            {% elseif inventoryMovement.patientBillingInventory %}
                                {% set qty = qty + inventoryMovement.quantity %}
                                <tr>
                                    <td>{{ inventoryMovement.createdAt|date('m/d/Y h:i a') }}</td>
                                    <td></td>
                                    <td><a href="{{ path('patient_detail', {patient_id: inventoryMovement.patientBillingInventory.patientBilling.patient.idEncoded, tab: 'b', invoiceNo: inventoryMovement.patientBillingInventory.patientBilling.invoiceNo}) }}" target="_blank">{{ inventoryMovement.quantity * -1 }}</a></td>
                                    <td></td>
                                    <td></td>
                                    <td>{{ qty }}</td>
                                </tr>
                            {% elseif inventoryMovement.transferInventoryDeliveryItem %}
                                {% set qty = qty + inventoryMovement.quantity %}
                                <tr>
                                    <td>{{ inventoryMovement.createdAt|date('m/d/Y h:i a') }}</td>
                                    <td></td>
                                    <td></td>
                                    <td><a href="{{ path('transfer_inventory_item', {transferInventory_id: inventoryMovement.transferInventoryDeliveryItem.transferInventoryItem.transferInventory.idEncoded}) }}" target="_blank">{{ inventoryMovement.quantity * -1 }}</a></td>
                                    <td></td>
                                    <td>{{ qty }}</td>
                                </tr>
                            {% elseif inventoryMovement.transferInventoryItemDelivery %}
                                {% set qty = qty + inventoryMovement.quantity %}
                                <tr>
                                    <td>{{ inventoryMovement.createdAt|date('m/d/Y h:i a') }}</td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td><a href="{{ path('transfer_inventory_item', {transferInventory_id: inventoryMovement.transferInventoryItemDelivery.transferInventoryItem.transferInventory.idEncoded}) }}" target="_blank">{{ inventoryMovement.quantity|number_format(0) }}</a></td>
                                    <td>{{ qty }}</td>
                                </tr>
                            {% endif %}
                        {% endfor %}
                        </tbody>
                        {#<tfoot>
                        <tr>
                            <td colspan="4" class="text-right">Quantity</td>
                            <td>{{ qty }}</td>
                        </tr>
                        </tfoot>#}
                    </table>
                    <br>
                </div>
            </div>
        {% endif %}
        {{ form_widget(form.action) }}
        {{ form_widget(form.id) }}
        {{ form_widget(form._token) }}
    </form>
    {% include '@RxConnexion/Admin/js.html.twig' %}
    <script language="javascript">
        //<![CDATA[
        jQ(document).ready(function () {
            inventory_form.settings.branch_autocomplete_url = '{{ path('health_facility_autocomplete') }}';
            inventory_form.settings.location_autocomplete_url = '{{ path('location_autocomplete') }}';
            inventory_form.settings.item_autocomplete_url = '{{ path('item_autocomplete') }}';
            inventory_form.init();
        });
        //]]>
    </script>
{% endblock %}