<vn-crud-model
    vn-id="model"
    url="FixedPrices/filter"
    limit="20"
    data="prices"
    order="itemFk"
    auto-load="false">
</vn-crud-model>
<vn-crud-model
    auto-load="true"
    url="Warehouses"
    data="warehouses"
    order="name">
</vn-crud-model>
<vn-portal slot="topbar">
    <vn-searchbar
        auto-state="false"
        panel="vn-fixed-price-search-panel"
        info="Search prices by item ID or code"
        placeholder="Search fixed prices"
        model="model">
    </vn-searchbar>
</vn-portal>
<div class="vn-w-xl">
    <vn-card>
        <smart-table 
            model="model"
            options="$ctrl.smartTableOptions" 
            expr-builder="$ctrl.exprBuilder(param, value)">
            <slot-table>
                <table>
                    <thead>
                        <tr>
                            <th field="itemFk">
                                <span translate>Item ID</span>
                            </th>
                            <th field="itemName">
                                <span translate>Description</span>
                            </th>
                            <th field="warehouseFk">
                                <span translate>Warehouse</span>
                            </th>
                            <th 
                                field="rate2"
                                vn-tooltip="Price By Unit">
                                <span translate>P.P.U.</span>
                            </th>
                            <th 
                                field="rate3"
                                vn-tooltip="Price By Package">
                                <span translate>P.P.P.</span>
                            </th>
                            <th field="minPrice">
                                <span translate>Min price</span>
                            </th>
                            <th field="started">
                                <span translate>Started</span>
                            </th>
                            <th field="ended">
                                <span translate>Ended</span>
                            </th>
                            <th shrink></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="price in prices">
                            <td shrink-field>
                                <vn-autocomplete
                                    class="dense"
                                    url="Items/withName"
                                    ng-model="price.itemFk"
                                    show-field="name"
                                    value-field="id"
                                    search-function="$ctrl.itemSearchFunc($search)"
                                    on-change="$ctrl.upsertPrice(price)"
                                    order="id DESC"
                                    tabindex="1">
                                    <tpl-item>
                                        <div>{{id}}</div>
                                        <div class="text-caption text-secondary">
                                            {{name}}
                                        </div>
                                    </tpl-item>
                                </vn-autocomplete>
                            </td>
                            <td vn-fetched-tags>
                                <div>
                                    <span
                                        vn-one
                                        ng-if="price.itemFk"
                                        ng-click="itemDescriptor.show($event, price.itemFk)"
                                        class="link">
                                        {{price.name}}
                                    </span>
                                    <vn-one ng-if="price.subName">
                                        <h3 title="{{price.subName}}">{{price.subName}}</h3>
                                    </vn-one>
                                </div>
                                <vn-fetched-tags
                                    max-length="6"
                                    item="price"
                                    tabindex="-1">
                                </vn-fetched-tags>
                            </td>
                            <td shrink-field-expand>
                                <vn-autocomplete
                                    vn-one
                                    ng-model="price.warehouseFk"
                                    data="warehouses"
                                    on-change="$ctrl.upsertPrice(price)"
                                    tabindex="2">
                                </vn-autocomplete>
                            </td>
                            <td shrink-field>
                                <vn-input-number
                                    ng-model="price.rate2"
                                    on-change="$ctrl.upsertPrice(price)"
                                    step="0.01">
                                </vn-input-number>
                            </td>
                            <td shrink-field>
                                <vn-input-number
                                    ng-model="price.rate3"
                                    on-change="$ctrl.upsertPrice(price)"
                                    step="0.01">
                                </vn-input-number>
                            </td>
                            <td shrink-field-expand class="minPrice">
                                <vn-check
                                    vn-one
                                    ng-model="price.hasMinPrice">
                                </vn-check>
                                <vn-input-number
                                    disabled="!price.hasMinPrice"
                                    ng-model="price.minPrice"
                                    on-change="$ctrl.upsertPrice(price)"
                                    step="0.01">
                                </vn-input-number>
                            </td>
                            <td shrink-date>
                                <vn-date-picker
                                    vn-one
                                    ng-model="price.started"
                                    on-change="$ctrl.upsertPrice(price)">
                                </vn-date-picker>
                            </td>
                            <td shrink-date>
                                <vn-date-picker
                                    vn-one
                                    ng-model="price.ended"
                                    on-change="$ctrl.upsertPrice(price)">
                                </vn-date-picker>
                            </td>
                            <td shrink>
                                <vn-icon-button
                                    icon="delete"
                                    vn-tooltip="Delete"
                                    ng-click="deleteFixedPrice.show({$index})">
                                </vn-icon-button>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <div class="vn-pa-md">
                    <vn-icon-button
                        vn-tooltip="Add fixed price"
                        icon="add_circle"
                        vn-bind="+"
                        ng-click="$ctrl.add()">
                    </vn-icon-button>
                </div>
            </slot-table>
        </smart-table>  
    </vn-card>
</div>
<vn-item-descriptor-popover
    vn-id="item-descriptor"
    warehouse-fk="$ctrl.vnConfig.warehouseFk">
</vn-item-descriptor-popover>
<vn-confirm
    vn-id="deleteFixedPrice"
    on-accept="$ctrl.removePrice($data.$index)"
    question="Are you sure you want to continue?"
    message="This row will be removed">
</vn-confirm>