<?php
$bool = array('state');
$dontRender = array('type'); // FIXME ! : later remove this line and remove the default value from Model ! 
$dropdown = array(
    'type' => $model->getListData('type'),   
);

if (PhonalisaLicenseManager::routingScriptExtensionMax() !== 0) {
    $dropdown = array_merge($dropdown, array('scriptExtension' => ScriptExtension::model()->listData));
} else {
    $dontRender = array_merge($dontRender, array('scriptExtension'));
}

$form = $this->beginWidget('ext.PlGrid.PlActiveForm', array(
    'id' => 'external-agent-form',
    'gridId' => 'external-agent-grid',
    'displayProgressBar' => false,
    'action' => $model->isNewRecord ? array('create') : array()
        ));
$htmlOptioins = array();
foreach ($model->attributes as $attributeName => $attributeValue) {

    if (in_array($attributeName, $dontRender)) {
        continue;
    }


    $htmlOptioins['data-translation-id'] = ' EXTERNAL_AGENT_MODEL_' . strtoupper($attributeName);
    if (in_array($attributeName, $bool)) {
        echo $form->checkBoxRow($model, $attributeName, $htmlOptioins);
    } else if (array_key_exists($attributeName, $dropdown)) {
        echo $form->dropdownListRow($model, $attributeName, $dropdown[$attributeName], $htmlOptioins);
    } else {
        echo $form->textFieldRow($model, $attributeName, $htmlOptioins);
    }
}
$this->endWidget();
?>
<script>
    $('#ExternalAgent_type').on('change', function () {
        toggleStateCheckbox();
    });

    function toggleStateCheckbox() {
        if ($('#ExternalAgent_type').val() == '<?php echo ExternalAgentTypes::callback ?>') {
            $('#ExternalAgent_state').parent().show();
//     $('#ExternalAgent_state').attr('checked',true);
        } else {
            $('#ExternalAgent_state').parent().hide();
            $('#ExternalAgent_state').attr('checked', false);
        }
    }
</script>