<script src="<?= getBaseUrl() ?>assets/plugins/jquery/jquery.min.js"></script>
<script src="<?= getBaseUrl() ?>assets/js/paho-mqtt/paho-mqtt.js"></script>
<script src="<?= getBaseUrl() ?>assets/js/mqtt-connect.js"></script>

<!-- Content Header (Page header) -->
<?php App\Controllers\BackendController::createService()->showBreadCrumbs( $arrmixBreadCrumbData ) ?>
<!-- /.content-header -->

<!-- Main content -->
<section class="content">
    <div class="container-fluid">
        <div class="js-show-alert-message">
            <?php App\Controllers\BackendController::createService()->showAlertMessage( $arrmixFlashData ) ?>
        </div>

        <div class="card card-default">
            <div class="card-header">
                <h3 class="card-title">Device Model: &nbsp; <b><span><?= ( DEVICE_MODE_CONVERTER ==  $arrmixNetworkSettingsDetails['device_mode'] ) ? 'Converter' : 'Gateway' ?></span></b></h3>
                <div class="card-tools">
                    <a href="<?= getBaseUrl() ?>basic-settings/edit?used_config_file=<?= $intUsedConfigFile ?>" class="btn btn-outline-info"> <i class="fas fa-pencil-alt"></i>&nbsp;&nbsp;Edit Basic Settings</a>
                </div>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-12">
                        <img src="<?= getBaseUrl() ?>assets/images/converter/default.png" alt="Converter Images" class="border converter-center-image">
                    </div>
                </div>

                <div class="row">
                    <div class="col-6">
                        <div class="card card-default">
                            <div class="card-header">
                                <h3 class="card-title"><span class="text-primary"><i class="fas fa-network-wired"></i> &nbsp; Network Settings</span></h3>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-6 form-group">
                                        <label class="control-label" for="js-mode">Mode Settings: &nbsp;</label>
                                        <span><?= ( SPT_MODE_RSTP ==  $arrmixNetworkSettingsDetails['mode_settings'] ) ? 'RSTP' : 'STP' ?></span>
                                    </div>    
                                    <div class="col-6 form-group">
                                        <label class="control-label" for="js-mode">Mode Settings Status: &nbsp;</label>
                                        <span class="badge badge-<?= ( SPT_MODE_ENABLED ==  $arrmixNetworkSettingsDetails['mode_settings_status'] ) ? 'success' : 'danger' ?>"><?= ( SPT_MODE_ENABLED ==  $arrmixNetworkSettingsDetails['mode_settings_status'] ) ? 'Enabled' : 'Disabled' ?></span>
                                    </div>    
                                </div> 

                                <div class="row">
                                    <div class="col-6">
                                        <div class="card card-default">
                                            <div class="card-header">
                                                <h3 class="card-title">&nbsp; ETH-1</span></h3>
                                            </div>
                                            <div class="card-body">
                                            
                                                <div class="row">
                                                    <div class="col-12 form-group">
                                                        <label class="control-label" for="js-eth1-mode">Mode: &nbsp;</label>
                                                        <span><?= ( EHT_MODE_STATIC ==  $arrmixNetworkSettingsDetails['eth1_mode'] ) ? 'Static' : 'DHCP' ?></span>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-12 form-group">
                                                        <label class="control-label" for="js-eth1-ip-address">Ip Address: &nbsp;</label>
                                                        <span><?= $arrmixNetworkSettingsDetails['eth1_ip_address'] ?></span>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-12 form-group">
                                                        <label class="control-label" for="js-eth1-subnet-mask">Subnet Mask: &nbsp;</label>
                                                        <span><?= $arrmixNetworkSettingsDetails['eth1_subnet_mask'] ?></span>
                                                    </div>
                                                </div>
                                                <div class="row">    
                                                    <div class="col-12 form-group">
                                                        <label class="control-label" for="js-eth1-default-gateway">Default Gateway: &nbsp;</label>
                                                        <span><?= $arrmixNetworkSettingsDetails['eth1_default_gateway'] ?></span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-6">
                                        <div class="card card-default">
                                            <div class="card-header">
                                                <h3 class="card-title">&nbsp; ETH-2</span></h3>
                                            </div>
                                            <div class="card-body">
                                                <div class="row">
                                                    <div class="col-12 form-group">
                                                        <label class="control-label" for="js-eth2-mode">Mode: &nbsp;</label>
                                                        <span><?= ( EHT_MODE_STATIC ==  $arrmixNetworkSettingsDetails['eth2_mode'] ) ? 'Static' : 'DHCP' ?></span>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-12 form-group">
                                                        <label class="control-label" for="js-eth2-ip-address">Ip Address: &nbsp;</label>
                                                        <span><?= $arrmixNetworkSettingsDetails['eth2_ip_address'] ?></span>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-12 form-group">
                                                        <label class="control-label" for="js-eth1-subnet-mask">Subnet Mask: &nbsp;</label>
                                                        <span><?= $arrmixNetworkSettingsDetails['eth2_subnet_mask'] ?></span>
                                                    </div>
                                                </div>
                                                <div class="row">    
                                                    <div class="col-12 form-group">
                                                        <label class="control-label" for="js-eth2-default-gateway">Default Gateway: &nbsp;</label>
                                                        <span><?= $arrmixNetworkSettingsDetails['eth2_default_gateway'] ?></span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div> 
                        </div> 
                    </div> 
                    <div class="col-3">
                        <div class="card card-default">
                            <div class="card-header">
                                <h3 class="card-title"><span class="text-info"><i class="fas fa-teeth"></i> &nbsp; COM Port</span></h3>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-12 form-group">
                                        <label class="control-label" for="js-baud">Baud Rate: &nbsp;</label>
                                        <span><?= $arrmixComSettingsDetails['baud_rate'] ?></span>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-12 form-group">
                                        <label class="control-label" for="js-data-bits">Data Bits: &nbsp;</label>
                                        <span><?= $arrmixComSettingsDetails['data_bits'] ?></span>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-12 form-group">
                                        <label class="control-label" for="js-flow_control">Flow Control: &nbsp;</label>
                                        <span class="badge badge-danger">OFF</span>
                                    </div>    
                                </div>

                                <div class="row">
                                    <div class="col-12 form-group">
                                        <label class="control-label" for="js-parity-bits">Parity Bits: &nbsp;</label>
                                        <span><?= getParityBitsName( $arrmixComSettingsDetails['parity_bits'] ) ?></span>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-12 form-group">
                                        <label class="control-label" for="js-stop-bits">Stop Bits: &nbsp;</label>
                                        <span><?= $arrmixComSettingsDetails['stop_bits'] ?></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-3">
                        <div class="card card-default">
                            <div class="card-header">
                                <h3 class="card-title"><span class="text-secondary"><i class="fas fa-clock"></i> &nbsp; Time Setting</span></h3>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-12 form-group">
                                        <div class="form-group">
                                            <label class="control-label" for="js-current-time">Set Date & Time: &nbsp;</label>
                                            <div class="input-group date" id="js-current-date-time" data-target-input="nearest">
                                                <input type="text" class="form-control datetimepicker-input" data-target="#js-current-date-time" id="js-input-current-date-time">
                                                <div class="input-group-append" data-target="#js-current-date-time" data-toggle="datetimepicker">
                                                    <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                                                </div>
                                            </div>
                                        </div>
                                    </div> 
                                </div>    
                            </div>
                            <div class="card-footer">
                                <button type="button" class="btn btn-outline-success" id="js-send-time-setting"><i class="fas fa-share"></i> &nbsp; Send</button>
                            </div>    
                        </div>  
                    </div>
                </div>   
                <!-- /.card-body -->
            </div>    
            <!-- /.card-body -->
        </div>
    </div><!-- /.container-fluid -->
</section>

<script>
    $(document).ready(function() {
        
        $( '#js-current-date-time' ).datetimepicker( { 
            icons: { 
                time: 'far fa-clock' 
            },
            allowInputToggle: true  
        } );

        initMqtt();

        $( '#js-send-time-setting' ).on( 'click', function() { 
            console.log( $( '#js-input-current-date-time' ).val() );
            
            var strSelectedDateTime = $( '#js-input-current-date-time' ).val();
            
            if( false == validateValue( strSelectedDateTime ) ) {
                showAlertMessage( 'danger', 'Please select date and time.' );
            }

            $( this ).prop( 'disabled', true );

            showAlertMessage( 'success', 'Date & Time has been send successfully.' );

            var objDate = new Date( strSelectedDateTime );
            var intEpochDateTime = objDate.getTime()/1000;

            var objPayload = {
                "method": "cmd",
                "msg_id": 1009,
                "epoch" : intEpochDateTime
            };

            publishMessage( JSON.stringify( objPayload ) );
        } );    
    });

    function onConnect() {
        console.log( "On Connect" );
        objMqttclient.subscribe( SUBSCRIBE_TOPIC );
    }

    function processMessage( objMessagePayload ) {

        console.log( 'Processing Message Payload.' );
        console.log(  objMessagePayload );

        objMessagePayload = JSON.parse( objMessagePayload );

        var intResponseCode = objMessagePayload.resp_of;

        switch( intResponseCode ) {
            case 1009:
                getSentDateTimeResponse( objMessagePayload.status );
                break;
            
            default:
                console.log( 'Unable to update UI for giving response code : ' + intResponseCode );
                break;
        }

        return true;
    }

    function getSentDateTimeResponse( intStatus ) {

        $( '#js-send-time-setting' ).prop( 'disabled', false );

        switch( intStatus ) {
            case 200:
                showAlertMessage( 'success', 'Time has been updated successfully.' );
                break;
            
            case 302:
                showAlertMessage( 'danger', 'Failed to update time.' );
                break;    
            
            case 100:
                showAlertMessage( 'danger', 'Invalid epoch received.' );
                break;    
            
            default:
                showAlertMessage( 'warning', 'Unable to get valid response.' );
                break;    
        }
    }

</script>