<vn-crud-model
    auto-load="true"
    vn-id="model"
    url="Entries/{{$ctrl.$params.id}}/getBuys"
    data="$ctrl.buys"
    limit="20">
</vn-crud-model>
<vn-watcher
    vn-id="watcher"
    data="$ctrl.buys">
</vn-watcher>
<vn-data-viewer
    model="model"
    class="vn-w-xl">
    <vn-card class="vn-pa-lg">
        <div class="tableWrapper">
            <vn-horizontal class="header">
                <vn-tool-bar class="vn-mb-md">
                    <vn-button
                        disabled="$ctrl.selectedBuys() == 0"
                        ng-click="deleteBuys.show()"
                        vn-tooltip="Delete buy(s)"
                        icon="delete">
                    </vn-button>
                </vn-tool-bar>
                <vn-one class="taxes" ng-if="$ctrl.sales.length > 0">
                    <p><vn-label translate>Subtotal</vn-label> {{$ctrl.ticket.totalWithoutVat | currency: 'EUR':2}}</p>
                    <p><vn-label translate>VAT</vn-label> {{$ctrl.ticket.totalWithVat - $ctrl.ticket.totalWithoutVat | currency: 'EUR':2}}</p>
                    <p><vn-label><strong>Total</strong></vn-label> <strong>{{$ctrl.ticket.totalWithVat | currency: 'EUR':2}}</strong></p>
                </vn-one>
            </vn-horizontal>
            <table class="vn-table">
                <thead>
                    <tr>
                        <th shrink>
                            <vn-multi-check model="model" on-change="$ctrl.resetChanges()">
                            </vn-multi-check>
                        </th>
                        <th translate center>Item</th>
                        <th translate center>Quantity</th>
                        <th translate center>Package</th>
                        <th translate>Stickers</th>
                        <th translate>Weight</th>
                        <th translate>Packing</th>
                        <th translate>Grouping</th>
                        <th translate>Buying value</th>
                        <th translate expand>Grouping price</th>
                        <th translate expand>Packing price</th>
                        <th translate>Import</th>
                    </tr>
                </thead>
                <tbody ng-repeat="buy in $ctrl.buys">
                    <tr>
                        <td shrink>
                            <vn-check tabindex="-1" ng-model="buy.checked">
                            </vn-check>
                        </td>
                        <td shrink>
                            <span
                                ng-if="buy.id"
                                ng-click="itemDescriptor.show($event, buy.item.id)"
                                class="link">
                                {{::buy.item.id}}
                            </span>
                            <vn-autocomplete ng-if="!buy.id" class="dense"
                                vn-focus
                                url="Items/withName"
                                ng-model="buy.itemFk"
                                show-field="id"
                                value-field="id"
                                search-function="$ctrl.itemSearchFunc($search)"
                                on-change="$ctrl.saveBuy(buy)"
                                order="id DESC"
                                tabindex="1">
                                <tpl-item>
                                    <div>{{::name}}</div>
                                    <div class="text-secondary text-caption">#{{::id}}</div>
                                </tpl-item>
                            </vn-autocomplete>
                        </td>
                        <td>
                            <vn-input-number class="dense"
                                title="{{::buy.quantity | dashIfEmpty}}"
                                ng-model="buy.quantity"
                                on-change="$ctrl.saveBuy(buy)">
                            </vn-input-number>
                        </td>
                        <td center>
                            <vn-autocomplete
                                vn-one
                                title="{{::buy.packagingFk | dashIfEmpty}}"
                                url="Packagings"
                                show-field="id"
                                value-field="id"
                                where="{freightItemFk: true}"
                                ng-model="buy.packagingFk"
                                on-change="$ctrl.saveBuy(buy)">
                            </vn-autocomplete>
                        </td>
                        <td>
                            <vn-input-number class="dense"
                                title="{{::buy.stickers | dashIfEmpty}}"
                                ng-model="buy.stickers"
                                on-change="$ctrl.saveBuy(buy)">
                            </vn-input-number>
                        </td>
                        <td>
                            <vn-input-number class="dense"
                                title="{{::buy.weight | dashIfEmpty}}"
                                ng-model="buy.weight"
                                on-change="$ctrl.saveBuy(buy)">
                            </vn-input-number>
                        </td>
                        <td>
                            <vn-input-number
                                title="{{::buy.packing | dashIfEmpty}}"
                                ng-model="buy.packing"
                                on-change="$ctrl.saveBuy(buy)">
                                <append>
                                    <vn-icon
                                        pointer
                                        ng-show="buy.groupingMode == '2'"
                                        ng-click="$ctrl.toggleGroupingMode(buy, 'packing')"
                                        icon="lock">
                                    </vn-icon>
                                    <vn-icon
                                        pointer
                                        ng-show="buy.groupingMode != '2'"
                                        ng-click="$ctrl.toggleGroupingMode(buy, 'packing')"
                                        icon="lock_open">
                                    </vn-icon>
                                </append>
                            </vn-input-number>
                        </td>
                        <td>
                            <vn-input-number
                                title="{{::buy.grouping | dashIfEmpty}}"
                                ng-model="buy.grouping"
                                on-change="$ctrl.saveBuy(buy)">
                                <append>
                                    <vn-icon
                                        pointer
                                        ng-show="buy.groupingMode == '1'"
                                        ng-click="$ctrl.toggleGroupingMode(buy, 'grouping')"
                                        icon="lock">
                                    </vn-icon>
                                    <vn-icon
                                        pointer
                                        ng-show="buy.groupingMode != '1'"
                                        ng-click="$ctrl.toggleGroupingMode(buy, 'grouping')"
                                        icon="lock_open">
                                    </vn-icon>
                                </append>
                            </vn-input-number>
                        </td>
                        <td>
                            <vn-input-number class="dense"
                                title="{{::buy.buyingValue | dashIfEmpty}}"
                                ng-model="buy.buyingValue"
                                on-change="$ctrl.saveBuy(buy)">
                            </vn-input-number>
                        </td>
                        <td>
                            <vn-input-number class="dense"
                                title="{{::buy.price2 | dashIfEmpty}}"
                                ng-model="buy.price2"
                                on-change="$ctrl.saveBuy(buy)">
                            </vn-input-number>
                        </td>
                        <td>
                            <vn-input-number class="dense"
                                title="{{::buy.price3 | dashIfEmpty}}"
                                ng-model="buy.price3"
                                on-change="$ctrl.saveBuy(buy)">
                            </vn-input-number>
                        </td>
                        <td>
                            <span
                                ng-if="buy.quantity != null && buy.buyingValue != null"
                                title="{{buy.quantity * buy.buyingValue | currency: 'EUR':2}}">
                                {{buy.quantity * buy.buyingValue | currency: 'EUR':2}}
                            </span>
                        </td>
                    </tr>
                    <tr class="dark-row">
                        <td shrink>
                        </td>
                        <td shrink>
                            <span translate-attr="{title: 'Item type'}">
                                {{::buy.item.itemType.code}}
                            </span>
                        </td>
                        <td number shrink>
                            <span translate-attr="{title: 'Item size'}">
                                {{::buy.item.size}}
                            </span>
                        </td>
                        <td center>
                            <span translate-attr="{title: 'Minimum price'}">
                                {{::buy.item.minPrice | currency: 'EUR':2}}
                            </span>
                        </td>
                        <td vn-fetched-tags colspan="8">
                            <div>
                                <vn-one title="{{::buy.item.concept}}">{{::buy.item.name}}</vn-one>
                                <vn-one ng-if="::buy.item.subName">
                                    <h3 title="{{::buy.item.subName}}">{{::buy.item.subName}}</h3>
                                </vn-one>
                            </div>
                            <vn-fetched-tags
                                max-length="6"
                                item="::buy.item"
                                tabindex="-1">
                            </vn-fetched-tags>
                        </td>
                    </tr>
                    <tr><td></td></tr>
                </tbody>
            </table>
        </div>
    </vn-card>
</vn-data-viewer>
<div fixed-bottom-right>
    <vn-vertical style="align-items: center;">
        <a ui-sref="entry.card.buy.import" >
            <vn-button class="round md vn-mb-sm"
                icon="publish"
                vn-tooltip="Import buys"
                tooltip-position="left">
            </vn-button>
        </a>
    </vn-vertical>
</div>
<vn-item-descriptor-popover
    vn-id="item-descriptor"
    warehouse-fk="$ctrl.vnConfig.warehouseFk">
</vn-item-descriptor-popover>
<vn-confirm
    vn-id="delete-buys"
    question="You are going to delete buy(s) from this entry"
    message="Continue anyway?"
    on-accept="$ctrl.deleteBuys()">
</vn-confirm>