@model OrderModel
@inject Grand.Business.Core.Interfaces.Common.Security.IPermissionService permisionService;
<vc:admin-widget widget-zone="order_details_address_top" additional-data="Model" />
@if (Model.IsShippable)
{
    @if (await permisionService.Authorize(Grand.Business.Core.Utilities.Common.Security.PermissionSystemName.Shipments))
    {
        <div class="card col-md-12">
            <div class="card-body">
                <h4 class="card-title">@Loc["Admin.Orders.Shipments"]</h4>
                <div class="card-text">
                    <div class="panel panel-default">
                        <vc:admin-widget widget-zone="order_details_shipping_top" additional-data="Model" />
                        <div class="panel-body">
                            <div id="shipments-grid"></div>
                        </div>
                        <div class="panel-footer">
                            @if (Model.CanAddNewShipments)
                            {
                                <button type="submit" id="btnAddNewShipment" name="btnAddNewShipment" onclick="javascript:setLocation('@(Url.Action("AddShipment", "Shipment", new { orderId = Model.Id, area = Constants.AreaAdmin }))'); return false;" class="k-button"><i class="fa fa-plus"></i>&nbsp;@Loc["Admin.Orders.Shipments.AddNew"]</button>
                            }
                        </div>
                        <vc:admin-widget widget-zone="order_details_shipping_bottom" additional-data="Model" />
                    </div>
                </div>
            </div>
        </div>


        <script>
        $(document).ready(function () {
            $("#shipments-grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("ShipmentsByOrder", "Shipment", new { orderId = Model.Id, area = Constants.AreaAdmin }))",
                            type: "POST",
                            dataType: "json",
                            data: addAntiForgeryToken
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors"
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                autoBind: false,
                pageable: {
                    refresh: true,
                    numeric: false,
                    previousNext: false,
                    info: false
                },
                //details grid
                detailInit: detailInit,
                scrollable: false,
                columns: [{
                    field: "ShipmentNumber",
                    title: "@Loc["Admin.Orders.Shipments.ID"]",
                    width: 50,
                    template: '<a class="k-link" href="@Url.Action("ShipmentDetails", "Shipment", new { area = Constants.AreaAdmin })/#=Id#">#=ShipmentNumber#</a>'
                }, {
                    field: "TrackingNumber",
                    title: "@Loc["Admin.Orders.Shipments.TrackingNumber"]",
                    width: 100,
                        template: '<a class="k-link" href="@Url.Action("ShipmentDetails", "Shipment", new { area = Constants.AreaAdmin })/#=Id#">#=kendo.htmlEncode(TrackingNumber)#</a>'
                }, {
                    field: "TotalWeight",
                    title: "@Loc["Admin.Orders.Shipments.TotalWeight"]",
                    width: 100
                }, {
                    field: "ShippedDate",
                    title: "@Loc["Admin.Orders.Shipments.ShippedDate"]",
                    width: 200,
                    type: "date",
                    format: "{0:G}"
                }, {
                    field: "DeliveryDate",
                    title: "@Loc["Admin.Orders.Shipments.DeliveryDate"]",
                    width: 200,
                    type: "date",
                    format: "{0:G}"
                }]
            });
        });

        //details grid
        function detailInit(e) {
            $("<div/>").appendTo(e.detailCell).kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("ShipmentsItemsByShipmentId", "Shipment", new { area = Constants.AreaAdmin }))?shipmentId="+e.data.Id,
                            type: "POST",
                            dataType: "json",
                            data: addAntiForgeryToken
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors"
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                scrollable: false,
                sortable: true,
                pageable: {
                    refresh: true,
                    numeric: false,
                    previousNext: false,
                    info: false
                },
                columns: [
                    {
                        field: "ProductName",
                        title: "@Loc["Admin.Orders.Shipments.Products.ProductName"]",
                        width: 400
                    },{
                        field: "ShippedFromWarehouse",
                        title: "@Loc["Admin.Orders.Shipments.Products.Warehouse"]",
                        width: 150
                    },{
                        field: "QuantityInThisShipment",
                        title: "@Loc["Admin.Orders.Shipments.Products.QtyShipped"]",
                        width: 150
                    },{
                        field: "ItemWeight",
                        title: "@Loc["Admin.Orders.Shipments.Products.ItemWeight"]",
                        width: 150
                    },{
                        field: "ItemDimensions",
                        title: "@Loc["Admin.Orders.Shipments.Products.ItemDimensions"]",
                        width: 150
                    }
                ]
            });
        }
        </script>
    }
}
else
{
    <div class="card col-md-6">
        <div class="note note-info">@Loc["Admin.Orders.ShippingInfo.NotRequired"]</div>
    </div>

}

<vc:admin-widget widget-zone="order_details_address_bottom" additional-data="Model" />
