 
<?php $__env->startSection('content'); ?>

<style type="text/css">

    table.dataTable thead > tr > th
    {
        padding-right: 0;
    }
    .heading
    {
        font-weight: 700;
    }
    .form-field
    {
        margin-bottom: 10px;
    }
    .form-fields
    {
        margin-bottom: 20px;
    }
    ol
    {
        padding-left: 20px;
    }
    ol li
    {
        margin-bottom: 10px;
    }
    .coupon-heading
    {
        font-size: 15px;
        font-weight: 700;
        color:#00aeef;
        margin-bottom: 10px;
    }
    .coupon-rows
    {
        margin-bottom:10px;
    }
    .inactive
    {
        pointer-events:none;
        background-color:#ccc;
    }

</style>

<div class="content-wrapper">
 <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>Active eWallet Booklets</h1>
    </section>
     <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box box-warning">
                    <div class="box-header with-border">
                        <?php echo $__env->make('includes.messages', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
                        <div class="pull-left"><a href="#" class="btn btn-primary send-booklet" >Send</a></div>
                        <div class="pull-right">
                          <div class="row">
                            <div class="col-lg-6">
                              <a href="<?php echo url('merchant/inactive_ebooklet'); ?>"" class="btn btn-primary">Inactive Booklets</a>
                            </div>
                            <div class="col-lg-6">
                              <a href="<?php echo url('merchant/ebooklet/create'); ?>" class="btn btn-primary">Create New Booklet</a>
                            </div>
                          </div>
                        </div>
                    </div>
                    <!-- /.box-header -->
                    <div class="box-body table-responsive">
                        <table id="data-table" class="table table-bordered table-hover">
                            <thead>
                            <tr>
                               <th  class="text-center">Booklet ID</th>
                               <th  class="text-center">Select</th>
                               <th  class="text-center">Booklet Name</th>
                               <th  class="text-center">Top Up Value</th>
                               <th  class="text-center">Validity Of Credits</th>
                               <th  class="text-center">Booklets Issued</th>
                               <th  class="text-center">Booklet Price</th>
                               <th  class="text-center">Total Earned</th>
                               <th  class="text-center">Sub Account Linking</th>
                               <th  class="text-center">Status</th>
                               <th  class="text-center">Action</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php
                            	$i=0;
                            	function IND_money_format($money)
                        		{
								    $len = strlen($money);
								    $m = '';
								    $money = strrev($money);
								    for($i=0;$i<$len;$i++)
								    {
								        if(( $i==3 || ($i>3 && ($i-1)%2==0) )&& $i!=$len)
								        {
								            $m .=',';
								        }
								        $m .=$money[$i];
							    	}
						    		return strrev($m);
								}
 
                            ?>
                            <?php foreach($booklets as $booklet): ?>
                            	<?php 
                                    $credit = IND_money_format($booklet->credit);
									$value = IND_money_format($booklet->price);
									$income = IND_money_format($booklet->income);
                            	?>
                                <tr id="<?php echo $booklet->id; ?>">
                                    <td class="text-center"><?php echo $booklet->id; ?></td>
                                    <td class="text-center"><input type="checkbox" class="booklet-checkbox"></td>
                                    <td class="booklet-name"><?php echo $booklet->name; ?></td>
                                    <td class="text-center credits"><?php echo $credit; ?> credits</td>
                                    <td class="text-center"><?php echo $booklet->credit_validity; ?> days</td>
                                    <td class="text-center"><a href="<?php echo url('merchant/ebooklet_report/'.$booklet->id); ?>"><?php echo $booklet->issued; ?></a></td>
                                    <td class="text-center booklet-price"><?php echo $value; ?></td>
                                    <td class="text-center"><?php echo $income; ?></td>
                                    <td class="text-center"><a href="#" data-toggle="modal" data-target="#sub_account_linking" data-links="<?php echo $booklet->allowed_subaccount; ?>" class="account-linking">Link</a></td>
                                    <td><span style="color:green">Active</span></td>
                                    <td class="text-center"><a href="<?php echo url('merchant/deactivate_ebooklet/'.$booklet->id); ?>">Deactivate</a></td>
                                </tr>
                            <?php $i++?>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
            <!-- /.col -->
        </div>
        <!-- /.row -->
    </section>
</div>

<!-- Modal for sending booklet -->
<div id="send_booklet" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <div class="modal-title"><h4>Send Booklet</h4></div>
            </div>
            <div class="modal-body">
                <form  class="send-booklet-form" method="post" action="<?php echo url('merchant/send_ebooklet'); ?>">
                    <div class="form-fields">
                        <label for="mobile_no" class="heading">Enter mobile number of recipient:</label>
                        <input type="text" class="form-control" id="mobile_no" placeholder="Enter the Mobile Number" name="mobile_no1" onkeypress="return isNumberKey(event)">
                    </div>
                    <div class="heading" style="margin-bottom:10px;text-align:center">-OR-</div>
                    <div class="form-fields">
                        <label for="mobile_no" class="heading">Select your recipient:</label>
                        <select class="form-control"  name="mobile_no2" id="recipient">
                                <option value="0" selected>Select Recipient</option>
                            <?php for($i=0; $i<count($user_details); $i++): ?>
                                <option value="<?php echo $user_details[$i]['mobile']; ?>"><?php echo $user_details[$i]["name"]; ?> (<?php echo $user_details[$i]["mobile"]; ?>)</option>
                            <?php endfor; ?>
                        </select>
                    </div> 
                    <div class="form-fields">
                      <div class="heading">Booklet Selected:</div>
                        <div class="coupon-selected">
                        </div>
                    </div>
                    <div class="form-field" style="text-align: center">
                      <button class="btn btn-primary submit-booklet">Submit</button>
                    </div>
                </form>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->
</div>

<!-- Modal for account Linking -->
<div id="sub_account_linking" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <div class="modal-title"><h4>Sub-Accounts allowed to send <span class="booklet_name"></span></h4></div>
            </div>
            <div class="modal-body">
                <form  class="account-linking-form" method="post" action="<?php echo url('merchant/link_account_ebooklet'); ?>">
                    <div class="row">
                        <div class="col-lg-3 form-fields">
                            <a href="#" id="selectall" class="btn btn-primary">Select All</a>
                        </div>
                        <div class="col-lg-3">
                            <a href="#" id="deselectall" class="btn btn-primary">Unselect All</a>
                        </div>
                        <div class="col-lg-6"></div>
                    </div>
                    <div class="row">
                        <div class="col-lg-4">
                            <div class="heading form-fields">Sub Accounts</div>
                        </div>
                        <div class="col-lg-4">
                            <div class="heading form-fields">Business Name</div>
                        </div>
                        <div class="col-lg-4"></div>
                    </div>
                    <?php foreach($employees as $employee): ?>
                        <div class="row">
                            <div class="col-lg-4">
                                <div class="form-field">
                                    <input type="checkbox" name="employees[]" value="<?php echo $employee->id; ?>" id="<?php echo $employee->id; ?>" class="employee_list"> <?php echo $employee->email; ?>

                                </div>
                            </div>
                            <div class="col-lg-4">
                                <div class="form-field"><?php echo $employee->business_name; ?></div>
                            </div>
                            <div class="col-lg-4"></div>
                        </div>
                    <?php endforeach; ?>
                    <input type="hidden" name="id" class="id" >
                    <div class="form-field" style="text-align: center">
                      <button class="btn btn-primary">Submit</button>
                    </div>
                </form>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->
</div>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>

$('#data-table').DataTable({
    "paging": true,
    "lengthChange": false,
    "searching": true,
    "ordering": false,
    "info": true,
    "autoWidth": false
});

$('#selectall').click(function() 
{
    $('.employee_list').prop('checked', true);
});

$('#deselectall').click(function () 
{
    $('.employee_list').prop('checked', false);
});

$('.account-linking').click(function()
{
    var booklet_id = $(this).parent().parent().attr('id');
    var booklet_name = $(this).parent().parent().find('.booklet-name').html();
    $('.booklet_name').html(booklet_name);

    var employee_ids  = new Array();
    employee_ids = ($(this).attr("data-links")).split(',');
    
    $(".employee_list").prop('checked', false);

    $.each(employee_ids, function( index, value )
    {
       $("#"+value).prop('checked', true);
    });

    $(".id").val(booklet_id);
});

$('.send-booklet').click(function()
{
    $(".coupon-selected").html("");
    if($('.booklet-checkbox:checked').length == 0)
    {
        alert("Please Select atleast 1 Booklet");
    }
    else
    {
    	var i=1;
        $('.booklet-checkbox:checked').each(function()
        {
    	 	var booklet_id = $(this).parent().parent().attr('id');
            alert (booklet_id);
            var booklet_name = $(this).parent().parent().find('.booklet-name').html();
            var credits = $(this).parent().parent().find('.credits').html();
            var booklet_price = $(this).parent().parent().find('.booklet-price').html();

            $(".coupon-selected").append('<div class="row" style="margin-top:10px;"><div class="col-lg-3"><input type="hidden" name="booklets[]" value="'+booklet_id+'">('+i+') '+booklet_name+'</div><div class="col-lg-9"> <span style="color:green">'+credits+' for '+booklet_price+' only</span></div></div>')
        	i++;
        });
        $("#send_booklet").modal("show");
    }
});

$('.submit-booklet').click(function(e)
{
    e.preventDefault();
    var mobile = $("#mobile_no").val();
    var recipient = $("#recipient").find('option:selected').attr('value');

    if(mobile==""&&recipient=="0")
    {
        alert("Enter mobile no. or select recipient");
    }
    else if(mobile!=""&&recipient!="0")
    {
         alert("Cannot use both mobile no. and recipient, Enter either of them. ")
    }
    else
    {
    	$(".send-booklet-form").submit();
    }
});

$('.deactivate-booklet').click(function()
{
    var id = $(this).attr("data-id");
    $(".deactiavte_booklet_id").val(id)
});

function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    {
        return false;
    }
    else
    {
        return true;
    }
}    

</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.merchant-backend-layout', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>