<?php

Yii::app()->setLanguage(isset(Yii::app()->session['lang']) ? Yii::app()->session['lang'] : 'ar');
$lang = Yii::app()->getLanguage();


// if($model->hasErrors()){
//   echo CHtml::errorSummary($model);
// }

?>
<section class="questionnaire">
    <section class="questionnaire-section1">
        <div class="container">
            <div class="row">
                <div class="col-sm-offset-1 col-xs-offset-0 col-sm-10 col-xs-12">
                    <?php

                    //  foreach($questionnaires as $questionnaire)
                    //  {
                    ?>
                    <div class="page-follower">

                        <span class="head-title"><?php echo $questionnaires->title ?></span>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-offset-1 col-xs-offset-0 col-sm-10 col-xs-12">
                    <img class="img-responsive img-header" src="<?php echo Yii::app()->request->baseUrl ?>/images/formImage.jpg" alt="form-img">
                </div>
            </div>
            <div class="row">
                <div class="col-sm-offset-1 col-xs-offset-0 col-sm-10 col-xs-12">
                    <div class="questionnaire-details">
                        <?php echo $questionnaires->intro; ?> </div>
                </div>
            </div>

            <?php echo '<form action="#" method="post" id="questionnaire_form" >';
            ?>
            <div class="row">
                <?php
                //  echo'   <form action="#">';
                // $questionnaire_questions=QuestionnaireQuestion::model()->findAllByAttributes(array('questionnaire_id'=>3));
                $questionnaire_questions = $questionnaires->questionnaireQuestions;
                // print_r($questionnaire_questions->title);

                foreach ($questionnaire_questions as $questionnaire_question) {
                ?>
                    <div class="col-sm-offset-1 col-xs-offset-0 col-sm-10 col-xs-12">
                        <div class="question">


                            <?php echo $questionnaire_question->title ?>

                            <?php
                            echo '<div class="question-answers">
                            
                               <div class="row">';
                            switch ($questionnaire_question->type) {
                                    //checkBox											  
                                case $questionnaire_question->type = 2:

                                    $i = 0;
                                    $options =  CJSON::decode($questionnaire_question->options); //textField checkBox

                                    if ($options && count($options)) {
                                        foreach ($options as $key => $value) {
                                            $i++;
                                            echo '
												<label class="checkbox-inline">
                                                      <input type="checkbox"  value="' . $value . '" name="CheckBox[' . $questionnaire_question->id . '-' . $i . ']" class="input-hidden-style">
                                                      <span class="checkmark"></span>' . $value . '
												</label>';
                                        }
                                    }
                                    echo '     </div>
                                              
                                           
                                      </div>
                                  </div>
                              </div>';
                                    break;


                                case $questionnaire_question->type = 1:
                                    $range_options = json_decode($questionnaire_question->options, true);
                                    $min = $range_options['min'] ?? null;
                                    $max = $range_options['max'] ?? null;
                                    $step = $range_options['step'] ?? null;

                                    $range_input = '<input type="range" value="0" name="Range[' . $questionnaire_question->id . ']" ' . ($questionnaire_question->required ? 'required' : '');
                                    if ($min) {
                                        $range_input .= ' min="' . $min . '"';
                                    }
                                    if ($max) {
                                        $range_input .= ' max="' . $max . '"';
                                    }
                                    if ($step) {
                                        $range_input .= ' step="' . $step . '"';
                                    }
                                    $range_input .= '>';

                                    echo '<div class="form-group">';
                                    echo '<div class="row">';

                                    echo '<div class="col-md-1 questionnaire-center-text">' . ($min ?? '') . '</div>';

                                    echo '<div class="col-md-10">' . $range_input . '</div>';

                                    echo '<div class="col-md-1 questionnaire-center-text">' . ($max ?? '') . '</div>';

                                    echo '</div>';
                                    echo '</div>';
                                    break;


                                case $questionnaire_question->type = 3:
                                    //radio button
                                    $options =  CJSON::decode($questionnaire_question->options);


                                    foreach ($options as $key => $value) {

                                        echo '
                                                    <label class="radio-inline">
                                                    <input type="radio" name="Radio[' . $questionnaire_question->id . ']" class="input-hidden-style" value="' . $value . '" ' . ($questionnaire_question->required == 1 ? "required" : "") . '>' . $value . '
                                                    <span class="radiomark"></span> 
                                                    </label>
                                                     ';
                                    }


                                    echo '</div>
                                      
                                </div>
                            </div>
                        </div>';
                                    break;
                                case $questionnaire_question->type = 4:
                                    //drop down list
                                    $options =  CJSON::decode($questionnaire_question->options);

                                    echo '<select  class="form-control input-style zero-percent-height" ' . ($questionnaire_question->required == 1 ? "required" : "") . ' name="Radio[' . $questionnaire_question->id . ']">
                                             <option value="0">' . Yii::t('admin', 'Place select') . '</option>';
                                    foreach ($options as $key => $value) {

                                        echo '
                                                    <label class="radio-inline">

                                                     
                                                        <option value="' . $value . '">' . $value . '</option>
                                                       
                                                    
                        
                                                    </label>
                                                     ';
                                    }


                                    echo '</select></div>
                                      
                                </div>
                            </div>
                        </div>';





                                    break;

                                case $questionnaire_question->type = 5:
                                    //radio button FACE
                                    //$options=  CJSON::decode($questionnaire_question->options);

                                    echo '<div class="smile-rate">';

                                    $faces = ['Good', 'normal', 'bad'];
                                    $j = 0;
                                    for ($i = 1; $i <= 3; $i++) {

                                        echo '
                                                    <label class="radio-inline">
                                                    <input type="radio" name="Radio[' . $questionnaire_question->id . ']" class="input-hidden-style" value="' . $faces[$j] . '" ' . ($questionnaire_question->required == 1 ? "required" : "") . '>
                                                   <img class="sad img-radio" src="' . Yii::app()->request->baseUrl . '/images/smileRed' . $i . '.png"> 
                                                    </label>
                                                     ';
                                        $j++;
                                    }


                                    echo '</div></div>
                                      
                                </div>
                            </div>
                        </div>';
                                    break;
                                case $questionnaire_question->type = 0:


                                    //^[a-zA-Z0-9!@#$&()\\-`.+,/\"]*$
                                    // /[~`!@#$%^&()_={}[\]:;,.<>+\/?-]/
                                    //pattern="[a-zA-Z0-9!@#$%^&()_={}[\]:;,.<>+/?-]"

                                    echo ' 
                                  
                                      <div class="form-group"> 
                                    <input type="text" name="Text[' . $questionnaire_question->id . ']" pattern="^[a-zA-Z|\u0621-\u064A\u0660-\u0669 ]+$"  title="' . ($lang == 'ar' ? 'الرجاء ادخال احرف فقط' : 'Please write letters only') . '" class="form-control input-style" id="text" placeholder="اكتب الإجابة هنا ..." ' . ($questionnaire_question->required == 1 ? "required" : "") . '>
                                  </div>
                                 </div> 
                                       
                              
                        </div>
                    </div>
                </div>
                                 ';
                                    break;
                                case $questionnaire_question->type = 6:
                                    // pattern="[0-9]" 
                                    echo ' 
                                  
                                      <div class="form-group"> 
                                    <input type="text" name="Text[' . $questionnaire_question->id . ']" pattern="[0-9]*" title="' . ($lang == 'ar' ? 'الرجاء ادخال ارقام فقط' : 'Please write number only') . '"  class="form-control input-style" id="text" placeholder="اكتب الإجابة هنا ..." ' . ($questionnaire_question->required == 1 ? "required" : "") . '>
                                  </div>
                                 </div> 
                                       
                              
                        </div>
                    </div>
                </div>';
                            }


                            ?>
                        <?php //die;

                    } ?>
                        </div>


                        <?php  //echo $questionnaires->verifyCode 
                        ?>

                        <div class="col-sm-offset-1 col-xs-offset-0 col-sm-10 col-xs-12">
                            <div class="row">
                                <?php
                                $this->widget('CCaptcha', array(
                                    'buttonOptions' => array('style' => 'display:block')
                                ));
                                echo CHtml::textField('verifyCode');

                                ?>


                            </div>
                        </div>


                        <div class="col-sm-offset-1 col-xs-offset-0 col-sm-10 col-xs-12">
                            <div class="row">
                                <!--<button onclick="myFunction()">Try it</button>-->
                                <!--<input type="submit" onclick="myFunction()" name="submit" class="btn center-block btn-send" value="إرسال">-->
                                <input type="submit" name="submit" class="btn center-block btn-send" value=<?php echo ($lang == 'ar' ? 'ارسال' : 'send'); ?>>
                            </div>
                        </div>

                        <!-- Modal -->
                        <div id="myModal" class="modal fade" role="dialog">
                            <div class="modal-dialog">
                                <!--Modal content-->
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" id="xclose" class="close" data-dismiss="modal">&times;</button>
                                    </div>
                                    <div class="modal-body" id="getCode">
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" id="bclose" class="btn btn-default" data-dismiss="modal"><?php echo ($lang == 'ar' ? 'اغلاق' : 'Close') ?></button>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>

            </div>

        </div>
        </form>


        <?php
        // click refresh button on page load
        // Yii::app()->clientScript->registerScript( 'refresh-captcha', 
        // '$(document).ready(function(){$("#yw0_button").click();});' ); 
        // 
        ?>
    </section>
</section>
<script type="application/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/site/scriptQuestionnaire"></script>
<script>
    //  $(document).ready(function(){

    //       var closePopupurl=false;
    //      $("#questionnaire_form").submit( function(e){
    //          e.preventDefault();

    //          $.ajax({
    //              type:"POST",
    //              url: "<?php //echo CController::createUrl('questionnaire') 
                            ?>",
    //              data: $("#questionnaire_form").serialize(),
    //              dataType: "json",
    //              success: function(response){
    //                  if(response.status=="sucsess")
    //                     closePopupurl=true;
    //                 else
    //                     closePopupurl=false;
    //                  $(".modal-body").html(response.msg);
    //                  $("#myModal").modal('show');
    //              },
    //              error: function (data) {
    //              console.log('An error occurred.');
    //              console.log(data);
    //              },
    //         });
    //     });

    //     $("#bclose").click(function(){
    //         if(closePopupurl)
    //          parent.window.location="<?php //echo CController::createUrl('/site/questionnaire') 
                                        ?>";
    //     });

    //     $("#xclose").click(function(){
    //     if(closePopupurl)
    //         parent.window.location="<?php //echo CController::createUrl('/site/questionnaire') 
                                        ?>";
    //     });
    //  });
</script>