<!-- 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">
    <!-- Row -->
    <div class="row">
        <!-- col-lg-12 -->
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between">
                    <?php if (has_permission(EDIT, 'ipd', 'ipd')) { ?>
                        <h6 class="card-header d-flex align-items-center">
                            <i class="fa fa-user-plus font-30 mr-10"></i>
                            <?php echo $this->lang->line('edit'); ?> <?php echo $this->lang->line('patient'); ?>
                        </h6>
                    <?php } ?>
                    <div class="align-items-center">
                        <?php if (has_permission(VIEW, 'ipd', 'ipd')) { ?>
                            <a href="<?php echo site_url('ipd/admitted'); ?>" class="btn btn-info btn-xs mr-10">
                                <i class="fa fa-users"></i>
                                <?php echo $this->lang->line('admitted'); ?> <?php echo $this->lang->line('patient'); ?>
                            </a>
                        <?php } ?>
                        <?php if (has_permission(ADD, 'ipd', 'ipd')) { ?>
                            <a href="<?php echo site_url('ipd/existing'); ?>" class="btn btn-success btn-xs mr-10">
                                <i class="fa fa-users"></i>
                                <?php echo $this->lang->line('existing'); ?> <?php echo $this->lang->line('patient'); ?>
                            </a>
                        <?php } ?>
                        <?php if (has_permission(VIEW, 'ipd', 'ipd')) { ?>
                            <a href="<?php echo site_url('ipd/discharge'); ?>" class="btn btn-xs btn-danger">
                                <i class="fa fa-users font-12 mr-5"></i>
                                <?php echo $this->lang->line('discharge'); ?> <?php echo $this->lang->line('patient'); ?> <?php echo $this->lang->line('list'); ?>
                            </a>
                        <?php } ?>
                        <?php if (has_permission(VIEW, 'ipd', 'ipd')) { ?>
                            <a href="<?php echo site_url('ipd/admitted'); ?>" class="btn btn-xs btn-primary">
                                <i class="fa fa-list font-12 mr-5"></i>
                                <?php echo $this->lang->line('admitted'); ?> <?php echo $this->lang->line('patient'); ?> <?php echo $this->lang->line('list'); ?>
                            </a>
                        <?php } ?>
                    </div>
                </div>

                <?php echo form_open_multipart(site_url('ipd/edit/' . $ipd->id), array('name' => 'edit', 'id' => 'edit', 'class' => 'needs-validation', 'novalidate' => 'novalidate'), ''); ?>
                <div class="card-body">
                    <div class="form-row">
                        <div class="col-md-5 mb-10">
                            <label for="doctor_id"><?php echo $this->lang->line('ref_by'); ?></label>
                            <select class="form-control custom-select select2" name="doctor_id" id="doctor_id">
                                <option selected value=""><?php echo $this->lang->line('select'); ?></option>
                                <?php foreach ($doctors as $obj) { ?>
                                    <option value="<?php echo $obj->user_id; ?>" <?php echo isset($ipd->doctor_id) && $ipd->doctor_id == $obj->user_id ? 'selected="selected"' : ""; ?>><?php echo $obj->name; ?></option>
                                <?php } ?>
                            </select>
                        </div>
                        <div class="col-md-5 mb-10">
                            <label for="reference_id"><?php echo $this->lang->line('reference'); ?></label>
                            <select class="form-control custom-select select2" name="reference_id" id="reference_id">
                                <option selected value=""><?php echo $this->lang->line('select'); ?></option>
                                <?php foreach ($references as $obj) { ?>
                                    <option value="<?php echo $obj->user_id; ?>" <?php echo isset($ipd->reference_id) && $ipd->reference_id == $obj->user_id ? 'selected="selected"' : ""; ?>><?php echo $obj->name; ?></option>
                                <?php } ?>
                            </select>
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="admission_date"><?php echo $this->lang->line('admission'); ?> <?php echo $this->lang->line('date'); ?> <span class="required">*</span></label>
                            <input type="text" class="form-control" name="admission_date" id="admission_date" value="<?php echo isset($ipd->admission_date) ?  date('Y-m-d H:i', strtotime($ipd->admission_date)) : ''; ?>" placeholder="<?php echo $this->lang->line('admission'); ?> <?php echo $this->lang->line('date'); ?>" required>
                            <div class="invalid-feedback">
                                <?php echo $this->lang->line('required_field'); ?>
                            </div>
                            <small id="admission_date" class="form-text text-muted">
                                <?php echo form_error('admission_date'); ?>
                            </small>
                        </div>
                        <div class="col-md-3 mb-10">
                            <label for="bed_group_id"><?php echo $this->lang->line('admitted'); ?> <?php echo $this->lang->line('ward'); ?> <span class="required">*</span></label>
                            <select class="form-control custom-select select2" name="bed_group_id" id="bed_group_id" required onchange="get_bed(this.value)">
                                <option selected value=""><?php echo $this->lang->line('select'); ?></option>
                                <?php foreach ($bed_groups as $obj) { ?>
                                    <option value="<?php echo $obj->id; ?>" <?php echo isset($ipd->bed_group_id) && $ipd->bed_group_id == $obj->id ? 'selected="selected"' : ""; ?>><?php echo $obj->name; ?> [<?php echo $obj->floor_name; ?>]</option>
                                <?php } ?>
                            </select>
                            <div class="invalid-feedback">
                                <?php echo $this->lang->line('required_field'); ?>
                            </div>
                            <small id="bed_group_id" class="form-text text-muted">
                                <?php echo form_error('bed_group_id'); ?>
                            </small>
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="bed_id"><?php echo $this->lang->line('bed'); ?> <?php echo $this->lang->line('number'); ?> <span class="required">*</span></label>
                            <select class="form-control custom-select select2" name="bed_id" id="bed_id" required>
                                <option selected value=""><?php echo $this->lang->line('select'); ?></option>
                                <?php foreach ($beds as $obj) { 
								if($ipd->bed_id == $obj->id || $obj->is_active == 0){ ?>
                                    <option value="<?php echo $obj->id; ?>" <?php echo isset($ipd->bed_id) && $ipd->bed_id == $obj->id ? 'selected="selected"' : ""; ?>><?php echo $obj->name; ?></option>
                                <?php } } ?>
                            </select>
                            <div class="invalid-feedback">
                                <?php echo $this->lang->line('required_field'); ?>
                            </div>
                            <small id="bed_id" class="form-text text-muted">
                                <?php echo form_error('bed_id'); ?>
                            </small>
                        </div>
                        <div class="col-md-5 mb-10">
                            <label for="contract_person"><?php echo $this->lang->line('contract'); ?> <?php echo $this->lang->line('person'); ?></label>
                            <select class="form-control custom-select select2" name="contract_person" id="contract_person">
                                <option selected value=""><?php echo $this->lang->line('select'); ?></option>
                                <?php foreach ($contracts as $obj) { ?>
                                    <option value="<?php echo $obj->user_id; ?>" <?php echo isset($ipd->contract_person) && $ipd->contract_person == $obj->user_id ? 'selected="selected"' : ""; ?>><?php echo $obj->name; ?></option>
                                <?php } ?>
                            </select>
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="contract_amount"><?php echo $this->lang->line('contract'); ?> <?php echo $this->lang->line('amount'); ?></label>
                            <input type="number" class="form-control" name="contract_amount" id="contract_amount" value="<?php echo isset($ipd->contract_amount) ?  $ipd->contract_amount : ''; ?>" placeholder="<?php echo $this->lang->line('contract'); ?> <?php echo $this->lang->line('amount'); ?>">
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="disease"><?php echo $this->lang->line('disease'); ?></label>
                            <select class="form-control custom-select" name="disease" id="disease">
                                <option selected value=""><?php echo $this->lang->line('select'); ?></option>
                                <?php $diseases = get_ipd_disease(); ?>
                                <?php foreach ($diseases as $key => $value) { ?>
                                    <option value="<?php echo $key; ?>" <?php echo isset($ipd->disease) && $ipd->disease == $key ? 'selected="selected"' : ""; ?>><?php echo $value; ?></option>
                                <?php } ?>
                            </select>
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="case"><?php echo $this->lang->line('case'); ?></label>
                            <input type="text" class="form-control" name="case" id="case" value="<?php echo isset($ipd->case) ?  $ipd->case : ''; ?>" placeholder="<?php echo $this->lang->line('case'); ?>" autocomplete="off">
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="pulse"><?php echo $this->lang->line('pulse'); ?></label>
                            <input type="text" class="form-control" name="pulse" id="pulse" value="<?php echo isset($ipd->pulse) ?  $ipd->pulse : ''; ?>" placeholder="<?php echo $this->lang->line('pulse'); ?>">
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="bp"><?php echo $this->lang->line('bp'); ?></label>
                            <input type="text" class="form-control" name="bp" id="bp" value="<?php echo isset($ipd->bp) ?  $ipd->bp : ''; ?>" placeholder="<?php echo $this->lang->line('bp'); ?>">
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="temp"><?php echo $this->lang->line('temp'); ?></label>
                            <input type="text" class="form-control" name="temp" id="temp" value="<?php echo isset($ipd->temp) ?  $ipd->temp : ''; ?>" placeholder="<?php echo $this->lang->line('temp'); ?>">
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="height"><?php echo $this->lang->line('height'); ?></label>
                            <input type="text" class="form-control" name="height" id="height" value="<?php echo isset($ipd->height) ?  $ipd->height : ''; ?>" placeholder="<?php echo $this->lang->line('height'); ?>">
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="weight"><?php echo $this->lang->line('weight'); ?></label>
                            <input type="text" class="form-control" name="weight" id="weight" value="<?php echo isset($ipd->weight) ?  $ipd->weight : ''; ?>" placeholder="<?php echo $this->lang->line('weight'); ?>">
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="casualty"><?php echo $this->lang->line('casualty'); ?></label>
                            <select class="form-control custom-select" name="casualty" id="casualty">
                                <option value="0" <?php echo isset($ipd->casualty) && $ipd->casualty == 0 ? 'selected="selected"' : ""; ?>><?php echo $this->lang->line('no'); ?></option>
                                <option value="1" <?php echo isset($ipd->casualty) && $ipd->casualty == 1 ? 'selected="selected"' : ""; ?>><?php echo $this->lang->line('yes'); ?></option>
                            </select>
                        </div>
                        <div class="col-md-2 mb-10">
                            <label for="symptoms"><?php echo $this->lang->line('symptoms'); ?></label>
                            <input type="text" class="form-control" name="symptoms" id="symptoms" value="<?php echo isset($ipd->symptoms) ?  $ipd->symptoms : ''; ?>" placeholder="<?php echo $this->lang->line('symptoms'); ?>">
                        </div>
                        <div class="col-md-6 mb-10">
                            <label for="remark"><?php echo $this->lang->line('remark'); ?></label>
                            <input type="text" class="form-control" name="remark" id="remark" value="<?php echo isset($ipd->remark) ?  $ipd->remark : ''; ?>" placeholder="<?php echo $this->lang->line('remark'); ?>" autocomplete="off">
                        </div>
                        <div class="col-md-6 mb-10">
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <span class="input-group-text"><?php echo $this->lang->line('patient'); ?> <?php echo $this->lang->line('history'); ?></span>
                                </div>
                                <textarea class="form-control" name="patient_history" id="patient_history" aria-label="<?php echo $this->lang->line('patient'); ?> <?php echo $this->lang->line('history'); ?>"><?php echo isset($ipd->patient_history) ?  $ipd->patient_history : ''; ?></textarea>
                            </div>
                        </div>
                        <div class="col-md-6 mb-10">
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <span class="input-group-text"><?php echo $this->lang->line('operation'); ?> <?php echo $this->lang->line('note'); ?></span>
                                </div>
                                <textarea class="form-control" name="operation_note" id="operation_note" aria-label="<?php echo $this->lang->line('operation'); ?> <?php echo $this->lang->line('note'); ?>"><?php echo isset($ipd->operation_note) ?  $ipd->operation_note : ''; ?></textarea>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Card Footer -->
                <div class="card-footer pt-0">
                    <div class="button-list">
                        <input type="hidden" name="patient_id" value="<?php echo $ipd->patient_id; ?>">
                        <input type="hidden" name="id" value="<?php echo $ipd->id; ?>">
                        <a class="btn btn-gradient-danger" href="<?php echo site_url('ipd/admitted'); ?>"><?php echo $this->lang->line('cancel'); ?></a>
                        <button class="btn btn-gradient-primary" type="submit"><?php echo $this->lang->line('update'); ?></button>
                    </div>
                </div>
                <?php echo form_close(); ?>
            </div>
        </div>
    </div>
    <!-- /Row -->
</div>
<!-- /Container -->

<!-- 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();

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

<script>
    function get_bed(bed_group_id) {
        $.ajax({
            type: "POST",
            url: "<?php echo site_url('ajax/get_bed_group_by_bed'); ?>",
            data: {
                bed_group_id: bed_group_id
            },
            async: false,
            success: function(response) {
                if (response) {
                    $('#bed_id').html(response);
                }
            }
        });
    };
</script>