<?php
include"initialize_page.php";
include"systemfunctions.php";
?>
<?php include('resources/header.php') ?>
<title></title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<?php include('resources/css-file.php') ?>
<?php include('resources/navigation.php') ?> 

<style>
    .hide{
        display:none;
    }
.table > tbody > tr > td{
    padding: 18px 7px;
}
.panel a {
    color: #1d89d0;
}
</style> 
<!-- Dashboard wrapper starts -->
<div class="dashboard-wrapper">

    <div class="main-container">                
        <div class="row">
            <div class="col-sm-12 ac-summ">
                <h2>Bill Pay</h2>
            </div>
        </div>
       

        <div class="row gutter">


    


            <div class="col-lg-10 col-md-10 col-sm-12 col-xs-12 col-md-offset-1">
               
                        <h4>Automatic recuming payments </h4>
                       
                       <div class="panel" id="successmessage" style="display: none;">
                            
                            <div class="panel-body">
                                <h4><i class="fa fa-check-circle" aria-hidden="true" style="
    font-size: 20px;
    margin: 5px;
    color: #15689e;
"></i> Payment Scheduled Successfully.
                            </div>
                        </div>
                        <div class="panel">
                            
                            <div class="panel-body">
                                 <form method="post" action="" id="update_bill_pay">
                                    <div class="form-group">
                                            <input type="hidden" name="q" value="update_bill_pay" class="form-control">
                                        </div>   
                                <table class="table-responsive table table-striped">
                                <thead>
                                    <th>Merchant Name</th>
                                    <th>Amount</th>
                                    <th>Pay From</th>
                                    <th>Delivered By (mm/dd)</th>
                                </thead> 
                                <tbody>
                                    <?php
                                    
                                    $select_query = "select * from x_merchants,x_billpay where x_user_id=$user_id and x_billpay.x_merchant_id=x_merchants.x_merchant_id and x_status=1 ";
                                $select_result = mysqli_query($con,$select_query);
                                $num = mysqli_num_rows($select_result);
                            
                                $i=0;
                                if($num >0)
                                {
                                    
                                    while($row = mysqli_fetch_array($select_result))
                                    {

                                        ?>
                                    <tr >
                                    <td><?php echo $row['x_nick_name'];?>
                                        <input type="hidden" name="key[]"  value="<?php echo $row['x_merchant_id']; ?>">
                                    </td>
                                    <td><input type="text" class="form-control" name="amount[]" value="<?php echo $row['x_amount']==0?'':$row['x_amount'];?>"></td>

                                    <td><select class="form-control" name="x_account_id[]" id= "accountid" required>
                                        <option value = ""
                                                >Select Account</option>
                                                <?php
                                                $select_query = "select * from x_anotheracc where x_userid = '$user_id' and x_status ='Y'";
                                                $select_result1 = mysqli_query($con, $select_query);
                                                $num1 = mysqli_num_rows($select_result1);



                                                if ($num1 > 0) {

                                                    while ($row1 = mysqli_fetch_array($select_result1)) {
                                                        $account_no = $row1['x_time'];
                                                        $account_type = $row1['x_actype'];
                                                        $account_id = $row1['x_id'];
                                                        ?>  
                                                <option value = "<?php echo $account_id; ?>" <?php echo $account_id==$row['x_account_id']?'selected':''; ?>><?php echo $account_no; ?>(<?php echo $account_type; ?>)</option>

                                                <?php
                                            }
                                        }
                                        ?>
                                    </select></td>

                                    <td><input type="text" class="form-control datepicker" name="delivered_by[]" placeholder="mm/dd" value="<?php echo $row['x_delivered_by'];?>"></td>
                                </tr>

                            <?php }
                            }
                            else{ ?>
                                <tr>
                                    <td colspan="4">No Merchant Found.</td>
                                </tr>
                                 <?php } ?>
                                </tbody>
                                <tfoot>
                                    <th></th>
                                    <th></th>
                                    <th></th>
                                    <th><button class="btn btn-info" type="submit">Make Payment</button></th>
                                </tfoot>
                                </table>
                                </form>
                            </div>
                        </div>
                  
            </div>
        </div>  
        </br>
    </div>
    <!-- Main container ends -->
</div>
<!-- Dashboard wrapper ends -->
<?php include('resources/footer.php') ?>
<?php include('resources/js-file.php') ?>
<script src="js/jQuery.style.switcher.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script>
$(document).ready(function(){
  $("#demo").on("hide.bs.collapse", function(){
    $("#collapses").html('<span class="glyphicon glyphicon-collapse-down"></span> Add a new Pay to account');
  });
  $("#demo").on("show.bs.collapse", function(){
    $("#collapses").html('<span class="glyphicon glyphicon-collapse-up"></span> Add a new Pay to account');
  });
});
</script>
<script type="text/javascript">
    $( function() {
   
   
      $( ".datepicker" ).datepicker({
      changeMonth: true,
      changeYear: false,
      dateFormat:'mm/dd'
    });

  

});
    $(document).ready(function () {
        $("#update_bill_pay").submit(function (e) {
            e.preventDefault();
            $.ajax({
                url: "controller.php", // Url to which the request is send
                type: 'POST', // Type of request to be send, called as method
                data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
                contentType: false, // The content type used when sending data to the server.
                cache: false, // To unable request pages to be cached
                processData: false, // To send DOMDocument or non processed data file it is set to false
                success: function (data)   // A function to be called if request succeeds
                {
                    if (data == 1)
                    {
                        //setTimeout(function(){  }, 3000);
                        /*alert('updated successfully');*/
                        $('#successmessage').show();
                        /*window.location.href = '<?php echo website_settingdata('x_weburl'); ?>user/billpay.php';*/
                    }
                    if (data == 0)
                    {
                        alert("Amount should be in numeric..");
                    }
                    
                }
            });
        });
    });

</script>

<script type="text/javascript">
    $(document).ready(function () {
        //test amount on fill


        $('#receiveracnumberid').change(function (e) {
            $("#transferamountx").val("");
            $("#converted_amount").text("");

            var accountnu = $("#receiveracnumberid").val();
            if (accountnu != "")
            {
                $.post("controller.php", {q: "test_accountnumber", accountnu: accountnu}, function (data)
                {
                    //alert(data);
                    if (data == 0)
                    {
                        //alert(accountnu + " is not Valid Account Number Please Check...");
                    } else
                    {
                        $("#receivernameid").val(data);
                    }
                }
                );

            }

        });

    });

</script>
<script type = "text/javascript" >
    $(document).ready(function () {
        //test amount on fill


        $('#transferamountx').change(function (e) {

            var amount = $("#transferamountx").val();
            var recevier_account = $("#receiveracnumberid").val();
            if (amount != "")
            {
                var accid = $("#accountid option:selected").val();

                $.post("controller.php", {q: "test_amount_localtransfer1", amount: amount, accid: accid, receveraccountno: recevier_account}, function (data)
                {
                   
                    if (data == 0)
                    {
                        alert("Amount should be in numeric..");
                    }
                    if (data == 1)
                    {

                        getFee(accid);
                        //alert("Min Amount For International Funds Transfer is  <?php echo currency_data("x_currencysymbol", $currency_id); ?>  <?php echo local_minmax1("min"); ?> and Max Amount is <?php echo currency_data("x_currencysymbol", $currency_id); ?> <?php echo local_minmax1("max"); ?>..");   
                    }
                    if (data == 2)
                    {
                        alert("Amount Less in your account to transfer..");
                    } else
                    {
                        $("#converted_amount").text(data);

                        //alert();
                    }
                }
                );


            }

        });

    });


    function getFee(accid)
    {

        $.post("controller.php", {q: "get_currency", accid: accid}, function (data)
        {

            //alert("Min Amount For International Funds Transfer is  <?php echo currency_data("x_currencysymbol", $currency_id); ?>  <?php echo local_minmax1("min"); ?> and Max Amount is <?php echo currency_data("x_currencysymbol", $currency_id); ?> <?php
echo local_minmax1("max")

//var fee = "Min Amount For International Funds Transfer is " + data + <?php echo local_minmax1("min");  
?> +" and Max Amount is "+ data + <?php echo local_minmax1("max"); ?>+ " ..." ;

            var fee = "Min Amount For International Funds Transfer is " + data +<?php echo local_minmax1("min"); ?> + " and Max Amount is " + data + <?php echo local_minmax1("max"); ?> + " ...";

            alert(fee);


        }
        );


        //return fee;                       
    }



</script>

<script type="text/javascript">
    $(document).ready(function () {
        //test amount on fill

        $("#accountid").on('change', function (e) {

            var accid = $("#accountid option:selected").val();

            if (accid != "")
            {


                $.post("controller.php", {q: "get_balance", accid: accid}, function (data)
                {
                    //alert(data); 
                    $("#accountbal").text(data);
                    $("#tramtORfee").removeClass('hide');

                }
                );
                $.post("controller.php", {q: "get_fee_international", accid: accid}, function (data)
                {
                    //alert(data); 

                    $("#transfer_fee").text(data);
                    $("#tramtORfee").removeClass('hide');

                }
                );

            } else
            {
                $("#accountbal").text("");
                $("#tramtORfee").addClass('hide');
            }
        });


    });

</script>