<vn-auto-search
    model="model">
</vn-auto-search>
<vn-card>
    <smart-table 
        model="model" 
        view-config-id="itemsIndex" 
        options="$ctrl.smartTableOptions" 
        expr-builder="$ctrl.exprBuilder(param, value)">
        <slot-table>
            <table>
                <thead>
                    <tr>
                        <th shrink></th>
                        <th field="id">
                            <span translate>Identifier</span>
                        </th>
                        <th field="grouping">
                            <span translate>Grouping</span>
                        </th>
                        <th field="packing">
                            <span translate>Packing</span>
                        </th>
                        <th field="name">
                            <span translate>Description</span>
                        </th>
                        <th field="stems">
                            <span translate>Stems</span>
                        </th>
                        <th field="size">
                            <span translate>Size</span>
                        </th>
                        <th field="typeFk">
                            <span translate>Type</span>
                        </th>
                        <th field="category">
                            <span translate>Category</span>
                        </th>
                        <th field="intrastat">
                            <span translate>Intrastat</span>
                        </th>
                        <th field="origin">
                            <span translate>Origin</span>
                        </th>
                        <th field="buyerFk">
                            <span translate>Buyer</span>
                        </th>
                        <th field="weightByPiece">
                            <span translate>Weight/Piece</span>
                        </th>
                        <th field="stemMultiplier">
                            <span translate>Multiplier</span>
                        </th>
                        <th field="active">
                            <span translate>Active</span>
                        </th>
                        <th field="producer">
                            <span translate>Producer</span>
                        </th>
                        <th field="landed">
                            <span translate>Landed</span>
                        </th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="item in model.data"
                        vn-anchor="::{
                            state: 'item.card.summary', 
                            params: {id: item.id}
                        }">
                        <td>
                            <img 
                                ng-src="{{::$root.imagePath('catalog', '50x50', item.id)}}"
                                zoom-image="{{::$root.imagePath('catalog', '1600x900', item.id)}}"
                                vn-click-stop
                                on-error-src/>
                        </td>
                        <td>
                            <span
                                vn-click-stop="itemDescriptor.show($event, item.id)"
                                class="link">
                                {{::item.id}}
                            </span>
                        </td>
                        <td>{{::item.grouping | dashIfEmpty}}</td>
                        <td>{{::item.packing | dashIfEmpty}}</td>
                        <td vn-fetched-tags>
                            <div>
                                <vn-one title="{{::item.name}}">{{::item.name}}</vn-one>
                                <vn-one ng-if="::item.subName">
                                    <h3 title="{{::item.subName}}">{{::item.subName}}</h3>
                                </vn-one>
                            </div>
                            <vn-fetched-tags
                                max-length="6"
                                item="item"
                                tabindex="-1">
                            </vn-fetched-tags>
                        </td>
                        <td>{{::item.stems}}</td>
                        <td>{{::item.size}}</td>
                        <td title="{{::item.typeName}}">
                            {{::item.typeName}}
                        </td>
                        <td title="{{::item.category}}">
                            {{::item.category}}
                        </td>
                        <td title="{{::item.intrastat}}">
                            {{::item.intrastat}}
                        </td>
                        <td>{{::item.origin}}</td>
                        <td title="{{::item.userName}}">
                            <span 
                                class="link" 
                                vn-click-stop="workerDescriptor.show($event, item.buyerFk)">
                                {{::item.userName}}
                            </span>
                        </td>
                        <td>{{::item.weightByPiece}}</td>
                        <td>{{::item.stemMultiplier}}</td>
                        <td>
                            <vn-check
                                disabled="true"
                                ng-model="::item.isActive">
                            </vn-check> 
                        </td>
                        <td>{{::item.producer | dashIfEmpty}}</td>
                        <td shrink-date>{{::item.landed | date:'dd/MM/yyyy'}}</td> 
                        <td>
                            <vn-horizontal class="buttons">
                                <vn-icon-button
                                    vn-click-stop="clone.show(item.id)"
                                    vn-tooltip="Clone"
                                    icon="icon-clone">
                                </vn-icon-button>
                                <vn-icon-button
                                    vn-click-stop="$ctrl.preview(item)"
                                    vn-tooltip="Preview"
                                    icon="preview">
                                </vn-icon-button>
                            </vn-horizontal>
                        </td>
                    </tr>
                </tbody>
            </table>
        </slot-table>
    </smart-table>
</vn-card>
<a ui-sref="item.create" vn-tooltip="New item" vn-bind="+" fixed-bottom-right>
    <vn-float-button icon="add"></vn-float-button>
</a>
<vn-item-descriptor-popover
    vn-id="item-descriptor"
    warehouse-fk="$ctrl.vnConfig.warehouseFk">
</vn-item-descriptor-popover>
<vn-worker-descriptor-popover 
    vn-id="worker-descriptor">
</vn-worker-descriptor-popover>
<vn-confirm 
    vn-id="clone"
    on-accept="$ctrl.onCloneAccept($data)"
    question="Do you want to clone this item?"
    message="All it's properties will be copied">
</vn-confirm>
<vn-popup vn-id="preview">
    <vn-item-summary
        item="$ctrl.itemSelected">
    </vn-item-summary>
</vn-popup>
<vn-contextmenu 
    vn-id="contextmenu" 
    targets="['smart-table']" 
    model="model"
    expr-builder="$ctrl.exprBuilder(param, value)">
        <slot-menu>
            <vn-item translate
                ng-if="contextmenu.isFilterAllowed()"
                ng-click="contextmenu.filterBySelection()">
                Filter by selection
            </vn-item>
            <vn-item translate 
                ng-if="contextmenu.isFilterAllowed()" 
                ng-click="contextmenu.excludeSelection()">
                Exclude selection
            </vn-item>
            <vn-item translate 
                ng-if="contextmenu.isFilterAllowed()" 
                ng-click="contextmenu.removeFilter()">
                Remove filter
            </vn-item>
            <vn-item translate 
                ng-click="contextmenu.removeAllFilters()">
                Remove all filters
            </vn-item>
        </slot-menu>
</vn-contextmenu>