<?php require_once "atp.header.php"; ?>

<section data-heading="speed" class="content-wrapper" id="stored-log">
	<div class="page-title">
		<h1>
			Stored Log
		</h1>
		<p>
            This page allows setting of the internal stored/trip log. Take care with this function, the old log value will not be recoverable.
            New log value should be entered in nautical miles.
		</p>
    </div>

    <div class="box top-wrapper">
        <div class="flex-row">
            <div class="flex-heading">
                <h2>Stored Log</h2>
            </div>
            <div class="flex-field">
                <div class="field-container" id="stored-log-field" 
                    data-field="<?php echo fieldEnum::Calc_Water_Log_nm_Total;?>"
                    data-sender="<?php echo PROCESSOR_ID;?>" 
                    data-instance="<?php echo PROCESSOR_INSTANCE;?>">
                    &nbsp;
                </div>
            </div>
            <div class="flex-row stored-log-row">
                <div class="input-unit">
                    <input type="number" id="stored-log-input" step=0.01>
                    <span>nm</span>
                </div>
                <a class="button button-gray button-medium" id="stored-log-button">Set Stored Log</a>
            </div>
        </div>
        <div class="flex-row">
            <div class="flex-heading">
                <h2>Trip Log</h2>
            </div>
            <div class="flex-field">
                <div class="field-container" id="trip-log-field" 
                    data-field="<?php echo fieldEnum::Calc_Water_Log_nm;?>"
                    data-sender="<?php echo PROCESSOR_ID;?>" 
                    data-instance="<?php echo PROCESSOR_INSTANCE;?>">
                    &nbsp;
                </div>
            </div>
            <div class="flex-row trip-log-row">
                <div class="input-unit">
                    <input type="number" id="trip-log-input" step=0.01>
                    <span>nm</span>
                </div>
                <a class="button button-gray button-medium" id="trip-log-button">Set Trip Log</a>
            </div>
        </div>
    </div>
</section>

<script>
$(document).ready(function() {
    var model = new ObservableModel();
    var controller = new BasicController(model);
    var view = new BasicView(controller);

    //Set stored log
    $('#stored-log-button').click(function() {
        if ($('#stored-log-input').val() >= 0) {
            $('#dialog-text').text('Are you sure you want to overwrite the stored log? The old log value will not be saved. Press Ok to continue.')
            $('#dialog').dialog({
                title: 'Are you sure?',
                buttons: {
                    Ok: function() {
                        $.ajax({
                            type: 'POST',
                            url: '/helpers.php',
                            data: {
                                action: 'queryProcessor',
                                query: 'SetStoredLog=' + $('#stored-log-input').val()
                            }
                        });
                        $(this).dialog('close');
                    },
                    Cancel: function() {
                        $(this).dialog('close');
                    }
                }
            }).dialog('open');
        } else {
            $('#dialog-text').text('Stored log value cannot be negative.')
            $('#dialog').dialog({
                title: 'Invalid value.',
                buttons: {
                    Ok: function() {
                        $(this).dialog('close');
                    }
                }
            }).dialog('open');
        }
	});

    //Set trip log
    $('#trip-log-button').click(function() {
        if ($('#trip-log-input').val() >= 0) {
            $('#dialog-text').text('Are you sure you want to overwrite the trip log? The old log value will not be saved. Press Ok to continue.')
            $('#dialog').dialog({
                title: 'Are you sure?',
                buttons: {
                    Ok: function() {
                        $.ajax({
                            type: 'POST',
                            url: '/helpers.php',
                            data: {
                                action: 'queryProcessor',
                                query: 'SetTripLog=' + $('#trip-log-input').val()
                            }
                        });
                        $(this).dialog('close');
                    },
                    Cancel: function() {
                        $(this).dialog('close');
                    }
                }
            }).dialog('open');
        } else {
            $('#dialog-text').text('Trip log value cannot be negative.')
            $('#dialog').dialog({
                title: 'Invalid value.',
                buttons: {
                    Ok: function() {
                        $(this).dialog('close');
                    }
                }
            }).dialog('open');
        }
	});
});
</script>

<?php require_once "footer.php"?>
