<div class="content-wrapper">
    <section class="content-header">
		<h1 class="module-highlight"><span class="number"><?php echo $this->lang->line('moduleNo'); ?></span> <?php echo $this->lang->line('module'); ?></h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-home"></i> <?php echo $this->lang->line('home'); ?></a></li>
            <li><a href="#"><?php echo $this->lang->line('module'); ?></a></li>
            <li class="active"><?php echo $this->lang->line('saleslist'); ?></li>
        </ol>
    </section>
	<section class="content">
		<div class="row">
			<div class="col-lg-3 col-xs-12 col-sm-6">
				<div class="small-box bg-dashboard-1">
					<div class="inner">
						<h4 id="dashboard-1" data-end="<?php echo $salesToday; ?>" class="number">RM0.00</h4>
						<p><?php echo date('d/m/Y'); ?></p>
					</div>
					<div class="icon">
						<i class="ion ion-stats-bars"></i>
					</div>
					<a href="javascript:fnLoad('today');" class="small-box-footer"><?php echo $this->lang->line('moreInfo'); ?> <i class="fa fa-arrow-circle-right"></i></a>
				</div>
			</div>
			<div class="col-lg-3 col-xs-12 col-sm-6">
				<div class="small-box bg-dashboard-2">
					<div class="inner">
						<h4 id="dashboard-2" data-end="<?php echo $salesThisWeek; ?>" class="number">RM0.00</h4>
						<p><?php echo date('d', strtotime('previous monday')).'-'.date('d/m/Y', strtotime('next sunday')); ?></p>
					</div>
					<div class="icon">
						<i class="ion ion-stats-bars"></i>
					</div>
					<a href="javascript:fnLoad('weekly');" class="small-box-footer"><?php echo $this->lang->line('moreInfo'); ?> <i class="fa fa-arrow-circle-right"></i></a>
				</div>
			</div>
			<div class="col-lg-3 col-xs-12 col-sm-6">
				<div class="small-box bg-dashboard-3">
					<div class="inner">
						<h4 id="dashboard-3" data-end="<?php echo $salesThisMonth; ?>" class="number">RM0.00</h4>
						<p><?php echo date('F/Y'); ?></p>
					</div>
					<div class="icon">
						<i class="ion ion-stats-bars"></i>
					</div>
					<a href="javascript:fnLoad('MTD');" class="small-box-footer"><?php echo $this->lang->line('moreInfo'); ?> <i class="fa fa-arrow-circle-right"></i></a>
				</div>
			</div>
			<div class="col-lg-3 col-xs-12 col-sm-6">
				<div class="small-box bg-dashboard-4">
					<div class="inner">
						<h4 id="dashboard-4" data-end="<?php echo $salesThisYear; ?>" class="number">RM0.00</h4>
						<p><?php echo date('Y'); ?></p>
					</div>
					<div class="icon">
						<i class="ion ion-stats-bars"></i>
					</div>
					<a href="javascript:fnLoad('YTD');" class="small-box-footer"><?php echo $this->lang->line('moreInfo'); ?> <i class="fa fa-arrow-circle-right"></i></a>
				</div>
			</div>
		</div>
        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
					<div class="box-header with-border">
						<label class="box-title"><?php echo $this->lang->line('saleslist'); ?></label>
						<div class="pull-right hidden-xs">
							<a href="<?php echo base_url() ?>sales/new_sale" class="btn btn-sm bg-olive"><i class="fa fa-plus fa-fw"></i> <?php echo $this->lang->line('newSales'); ?></a>
							<button onClick="conf_delete_sales(this);" class="btn bg-red btn-sm delete" disabled><i class="fa fa-trash fa-fw"></i> <?php echo $this->lang->line('deleteSales'); ?></button>
							<button onClick="print_sales_list(this)" class="btn bg-orange btn-sm" <?php echo ($list_sales)?'':'disabled'; ?>><i class="fa fa-print fa-fw"></i> <?php echo $this->lang->line('printListing'); ?></button>
      					</div>
						<div class="pull-right btn-group-vertical hidden-lg hidden-sm hidden-md">
							<a href="<?php echo base_url() ?>sales/new_sale" class="btn btn-sm bg-olive"><i class="fa fa-plus fa-fw"></i> <?php echo $this->lang->line('newSales'); ?></a>
							<button onClick="conf_delete_sales(this);" class="btn bg-red btn-sm delete" <?php echo ($list_sales)?'':'disabled'; ?>><i class="fa fa-trash fa-fw"></i> <?php echo $this->lang->line('deleteSales'); ?></button>
							<button onClick="print_sales_list(this)" class="btn bg-orange btn-sm" <?php echo ($list_sales)?'':'disabled'; ?>><i class="fa fa-print fa-fw"></i> <?php echo $this->lang->line('printListing'); ?></button>
      					</div>
					</div>
					<form method="post" class="form-horizontal" action="<?php echo base_url(); ?>sales/saleslist">
						<div class="box-body">
							<fieldset class="fsStyle">
								<legend class="legendStyle">
									<a href="javascript:void(0);" data-toggle="collapse" data-target="#filter"><?php echo $this->lang->line('PleaseSelectDateToSearchYourSales'); ?></a>
								</legend>
								<div id="filter" class="row collapse in">
									<div class="form-group col-sm-5">
										<label for="dateFrom" class="col-sm-4 col-sm-offset-2 control-label"><?php echo $this->lang->line('from'); ?>: </label>
										<div class="col-sm-6">
											<input type="text" id="dateFrom" name="dateFrom" class="form-control datetimepicker" value="<?php echo date("d-m-Y", strtotime($dateFrom)); ?>" placeholder="<?php echo $this->lang->line('from'); ?>" />
										</div>
									</div>
									<div class="form-group col-sm-5">
										<label for="dateTo" class="col-sm-4 col-sm-offset-2 control-label"><?php echo $this->lang->line('to'); ?>: </label>
										<div class="col-sm-6">
											<input type="text" id="dateTo" name="dateTo" class="form-control datetimepicker" value="<?php echo date("d-m-Y", strtotime($dateTo)); ?>" placeholder="<?php echo $this->lang->line('to'); ?>">
										</div>
									</div>
									<div class="form-group col-sm-2">
										<button type="submit" class="btn btn-primary search hidden-xs"><?php echo $this->lang->line('search'); ?></button>
										<button type="submit" class="btn btn-primary search hidden-lg hidden-sm hidden-md pull-right" style="margin-right:20px;"><?php echo $this->lang->line('search'); ?></button>
									</div>
								</div>
							</fieldset>
						</div>
					</form>
                    <div class="box-header with-border"></div>
                    <div class="box-body">
                        <form id="frm_submit_delete_sales" method="post" action="<?php echo base_url(); ?>sales/delete_sales">
							<table id="datatable_saleslist" class="table table-bordered table-striped datatables">
	                            <thead>
	                                <tr>
	                                    <th class="text-center col-sm-1"><?php echo $this->lang->line('no'); ?></th>
	                                    <th class="text-center col-sm-2"><?php echo $this->lang->line('date'); ?></th>
	                                    <th class="col-sm-5"><?php echo $this->lang->line('formno'); ?></th>
	                                    <th class="text-right col-sm-2"><?php echo $this->lang->line('amount'); ?></th>
	                                    <th class="text-center col-sm-1"><?php echo $this->lang->line('print'); ?></th>
	                                    <th class="text-center col-sm-1"><input onchange="select_or_unselect_all(this)" type="checkbox" /></th>
	                                </tr>
	                            </thead>
								<tbody>
								<?php
									if ($list_sales) {
										$i = 1;
										foreach ($list_sales as $row) {
								?>
									<tr>
										<td class="text-center col-sm-1"><?php echo $i++; ?></td>
										<td class="text-center col-sm-2"><?php echo date("d-m-Y", strtotime($row->saleDate)); ?></td>
										<td class="col-sm-5"><a href="<?php echo base_url().'sales/edit_sale/'.$row->ID; ?>"><?php echo $row->formNo; ?></a></td>
										<td class="text-right col-sm-2"><?php echo number_format($row->grandTotal,2); ?></td>
										<td class="text-center col-sm-1">
											<a href="<?php echo base_url().'sales/print_sale/'.$row->ID; ?>" class="btn btn-primary btn-sm"><i class="fa fa-print"></i></a>
										</td>
										<td class="text-center col-sm-1">
											<input type="checkbox" name="sales_id[]" onChange="enabled_or_disabled_delete_btn(this)" value="<?php echo $row->ID; ?>" />
										</td>
									</tr>
								<?php
										}
									}
								?>
								</tbody>
							</table>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>
<script type="text/javascript">
	var options = {
		useEasing: true,
		useGrouping: true,
		separator: ',',
		decimal: '.',
		prefix: 'RM',
		suffix: ''
	};
    $(function() {
        $('.datatables').DataTable({
			responsive: true
		});
        $('.datetimepicker').datetimepicker({
            viewMode: 'months',
            format: 'DD-MM-YYYY'
        });
		$('h4.number').each(function() {
			var counter = new CountUp($(this).attr('id'), 0, $(this).attr('data-end'), 2, 2, options);
			counter.start();
		});
    });
	$(function() {

	});
    function conf_delete_sales(me) {
		confirmBox('delete_selected_sales()', '<?php echo $this->lang->line('msgconfirm'); ?>');
    }
    function delete_selected_sales() {
		$('#frm_submit_delete_sales').submit();
    }
    function print_sales_list() {
        var dateFrom = $('#dateFrom').val();
        var dateTo = $('#dateTo').val();
        window.location = '<?php echo base_url(); ?>sales/print_sales/' + dateFrom + '/' + dateTo;
    }
    function select_or_unselect_all(me) {
		var oTable = $('#datatable_saleslist').dataTable();
        if ($(me).is(':checked')) {
            $(oTable.fnGetNodes()).find(':checkbox').prop('checked', true);
            $('.delete').prop('disabled', false);
        } else {
            $(oTable.fnGetNodes()).find(':checkbox').prop('checked', false);
            $('.delete').prop('disabled', true);
        }
    }
    function enabled_or_disabled_delete_btn(me) {
		if ($(me).is(':checked')) {
            $('.delete').prop('disabled', false);
        } else {
            var oTable = $('#datatable_saleslist').dataTable();
            $(oTable.fnGetNodes()).find('input[type=checkbox] :checked');
            var notfind = true;
            $(oTable.fnGetNodes()).find(':checkbox:checked').each(function() {
                $('.delete').prop('disabled', false);
                notfind = false;
				return true;
            });
            if (notfind) {
                 $('.delete').prop('disabled', true);
            }
        }
    }
	function fnLoad(action) {
		switch (action) {
			case 'today':
				$('#dateFrom').val('<?php echo date('d-m-Y'); ?>');
				$('#dateTo').val('<?php echo date('d-m-Y'); ?>');
				break;
			case 'weekly':
				$('#dateFrom').val('<?php echo date('d-m-Y', strtotime('previous monday')); ?>');
				$('#dateTo').val('<?php echo date('d-m-Y', strtotime('previous sunday')); ?>');
				break;
			case 'MTD':
				$('#dateFrom').val('01-<?php echo date('m-Y'); ?>');
				$('#dateTo').val('<?php echo date('d-m-Y'); ?>');
				break;
			case 'YTD':
				$('#dateFrom').val('01-01-<?php echo date('Y'); ?>');
				$('#dateTo').val('31-12-<?php echo date('Y'); ?>');
				break;
		}
		$('.search').click();
	}
</script>
