<script src="<?php echo base_url().'assets/js/script.js'?>" type="text/javascript"></script>

<div class="row">
    <div class="col-md-12">
        <table class="table table-hover table-bordered table-striped">
            <tr>
                <th>
                    <strong>Payment By</strong>
                </th>
                <td>
                    <strong>Balance Amount</strong>
                </td>
                <td>
                    <strong>Payment Amount</strong>
                </td>
                <td>
                    <strong>Available Balance</strong>
                </td>
            </tr>
            <tr>
                <td>
                    Cash in hand
                </td>
                <td>
                    <?php echo 'Rs. '.number_format($stock_details->cash_in_hand,2,'.',','); ?>
                    <input type="hidden" class="form-control input-medium" id="cash_in_hand" name="cash_in_hand" value="<?php echo $stock_details->cash_in_hand; ?>">
                </td>
                <td>
                    <input type="number" class="form-control input-medium payment_items" id="cash_payment" name="cash_payment">
                </td>
                <td>
                    <div id="div_cash_available_balance"><?php echo 'Rs. '.number_format($stock_details->cash_in_hand,2,'.',','); ?></div>
                    <input type="hidden" class="form-control input-medium" id="txt_cash_available_balance" name="txt_cash_available_balance">
                </td>
            </tr>
            <tr>
                <td>
                    Gold - <?php echo $stock_details->tf_gold_in_hand.' (g) x '; ?> <input type="number" class="form-control-inline input-small" id="txt_24k_rate" name="txt_24k_rate"> (24K Rate)
                    <input type="hidden" class="form-control input-medium" id="txt_avb_gold_amount" name="txt_avb_gold_amount" value="<?php echo $stock_details->tf_gold_in_hand; ?>">
                </td>
                <td>
                    <div id="div_gold_cash_amount"><?php echo 'Rs. '.number_format('0.00',2,'.',','); ?></div>
                    <input type="hidden" class="form-control input-medium" id="txt_gold_cash_amount" name="txt_gold_cash_amount">
                </td>
                <td>
                    <input type="number" class="form-control input-medium payment_items" id="gold_cash_payment" name="gold_cash_payment">
                    <input type="hidden" class="form-control input-medium" id="txt_gold_amount" name="txt_gold_cash_amount">
                </td>
                <td>
                    <div id="div_gold_cash_available_balance"><?php echo 'Rs. '.number_format('0.00',2,'.',',').' - '.$stock_details->tf_gold_in_hand.' g'; ?></div>
                    <input type="hidden" class="form-control input-medium" id="txt_gold_available_balance" name="txt_gold_available_balance">
                </td>
            </tr>
            <?php foreach($bank_accounts as $bank_account){ ?>
            <tr>
                <input type="hidden" class="form-control input-medium" value="<?php echo $bank_account['id']; ?>" name="bank_account_id[]">
                <td>
                    Cash at bank - <?php echo $bank_account['bank_name'].' - '.$bank_account['account_number']; ?>
                </td>
                <td>
                    <?php echo 'Rs. '.number_format($bank_account['bank_balance'],2,'.',','); ?>
                    <input type="hidden" class="form-control input-medium" id="bank_balance_<?php echo $bank_account['id']; ?>" value="<?php echo $bank_account['bank_balance']; ?>">
                </td>
                <td>
                    <input type="number" class="form-control input-medium payment_items bank_payment" id="bank_payment_<?php echo $bank_account['id']; ?>" name="bank_payment[]" item-id="<?php echo $bank_account['id']; ?>">
                </td>
                <td>
                    <div id="div_cash_available_balance_<?php echo $bank_account['id']; ?>"><?php echo 'Rs. '.number_format($bank_account['bank_balance'],2,'.',','); ?></div>
                    <input type="hidden" class="form-control input-medium" id="txt_bank_available_balance_<?php echo $bank_account['id']; ?>" name="txt_bank_available_balance[]">
                </td>
            </tr>
            <?php } ?>
            <?php foreach($settlement_creditors as $settlement_creditor){ ?>
            <tr>
                <input type="hidden" class="form-control input-medium" value="<?php echo $settlement_creditor['id']; ?>" name="settlement_creditor_id[]">
                <td>
                    Creditor - <?php echo $settlement_creditor['creditor_name']; ?>
                </td>
                <td>
                    -
                </td>
                <td>
                    <input type="number" class="form-control input-medium creditor_payment payment_items" id="creditor_payment_<?php echo $settlement_creditor['id']; ?>" name="creditor_payment[]" item-id="<?php echo $settlement_creditor['id']; ?>">
                </td>
                <td>
                    -
                </td>
            </tr>
            <?php } ?>
            <tr>
                <td colspan="2">
                    <strong>Total Balance Amount</strong>
                </td>
                <td colspan="2">
                    <strong><div id="div_total_bal_amount">Rs. 0.00</div></strong>
                </td>
            </tr>
        </table>
        <div id="error_msg_bal_amount" class="alert alert-danger hidden"></div>
    </div>
</div> 

<div class="row">
    <div class="col-md-12">
        <?php if($una_purchases){ ?>
        <h4>UNA Purchases</h4>
        <hr>
        <table class="table table-striped table-bordered table-hover">
            <thead>
                <tr>
                    <th><strong>#</strong></th>
                    <th><strong>Purchase Date</strong></th>
                    <th><strong>UNA Amount</strong></th>
                    <th><strong>UNA Rate</strong></th>
                    <th><strong>Total Amount</strong></th>
                    <th><strong>Paid Amount</strong></th>
                    <th><strong>Balance Amount</strong></th>
                    <th><strong>Payment Amount</strong></th>
                    <th><strong>Balance Amount</strong></th>
                    <th><strong>Action</strong></th>
                </tr>
            </thead>
            <tbody>
                <?php 
                $total_una_amount = 0;
                $total_lkr_amount = 0;
                $total_paid_amount = 0;
                $total_bal_amount = 0;
                $x = 0;
                foreach($una_purchases as $una_purchase) { ?>                                                        
                <tr>
                    <td><?php echo $una_purchase['id']; ?></td>
                    <td><?php echo date("d-m-Y", strtotime($una_purchase['date'])); ?></td>
                    <td><?php $total_una_amount += $una_purchase['una_amount']; echo 'AED. '.number_format($una_purchase['una_amount'],2,'.',','); ?></td>
                    <td><?php echo 'Rs. '.number_format($una_purchase['una_rate'],2,'.',','); ?></td>
                    <td><?php $total_lkr_amount += $una_purchase['lkr_amount']; echo 'Rs. '.number_format($una_purchase['lkr_amount'],2,'.',','); ?></td>
                    <td><?php $total_paid_amount += $una_purchase['paid_amount']; echo 'Rs. '.number_format($una_purchase['paid_amount'],2,'.',','); ?></td>
                    <td><?php $total_bal_amount += $una_purchase['bal_amount']; echo 'Rs. '.number_format($una_purchase['bal_amount'],2,'.',','); ?></td>
                    <td>
                        <div class="input-group input-small">
                            <span class="input-group-addon">
                                Rs.
                            </span>
                            <input type="number" class="form-control input-small una_payment_amount" id="una_payment_amount<?php echo $una_purchase['id']; ?>" item-id="<?php echo $una_purchase['id']; ?>" bal-amount="<?php echo $una_purchase['bal_amount']; ?>" name="una_payment_amount[<?php echo $x; ?>]">
                        </div>
                    </td>
                    <td class="hidden">
                        <input type="number" class="form-control input-small una_balance_amount" id="una_balance_amount<?php echo $una_purchase['id']; ?>" name="una_balance_amount[<?php echo $x; ?>]" value="<?php echo $una_purchase['bal_amount']; ?>">
                        <input type="number" class="form-control input-small" value="<?php echo $una_purchase['id']; ?>" name="una_ids[<?php echo $x; ?>]">
                    </td>
                    <td id="div_una_bal_amount<?php echo $una_purchase['id']; ?>" style="color: #2d823a"><?php echo 'Rs. '.number_format($una_purchase['bal_amount'],2,'.',','); ?></td>
                    <td><a data-toggle="tooltip" title="View UNA Details" class="btn default btn-xs blue-stripe" href="<?php echo base_url().'view_una?una_id='.$una_purchase['id'];?>" target="_blank"> View</a></td>
                </tr> 
                <?php $x++; }?> 
                <tr>
                    <th colspan="2">Total</th>
                    <th><?php echo 'AED. '.number_format($total_una_amount,2,'.',','); ?></th>
                    <th></th>
                    <th><?php echo 'Rs. '.number_format($total_lkr_amount,2,'.',','); ?></th>
                    <th><?php echo 'Rs. '.number_format($total_paid_amount,2,'.',','); ?></th>
                    <th>
                        <input type="hidden" class="form-control input-small" id="tot_amount_to_be_paid" value="<?php echo $total_bal_amount; ?>" name="tot_amount_to_be_paid">
                        <?php echo 'Rs. '.number_format($total_bal_amount,2,'.',','); ?>
                    </th>
                    <th id="una_total_paid">Rs. 0.00</th>
                    <input type="hidden" class="form-control input-small" id="tot_payment_amount" value="0.00" name="tot_payment_amount">
                    <th id="una_total_balance_amount">Rs. 0.00</th>
                    <th></th>
                </tr>  
            </tbody>
        </table>  
    <?php } ?>
    </div>
</div>