

var SINGLE_FLIGHT_ADMIN = function(parent) {
    this.parent = parent;
    var _this = this;
    this.TPRSContextMenu = null;
    this.init = function() {

        //ADMIN NAV MENU
        addNavRightItem('Add Tprs', 'addtprs', 'addtprs');
        addNavRightItem('Save Flight', 'saveflight', 'saveflight');
        addNavRightDivider();
        addNavRightItem('Lock Flight', 'lockflight', 'lockflight');

        $('#addtprs').on('click', function() {
            _this.selectFlightTPRS();
        });

        $('#saveflight').on('click', function() {
            //AJAX SAVE FLIGHT DATA
            //GET FLIGHT INFO ENTERED
            //SAVE CURRENT FLIGHT TPRS
//            _this.saveFlight();
        });

        $('#lockflight').on('click', function() {
            //SAVE THE FLIGHT THEN LOCK THE FLIGHT
//            _this.saveFlight().done(setFlightLock);
        });


    };
    this.initContextMenus = function() {
//        _this.TPRSContextMenu = new contextMenu($('.tprs_tr'));
        _this.parent.tprsContextMenu.addMenuItem('Remove', 'glyphicon glyphicon-remove', function(item) {
            if (item !== undefined) {
//            get('/testmain.php', {page: '/flights/page/single_flight_page.php', flight_id: item, ac_id: ac_id}, "get");
                //TODO REMOVE ITEM FROM aircraft_tprs, reload TPRS
                _this.removeTPRSID(item);

                //RELOAD TABLE
//                _this.loadFlightTPRS();
//                menu.hide();
            }
        });
    };

    this.selectFlightTPRS = function() {
        $('#modal_div').empty();
        //LOAD PARAM MODAL
        $('#modal_div').load("/script/modals_view.php #select_modal", function() {
            $('#modal_div').find('.modal-title').text('Parameters');
            //POPULATAE PARAM MODAL
            $.ajax({
                type: "POST",
                url: '/tprs/js_tprs_ajax.php',
                data: {action: 'getTprsModel',
                    model_id: _this.parent.ac_info['MODEL_ID']},
                success: function(output) {
                    console.log(output);
                    var model_tprs = $.parseJSON(output);
                    //TODO PULL MODAL FROM modals.php
                    $.post('/tprs/view/tprs_table_view.php', {tprs: model_tprs}, function(data) {
                        $('#modal_div').find('.modal-body').empty().html(data);
//                        $('#tprs_table').addClass('tablesorter');
//                        $('#tprs_table').addClass('tablesorter-bootstrap');
                        $('#select_modal').modal('show');

//                            initTable();

                        $('.tprs_tr').on('click', function() {
                            //ADD TPRS TO flight_tprs array
//                            console.log(_this.parent.);
                            var tprsINFO = new ajaxJSONGet();
                            tprsINFO.setURL('/tprs/js_tprs_ajax.php');
                            tprsINFO.setDATA({action: 'getTPRSInfo', tprs_id: $(this).attr('value')});
                            tprsINFO.get().done(function(data) {
                                _this.parent.flight.FLIGHT_TPRS_ARRAY.push(data);
//                                _this.parent.flightTPRSLoad.setDATA(_this.parent.flight.FLIGHT_TPRS_ARRAY); //send this data
                                _this.parent.flightTPRSLoad.load();
                            });
                            $('.modal').modal('hide');
                        });
                    });
                }
            });

        });

    };

    this.saveFlight = function() {
//        var deferred = $.Deferred();
//        var send_ac_info =
//                $.ajax({
//                    type: "POST",
//                    url: '/flights/js_flights_ajax.php',
//                    data: {action: 'setFlight',
//                        flight_id: flight_id,
//                        ac_info: JSON.stringify(ac_info),
//                        flight_info: JSON.stringify(read_FlightInfoView_data()),
//                        flight_tprs: JSON.stringify(flight_tprs)
//                    },
//                    success: function(output) {
//                        //console.log(output) ;
//                        //CALL THIS PAGE WITH FLIGHT ID AND AC_ID from OUTPUT
//                        var save_flight_data = $.parseJSON(output);
//                        flight_id = save_flight_data['FLIGHT_ID'];
//                        deferred.resolve();
//                    }
//                });
//        return deferred;
    };

    this.removeTPRSID = function(tprs_id) {
        console.log(_this.parent.flight.FLIGHT_TPRS_ARRAY);
        _this.parent.flight.FLIGHT_TPRS_ARRAY = _this.parent.flight.FLIGHT_TPRS_ARRAY
                .filter(function(el) {
                    return el.TPRS_ID !== tprs_id;
                });
        console.log(_this.parent.flight.FLIGHT_TPRS_ARRAY);
        _this.parent.flightTPRSLoad.setDATA(_this.parent.flight.FLIGHT_TPRS_ARRAY); //send this data
        _this.parent.flightTPRSLoad.load();
    };


//    this.setFlightLock = function() {
//        $.ajax({
//            type: "POST",
//            url: '/flights/js_flights_ajax.php',
//            data: {action: 'setFlightLock',
//                flight_id: flight_id,
//                ac_id: ac_id},
//            success: function(output) {
//                //CALL THIS PAGE WITH FLIGHT ID AND AC_ID from OUTPUT
//                console.log(output);
//            }
//        });
//    };
};