@extends('dealer.layout.master')
@section('page_title', 'Invoice/Claim (Residential)')
@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-panel {
			padding: 0px !important;
		}
	</style>
	<!-- Table dealer -->
	<div class="fluid">
		<div class="widget grid12 widgetBg rightTabs">
			<div class="whead"><h6>Take Note: </h6></div>
			<div style="padding:10px; font-size: 14px; text-align: center">
				<p>Please choose <b>Year</b> & <b>Month</b> to calculate the incentives</p>
				<b> 'R': Residential  | 'B': Business </b>
				<p style="color: red">** All the forms that have been activated by today, will be updated after midnight **</p>
			</div>
		</div>
	</div>

	<div class="fluid">
		<div class="widget grid12 widgetBg rightTabs">
			<div class="whead"><h6>Data Table</h6></div>
			<div class="tabs">
                <ul>
                    <li><a href="#tabs-11">Activated Form</a></li>
                </ul>

                <div id="tabs-11">
                    <ul class="tToolbar">
						<div id="link1"></div>
					</ul>
					<div id="dyn2" class="shownpars cuss">
						<a class="tOptions" title="Options"><img src="{{ url('assets/img/icons/options') }}" alt="" /></a>
						<table id="claim" cellpadding="0" cellspacing="0" border="0" class="dTable tMedia" width="100%">
							<thead>
								<tr>
									<th>#</th>
									<th>Contract</th>
									<th>W/O</th>
									<th>Activation.</th>
									<th>Name</th>
									<th>Address</th>
									<th>Package</th>
									<th>Retail Price</th>
									<th>Incentives (%)</th>
									<th>Incentives (RM)</th>
									<th>Total Incentives</th>
									<th>Claimed</th>
								</tr>
							</thead>
						</table>
					</div>
                </div>

			</div>
		</div>
	</div>
@endsection
@section('external_js')
<script>
	$(document).ready(function(){

		var origin   = window.location.origin; 
		var curr = '{{ $user->_id }}';
		var company = '{{ $user->StaffDetail->company_id }}';
		var year = null;
		var month = null;

		var url = origin+'/dealer/claim-all/'+company+'/'+year+'/'+month+'/null';

		var tableClaim = $('#claim').DataTable({
			"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,
			"columns": [
				{ "data": "index","name": "index"},
				{ "data": "contract","name": "contract"},
				{ "data": "wo","name": "wo"},
				{ "data": "activated_dt","name": "activated_dt"},
				{ "data": "name","name": "name"},
				{ "data": "address","name": "address"},
				{ "data": "package","name": "package"}, 
				{ "data": "retail_price","name": "retail_price"}, 
				{ "data": "incentives1","name": "incentives1"}, 
				{ "data": "incentives2","name": "incentives2"}, 
				{ "data": "total","name": "total"}, 
				{ "data": "claimed","name": "claimed"}, 
			],	
			"columnDefs": [
			{
				"render": function ( data, type, row, meta ) {
					if (data.indexOf("New/") >= 0) {
						var split = data.split('/');
						return '<small class="label label-warning">'+split[0]+'</small>&nbsp;'+(meta.row+1)+split[1];
					}else {
						return (meta.row+1)+data;
					}
				},
				"targets": 0
			},
			{
				"render": function ( data, type, row ) {
					// return '<a class="tablectrl_medium bGold tipS ml10 modal_open" title="View Status Form" data-cid="'+data+'" onclick="showHistory(\''+data+'\')"><span class="iconb" data-icon="&#xe157;"></span></a>';
					return data;
				},
				"targets": 10
			},
			],				
		});	

		// Filter by Year & Month
		$('#fbyear,#fbmonth,#fbapps').on('change',function(){
			if($('#fbyear').val() != '' && $('#fbmonth').val() != '' && $('#fbapps').val() == ''){

				var link = origin+'/dealer/download/claim/'+company+'/'+$('#fbyear').val()+'/'+$('#fbmonth').val()+'/null/pdf';
				tableClaim.ajax.url( origin+'/dealer/claim-all/'+company+'/'+$('#fbyear').val()+'/'+$('#fbmonth').val()+'/null' ).load();
				$('#link1').html('<li><a target="_blank" href="'+link+'" title=""><span class="icos-download"></span>Download PDF</a></li>');

			}else if($('#fbyear').val() != '' && $('#fbmonth').val() != '' && $('#fbapps').val() != ''){

				var link = origin+'/dealer/download/claim/'+company+'/'+$('#fbyear').val()+'/'+$('#fbmonth').val()+'/'+$('#fbapps').val()+'/pdf';
				tableClaim.ajax.url( origin+'/dealer/claim-all/'+company+'/'+$('#fbyear').val()+'/'+$('#fbmonth').val()+'/'+$('#fbapps').val() ).load();
				$('#link1').html('<li><a target="_blank" href="'+link+'" title=""><span class="icos-download"></span>Download PDF</a></li>');

			}else if($('#fbyear').val() == '' && $('#fbmonth').val() == '' && $('#fbapps').val() == ''){

				tableClaim.ajax.url( origin+'/dealer/claim-all/'+company+'/null/null/null' ).load();

			}else if($('#fbyear').val() == '' && $('#fbmonth').val() == '' && $('#fbapps').val() != ''){

				tableClaim.ajax.url( origin+'/dealer/claim-all/'+company+'/null/null/'+$('#fbapps').val() ).load();

			}
		});

	});
</script>
@endsection