@extends('dealer.layout.master')
@section('page_title', 'Dashboard')
@section('subpage', 'Graph & Summary Report')
@section('img_profile', asset($user->StaffDetail->user_pic))
@section('login_name', $user->StaffDetail->name )
@section('last_login', $user->last_login_at )

@section('content')
	<style>
		.ui-tabs .ui-tabs-nav li.ui-tabs-active a { color: #EA5730 !important; }
		.ui-widget-content .bRed { color: #fff !important; }
	</style>
	<!-- Table dealer -->
	<div class="fluid">
		<div class="widget grid2 widgetH"></div>
		<div class="widget grid8">
			<div class="whead"><h6>Agent's List</h6></div>
			<div id="dyn1" class="shownpars cuss responsive">
				<a class="tOptions" title="Options"><img src="{{ url('assets/img/icons/options') }}" alt="" /></a>
				<table id="agent" cellpadding="0" cellspacing="0" border="0" class="dTable tMedia" width="100%">
					<thead>
						<tr>
							<th>#</th>
							<th width="50">Image</th>
							<th>Name</th>
							<th>IC</th>
							<th>Email</th>
							<th>Phone</th>
							<th width="90">Actions</th>
						</tr>
					</thead>
				</table>
			</div>
			<div class="dialog-delete" title="Delete Agent">
	            <p id="inner-delete"></p>
	        </div>
		</div> 
		<div class="widget grid2 widgetH"></div>
	</div>
@endsection

@section('external_js')
<script>
	function deleteA(cid,name) {
		var cid = cid;
		var name = name;

		$('.dialog-delete').attr('data-id' , cid); 
		document.getElementById("inner-delete").innerHTML = "<p>Are you sure want to delete this agent ( " + name + " )?</p>";

		$('.dialog-delete').dialog('open');
		return false;
	};

	$(document).ready(function(){

		var origin   = window.location.origin; 
		var curr = '{{ $user->_id }}';

			//===== Dynamic table toolbars =====//		
			
			$('#dyn1 .tOptions').click(function () {
				$('#dyn .tablePars').slideToggle(200);
			});	
			
			$('.tOptions').click(function () {
				$(this).toggleClass("act");
			});
			
			//======= Filter data table ========//

			$('#agent').DataTable({
				"scrollX": true,
				"bJQueryUI": false,
				"bAutoWidth": true,
				"bSort": false,
				"sPaginationType": "full_numbers",
				"sDom": '<"tablePars"fl>t<"tableFooter"ip>',
				"oLanguage": {
					"sLengthMenu": "<span class='showentries'>Show entries:</span> _MENU_"
				},
				"ordering": false,
				"serverSide": true,
				"processing": true,
				"ajax": "{{ url('/dealer/get-agent-list') }}",
				"columns": [
				{ "data": "index","name": "index"},
				{ "data": "image","name": "image"},
				{ "data": "name","name": "name"},
				{ "data": "ic","name": "ic"},
				{ "data": "email","name": "email"},
				{ "data": "phone","name": "phone"},
				{ "data": "action","name": "action"}, 
				],	
				"columnDefs": [
					{
						"render": function ( data, type, row ) {
							return '<a href="'+origin+'/assets/img/big.png" title="" class="lightbox"><img src="'+origin+'/'+data+'" alt="" width="37px" height="auto"/></a>';
						},
						"targets": 1
					},
					{
						"render": function ( data, type, row ) {

							if (data.indexOf("-You") >= 0) {
								var split = data.split('-');
								return '<small class="label label-info">'+split[1]+'</small>&nbsp;'+split[0];
							}else if (data.indexOf("New/") >= 0) {
								var split = data.split('/');
								return '<small class="label label-warning">'+split[0]+'</small>&nbsp;'+split[1];
							}else {
								return data;
							}
						},
						"targets": 2
					},
					{
						"render": function ( data, type, row ) {
							if(data == curr)
							{
								var link = "{{ url('/dealer/agent/edit-agent') }}"+"/"+data;
								return '<a href="'+link+'" class="tablectrl_medium bLightBlue tipS" title="Edit Dealer"><span class="iconb" data-icon="&#xe003;"></span></a>';
							}else {
								var link = "{{ url('/dealer/agent/edit-agent') }}"+"/"+data;
								return '<a href="'+link+'" class="tablectrl_medium bLightBlue tipS" title="Edit Dealer"><span class="iconb" data-icon="&#xe003;"></span></a> <a class="tablectrl_medium bRed tipS delModule" onclick="deleteA(\''+data+'\',\'' + row.name + '\', )" title="Delete Agent"><span class="iconb" data-icon="&#xe136;"></span></a>';
							}
						},
						"targets": 6
					},
				],				
			});	

			//===== Modal =====//
			$('.dialog-delete').dialog({
				autoOpen: false, 
				width: 400,
				modal: true,
				buttons: {
					"Yes!": function () {
						var cid = $(this).data('id');

						$.ajax({
							type: "POST",
							url: "{{url('/dealer/delete-agent')}}",
							data: { "_token": "{{ csrf_token() }}", id: cid },
							success:function(data){
								if(data == "true"){
									window.location.reload();
								}else if(data == "false"){
									alert("ERROR!! Cant Delete Agent !");
								}
							}
						});
					},
					"Cancel": function () {
						$(this).dialog("close");
						$('.dialog-delete').removeAttr('data-id'); 
					}
				}
			});		

			$('.tipS').tipsy({gravity: 's',fade: true, html:true});
		});
	</script>
@endsection