var OrderBoardApp = angular.module('OrderBoardApp', []);

OrderBoardApp.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
}
]);

OrderBoardApp.controller("OrderBoard", function ($scope, $http) {
    var wholeCount;
    var sortField;
    var initializing = true
    var sortFieldDefer = $.Deferred();
    var resultsToSkip = function (pageNum) {
        return 50 * (pageNum - 1);
    };
    //gets datasource
    $http.get('api/values?skip=0&take=50').success(function (data) {
        loadGrid(data);
    });
    var loadGrid = function (sqlData) {
        $("#grid").kendoGrid({
            dataSource: new kendo.data.DataSource({
                data: sqlData,
                serverSorting: true,
                serverPaging: true, 
                transport: {
                    read: function (options) {
                            //defines initial load
                        if (initializing) {
                            $http.get('api/values?skip=' + (options.data.page - 1) * options.data.pageSize + '&take=' + options.data.pageSize + "&sortField=" + sortField).success(function (JSONdata) {
                                wholeCount = JSONdata.count;
                                options.success(JSONdata.data);
                                initializing = false;
                            });
                        }
                            //if sorting
                        else if (options.data.sort) {
                            //promise so sortField is defined before this function runs
                            $.when(sortFieldDefer).done(function () {
                                //uses built in Kendo properties to decide whether ascending or descending, and then calls particular URL based on direction 
                                var direction = options.data.sort[0].dir;
                                if (direction == "asc") {
                                    $http.get('api/values?skip=' + (options.data.page - 1) * options.data.pageSize + '&take=' + options.data.pageSize + "&sortField=" + sortField).success(function (JSONdata) {
                                        wholeCount = JSONdata.count;
                                        options.success(JSONdata.data);
                                    });
                                }
                                else {
                                    $http.get('api/values?skip=' + (options.data.page - 1) * options.data.pageSize + '&take=' + options.data.pageSize + "&sortField=" + sortField + "&ordByDesc=" + true).success(function (JSONdata) {
                                        wholeCount = JSONdata.count;
                                        options.success(JSONdata.data);
                                    });
                                }
                            });
                        }
                            //paging
                        else {
                            $http.get('api/values?skip=' + (options.data.page - 1) * options.data.pageSize + '&take=' + options.data.pageSize + "&sortField=" + sortField).success(function (JSONdata) {
                                wholeCount = JSONdata.count;
                                options.success(JSONdata.data);
                            });
                        }
                    }

                },
                pageSize: 50,
                //sets up grid columns and options
                schema: {
                    model: {
                        id: "OrderStuff",
                        fields: {
                            OrderNumber: {
                                type: "number",
                                editable: false
                            },
                            OrderStation: {
                                type: "string",
                                editable: false
                            },
                            CompanyName: {
                                type: "string",
                                editable: false
                            },
                            Contact: {
                                type: "string",
                                editable: false
                            },
                            Description: {
                                type: "string",
                                editable: false
                            },
                            Status: {
                                type: "string",
                                editable: false
                            },
                            Station_at_Sale: {
                                type: "string",
                                editable: false
                            },
                            OrderNotes: {
                                type: "string",
                                editable: false,
                            }
                        }
                    },
                    total: function (response) {
                        return wholeCount;
                    }
                }
            }),
            pageable: {
                numeric: true,
                refresh: false,
                pageSize: true,
                pageSizes: [10, 20, 50],
                info: false,
                buttonCount: 5,
                input: true,
            },
            filterable: false,            
            sortable: {
                allowUnsort: false
            },

            columns: [{
                field: "OrderNumber",
                title: "Order",
                width: 100
            }, {
                field: "CompanyName",
                title: "Company Name",
                width: 150
            }, {
                field: "Contact",
                title: "Contact Name",
                width: 150
            }, {
                field: "Description",
                title: "Order Descrption",
                width: 200
            }, {
                field: "Status",
                title: "Order Status",
                width: 100
            }, {
                field: "OrderStation",
                title: "Order Station",
                width: 150,
            }, {
                field: "Station_at_Sale",
                title: "Order Station at Sale",
                width: 150
            }, {
                field: "OrderNotes",
                title: "Order Notes",
                width: 125
            }]
        });

    };
    //assigns which row to sort when sort is clicked
    $("#grid").on("click", "th", function (sortingValue) {

        switch (sortingValue.currentTarget.textContent) {
            case "Order Station":
                sortField = "OrderStation";
                break;
            case "Company Name":
                sortField = "CompanyName";
                break;
            case "Contact Name":
                sortField = "Contact";
                break;
            case "Order Description":
                sortField = "Description";
                break;
            case "Status":
                sortField = "Status";
                break;
            case "Order":
                sortField = "OrderNumber";
                break;
            case "Order Station at Sale":
                sortField = "Station_at_Sale";
                break;
            case "Order Notes":
                sortField = "OrderNotes";
                break;
            default:
                sortField = "OrderNumber";
        }
        sortFieldDefer.resolve();
    });
});