poconsole.controller('wellTestCtrl', function($scope, Page, WellTest) {
	Page.setTitle('Well Tests');
    Page.setPage('welltest');

    $scope.loadWellTestData = function(page_number){
        var getData = WellTest.getWellTestPage(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.well_test_vals = d.objects;
            $scope.num_pages = d.total_pages;
            $scope.page_list = [];
            $scope.page_num = d.page;
            $scope.total = d.total;
        });
    };

    $scope.loadWellTestData(1);

    $scope.startCreateWellTest = function(){
        $scope.newWellTest = {
            'duration_hours': 24,
            'volume_h2o_actual': 0,
            'volume_oil_actual': 0,
            'volume_gas_actual': 0,
            'volume_h2o_projected': 0,
            'volume_oil_projected': 0,
            'volume_gas_projected': 0,
            'api_gravity_oil': 2.0,
            'spc_gravity_h2o': 1.011,
            'created_on': Date.create()
        };
    };

    $scope.submitNewWellTest = function(){
        var temp_date = Date.create($scope.newWellTest.created_on);
        $scope.newWellTest.created_on = temp_date.utc().format('{yyyy}-{MM}-{dd}T{HH}:{mm}:00.00000');
        var createWellTest = WellTest.createWellTest($scope.newWellTest);
        createWellTest.then(function(response_data){
            console.log("Response from API");
            console.log(response_data);
            $scope.loadWellTestData(1);
        });
    };

    $scope.startEditWellTest = function(id){
        var getEditWellTestData = WellTest.getWellTest(id);
        getEditWellTestData.then(function(d){
            $scope.editWellTest = d;
            $scope.editWellTest.created_on = Date.utc.create($scope.editWellTest.created_on);
        });
    };

    $scope.submitEditWellTest = function(){
        var temp_date = Date.create($scope.editWellTest.created_on);
        $scope.editWellTest.created_on = temp_date.utc().format('{yyyy}-{MM}-{dd}T{HH}:{mm}:00.00000');
        var updateWellTest = WellTest.updateWellTest($scope.editWellTest);
        updateWellTest.then(function(response_data){
            console.log("Response from API");
            console.log(response_data);
            $scope.loadWellTestData(1);
        });
    };

    $scope.openDeleteWellTest = function(id){
        var getDeleteWellTestData = WellTest.getWellTest(id);
        getDeleteWellTestData.then(function(d){
            $scope.deleteWellTest = d;
        });
    };

    $scope.submitDeleteWellTest = function(){
        var deleteSelectedWellTest = WellTest.deleteWellTest($scope.deleteWellTest._id);
        deleteSelectedWellTest.then(function(data){
            $scope.loadWellTestData(1);
        });
    };




});
