<div class="page-header">
	<h3 class="page-title">
		<span class="page-title-icon bg-gradient-primary text-white mr-2">
			<i class="mdi mdi-account-convert"></i>
		</span>
		Task Request
	</h3>
	<nav aria-label="breadcrumb">
		<ul class="breadcrumb">
			<li class="breadcrumb-item active" aria-current="page">
				<span></span>Overview
				<i class="mdi mdi-alert-circle-outline icon-sm text-primary align-middle"></i>
			</li>
		</ul>
	</nav>
</div>

<div class="row">
	<div class="col-12 grid-margin">
		<div class="card shadow border10">
			<div class="card-body">
				<h4 class="card-title">All Requestes</h4>
				<div class="table-responsive">
					<table id="datatable1" class="table dataTable display">
						<thead>
						<tr>
							<th>#</th>
							<th>Task Url</th>
							<th>Status</th>
							<th>
								Requested At
							</th>
							<th>
								Actions
							</th>
						</tr>
						</thead>
						<tbody>
						<?php $serial=1; foreach($pending_tasks as $task):?>
							<tr>
								<td><?= $serial++ ?></td>
								<td><a href="<?= $task->url ?>" target="_blank"><?= $task->url ?></a></td>
								<td>

										<span class="badge <?php if($task->status == 2){echo 'badge-danger';}elseif($task->status == 1){echo 'badge-success';}else{echo 'badge-info';}?>">

										<?php if($task->status==1):?>
											Accepted
										<?php elseif($task->status==2):?>
											Rejected
										<?php else:?>
											Pending
										<?php endif;?>
									</span>

								</td>
								<td><?= nice_date($task->created_at, 'd-M-y') ?></td>
								<td>
									<button id="<?=$task->id?>" onclick="accpt_task(this);"    data-id="<?=$task->id?>" data-user_id="<?=$task->user_id?>" class='btn btn-sm btn-rounded btn-gradient-success'><i class="mdi mdi-check"></i> <i class="mdi mdi-sync mdi-spin a_ldr"></i> </button>
									&nbsp &nbsp <button id="<?=$task->id?>" onclick="reject_taks(this)" data-id="<?=$task->id?>" class='btn btn-sm btn-rounded btn-gradient-danger'><i class="mdi mdi-close"></i><i class="mdi mdi-sync mdi-spin r_ldr"></i></button>
								</td>
							</tr>
						<?php endforeach; ?>
						</tbody>
					</table>fore
				</div>
			</div>
		</div>
	</div>
</div>

<div class="page-header">
	<h3 class="page-title">
		<span class="page-title-icon bg-gradient-primary text-white mr-2">
			<i class="mdi mdi-history"></i>
		</span>
		Task Request History
	</h3>
	<nav aria-label="breadcrumb">
		<ul class="breadcrumb">
			<li class="breadcrumb-item active" aria-current="page">
				<span></span>Overview
				<i class="mdi mdi-alert-circle-outline icon-sm text-primary align-middle"></i>
			</li>
		</ul>
	</nav>
</div>

<div class="row">
	<div class="col-12 grid-margin">
		<div class="card shadow border10">
			<div class="card-body">
				<h4 class="card-title">Approved Tasks</h4>
				<div class="table-responsive">
					<table id="datatable" class="table dataTable display">
						<thead>
						<tr>
							<th>#</th>
							<th>Task Url</th>
							<th>Income</th>
							<th>Status</th>
							<th>
								Requested At
							</th>
							<th>
								Approved At
							</th>
						</tr>
						</thead>
						<tbody>
						<?php $serial=1; foreach($approve_tasks as $task):?>
							<tr>
								<td><?= $serial++ ?></td>
								<td><a href="<?= $task->url ?>" target="_blank"><?= $task->url ?></a></td>
								<td>‎৳ <?=number_format($task->income,2) ?></td>
								<td>

										<span class="badge <?php if($task->status == 2){echo 'badge-danger';}elseif($task->status == 1){echo 'badge-success';}else{echo 'badge-info';}?>">

										<?php if($task->status==1):?>
											Accepted
										<?php elseif($task->status==2):?>
											Rejected
										<?php else:?>
											Pending
										<?php endif;?>
									</span>

								</td>
								<td><?= nice_date($task->created_at, 'd-M-y') ?></td>
								<td><?= nice_date($task->updated_at, 'd-M-y') ?></td>

							</tr>
						<?php endforeach; ?>
						</tbody>
					</table>
				</div>
			</div>
		</div>
	</div>
</div>



<script>
	$(document).ready(function () {
		$('#datatable').DataTable();
		$(".a_ldr,.r_ldr").hide();
	});
	
	$(document).ready(function() {
    $('#datatable1').DataTable();
    $(".a_ldr,.r_ldr").hide();
	});

	function accpt_task(e) {

		let id = $(e).data('id');
		let taskId = $(e).attr('id');
		let user_id = $(e).data('user_id');
		let data = {
			id: id,
			user_id:user_id
		};

		if (confirm('Are You sure to Accept ?')) {
			 $('#' + taskId).find(".a_ldr").show()
			$.ajax({
				type: "post",
				url: "<?=base_url('MarketingCtrl/task_accepted')?>",
				data: data,
				dataType: 'json',
				success: function (response) {
					console.log(response)
					if(response.success===true){

						location.reload();
					}
					else if(response.success===false){
						alert("Sometimes Wrong,Please try again !!");

					}
				},
				error:function () {
					alert('Error');
				}
			});
		}

	}

	function reject_taks(e) {
		let id = $(e).data('id');
		let taskId = $(e).attr('id');
		let data = {
			id: id
		};

		if (confirm('Are You sure to Decline ?')) {
			$('#' + taskId).find(".a_ldr").show()
			$.ajax({
				type: "post",
				url: "<?=base_url('MarketingCtrl/task_rejected')?>",
				data: data,
				dataType: 'json',
				success: function (response) {
					if(response.success===true){

						location.reload();
					}
					else {
						location.reload();


					}
				},
				error:function () {
					alert("Error");
				}
			});
		}

	}
</script>
