<?php
$script = 'var targetOnTimeout = $("#Queue_target_on_timeout");'
        . 'var targetOnNoAgents = $("#Queue_target_on_no_agents");'
        . 'var targetOnFull = $("#Queue_target_on_full");'
        . 'if (targetOnTimeout.val() == ' . TargetType::Manual . ') {'
        . 'checkManualNumber(targetOnTimeout[0])'
        . '};'
        . 'if (targetOnNoAgents.val() == ' . TargetType::Manual . ') {'
        . 'checkManualNumber(targetOnNoAgents[0])'
        . '};'
        . 'if (targetOnFull.val() == ' . TargetType::Manual . ') {'
        . 'checkManualNumber(targetOnFull[0])'
        . '};';

Yii::app()->clientScript->registerScript('manualTarget', $script);
cs()->registerScriptFile(Yii::app()->baseUrl . '/js/vmAnnouncementVisibility/vmAnnouncementVisibility.js', CClientScript::POS_HEAD);

$visibleStyle = 'style="display:none; margin-left: 180px;"';

echo $form->textFieldRow($model, 'name', array(
    'class' => 'contextHelp-span3',
    'data-translation-id' => 'QUEUE_MODEL_NAME',
        )
);

if (cfg('DP_BRANCH_PREFIX')) {

    echo $form->textFieldRow($model, 'extension', array(
        'prepend' => cfg('DP_BRANCH_PREFIX'),
        'style' => 'width: 179px;',
        'class' => 'contextHelp-span3',
        'data-translation-id' => 'QUEUE_MODEL_EXTENSION',
            )
    );
} else {
    echo $form->textFieldRow($model, 'extension', array(
        'class' => 'contextHelp-span3',
        'data-translation-id' => 'QUEUE_MODEL_EXTENSION',
        'disabled' => !$model->isNewRecord
            )
    );
}
echo $form->textFieldRow($model, 'cid_prefix', array(
    'class' => 'contextHelp-span3',
    'data-translation-id' => 'QUEUE_MODEL_CID_PREFIX',
        )
);
$announcementFileList = Queue::getAnnouncementFileList();
echo $form->dropDownListRow($model, 'announce_file', $announcementFileList, array(
    'class' => 'contextHelp-span3',
    'data-translation-id' => 'QUEUE_MODEL_ANNOUNCE_FILE',
        )
);

echo $form->dropDownListRow($model, 'moh', $model->getAudioFileListWithJustRingOption(), array(
    'class' => 'contextHelp-span3',
    'data-translation-id' => 'QUEUE_MODEL_MOH',
        )
);
echo $form->dropDownListRow($model, 'strategy', $model->getQueueStrategyList(), array(
    'class' => 'contextHelp-span3',
    'data-translation-id' => 'QUEUE_MODEL_STRATEGY',
        )
);
$targetList = $model->getTargetList();

echo '<div class="target_on_timeout">';
echo $form->dropDownListRow($model, 'target_on_timeout', $targetList, array(
    'class' => 'contextHelp-span3',
    'data-translation-id' => 'QUEUE_MODEL_TARGET_ON_TIMEOUT',
    'data-translation-target' => 'TARGET_ON_TIMEOUT_DROPDOWN',
    'onChange' => 'checkManualNumber(this);',
    'options' => array(
        $model->getSelectedValueForTarget('target_on_timeout') => array('selected' => true)
    )
        )
);

if ($model->target_on_timeout == TargetType::Manual) {
    $visibleStyle = 'style="display:block;"';
}
echo '<div class="targetManualTarget"' . $visibleStyle . '>';
echo CHtml::textField('Queue[target_on_timeout-manual]', $model->target_type_on_timeout == TargetType::Manual ? $model->target_on_timeout : '', array(
    'class' => 'hideableInput span2 ',
        )
);
echo '</div></div>';

echo $form->dropDownListRow($model, 'target_on_timeout_meta', $announcementFileList, array(
    'class' => 'contextHelp-span3',
    'data-translation-id' => 'QUEUE_MODEL_TARGET_ON_TIMEOUT_META',
        )
);

echo '<div class="target_type_on_no_agents">';
echo $form->dropDownListRow($model, 'target_on_no_agents', $targetList, array(
    'class' => 'contextHelp-span3',
    'data-translation-id' => 'QUEUE_MODEL_TARGET_ON_NO_AGENTS_LOGGED_IN',
    'data-translation-target' => 'TARGET_ON_TIMEOUT_DROPDOWN',
    'onChange' => 'checkManualNumber(this);',
    'options' => array(
        $model->getSelectedValueForTarget('target_on_no_agents') => array('selected' => true)
    )
        )
);

if ($model->target_on_no_agents == TargetType::Manual) {
    $visibleStyle = 'style="display:block;"';
}
echo '<div class="targetManualTarget"' . $visibleStyle . '>';
echo CHtml::textField('Queue[target_on_no_agents-manual]', $model->target_type_on_no_agents == TargetType::Manual ? $model->target_on_no_agents : '', array(
    'class' => 'hideableInput span2 ',
        )
);
echo '</div></div>';

echo $form->dropDownListRow($model, 'target_on_no_agents_meta', $announcementFileList, array(
    'class' => 'contextHelp-span3',
    'data-translation-id' => 'QUEUE_MODEL_TARGET_ON_NO_AGENTS_META',
        )
);


echo '<div class="target_on_full">';
echo $form->dropDownListRow($model, 'target_on_full', $targetList, array(
    'class' => 'contextHelp-span3',
    'data-translation-id' => 'QUEUE_MODEL_TARGET_ON_FULL',
    'data-translation-target' => 'TARGET_ON_TIMEOUT_DROPDOWN',
    'onChange' => 'checkManualNumber(this);',
    'options' => array(
        $model->getSelectedValueForTarget('target_on_full') => array('selected' => true)
    )
        )
);

if ($model->target_on_full == TargetType::Manual) {
    $visibleStyle = 'style="display:block;"';
}
echo '<div class="targetManualTarget"' . $visibleStyle . '>';
echo CHtml::textField('Queue[target_on_full-manual]', $model->target_type_on_full == TargetType::Manual ? $model->target_on_full : '', array(
    'class' => 'hideableInput span2 ',
        )
);
echo '</div></div>';

echo $form->dropDownListRow($model, 'target_on_full_meta', $announcementFileList, array(
    'class' => 'contextHelp-span3',
    'data-translation-id' => 'QUEUE_MODEL_TARGET_ON_FULL_META',
        )
);
?>

<style>
    .hideableClose{
        cursor: pointer;
        background-color: #fff; /*FIXME */

    }
    .hideableClose:after{
        color: #999;
        content: "\f00d";
        font-family: FontAwesome;
        margin-left: -20px;
        vertical-align: middle;
        font-style: normal;
    }

    .hideableClose:hover:after{
        color: #666;
    }
</style>
<script>
    $('<i class="hideableClose" ></i>').insertAfter($('.hideableInput'));
    $(".hideableInput").parent().on('click', '.hideableClose', function (el) {
        $parent = $(this).parent();
        $parent.hide();
        $parent.prev().css("display", "block");
        $parent.prev().find('select').val('');

        $manualTarget = $($parent).parent();
        $($manualTarget[0]).find('.controls').css("display", "block");
    });

    function checkManualNumber(elem) {
        var manualTarget = "<?php echo TargetType::Manual ?>";

        if (elem.options[elem.selectedIndex].value === manualTarget) {
            $(elem).parent().hide();
            $(elem).parents().parents().eq(1).find('.targetManualTarget').css("display", "block");
            $(elem).parents().parents().eq(1).find('.targetManualTarget').appendTo($(elem).parent().parent());
        } else {
            $(elem).parents().parents().eq(1).find('.targetManualTarget').hide();
        }
    }

    $(function () {
        var modelName = 'Queue';
        var metaList = [
            'target_on_timeout',
            'target_on_no_agents',
            'target_on_full'
        ];
        new $.vmAnnouncementVisibility(modelName, metaList);
    });
</script>