<!-- Breadcrumb -->
<nav class="hk-breadcrumb" aria-label="breadcrumb">
    <ol class="breadcrumb breadcrumb-light bg-transparent">
        <li class="breadcrumb-item"><a href="<?php echo site_url('dashboard'); ?>"><?php echo $this->lang->line('dashboard'); ?></a></li>
        <li class="breadcrumb-item active" aria-current="page"><?php echo $this->lang->line('purchase'); ?></li>
    </ol>
</nav>
<!-- /Breadcrumb -->

<!-- Container -->
<div class="container">
    <!-- Title -->
    <div class="hk-pg-header">
        <h4 class="hk-pg-title">
            <span class="pg-title-icon"><i class="fa fa-arrow-circle-o-down"></i></span>
            <?php echo $this->lang->line('manage_invoice'); ?>
        </h4>

        <div class="align-items-center">
            <?php if (has_permission(ADD, 'pharmacy', 'purchase')) { ?>
                <button class="btn btn-xs btn-outline-light btn-wth-icon icon-wthot-bg" onclick="printBill(<?php echo $this->uri->segment(4, 0); ?>);">
                    <span class="icon-label"><i class="fa fa-print"></i> </span>
                    <span class="btn-text"><?php echo $this->lang->line('print'); ?> </span>
                </button>
            <?php } ?>

            <?php if (has_permission(VIEW, 'pharmacy', 'purchase')) { ?>
                <a href="<?php echo site_url('pharmacy/purchase/history/' . $this->uri->segment(4, 0)); ?>" class="btn btn-violet btn-xs">
                    <i class="fa fa-history font-12 mr-5"></i>
                    <?php echo $this->lang->line('payment'); ?> <?php echo $this->lang->line('history'); ?>
                </a>
            <?php } ?>

            <?php if (has_permission(ADD, 'pharmacy', 'purchase')) { ?>
                <?php if ($invoice->paid_status == 'unpaid') { ?>
                    <button onclick="GetPaymentData(<?php echo $this->uri->segment(4, 0); ?>)" type="button" class="btn btn-teal btn-xs" data-toggle="modal" data-target="#addPaymentModal">
                        <i class="fa fa-money font-12 mr-5"></i>
                        <?php echo $this->lang->line('add'); ?> <?php echo $this->lang->line('payment'); ?>
                    </button>
                <?php } ?>
            <?php } ?>

            <?php if (has_permission(VIEW, 'pharmacy', 'refund')) { ?>
                <a href="<?php echo site_url('pharmacy/refund'); ?>" class="btn btn-xs btn-info">
                    <i class="fa fa-undo font-12 mr-5"></i>
                    <?php echo $this->lang->line('refund'); ?>
                </a>
            <?php } ?>
            <?php if (has_permission(VIEW, 'pharmacy', 'stock')) { ?>
                <a href="<?php echo site_url('pharmacy/stock'); ?>" class="btn btn-xs btn-success">
                    <i class="fa fa-bar-chart font-12 mr-5"></i>
                    <?php echo $this->lang->line('stock'); ?>
                </a>
            <?php } ?>
            <?php if (has_permission(VIEW, 'pharmacy', 'sale')) { ?>
                <a href="<?php echo site_url('pharmacy/sale'); ?>" class="btn btn-xs btn-danger">
                    <i class="fa fa-balance-scale font-12 mr-5"></i>
                    <?php echo $this->lang->line('sale'); ?>
                </a>
            <?php } ?>
        </div>
    </div>
    <!-- /Title -->


    <!-- Row -->
    <div class="row">
        <div class="col-xl-12">
            <section class="hk-sec-wrapper hk-invoice-wrap pa-35">
                <div class="invoice-to-wrap pb-0">
                    <div class="row">
                        <div class="col-md-8 mb-30">
                            <span class="d-block text-uppercase mb-5 font-13">billing to</span>
                            <h6 class="mb-5 text-uppercase"><?php echo $manufacturer->name ?? ''; ?></h6>
                            <address>
                                <span class="d-block">Address : <?php echo $manufacturer->address; ?></span>
                                <span class="d-block">Phone : <?php echo $manufacturer->phone; ?></span>
                                <span class="d-block">Email : <?php echo $manufacturer->email; ?></span>
                                <span class="d-block">Fax : <?php echo $manufacturer->fax; ?></span>
                                <span class="d-block">Web : <?php echo $manufacturer->web; ?></span>
                            </address>
                        </div>
                        <div class="col-md-4 mb-30">
                            <span class="d-block text-uppercase mb-5 font-13">billing from</span>
                            <h6 class="mb-5 text-uppercase"><?php echo $this->global_setting->brand_title; ?></h6>
                            <address>
                                <span class="d-block"><?php echo $this->global_setting->address; ?></span>
                                <span class="d-block">Phone: <?php echo $this->global_setting->phone; ?></span>
                                <span class="d-block">Email: <?php echo $this->global_setting->email; ?></span>
                                <span class="d-block text-red font-weight-bold">Invoice#: <?php echo $invoice->custom_invoice_id; ?></span>
                                <?php if ($invoice->prev_invoice_id > 0  && $invoice->prev_invoice_id != null) { ?>
                                    <span class="d-block text-mute font-11">
                                        Prev Invoice#:
                                        <?php
                                        $prev =  $this->purchase->get_single('medicine_purchases', array('id' => $invoice->prev_invoice_id));
                                        echo isset($prev->custom_invoice_id) ? $prev->custom_invoice_id : '';
                                        ?>
                                    </span>
                                <?php } ?>
                                <span class="d-block">Purchase Date: <?php echo date('d/m/Y, h:ia', strtotime($invoice->purchase_date)); ?></span>
                                <?php if ($invoice->paid_status == 'unpaid' && $invoice->due_amount > 0) { ?>
                                    <span class="d-block text-uppercase text-red font-weight-bold">Due: <?php echo $invoice->due_amount; ?></span>
                                <?php } ?>
                                <span class="d-block">Created By: <?php echo $employee->name; ?></span>
                            </address>
                        </div>
                    </div>
                </div>
                <?php if (isset($details) & !empty($details)) { ?>
                    <div class="invoice-details">
                        <div class="table-wrap">
                            <div class="table-responsive">
                                <table class="table table-sm table-border mb-0">
                                    <thead class="text-center bg-gradient-sunset">
                                        <tr>
                                            <th class="text-white font-weight-700 text-center w-10">SL. No</th>
                                            <th class="text-white font-weight-700 text-left w-15">Category</th>
                                            <th class="text-white font-weight-700 text-left w-20">Medicine Name</th>
                                            <th class="text-white font-weight-700 text-center w-10">Batch No</th>
                                            <th class="text-white font-weight-700 text-center w-15">Expiry Date</th>
                                            <th class="text-white font-weight-700 text-center w-10">Quantity</th>
                                            <th class="text-white font-weight-700 text-center w-10">Rate</th>
                                            <th class="text-white font-weight-700 text-center w-10">Total</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php $count = 1; ?>
                                        <?php foreach ($details as $obj) { ?>
                                            <tr>
                                                <td class="text-center"><?php echo $count++; ?></td>
                                                <td class="text-left"><?php echo $obj->category; ?></td>
                                                <td class="text-left"><?php echo $obj->medicine_name; ?></td>
                                                <td class="text-right"><?php echo $obj->batch_no; ?></td>
                                                <td class="text-right"><?php echo $obj->expiry_date; ?></td>
                                                <td class="text-center"><?php echo $obj->quantity; ?></td>
                                                <td class="text-right"><?php echo $obj->purchase; ?></td>
                                                <td class="text-right"><?php echo $obj->total; ?></td>
                                            </tr>
                                        <?php } ?>
                                        <tr class="bg-transparent">
                                            <td colspan="7" class="text-right text-light">Sub Total</td>
                                            <td class="text-right"><?php echo $invoice->total_bill; ?></td>
                                        </tr>
                                        <tr class="bg-transparent">
                                            <td colspan="7" class="text-right text-light border-top-0">Discount</td>
                                            <td class="text-right border-top-0"><?php echo $invoice->discount; ?></td>
                                        </tr>
                                        <tr class="bg-transparent">
                                            <td colspan="7" class="text-right text-light border-top-0">Net Payable</td>
                                            <td class="text-right border-top-0"><?php echo $invoice->net_payable; ?></td>
                                        </tr>
                                        <tr class="bg-transparent">
                                            <td colspan="7" class="text-right text-light border-top-0">Advanced Amount</td>
                                            <td class="text-right border-top-0"><?php echo $invoice->advanced; ?></td>
                                        </tr>
                                    </tbody>
                                    <tfoot class="border-bottom border-1">
                                        <tr>
                                            <th colspan="7" class="text-right font-weight-600">Due Amount</th>
                                            <th class="text-right font-weight-600 text-red"><?php echo $invoice->due_amount; ?></th>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                    </div>
                <?php } ?>
            </section>
        </div>
    </div>
    <!-- /Row -->
</div>
<!-- /Container -->

<!-- Add Payment Modal -->
<div class="modal fade" id="addPaymentModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalForms" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><?php echo $this->lang->line('add'); ?> <?php echo $this->lang->line('payment'); ?></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <form id="addPaymentForm" method="POST">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="date"><?php echo $this->lang->line('payment'); ?> <?php echo $this->lang->line('date'); ?></label>
                        <input type="text" class="form-control" name="date" id="date" value="<?php echo isset($date) ?  $date : ''; ?>" placeholder="<?php echo $this->lang->line('payment'); ?> <?php echo $this->lang->line('date'); ?>" required>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="due"><span class=" text-red"><?php echo $this->lang->line('due'); ?> <?php echo $this->lang->line('amount'); ?></span></label>
                                <input type="text" readonly class="form-control" id="due_amount" placeholder="<?php echo $this->lang->line('due'); ?> <?php echo $this->lang->line('amount'); ?>">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="second_less"><?php echo $this->lang->line('second_less'); ?></label>
                                <input type="text" class="form-control" name="second_less" id="second_less" value="<?php echo isset($second_less) ?  $second_less : ''; ?>" placeholder="<?php echo $this->lang->line('second_less'); ?>">
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="amount"><?php echo $this->lang->line('payment'); ?> <?php echo $this->lang->line('amount'); ?></label>
                        <input type="text" class="form-control" name="amount" id="amount" value="<?php echo isset($amount) ?  $amount : ''; ?>" placeholder="<?php echo $this->lang->line('payment'); ?> <?php echo $this->lang->line('amount'); ?>" required>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <div class="input-group-prepend">
                                <span class="input-group-text"><?php echo $this->lang->line('remark'); ?></span>
                            </div>
                            <textarea class="form-control" name="note" id="note" aria-label="<?php echo $this->lang->line('remark'); ?>"><?php echo isset($note) ?  $note : ''; ?></textarea>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <input type="hidden" id="invoice_id" name="invoice_id" value="<?php echo $this->uri->segment(4, 0); ?>">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal"><?php echo $this->lang->line('cancel'); ?></button>
                    <button type="submit" class="btn btn-primary"><?php echo $this->lang->line('submit'); ?></button>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">
    $("#second_less").on("keyup", function() {
        var val = +this.value || 0;
        $("#amount").val(parseFloat($("#due_amount").val() - val).toFixed(2));
    });
</script>


<!-- Daterangepicker -->
<link href="<?php echo VENDOR_URL; ?>daterangepicker/daterangepicker.css" rel="stylesheet" type="text/css" />
<script src="<?php echo VENDOR_URL; ?>moment/min/moment.min.js"></script>
<script src="<?php echo VENDOR_URL; ?>daterangepicker/daterangepicker.js"></script>

<script>
    $("#date").daterangepicker({
        singleDatePicker: true,
        startDate: new Date(),
        showDropdowns: true,
        minYear: 1901,
        timePicker: true,
        timePicker24Hour: true,
        timePickerIncrement: 1,
        timePickerSeconds: false,
        autoUpdateInput: true,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
        }
    });
</script>

<script type="text/javascript">
    function GetPaymentData(id) {
        $.ajax({
            url: '<?php echo site_url('pharmacy/purchase/get_payment_data/') ?>' + id,
            dataType: 'json',
            success: function(data) {
                $('#due_amount').val(data.amount);
                $('#amount').val(data.amount);
            }
        });
    }
    $(document).ready(function(e) {
        $("#addPaymentForm").on('submit', (function(e) {
            e.preventDefault();
            $.ajax({
                url: '<?php echo base_url('pharmacy/purchase/add_payment'); ?>',
                type: "POST",
                data: new FormData(this),
                dataType: 'json',
                contentType: false,
                cache: false,
                processData: false,
                success: function(data) {
                    if (data.status == "failed") {
                        var message = "";
                        $.each(data.error, function(index, value) {
                            message += value;
                        });
                        $.toast({
                            text: message,
                            position: 'top-right',
                            loaderBg: '#7a5449',
                            class: 'jq-toast-danger',
                            hideAfter: 3500,
                            stack: 6,
                            showHideTransition: 'fade'
                        });

                    } else {
                        $.toast({
                            text: data.message,
                            position: 'top-right',
                            loaderBg: '#7a5449',
                            class: 'jq-toast-primary',
                            hideAfter: 3500,
                            stack: 6,
                            showHideTransition: 'fade'
                        });
                        $("#addPaymentModal").hide();
                    }
                },
                complete: function() {
                    window.location.reload(true);
                }
            });
        }));
    });
</script>

<script>
    function printBill(id) {
        $.ajax({
            url: '<?php echo site_url('pharmacy/purchase/print'); ?>',
            type: 'POST',
            data: {
                id: id,
            },
            success: function(result) {
                $("#testdata").html(result);
                popup(result);
            }
        });
    }

    function popup(data) {
        var frame1 = $('<iframe />');
        frame1[0].name = "frame1";
        frame1.css({
            "position": "absolute",
            "top": "-1000000px"
        });
        $("body").append(frame1);
        var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;
        frameDoc.document.open();
        //Create a new HTML document.
        frameDoc.document.write('<html>');
        frameDoc.document.write('<head>');
        frameDoc.document.write('<title>Print Invoice</title>');

        frameDoc.document.write('</head>');
        frameDoc.document.write('<body>');
        frameDoc.document.write(data);
        frameDoc.document.write('</body>');
        frameDoc.document.write('</html>');
        frameDoc.document.close();
        setTimeout(function() {
            window.frames["frame1"].focus();
            window.frames["frame1"].print();
            frame1.remove();
        }, 500);

        return true;

    }
</script>