<?php if(!empty($poll)): ?>
  <?php if($type == 1): ?>
    <div class="poling-widget">
      <div class="pst-block-head">
          <h2 class="title-4"><strong><?php echo e(trans('resource.polling')); ?></strong></h2>
      </div>
        <div class="pst-block">
            <input type="hidden" name="poll_id" value="<?php echo e($poll->id); ?>"/>
            <?php echo e(csrf_field()); ?>

            <div class="pst-block-main">
                <div class="poling-block">
                    <h5 class="title-9"><?php echo e($poll->source); ?></h5>
                    <ul class="poling-list">
                        <?php if(Session::get('poll')): ?>
                            <?php $__currentLoopData = $answers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $answer): $__env->incrementLoopIndices(); $loop = $__env->getFirstLoop(); ?>
                              <li>
                                <label class="rd-label" ><?php echo e($answer->source); ?></label>&nbsp;-&nbsp;
                                <label class="rd-label" ><?php echo e($answer->total); ?></label>
                              </li>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getFirstLoop(); ?>
                        <?php else: ?>
                            <?php $__currentLoopData = $answers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $answer): $__env->incrementLoopIndices(); $loop = $__env->getFirstLoop(); ?>
                              <li>
                                <input type="radio" name="poll" id="answer<?php echo e($answer->id); ?>" value="<?php echo e($answer->id); ?>" />
                                <label class="rd-label" for="test1"><?php echo e($answer->source); ?></label>
                              </li>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getFirstLoop(); ?>
                        <?php endif; ?>
                    </ul>
                    <?php if(!Session::get('poll')): ?>
                        <button class="btnSubmit btn btn-3 btn-success" onclick="submitPoll()"><?php echo e(trans('resource.poll.givepoll')); ?></button>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <script>
            function submitPoll(){
                if($("[name=poll]").val() != undefined && $("[name=poll]").val() != null){
                    $.post("/submitpoll", {'answer_id': $("[name=poll]:checked").val(), 'poll_id' : $('[name=poll_id]').val(), '_token' : $("[name='_token']").val()}, function(data){
                        $obj = "";
                        $.each(data, function(i, v){
                            $obj += '<li><label class="rd-label" >'+v.source+'</label>&nbsp;-&nbsp;<label class="rd-label" >'+v.total+'</label></li>';
                        });
                        $(".btnSubmit").remove();
                        $(".poling-list").html($obj);

                        refreshPollDashboard();
                    });
                }
            }
        </script>
    </div>
  <?php elseif($type == 2): ?>
    <input type="hidden" name="poll_id" value="<?php echo e($poll->id); ?>"/>
    <?php echo e(csrf_field()); ?>

    <div>
      <div id="pie">
          <div id="pieChartContainerPoll" style="max-width: 800px; margin: 0 auto"></div>
      </div>

      <script>

      $(function () {
          refreshPollDashboard();
      });

      function refreshPollDashboard(){
        $.post("/getpoll", {'_token' : $('[name="_token"]').val(), "poll_id" : '<?php echo $poll->id; ?>'}, function(data){


            var chartData = [];

            $.each(data, function(i, v){
                var tmpObj = {};
                tmpObj['name'] = v.source;
                tmpObj['value'] = v.total;
                console.log(tmpObj);
                chartData.push(tmpObj);
            });

            console.log(chartData);

            $("#pieChartContainerPoll").dxPieChart({
                dataSource: chartData,
                series: {
                    argumentField: 'name',
                    valueField: 'value',
                    type: 'doughnut',
                    label: {
                        visible: true,
                        connector: { visible: true },
                        format: {
                            type: 'largeNumber',
                            precision: 0
                        }
                    }
                },
                palette: 'Ocean',
                adaptiveLayout: {
                    width: 300
                },
                legend: {
                    horizontalAlignment: 'center',
                    verticalAlignment: 'bottom'
                }

            });



        });
      }
      </script>
    </div>
  <?php endif; ?>
<?php endif; ?>
