<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div class="content-wrapper">
	<section class="content-header">
		<?php echo $pagetitle; ?>
		<?php echo $breadcrumb; ?>
	</section>
	<section class="content">
		<!--
		<div class="row">i
			<div class="col-xs-12">
				<button onclick="button_modal_add()" class="btn btn-default"><i class="fa fa-plus"></i> 미디어 등록</button>
			</div>
		</div>
		-->
		<div class="row">
			<div class="col-md-12">
				<div class="box">
					<div class="box-body">
						<table id="table_question" class="table table-striped table-hover">
							<thead>
								<tr>
									<th>인덱스</th>
									<th>아이디</th>
									<th>이름</th>
									<th>총점</th>
									<th>상세</th>
								</tr>
							</thead>
							<tbody>
								<?php foreach ($surveys as $survey){ ?>
								<tr>
									<td><?= $survey->index ?></td>
									<td><?= $survey->id ?></td>
									<td><?= $survey->name ?></td>
									<td><?= $survey->amount ?></td>
									<td><?= '<button onclick="button_modal(\''.$survey->index.'\',\''.$survey->id.'\',\''.$survey->name.'\',\''.$survey->amount.'\')" class="btn btn-xs btn-default">확인</button>' ?></td>
								</tr>
								<?php } ?>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
	</section>
</div>
<div id="div_modal" class="modal fade" role="dialog">
	<div>
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal">&times;</button>
				<h4 class="modal-title">설문 정보</h4>
			</div>
			<div class="modal-body">
				<label>인덱스</label>
				<input id="input_index" class="form-control" disabled>
				<br>
				<label>아이디</label>
				<input id="input_id" class="form-control" disabled>
				<br>
				<label>이름</label>
				<input id="input_name" class="form-control" disabled>
				<br>
				<label>총점</label>
				<input id="input_amount" class="form-control" disabled>
				<br>
				<label>질문 / 답변</label>
				<div class="row" style="padding: 0px 15px;">
				<?php foreach($questions as $key=>$value){
					echo "<div class='col-6'>";
					echo "<span>".$value->question."</span>";
					echo "</div><div class='col-6'>";
					echo "<input name='a".$key."' disabled>";
					echo "</div>";
				} ?>
				</div>
			</div>
		</div>
	</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/animsition/3.2.1/js/animsition.min.js"></script>
<script>
$(document).ready(function (){
	$(document).on('hide.bs.modal','#div_modal', function () {
		$('#div_modal_body_mission').empty();
		$('#div_modal_footer_mission').empty();
	});

	var table_question = new DataTable('#table_question', {
		columnDefs: [{
			targets: 2,
			render: DataTable.render.ellipsis(50, true),
		}],
		pageLength: 25,
		layout: {
			topStart: {
				pageLength: {
					menu: [10, 25, 100]
				}
			}
		}
	});
});

function button_modal(index, id, name, amount){
	$('#input_index').val(index);
	$('#input_id').val(id);
	$('#input_name').val(name);
	$('#input_amount').val(amount);

	$.ajax({
		url: '<?= base_url() ?>admin/survey_results/get_answers',
		type: 'POST',
		data: {
			index: index,
		},
		success: function (res){
			var answers = JSON.parse(JSON.parse(res));
			for(var i = 0; i < 5; i++){
				$('input[name="a' + i.toString() + '"]').val(answers['a' + i.toString()]);
			}
			$('#div_modal').modal('show');
		},
		error: function (data, status, err){
			console.log(data);
			console.log(err);
		}
	});
}
</script>
