
<div class="page-header">
	<h3 class="page-title">
		<span class="page-title-icon bg-gradient-primary text-white mr-2">
			<i class="mdi mdi-phone-voip"></i>
		</span>
		Marketing Task
	</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 >
	<div class="card">
		<div class="card-body">

			<form id="formABC" class="forms-sample" method="post" action="<?=base_url('MarketingCtrl/task_store')?>">
				<div class="row form-group">
					<div class="col-12">
						<label for="description">Social Category</label>
						<select class="form-control" name="category_id">
							<option disabled selected>Choose Category</option>
							<?php foreach ($categories as $task):?>
								<option value="<?=$task->id?>"><?=$task->category_name?></option>
							<?php endforeach;?>
						</select>
					</div>
				</div>
				<div class="row form-group">
					<div class="col-12">
						<label for="description">Task </label>
						<textarea class="form-control" name="task" rows="5" id="task"></textarea>
					</div>
				</div>


				<div class="row form-group">
					<div class="col-md-6">
						<div class="form-group row">
							<label class="col-sm-3 col-form-label">Status</label>
							<div class="col-sm-4">
								<div class="form-check">
									<label class="form-check-label">
										<input type="radio" class="form-check-input" name="status" id="membershipRadios1" value="1" >
										Active
										<i class="input-helper"></i></label>
								</div>
							</div>
							<div class="col-sm-5">
								<div class="form-check">
									<label class="form-check-label">
										<input type="radio" class="form-check-input" name="status" id="membershipRadios2" value="0" >
										Non-Active
										<i class="input-helper"></i></label>
								</div>
							</div>
						</div>
					</div>
				</div>

				<button id="btnSubmit" class="btn btn-gradient-primary mr-2">Submit</button>

			</form>
		</div>
	</div>
</div>
<br>
<br>
<div class="row">
	<div class="col-md-12 grid-margin stretch-card shadow p-0">
		<div class="card border10">
			<div class="card-body p-3">
				<h4 class="card-title">All Category</h4>
				<div class="table-responsive">
					<table class="table display datatable" id="" >
						<thead>
						<tr>
							<th>#</th>
							<th>
								Category Name
							</th>
							<th>
								Task
							</th>

							<th>
								Status
							</th>
							<th>Action</th>
						</tr>
						</thead>
						<tbody>
						<?php $serial=1; foreach($tasks as $task):?>
							<tr>
								<td><?=$serial++?></td>
								<td><?=$task->category_name?></td>
								<td><?=$task->task?></td>
								<td><span class="badge badge-pill badge-dark"><?php if($task->status==1) echo 'Active'; else echo 'Non-Active'?></span></td>
								<td>
									<a onclick="return confirm('Are You Sure ?')" href="<?=base_url("/MarketingCtrl/task_delete?id=".$task->id)?>" class="btn btn-rounded btn-inverse-danger btn-sm"><i class="mdi mdi-delete-forever"></i></a>
									<a onclick="edit_data_set(this)" data-task="<?=$task->task?>" data-toggle="modal" data-target="#editModal" data-id="<?=$task->id?>" data-category_id="<?=$task->category_id?>" class="btn btn-rounded btn-inverse-danger btn-sm"><i class="mdi mdi-tooltip-edit"></i></a>
									<a>
										<label class="switch">
											<input  onchange="taskStatus(this)" data-id="<?=$task->id?>" data-status="<?=$task->status?>" type="checkbox" <?php if($task->status==1) echo 'checked'?>>
											<span class="slider round"></span>
										</label>
									</a>
								</td>
							</tr>
						<?php endforeach?>
						</tbody>
					</table>
				</div>
			</div>
		</div>
	</div>
</div>

<div class="col-12 modal fade" id="editModal" >
	<div class="modal-dialog">
		<div class="modal-content">

			<!-- Modal Header -->
			<div class="modal-header">
				<h4 class="modal-title"> <i class="mdi mdi-coin"></i> Edit</b></h4>
				<button type="button" class="close" data-dismiss="modal">&times;</button>
			</div>

			<!-- Modal body -->
			<div class="modal-body">
				<input type="hidden" id="task_id">
				<h6><i class="md mdi-clipboard"></i> Category Name</h6>
				<select class="form-control" id="category_id">
					<?php foreach ($categories as $category):?>

						<option value="<?=$category->id?>"><?=$category->category_name?></option>
					<?php endforeach;?>
				</select><br>
				<h6><i class="mdi mdi-message-outline"></i> Tasks</h6>
				<textarea id="e_task" cols="30" rows="3" class="form-control" placeholder="Write Note....." ></textarea>


				<div id="b_result" class="text-center"></div>
			</div>

			<!-- Modal footer -->
			<div class="modal-footer">
				<button onclick="updateTask()" type="button" class="btn btn-info">Update <i class="mdi mdi-sync mdi-spin loading"></i> </button>
				<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
			</div>

		</div>
	</div>
</div>

<script>


	function edit_data_set(e) {
		$("#task_id").val($(e).data('id'));

		$("#e_task").val($(e).data('task'));

		let cat_id = $(e).data('category_id');
		$('#category_id  option[value='+cat_id+']').prop("selected", true);

	}

	function updateTask() {
		let id = $("#task_id").val();
		let task = $("#e_task").val();
		let cat_id = $("#category_id").val();
		var formData = new FormData();
		formData.append('id',id);
		formData.append('category_id',cat_id);
		formData.append('task',task);
		console.log(formData);

		$.ajax({
			type: "post",
			url: "<?=base_url('MarketingCtrl/task_update/')?>",
			data:formData,
			contentType : false,
			processData : false,
			dataType: "json",
			success: function (response) {

				if (response.success === true) {
					alert("Categoroy Update Successfully !!");
					location.reload();

				}
				else
				{
					alert(1);
				}
			}
		});


	}



	function taskStatus(e) {
		//alert(CKEDITOR.instances['task'].getData());
		if(confirm('Are Sure to change Category status ?')){
			let id = $(e).data('id');
			let status = $(e).data('status');

			$.ajax({
				type: "post",
				url: "<?=base_url('MarketingCtrl/task_status')?>",
				data: {id:id,status:status},
				dataType: "json",
				success: function (response) {
					if(response.success==true)
					{
						location.reload();
					}
					else
					{
						alert("sometimes wrong");

					}
				}
			});
		}
	}


</script>
