@extends('market.layout.master')
@section('ptitle', 'Marketing')
@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; }
		.dt-buttons
		{
			background: -webkit-linear-gradient(top, #F8F8F8 0%, #EFEFEF 100%);
		}
		.icos-download
		{
			padding-top: 0px;
		}
		.buttons-excel
		{
			padding: 7px 12px;
			display: block;
			color: #808080;
			font-size: 11px;
		}
	</style>
	<!-- Table dealer -->
	<div class="fluid">
		<div class="widget grid12">		
			<div class="whead"><h6>Work Order'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" style="width:100%">
					<thead>
						<tr>
							<th>#</th>
							<th>Service</th>
							<th>W/O</th>
							<th>Cust. Name</th>
							<th>Contact No.</th>
							<th>Unit No.</th>
							<th>Building</th>
							<th>Postcode</th>
							<th>City</th>
							<th>Contractor</th>
							<th>Installer</th>
							<th>Date</th>
							<th>Status</th>
							<!-- <th width="90">Actions</th> -->
						</tr>
					</thead>
				</table>
			</div> 			
		</div>
	</div>
@endsection

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

		$('.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 ========//

			var table = $('#agent').DataTable({
				"dom": 'Br<"tablePars"fl>t<"tableFooter"ip>',
				"scrollX": true,
				"buttons": [{
					extend:'excel',
					text:'<span class="icos-download"></span>Download as Excel',
					exportOptions: 
					{
						modifier: {
							sLengthMenu: 'all'
						}
					}
		            // 'copyHtml5', 'csv', 'excelHtml5', 'pdf', 'print'
		        }],
				"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": false,
				"processing": true,
				"ajax": "{{ url('/marketing/work-order/get-wo-list') }}",
				"columns": [
				{ "data": "index","name": "index"},
				{ "data": "nature_work","name": "nature_work"},
				{ "data": "wo","name": "wo"},
				{ "data": "customer_name","name": "customer_name"},
				{ "data": "customer_phone","name": "customer_phone"},
				{ "data": "customer_unit","name": "customer_unit"},
				{ "data": "customer_building", "name": "customer_building"},
				{ "data": "customer_postcode","name": "customer_postcode"},
				{ "data": "customer_city","name": "customer_city"}, 
				{ "data": "contractor_id","name": "contractor_id"},
				{ "data": "installer_id","name": "installer_id"},
				{ "data": "dateTimeEnd","name": "dateTimeEnd"},
				{ "data": "status","name": "status"},
				],	
				 "columnDefs": [
					{
						"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": 0
					},
					{
						"render": function ( data, type, row ) {
							var link = "{{ url('/marketing/work-order/generate-pdf') }}"+"/"+data;
							return '<a href="'+link+'" target="_blank"><b>'+data+'</b></a>';
						},
						"targets": 2
					},
				],					
			});	

			//===== Modal =====//		

			$('#fbyear,#fbmonth,#fbapps').change(function() 
			{
				if($('#fbyear').val() != '' && $('#fbmonth').val() != '' && $('#fbapps').val() == '')
				{
					table.ajax.url( origin+'/marketing/work-order/filter/'+$('#fbyear').val()+'/'+$('#fbmonth').val()+'/null' ).load();
					// alert('TEST');
				}
				else if($('#fbyear').val() == '' && $('#fbmonth').val() == '' && $('#fbapps').val() == '') //done
				{
					table.ajax.url( origin+'/marketing/work-order/filter/null/null/null' ).load();
				}

				else if($('#fbyear').val() != '' && $('#fbmonth').val() != '' && $('#fbapps').val() != '')
				{
					table.ajax.url( origin+'/marketing/work-order/filter/'+$('#fbyear').val()+'/'+$('#fbmonth').val()+'/'+$('#fbapps').val() ).load();	
				}

				else if($('#fbyear').val() == '' && $('#fbmonth').val() == '' && $('#fbapps').val() != '')  //done
				{
					table.ajax.url( origin+'/marketing/work-order/filter/null/null/'+$('#fbapps').val() ).load();
				}

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