<?php
Yii::app()->clientScript->registerCoreScript('jquery.ui');
cs()->registerScriptFile(Yii::app()->baseUrl . '/js/plDialog/plDialog.js', CClientScript::POS_HEAD);

$form = $this->beginWidget('ext.PlGrid.PlActiveForm', array(
    'id' => 'network-adapter-form',
    'gridId' => 'network-adapter-grid',
    'onSubmitSuccess' => 'function () {writeNetworkConfig();}',
    'action' => $model->isNewRecord ? array('create') : array()
        ));

$class = 'span3';

echo $form->textFieldRow($model, 'ip', array(
    'class' => $class,
    'data-translation-id' => 'NETWORK_INTERFACE_MODEL_IP',
));

echo $form->textFieldRow($model, 'netmask', array(
    'class' => $class,
    'data-translation-id' => 'NETWORK_INTERFACE_MODEL_NETMASK',
));

echo $form->textFieldRow($model, 'gateway', array(
    'class' => $class,
    'data-translation-id' => 'NETWORK_INTERFACE_MODEL_GATEWAY',
));

echo $form->textFieldRow($model, 'dns', array(
    'class' => $class,
    'data-translation-id' => 'NETWORK_INTERFACE_MODEL_DNS',
));

echo $form->checkBoxRow($model, 'isPrimary', array(
    'data-translation-id' => 'NETWORK_INTERFACE_MODEL_IS_PRIMARY',
));

echo $form->errorSummary($model);
$this->endWidget();
?>
<script>

    function writeNetworkConfig() {
        $.ajax({
            url: "<?php echo url('networkAdapterSettings/default/writeNetworkConfig'); ?>",
            type: "POST",
            dataType: "json",
            data: {},
            success: function (result) {
                if (result === 'ok' && $('#NetworkInterface_isPrimary').prop('checked')) {
                    writeDHCPSettings();
                } else {
                    window.location.reload();
                }
            }
        });
    }

    function writeDHCPSettings() {
        var id = '<?php echo $model->id; ?>';
        $.ajax({
            url: "<?php echo url('networkAdapterSettings/default/writeDHCPSettings'); ?>",
            type: "POST",
            dataType: "json",
            data: {
                id: id
            },
            success: function (result) {
                if (result === 'ok') {
                    showDialog();
                } else {
                    window.location.reload();
                }
            }
        });
    }

    function showDialog() {
        $('#network-adapter-grid_Modal').modal('hide');
        var options = {
            confirmMessage: "<?php echo __('NETWORK_ADAPTER_SETTINGS_DIALOG_TEXT'); ?>",
            dialogHeader: "<?php echo __('NETWORK_ADAPTER_SETTINGS_DIALOG_HEADER'); ?>",
            confirmText: "<?php echo __('NETWORK_ADAPTER_SETTINGS_ACKNOWLEDGE_BUTTON'); ?>",
            cancelText: "<?php echo __('NETWORK_ADAPTER_SETTINGS_CANCEL_BUTTON'); ?>",
            confirmCmd: function () {
                confirmCmd();
            }
        };

        var plDialog = new $.plDialog(options);
        plDialog.showConfirmDialog();
    }

    function confirmCmd() {
        var id = '<?php echo $model->id; ?>';
        $.ajax({
            url: "<?php echo url('networkAdapterSettings/default/rebootSystem'); ?>",
            type: "POST",
            dataType: "json",
            data: {
                id: id
            },
            success: function (url) {
                window.location.href = url;
            }
        });
    }

</script>