<!-- 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('ipd'); ?></li>
    </ol>
</nav>
<!-- /Breadcrumb -->

<!-- Container -->
<div class="container-fluid">
    <!-- Row -->
    <div class="row">
        <?php echo $this->load->view('ipd/patient-info'); ?>

        <!-- col-lg-12 -->
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between">
                    <?php if (has_permission(VIEW, 'ipd', 'charge')) { ?>
                        <h6 class="card-header d-flex align-items-center">
                            <i class="fa fa-money font-30 mr-10"></i>
                            <?php echo $this->lang->line('charge'); ?> <?php echo $this->lang->line('list'); ?>
                        </h6>
                    <?php } ?>
                    <?php echo form_open_multipart(site_url('ipd/charge/index/' . $patient->id), array('name' => 'search', 'id' => 'search', 'class' => 'needs-validation', 'novalidate' => 'novalidate', 'method' => 'get'), ''); ?>
                    <div class="form-group">
                        <div class="input-group input-group-sm">
                            <input type="text" class="form-control" name="search" id="search" value="<?php echo isset($search) ?  $search : ''; ?>" placeholder="<?php echo $this->lang->line('search'); ?>">
                            <div class="input-group-append">
                                <button class="btn btn-gradient-primary" type="submit"><?php echo $this->lang->line('search'); ?></button>
                            </div>
                        </div>
                    </div>
                    <?php echo form_close(); ?>

                    <?php if ($patient->discharged == 0 || $this->session->userdata('role_id') == SUPER_ADMIN ||  !empty(custom_permission('modified_discharged'))) { ?>
                        <?php if (has_permission(ADD, 'ipd', 'charge')) { ?>
                            <div class="align-items-center">
                                <button type="button" class="btn btn-xs btn-primary" data-toggle="modal" data-target="#addChargeModal">
                                    <i class="fa fa-plus font-12 mr-5"></i>
                                    <?php echo $this->lang->line('add'); ?> <?php echo $this->lang->line('charge'); ?>
                                </button>
                            </div>
                        <?php } ?>
                    <?php } ?>
                </div>
                <div class="card-body">
                    <div class="table-wrap">
                        <div class="table-responsive">
                            <table class="table table-sm mb-0">
                                <thead>
                                    <tr>
                                        <th><?php echo $this->lang->line('sl_no'); ?></th>
                                        <th><?php echo $this->lang->line('date'); ?></th>
                                        <th><?php echo $this->lang->line('name'); ?></th>
                                        <th><?php echo $this->lang->line('rate'); ?></th>
                                        <th><?php echo $this->lang->line('quantity'); ?></th>
                                        <th><?php echo $this->lang->line('charge'); ?></th>
                                        <th><?php echo $this->lang->line('status'); ?></th>
                                        <th class="w-15"><?php echo $this->lang->line('action'); ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php if (isset($charges) && !empty($charges)) { ?>
                                        <?php foreach ($charges as $obj) { ?>
                                            <tr>
                                                <td><?php echo $sl_count; ?></td>
                                                <td><?php echo date('d/m/Y h:i A', strtotime($obj->date)); ?></td>
                                                <td><?php echo $obj->charge_name; ?></td>
                                                <td><?php echo $obj->rate; ?></td>
                                                <td><?php echo $obj->quantity; ?></td>
                                                <td><?php echo $obj->charge; ?></td>
                                                <td>
                                                    <?php if ($obj->charge_status == 'complete') {
                                                        echo "<span class='badge badge-success'>" . $this->lang->line('complete') . "</span>";
                                                    } elseif ($obj->charge_status == 'rejected') {
                                                        echo "<span class='badge badge-danger'>" . $this->lang->line('rejected') . "</span>";
                                                    } elseif ($obj->charge_status == 'canceled') {
                                                        echo "<span class='badge badge-warning'>" . $this->lang->line('cancel') . "</span>";
                                                    } else {
                                                        echo "<span class='badge badge-info'>" . $this->lang->line('pending') . "</span>";
                                                    } ?>
                                                </td>
                                                <td>
                                                    <?php if (has_permission(VIEW, 'ipd', 'charge')) { ?>
                                                        <button type="button" onclick="get_view_modal(<?php echo $obj->id; ?>);" data-toggle="modal" data-target=".viewModalCenter" class="btn btn-primary btn-xs">
                                                            <i class="fa fa-eye"></i>
                                                        </button>
                                                    <?php } ?>

                                                    <?php if ($patient->discharged == 0 || $this->session->userdata('role_id') == SUPER_ADMIN ||  !empty(custom_permission('modified_discharged'))) { ?>
                                                        <?php if (has_permission(EDIT, 'ipd', 'charge')) { ?>
                                                            <button type="button" onclick="editCharge('<?php echo $obj->id; ?>')" class="btn btn-xs btn-success" data-toggle="modal" data-target="#editChargeModal">
                                                                <i class="fa fa-pencil-square-o"></i>
                                                            </button>
                                                        <?php } ?>
                                                    <?php } ?>

                                                    <?php if ($patient->discharged == 0 || $this->session->userdata('role_id') == SUPER_ADMIN ||  !empty(custom_permission('modified_discharged'))) { ?>
                                                        <?php if (has_permission(DELETE, 'ipd', 'charge')) { ?>
                                                            <a class="btn btn-xs btn-danger round" href="<?php echo site_url('ipd/charge/delete/' . $obj->id); ?>" onclick="javascript: return confirm('<?php echo $this->lang->line('confirm_alert'); ?>');"> <i class="fa fa-trash text-white"></i></a>
                                                        <?php } ?>
                                                    <?php } ?>
                                                </td>
                                            </tr>
                                            <?php $sl_count++; ?>
                                        <?php } ?>
                                    <?php } else { ?>
                                        <tr class="text-center">
                                            <td colspan="8"><?php echo $this->lang->line('no_data_found'); ?></td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <!-- Card Footer -->
                <?php if ($this->pagination->create_links()) { ?>
                    <div class="card-footer justify-content-end">
                        <?php echo $this->pagination->create_links(); ?>
                    </div>
                <?php } ?>
            </div>
        </div>
    </div>
    <!-- /Row -->
</div>
<!-- /Container -->

<!-- Add Consultan Modal-->
<div class="modal fade" id="addChargeModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalForms" aria-hidden="true">
    <div class="modal-dialog modal-lg 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('charge'); ?></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <form id="addChargeForm" enctype="multipart/form-data" method="POST" action="<?php echo site_url('ipd/charge/add'); ?>">
                <div class="modal-body">
                    <div class="form-row">
                        <div class="col-md-6 mb-10">
                            <label for="charge_id"><?php echo $this->lang->line('name'); ?></label>
                            <select class="form-control custom-select select2" name="charge_id" id="charge_id" onchange="get_charge(this.value)" required>
                                <option selected value=""><?php echo $this->lang->line('select'); ?></option>
                                <?php foreach ($charge_items as $obj) { ?>
                                    <option value="<?php echo $obj->id; ?>" <?php echo isset($charge_id) && $charge_id == $obj->id ? 'selected="selected"' : ""; ?>><?php echo $obj->name; ?></option>
                                <?php } ?>
                            </select>
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="rate"><?php echo $this->lang->line('rate'); ?></label>
                            <input type="text" class="form-control" name="rate" id="rate" value="<?php echo isset($rate) ?  $rate : ''; ?>" placeholder="<?php echo $this->lang->line('rate'); ?>" required>
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="quantity"><?php echo $this->lang->line('quantity'); ?></label>
                            <input type="text" min="0" class="form-control" name="quantity" id="quantity" value="<?php echo isset($quantity) ?  $quantity : '1'; ?>" placeholder="<?php echo $this->lang->line('quantity'); ?>" required>
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="charge"><?php echo $this->lang->line('charge'); ?></label>
                            <input type="text" class="form-control" name="charge" id="charge" value="<?php echo isset($charge) ?  $charge : ''; ?>" placeholder="<?php echo $this->lang->line('charge'); ?>" required>
                        </div>
                        <div class="col-md-3 mb-10">
                            <label for="date"><?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('date'); ?>" required>
                        </div>
                        <div class="col-md-9 mb-10">
                            <label for="note"><?php echo $this->lang->line('remark'); ?></label>
                            <input type="text" class="form-control" name="note" id="note" value="<?php echo isset($note) ?  $note : ''; ?>" placeholder="<?php echo $this->lang->line('remark'); ?>">
                        </div>
                    </div>
                </div>
                <div class="modal-footer pt-0">
                    <div class="button-list">
                        <input type="hidden" name="ipd_id" value="<?php echo $patient->id; ?>">
                        <input type="hidden" name="patient_id" value="<?php echo $patient->patient_id; ?>">
                        <button type="button" class="btn btn-gradient-danger" data-dismiss="modal"><?php echo $this->lang->line('cancel'); ?></button>
                        <button class="btn btn-gradient-primary" type="submit"><?php echo $this->lang->line('submit'); ?></button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Edit Charge Modal-->
<div class="modal fade" id="editChargeModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalForms" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><?php echo $this->lang->line('edit'); ?> <?php echo $this->lang->line('charge'); ?></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <form id="editChargeForm" enctype="multipart/form-data" method="POST" action="#">
                <div class="modal-body">
                    <div class="form-row">
                        <div class="col-md-6 mb-10">
                            <label for="edit_charge_id"><?php echo $this->lang->line('name'); ?></label>
                            <select class="form-control custom-select select2" name="charge_id" id="edit_charge_id" onchange="edit_get_charge(this.value)" required>
                                <option selected value=""><?php echo $this->lang->line('select'); ?></option>
                                <?php foreach ($charge_items as $obj) { ?>
                                    <option value="<?php echo $obj->id; ?>" <?php echo isset($charge_id) && $charge_id == $obj->id ? 'selected="selected"' : ""; ?>><?php echo $obj->name; ?></option>
                                <?php } ?>
                            </select>
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="edit_rate"><?php echo $this->lang->line('rate'); ?></label>
                            <input type="text" class="form-control" name="rate" id="edit_rate" value="<?php echo isset($rate) ?  $rate : ''; ?>" placeholder="<?php echo $this->lang->line('rate'); ?>" required>
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="edit_quantity"><?php echo $this->lang->line('quantity'); ?></label>
                            <input type="text" min="0" class="form-control" name="quantity" id="edit_quantity" value="<?php echo isset($quantity) ?  $quantity : ''; ?>" placeholder="<?php echo $this->lang->line('quantity'); ?>" required>
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="edit_charge"><?php echo $this->lang->line('charge'); ?></label>
                            <input type="text" class="form-control" name="charge" id="edit_charge" value="<?php echo isset($charge) ?  $charge : ''; ?>" placeholder="<?php echo $this->lang->line('charge'); ?>" required>
                        </div>
                        <div class="col-md-3 mb-10">
                            <label for="edit_date"><?php echo $this->lang->line('date'); ?></label>
                            <input type="text" class="form-control" name="date" id="edit_date" value="<?php echo isset($date) ?  $date : ''; ?>" placeholder="<?php echo $this->lang->line('date'); ?>" required>
                        </div>
                        <div class="col-md-3 mb-10">
                            <label for="edit_charge_status"><?php echo $this->lang->line('status'); ?></label>
                            <select class="form-control custom-select" name="charge_status" id="edit_charge_status" required>
                                <?php $invoice_status = get_invoice_status(); ?>
                                <?php foreach ($invoice_status as $key => $value) { ?>
                                    <option value="<?php echo $key; ?>" <?php echo isset($charge_status) && $charge_status == $key ? 'selected="selected"' : ""; ?>><?php echo $value; ?></option>
                                <?php } ?>
                            </select>
                        </div>
                        <div class="col-md-6 mb-10">
                            <label for="edit_note"><?php echo $this->lang->line('remark'); ?></label>
                            <input type="text" class="form-control" name="note" id="edit_note" value="<?php echo isset($note) ?  $note : ''; ?>" placeholder="<?php echo $this->lang->line('remark'); ?>">
                        </div>
                    </div>
                </div>
                <div class="modal-footer pt-0">
                    <div class="button-list">
                        <input type="hidden" name="edit_id" id="edit_id">
                        <input type="hidden" name="ipd_id" id="edit_ipd_id" value="<?php echo $patient->id; ?>">
                        <input type="hidden" name="patient_id" id="edit_patient_id" value="<?php echo $patient->patient_id; ?>">
                        <button type="button" class="btn btn-gradient-danger" data-dismiss="modal"><?php echo $this->lang->line('cancel'); ?></button>
                        <button class="btn btn-gradient-primary" type="submit"><?php echo $this->lang->line('update'); ?></button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


<script>
    function editCharge(id) {
        $.ajax({
            url: '<?php echo site_url('ipd/charge/get_charge/') ?>' + id,
            dataType: 'json',
            success: function(data) {
                $('#edit_id').val(data.id);
                $('#edit_ipd_id').val(data.ipd_id);
                $('#edit_patient_id').val(data.patient_id);
                $('#edit_charge_id').val(data.charge_id).trigger('change');
                $('#edit_rate').val(Math.floor(data.rate));
                $('#edit_quantity').val(data.quantity);
                $('#edit_charge').val(Math.floor(data.charge));
                $('#edit_date').val(data.date);
                $('#edit_charge_status').val(data.charge_status);
                $('#edit_note').val(data.note);
            }
        });
    }

    $(document).ready(function(e) {
        $("#editChargeForm").on('submit', (function(e) {
            e.preventDefault();
            if ($("#edit_doctor_id").val() != "" && $("#edit_date").val() != "") {
                $.ajax({
                    url: '<?php echo site_url('ipd/charge/edit') ?>',
                    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) {
                                $("." + index).html(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'
                            });
                            location.reload(true);
                        }
                        $('#editChargeForm').find('form')[0].reset();
                    },
                    error: function() {
                        alert("Fail")
                    }
                });
            } else {
                $.toast({
                    text: "Please select all required field",
                    position: 'top-right',
                    loaderBg: '#7a5449',
                    class: 'jq-toast-danger',
                    hideAfter: 3500,
                    stack: 6,
                    showHideTransition: 'fade'
                });
            }
        }));
    });
</script>

<div class="modal fade viewModalCenter" tabindex="-1" role="dialog" aria-labelledby="viewModalCenter" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header bg-gradient-primary">
                <h5 class="modal-title text-white"><?php echo $this->lang->line('charge'); ?> <?php echo $this->lang->line('info'); ?></h5>
                <button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body modal_data bg-gradient-secondary"></div>
        </div>
    </div>
</div>

<script type="text/javascript">
    function get_view_modal(id) {
        $('.modal_data').html('<p style="padding: 20px;"><p style="padding: 20px;text-align:center;"><img src="<?php echo IMG_URL; ?>loader.gif" /></p>');
        $.ajax({
            type: "POST",
            url: "<?php echo site_url('ipd/charge/get_single_charge'); ?>",
            data: {
                id: id
            },
            success: function(response) {
                if (response) {
                    $('.modal_data').html(response);
                }
            }
        });
    }
</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>

<!-- Select2 -->
<link href="<?php echo VENDOR_URL; ?>select2/dist/css/select2.min.css" rel="stylesheet" type="text/css" />
<script src="<?php echo VENDOR_URL; ?>select2/dist/js/select2.full.min.js"></script>

<style>
    .select2-container .select2-selection--single {
        height: calc(2.25rem + 4px) !important;
    }

    .select2-container--default .select2-search--dropdown .select2-search__field {
        color: #324148;
        border-width: 2px;
        border-color: #e0e3e4;
        padding: .375rem .75rem;
        height: calc(2.25rem + 4px);
    }

    .select2-container--default .select2-selection--single .select2-selection__arrow b {
        margin-top: 5px;
    }
</style>
<script>
    $(".select2").select2();

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

<script type="text/javascript">
    function get_charge(charge_id) {
        $.ajax({
            type: "POST",
            url: "<?php echo site_url('ipd/charge/get_ipd_charge'); ?>",
            data: {
                charge_id: charge_id
            },
            dataType: 'json',
            success: function(response) {
                if (response) {
                    $('#rate').val(Math.floor(response.charge));
                    $('#charge').val($('#rate').val() * $('#quantity').val());
                } else {
                    $('#rate').val('0');
                }
            }
        })
    }

    function edit_get_charge(charge_id) {
        $.ajax({
            type: "POST",
            url: "<?php echo site_url('ipd/charge/get_ipd_charge'); ?>",
            data: {
                charge_id: charge_id
            },
            dataType: 'json',
            success: function(response) {
                if (response) {
                    $('#edit_rate').val(Math.floor(response.charge));
                } else {
                    $('#edit_rate').val('0');
                }
            }
        })
    }

    $('#rate, #quantity').keyup(function() {
        var rate = parseFloat($('#rate').val()) || 0;
        var box = parseFloat($('#quantity').val()) || 0;
        $('#charge').val(rate * box);
    });

    $('#edit_rate, #edit_quantity').keyup(function() {
        var rate = parseFloat($('#edit_rate').val()) || 0;
        var box = parseFloat($('#edit_quantity').val()) || 0;
        $('#edit_charge').val(rate * box);
    });
</script>