{% set userData = app.session.get('userData') %}
<form id="form-transferInventoryDelivery" method="POST">
    <h5>{{ page_title }}</h5>
    <div id="flash-messages-content-modal"></div>
    <div class="row max-width-initial">
        <div class="large-6 columns">
            <p class="form-hint-text">Enter basic information below</p>
            <div class="row">
                <div class="large-4 large-text-right columns">
                    {{ form_label(form.drNo) }}
                </div>
                <div class="large-8 columns">
                    <input type="text" readonly="readonly" title="Auto-generated" style="cursor: help" value="{{ (transferInventory.branchFrom.id  == transferInventory.branchTo.id ? '' : (transferInventory.branchFrom.id)) ~ transferInventoryDelivery.drDate.format('Y.m.')|default('') ~ transferInventoryDelivery.drNo|strPad('4', '0')|default('') }}" autocomplete="off">
                    {{ form_widget(form.drNo) }}
                </div>
            </div>
            <div class="row">
                <div class="large-4 large-text-right columns">
                    {{ form_label(form.drDate) }}
                </div>
                <div class="large-8 columns">
                    {{ form_widget(form.drDate) }}
                </div>
            </div>
        </div>
        <div class="large-6 columns">
            <p class="form-hint-text">Enter other details below</p>
            <div class="row">
                <div class="large-12 columns">
                    {{ form_label(form.note) }}
                    {{ form_widget(form.note) }}
                </div>
            </div>
        </div>
        <div class="large-12 columns">
            <p class="form-hint-text">Items to deliver</p>
            <table class="display compact responsive transferInventoryItem-datalist-forDelivery" id="transferInventoryItem-datalist-forDeliveryz" width="100%">
                <thead>
                <tr>
                    <th>Item</th>
                    <th class="text-right">Qty. Requested</th>
                    <th class="text-right">Qty. Delivered</th>
                    <th class="text-right">Remaining Qty. to Deliver</th>
                    <th></th>
                    {#<th class="text-right">Qty. in Stock</th>#}
                    {#<th>Location</th>#}
                    {#<th class="text-right">Qty. to Deliver</th>#}
                </tr>
                </thead>
                <tbody>
                {% set branchId = transferInventory.branchFrom.id %}
                {% set otherForm_index = -1 %}
                {% for tii in transferInventory.transferInventoryItems %}
                    {#{% set tidi = transferInventoryDelivery ? transferInventoryDelivery.transferInventoryDeliveryItemByTranInvItemId(tii.id) : null %}#}
                    {#{% set tidiQty = tidi ? tidi.quantity : 0 %}#}
                    {#{% set qtyForDelivery = tii.quantityForDelivery + tidiQty %}#}
                    {% set qtyForDelivery = tii.quantity - tii.quantityDelivered %}
                    {% set item = tii.item %}
                    {% set itemDesc = item.description %}
                    {#{% set inv = item.firstInventoryByBranchIdAndStatus(branchId, constant('ZapconBundle\\Entity\\InventoryEntity::STATUS_STOCK_ON_HAND')) %}#}
                    {#{% set invQty = inv ? inv.quantity + tidiQty : 0 %}#}
                    {% set invs = item.getInventoriesByBranchIdAndStatus(branchId, constant('RxConnexionBundle\\Entity\\InventoryEntity::STATUS_STOCK_ON_HAND')) %}
                    <tr>
                        <td valign="top">{{ itemDesc }}</td>
                        <td valign="top" class="text-right">{{ tii.quantity|number_format(0, '.', ',') }}</td>
                        <td valign="top" class="text-right">{{ (tii.quantityDelivered)|number_format(0, '.', ',') }}</td> {# - tidiQty#}
                        <td valign="top" class="text-right">{{ qtyForDelivery|number_format(0, '.', ',') }}</td>
                        <td>
                            <table class="inventory-locations-datalist" style="width: 100%;">
                                <thead>
                                    <tr>
                                        <th>Location</th>
                                        <th class="text-right">Stock</th>
                                        <th class="text-right">Qty. to Deliver</th>
                                    </tr>
                                </thead>
                                <tbody>
                                {% for inv in invs %}
                                    {% set otherForm_index = otherForm_index + 1 %}
                                    {% set tidi = transferInventoryDelivery ? transferInventoryDelivery.transferInventoryDeliveryItemByTranInvItemIdAndInvId(tii.id, inv.id) : null %}
                                    <tr>
                                        <td>{{ inv.location.description|default('') }}</td>
                                        <td class="text-right">{{ inv.quantity|default(0) }}</td>
                                        <td class="text-right">
                                            <input type="hidden" name="otherForm[items][{{ otherForm_index }}][id]" value="{{ tidi ? tidi.id : 0 }}" />
                                            <input type="hidden" name="otherForm[items][{{ otherForm_index }}][tii]" value="{{ tii.id }}" />
                                            <input type="hidden" name="otherForm[items][{{ otherForm_index }}][inventoryId]" value="{{ inv.id|default('') }}" />
                                            <input type="hidden" name="otherForm[items][{{ otherForm_index }}][qtyOnStock]" value="{{ inv.quantity|default(0) }}" />
                                            <input type="hidden" name="otherForm[items][{{ otherForm_index }}][qtyForDelivery]" value="{{ qtyForDelivery }}" />
                                            <input type="hidden" name="otherForm[items][{{ otherForm_index }}][qtyDelivered]" value="{{ tii.quantityDelivered }}" />
                                            <input type="hidden" name="otherForm[items][{{ otherForm_index }}][itemDesc]" value="{{ itemDesc }}" />
                                            <input type="text" class="numeric pull-right" name="otherForm[items][{{ otherForm_index }}][qty]" value="{{ tidi ? tidi.quantity|number_format(0, '.', ',') : '' }}"/>
                                        </td>
                                    </tr>
                                {% endfor %}
                                </tbody>
                            </table>
                        </td>
                        {#<td class="text-right">{{ invQty|number_format(0, '.', ',') }}</td>#}
                        {#<td>#}
                            {#<input type="hidden" name="otherForm[items][{{ loop.index0 }}][id]" value="{{ tii.id }}" />#}
                            {#<input type="hidden" name="otherForm[items][{{ loop.index0 }}][inventoryId]" value="{{ inv ? inv.id : null }}" />#}
                            {#<input type="hidden" name="otherForm[items][{{ loop.index0 }}][qtyOnStock]" value="{{ invQty }}" />#}
                            {#<input type="hidden" name="otherForm[items][{{ loop.index0 }}][qtyForDelivery]" value="{{ qtyForDelivery }}" />#}
                            {#<input type="hidden" name="otherForm[items][{{ loop.index0 }}][itemDesc]" value="{{ itemDesc }}" />#}
                            {#<input type="text" name="otherForm[items][{{ loop.index0 }}][qty]" value="{{ tidi ? tidi.quantity|number_format(0, '.', ',') : '' }}" class="pull-right" />#}
                        {#</td>#}
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    <hr>
    <div class="row max-width-initial">
        <div class="small-12 columns no-padding-right">
            <div class="button-container text-right">
                <button type="button" class="button just-continue href-transferInventoryDelivery-action" 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 Transfer Inventory Delivery Delete' in userData.accesses) %}
                    <button type="button" class="ask-continue button alert href-transferInventoryDelivery-action" name="action" value="d"
                            data-message="Are you sure you want to delete this record?"
                    >Delete</button>
                {% endif %}
            </div>
        </div>
    </div>
    {{ form_widget(form.transferInventory) }}
    {{ form_widget(form.action) }}
    {{ form_widget(form.id) }}
    {{ form_widget(form._token) }}
</form>