<?php
/* @var $this QuestionnaireQuestionController */
/* @var $model QuestionnaireQuestion */
/* @var $form CActiveForm */
?>

<div class="col-xs-12">

    <?php
    $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
        'id' => 'questionnaire-question-form',
        // Please note: When you enable ajax validation, make sure the corresponding
        // controller action is handling ajax validation correctly.
        // There is a call to performAjaxValidation() commented in generated controller code.
        // See class documentation of CActiveForm for details on this.
        'enableAjaxValidation' => true,
        'clientOptions' => array(
            'validateOnSubmit' => true,
        ),
    ));
    ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <?php echo $form->textFieldControlGroup($model, 'title', array('maxlength' => 200)); ?>


    <?php echo $form->dropDownListControlGroup($model, 'required', array(0 => Yii::t('admin', 'No'), 1 => Yii::t('admin', 'Yes'))); ?>

    <?php
    $ajaxOptions = array(
        'type' => 'POST',
        'data' => new CJavaScriptExpression('jQuery("#questionnaire-question-form").serialize()'),
        'url' => CController::createUrl('/admin/questionnaire/questionnaireQuestion/options'),
        'update' => '#question_options',
        'beforeSend' => 'function() {          
			$("#question_options-container").hide();
			$(".loading-sm").show();
		    }',
        'complete' => 'function() {
			$("#question_options-container").show();
			$(".loading-sm").hide();
		    }',
            //'data'=>'js:javascript statement' 
            //leave out the data key to pass all form values through
    );
    echo $form->dropDownListControlGroup($model, 'type', QuestionnaireQuestion::getTypes(), array(
        'ajax' => $ajaxOptions, 'empty' => ''));
    ?>

    <div class="row">
        <div class="col-md-5 col-md-offset-3">
            <div class="loading-sm center">
                <small><?php echo Yii::t('admin', 'Loading Options...'); ?></small>
            </div>

            <div class="widget-box <?php echo $model->isNewRecord ? 'display-none' : '' ?>" id="question_options-container">
                <div class="widget-header">
                    <h4><?php echo Yii::t('admin', 'Options'); ?></h4>
                </div>
                <div class="widget-body">
                    <div class="widget-main clearfix">
                        <div id="question_options" class="col-xs-12">
                            <?php
                            if (!$model->isNewRecord):
//                                $ajax = CHtml::ajax($ajaxOptions);
//                                Yii::app()->clientScript->registerScript('init-questionnaire-question-options', $ajax, CClientScript::POS_READY);
                                $types = QuestionnaireQuestion::getTypes();
                                if (isset($types[$model->type])) {
                                    switch ($model->type) {
                                        case QuestionnaireQuestion::TYPE_TEXT:
                                            $this->renderPartial('types/_text', array('options' => CJSON::decode($model->options)), false, true);
                                            break;
                                        case QuestionnaireQuestion::TYPE_RANGE:
                                            $this->renderPartial('types/_range', array('options' => CJSON::decode($model->options)), false, true);
                                            break;
                                        case QuestionnaireQuestion::TYPE_MULTIPLE_CHOICES:
                                            $this->renderPartial('types/_multiple_choices', array('options' => CJSON::decode($model->options)), false, true);
                                            break;
                                        case QuestionnaireQuestion::TYPE_CHECKBOXES:
                                            $this->renderPartial('types/_multiple_choices', array('options' => CJSON::decode($model->options)), false, true);
                                            break;
                                        case QuestionnaireQuestion::TYPE_DROPDOWN:
                                            $this->renderPartial('types/_multiple_choices', array('options' => CJSON::decode($model->options)), false, true);
                                            break;
                                    }
                                }
                            endif;
                            ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <?php
    echo TbHtml::formActions(array(
        TbHtml::submitButton($model->isNewRecord ? Yii::t('admin', 'Create') : Yii::t('admin', 'Save'), array('color' => TbHtml::BUTTON_COLOR_PRIMARY)),
        TbHtml::resetButton(Yii::t('admin', 'Reset')),
    ));
    ?>    
    <?php $this->endWidget(); ?>

</div>