var eHaveApp = angular.module('eHaveApp', [ "kendo.directives" ]);

    // create the controller and inject Angular's $scope
    eHaveApp.controller('mainController', ['$scope', 'dataService', function($scope, dataService) {
        
        var init = function () {
            makeGrid();
            //$scope.masterGrid.dataSource.read();
        }

        $scope.masterGrid = {};
        
         var getPatientData = function (options){
            return dataService.getPatientData(options.data).then(
                function successCallback(response) {
                    options.success(response);
                    console.log(response);
                },
                function errorCallback(response) {
                    // handle error
                }
            );
        };
         var getTestData = function (options){
            return dataService.getTestData(options.data).then(
                function successCallback(response) {
                    options.success(response);
                    console.log(response);
                },
                function errorCallback(response) {
                    // handle error
                }
            );
        };
        
        var makeGrid = function () {
            $scope.masterGrid.dataSource = new kendo.data.DataSource({
                schema:{
                    data: function (response) {
                        return response.data.Data;
                    },
                    model: {
                        fields: {
                            MRN:  { type: "string", editable: false },
                        }
                    },
                    total: function(response) {
                        return response.data.Total;
                    }
                },
                transport:{
                    read: function(options) {
                        return getPatientData(options);
                    },
                },
                pageSize: 5,
                serverPaging: true,
                serverSorting: true,
                sort: { field: "MRN", dir: "desc" }
            });

            $scope.masterGrid.gridOptions = {
                columns: [
                    {
                        title: "MRN",
                        field: "MRN",
                        width: "110px",
                        sortable: true
                    },{
                        title: "First Name",
                        field: "firstName"
                    },{
                        title: "Last Name",
                        field: "lastName"
                    },{
                        title: "Gender",
                        field: "sex",
                        width: "80px",
                        template: kendo.template($("#genderTemplate").html()),
                    },{
                        title: "DoB",
                        field: "birthDate",
                        width: "110px",
                    }
                ],
                dataBound: function() {
                    this.expandRow(this.tbody.find("tr.k-master-row").first());
                },
                selectable: 'row',
                pageable: true,
                change: function () {
                    var selectedRows = this.select();
                    var rowData = this.dataItem(selectedRows[0]);
                }
            };
            
            $scope.detailGridOptions = function(dataItem) {
                console.log(dataItem)
                return {
                    dataSource: {
                        transport: {
                            read: function(options) {
                                return getTestData(options);
                            }
                        },
                        serverPaging: true,
                        serverSorting: true,
                        serverFiltering: true,
                        pageSize: 5
                    },
                    scrollable: false,
                    sortable: true,
                    pageable: true,
                    columns: [
                        { field: "timestamp", title:"Date/Time" },
                        { field: "resultType", title:"Type" },
                        { field: "resultValue", title:"Value" }
                    ]
                };
            };            
        };
        
        init();
        
    }]);