poconsole.controller('gaugeOffCtrl',function($scope, Page, $q, $http) {
    Page.setTitle('Gauge Off');
    Page.setPage('gaugeoff');
    var colors = ['#d7191c','#fdae61','#abdda4','#2b83ba'];
    var getGaugeOffData = function(page_number){
        var deferred = $q.defer();
        $http.get('/api/gauge_off?q={"order_by":[{"field":"created_on","direction":"desc"}]}&page='+ page_number).success(function(data) {
            deferred.resolve(data);
        });
        return deferred.promise;
    };

    $scope.loadGaugeOffData = function(page_number){
        var getData = getGaugeOffData(page_number);
        getData.then(function(d){
            for(var j = 0; j < d.objects.length; j++){
                // console.log({'Date Before':d.objects[j].created_on});
                d.objects[j].created_on = Date.create(d.objects[j].created_on + "Z", { fromUTC: true });
                // console.log({'Date After':d.objects[j].created_on});
            }
            $scope.gauge_off_vals = d.objects;
            $scope.num_pages = d.total_pages;
			$scope.per_page = d.objects.length;
            $scope.page_list = [];
            $scope.page_num = d.page;
            $scope.total = d.total;
            var stroke_columns = [
                {key: "downhole_gross_stroke_average", label: "DH Gross Stroke Avg"},
                {key: "downhole_net_stroke_average", label: "DH Net Stroke Avg"},
                {key: "surface_stroke_length_average", label: "Surface Stroke Avg"},
                {key: "tubing_movement_average", label: "Tubing Movement Avg"}
            ];
            var monetary_columns = [
                {key: "electricity_cost_total", label: "Electricity Cost"},
                {key: "lifting_cost_average", label: "Lifting Cost Avg"}
            ];
            var energy_columns = [
                {key: "kWh_used_total", label: "kWh Used"},
                {key: "kWh_regen_total", label: "kWh Regen."}
            ];
            var load_columns = [
                {key: "min_pr_load", label: "Min. PR Load"},
                {key: "peak_pr_load", label: "Peak PR Load"}
            ];
            var other_columns = [

                {key: "percent_run", label: "Percent Run"},
                {key: "fluid_level_average", label: "Fluid Level Avg"},
                {key: "inflow_rate_average", label: "Inflow Rate Avg"},
                {key: "polished_rod_hp_average", label: "PR HP Avg"},
                {key: "pump_hp_average", label: "Pump HP Avg"},
                {key: "spm_average", label: "SPM Avg"}
            ];

            $scope.stroke_options = {
                series: [],
                axes: {
                    x: {
                        key: "created_on",
                        type: "date"
                    }
                }
            };

            $scope.monetary_options = {
                series: [],
                axes: {
                    x: {
                        key: "created_on",
                        type: "date"
                    }
                }
            };

            $scope.energy_options = {
                series: [],
                axes: {
                    x: {
                        key: "created_on",
                        type: "date"
                    }
                }
            };
            $scope.load_options = {
                series: [],
                axes: {
                    x: {
                        key: "created_on",
                        type: "date"
                    }
                }
            };
            $scope.other_options = {
                series: [],
                axes: {
                    x: {
                        key: "created_on",
                        type: "date"
                    }
                }
            };

            $scope.graphData = {};
            $scope.graphData.go_data = d.objects;
            for (var s_col in stroke_columns){
                $scope.stroke_options.series.push(
                    {
                        axis: 'y',
                        dataset: 'go_data',
                        key: stroke_columns[s_col].key,
                        label: stroke_columns[s_col].label,
                        id: stroke_columns[s_col].key,
                        type: ['line'],
                        color: colors[s_col % colors.length],
                        drawDots:false
                    }
                );
            }

            for (var m_col in monetary_columns){
                $scope.monetary_options.series.push(
                    {
                        axis: 'y',
                        dataset: 'go_data',
                        key: monetary_columns[m_col].key,
                        label: monetary_columns[m_col].label,
                        id: monetary_columns[m_col].key,
                        type: ['line'],
                        color: colors[m_col % colors.length],
                        drawDots:false
                    }
                );
            }

            for (var e_col in energy_columns){
                $scope.energy_options.series.push(
                    {
                        axis: 'y',
                        dataset: 'go_data',
                        key: energy_columns[e_col].key,
                        label: energy_columns[e_col].label,
                        id: energy_columns[e_col].key,
                        type: ['line'],
                        color: colors[e_col % colors.length],
                        drawDots:false
                    }
                );
            }

            for (var l_col in load_columns){
                $scope.load_options.series.push(
                    {
                        axis: 'y',
                        dataset: 'go_data',
                        key: load_columns[l_col].key,
                        label: load_columns[l_col].label,
                        id: load_columns[l_col].key,
                        type: ['line'],
                        color: colors[l_col % colors.length],
                        drawDots:false
                    }
                );
            }

            for (var o_col in other_columns){
                $scope.other_options.series.push(
                    {
                        axis: 'y',
                        dataset: 'go_data',
                        key: other_columns[o_col].key,
                        label: other_columns[o_col].label,
                        id: other_columns[o_col].key,
                        type: ['line'],
                        color: colors[o_col % colors.length],
                        drawDots:false
                    }
                );
            }

        });
    };

    $scope.loadGaugeOffData(1);


});
