<link rel="stylesheet" type="text/css" href="<?= SITE_URL ?>public/admin/assets/vendor/sweetalert2/dist/sweetalert2.min.css">
<script src="<?= SITE_URL ?>public/admin/assets/vendor/sweetalert2/dist/sweetalert2.min.js"></script>
<!-- <link rel="stylesheet" href="https://cdn.datatables.net/2.0.8/css/dataTables.dataTables.css" />

<script src="https://cdn.datatables.net/2.0.8/js/dataTables.js"></script> -->
<style>
    .card {
        height: auto;
    }

    td.day.disabled {
        background-color: #ddd !important;
    }
</style>
<div class="page-titles">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="javascript:void(0)">Dashboard</a></li>
        <li class="breadcrumb-item active"><a href="javascript:void(0)">Property Rates</a></li>
    </ol>
</div>

<!-- <fieldset style="margin: 25px 0px;">
    <div class="col-md-12 text-center">
        <button type="button" class="importButton btn btn-primary waves-effect ">Import Data</button>
    </div>
</fieldset> -->


<!-- row -->
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h4 class="card-title"><?= $label ?> Property Rates Data</h4>
                <label class="card-title">Please do not include currency symbol</label>
            </div>
            <div class="card-body">
                <form action="<?= ADMIN_URL . 'property/save-rates' ?>" method="post" enctype="multipart/form-data">
                    <div class="row">
                        <?php if (isset($property_rates_edit['id'])) { ?>
                            <?php if (!empty($property_rates_edit['id'])) { ?>
                                <input type="hidden" name="id" value="<?= $property_rates_edit['id'] ?>">
                            <?php } ?>
                        <?php } ?>
                        <div class="mb-3 col-lg-3 col-md-3 col-sm-12">
                            <label class="form-label">Season Name</label>
                            <input type="text" class="form-control" placeholder="Season Name" required="" value="<?= $property_rates_edit['seasonname'] ?? '' ?>" name="name">
                        </div>
                        <div class="mb-3 col-lg-2 col-md-2 col-sm-12">
                            <label class="form-label">Maximum Persons</label>
                            <input type="number" class="form-control" placeholder="Maximum Persons" value="<?= $property_rates_edit['minimumpersons'] ?? '' ?>" name="person">
                        </div>
                        <div class="mb-3 col-lg-2 col-md-2 col-sm-12">
                            <label class="form-label">Minimum Stay Night</label>
                            <input type="number" class="form-control" placeholder="Minimum Stay Night" value="<?= $property_rates_edit['minimumstay'] ?? '' ?>" name="stay_night">
                        </div>
                        <div class="mb-3 col-lg-2 col-md-2 col-sm-12">
                            <div class="form-group">
                                <label class="form-label">From Date</label>
                                <div class='input-group date' id='startDate'>
                                    <input type="text" class="form-control start_date" placeholder="From Date" required="" value="<?= $property_rates_edit['fromdate'] ?? '' ?>" name="from">
                                    <span class="input-group-addon input-group-text">
                                        <span class="fa fa-calendar"></span>
                                    </span>
                                </div>
                            </div>
                        </div>
                        <div class="mb-3 col-lg-2 col-md-2 col-sm-12">
                            <div class="form-group">
                                <label class="form-label">To Date</label>
                                <div class='input-group date' id='endDate'>
                                    <input type="text" class="form-control end_date" placeholder="To Date" required="" value="<?= $property_rates_edit['todate'] ?? '' ?>" name="to">
                                    <span class="input-group-addon input-group-text">
                                        <span class="fa fa-calendar"></span>
                                    </span>
                                </div>
                            </div>
                        </div>
                        <div class="mb-3 col-lg-2 col-md-2 col-sm-12">
                            <label class="form-label">Nightly Rate</label>
                            <input type="number" class="form-control" placeholder="Nightly Rate" value="<?= $property_rates_edit['nightly'] ?? '' ?>" name="night">
                        </div>
                        <div class="mb-3 col-lg-2 col-md-2 col-sm-12">
                            <label class="form-label">Weekly Rate</label>
                            <input type="number" class="form-control" placeholder="Weekly Rate" value="<?= $property_rates_edit['weekly'] ?? '' ?>" name="week">
                        </div>
                        <div class="mb-3 col-lg-2 col-md-2 col-sm-12">
                            <label class="form-label">Weekend Rate</label>
                            <input type="number" class="form-control" placeholder="Weekend Rate" value="<?= $property_rates_edit['weekend'] ?? '' ?>" name="weekend">
                        </div>
                        <div class="mb-3 col-lg-2 col-md-2 col-sm-12">
                            <label class="form-label">Monthly Rate</label>
                            <input type="number" class="form-control" placeholder="Monthly Rate" value="<?= $property_rates_edit['monthly'] ?? '' ?>" name="monthly">
                        </div>
                        <div class="mb-3 col-lg-2 col-md-2 col-sm-12 pt-3">
                            <input type="submit" class="btn btn-sm btn-primary me-2" name="register_property" value="Save">
                            <a href="<?= ADMIN_URL . 'property/rates/' ?>" class="btn btn-danger btn-sm"><i class="fa fa-times"></i></a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <div class="card">
            <div class="card-header">
                <h4 class="card-title">Property Rates List</h4>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="display table" id="myTable" style="min-width: 850px">
                        <thead>
                            <tr>
                                <td>#</td>
                                <th>Season Name </th>
                                <th>Maximum Persons</th>
                                <th>Minimum Stay Night</th>
                                <th>From Date</th>
                                <th>To Date</th>
                                <th>Nightly Rate</th>
                                <th>Weekend Rate</th>
                                <th>Weekly Rate</th>
                                <th>Monthly Rate</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ($property_rates_count > 0) { ?>
                                <?php foreach ($property_rates_list as $ev) { ?>
                                    <tr>
                                        <td><?= ++$start_counter; ?></td>
                                        <td><?php echo $ev['seasonname']; ?></td>
                                        <td><?php echo $ev['minimumpersons']; ?></td>
                                        <td><?php echo $ev['minimumstay']; ?></td>
                                        <td><?php echo date('d, M Y', strtotime($ev['fromdate'])); ?></td>
                                        <td><?php echo date('d, M Y', strtotime($ev['todate'])); ?></td>
                                        <td><?php echo $ev['nightly']; ?></td>
                                        <td><?php echo $ev['weekend']; ?></td>
                                        <td><?php echo $ev['weekly']; ?></td>
                                        <td><?php echo $ev['monthly']; ?></td>
                                        <td>
                                            <a href="<?= ADMIN_URL . 'property/rates/' . encryptData($ev['id']) ?>" class="btn btn-success btn-sm"><i class="fa fa-pencil"></i> Edit</a>
                                            <a href="javascript:void(0);" onclick="deleteData(this)" data-href="<?= ADMIN_URL . 'property/delete-rates/' . encryptData($ev['id']) ?>" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i> Delete</a>
                                        </td>
                                    </tr>
                                <?php } ?>
                            <?php } else { ?>
                                <tr>
                                    <td colspan="6" class="text-center">No Records Found</td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>

                <?php if ($property_rates_count > 0) { ?>
                    <div class="col-lg-12">
                        <p class="mb-2 me-3">
                            <?= $property_rates_pagination->displayLinks(ADMIN_URL . 'property/types'); ?>
                        </p>
                    </div>
                <?php } ?>
            </div>
        </div>
        <div class="card">
            <div class="card-header">
                <h4 class="card-title">Extra Notes</h4>
            </div>
            <div class="card-body">
                <form action="<?= ADMIN_URL . 'property/save-additional-rates' ?>" method="post">
                    <div class="row">
                        <div class="mb-3 col-lg-3 col-md-3 col-sm-12">
                            <label class="form-label">Admin Fee</label>
                            <input type="text" class="form-control" placeholder="Admin Fee" value="<?= $property_data['admin_fee'] ?? '' ?>" name="admin_fee">
                        </div>

                        <div class="mb-3 col-lg-3 col-md-3 col-sm-12">
                            <label class="form-label">Cleaning Fee</label>
                            <input type="text" class="form-control" placeholder="Cleaning Fee" value="<?= $property_data['cleaning_fee'] ?? '' ?>" name="cleaning_fee">
                        </div>

                        <div class="mb-3 col-lg-3 col-md-3 col-sm-12">
                            <label class="form-label">Refundable Damage Deposit fee</label>
                            <input type="text" class="form-control" placeholder="Refundable Damage Deposit fee" value="<?= $property_data['refundable_damage_deposit_fee'] ?? '' ?>" name="refundable_damage_deposit_fee">
                        </div>

                        <div class="mb-3 col-lg-3 col-md-3 col-sm-12">
                            <label class="form-label">Damage Waiver Fee</label>
                            <input type="text" class="form-control" placeholder="Damage Waiver Fee" value="<?= $property_data['damage_waiver_fee'] ?? '' ?>" name="damage_waiver_fee">
                        </div>

                        <div class="mb-3 col-lg-3 col-md-3 col-sm-12">
                            <label class="form-label">Pet Fee</label>
                            <input type="text" class="form-control" placeholder="Pet Fee" value="<?= $property_data['pet_fee'] ?? '' ?>" name="pet_fee">
                        </div>

                        <div class="mb-3 col-lg-3 col-md-3 col-sm-12">
                            <label class="form-label">Extra Person Fee</label>
                            <input type="text" class="form-control" placeholder="Extra Person Fee" value="<?= $property_data['extra_person_fee'] ?? '' ?>" name="extra_person_fee">
                        </div>

                        <div class="mb-3 col-lg-3 col-md-3 col-sm-12">
                            <label class="form-label">Pool Heating Fee</label>
                            <input type="text" class="form-control" placeholder="Pool Heating Fee" value="<?= $property_data['pool_heating_fee'] ?? '' ?>" name="pool_heating_fee">
                        </div>

                        <div class="mb-3 col-lg-3 col-md-3 col-sm-12">
                            <label class="form-label">Change Over</label>
                            <input type="text" class="form-control" placeholder="Change Over" value="<?= $property_data['change_over'] ?? '' ?>" name="change_over">
                        </div>

                        <div class="mb-3 col-lg-3 col-md-3 col-sm-12">
                            <label class="form-label">Service Fee(%)</label>
                            <input type="text" class="form-control" placeholder="Service Fee(%)" value="<?= $property_data['service_fee'] ?? '' ?>" name="service_fee">
                        </div>

                        <div class="mb-3 col-lg-3 col-md-3 col-sm-12">
                            <label class="form-label">Tax(%)</label>
                            <input type="text" class="form-control" placeholder="Tax(%)" value="<?= $property_data['tax'] ?? '' ?>" name="tax">
                        </div>

                        <div class="mb-3 col-lg-3 col-md-3 col-sm-12">
                            <label class="form-label">Check In Time</label>
                            <input type="time" class="form-control" placeholder="Check In" value="<?= $property_data['check_in_time'] ?? '' ?>" name="check_in_time">
                        </div>

                        <div class="mb-3 col-lg-3 col-md-3 col-sm-12">
                            <label class="form-label">Check Out Time</label>
                            <input type="time" class="form-control" placeholder="Check Out" value="<?= $property_data['check_out_time'] ?? '' ?>" name="check_out_time">
                        </div>

                        <div class="mb-3 col-lg-12 col-md-12 col-sm-12">
                            <textarea class="form-control" placeholder="note" id="note" name="note"><?= $property_data['rate_note'] ?? '' ?></textarea>
                        </div>

                        <div class="mb-3 col-lg-12 col-md-12 col-sm-12 pt-3">
                            <input type="submit" class="btn btn-sm btn-success me-2" name="register_property" value="Update">
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>


<script src='https://cdn.ckeditor.com/4.14.0/full-all/ckeditor.js'></script>
<script>
    var editor = CKEDITOR.replace('note', {
        height: 300,
        extraPlugins: 'mentions',
        filebrowserUploadUrl: '/uploader/upload.php',
    });
    // $(document).ready(function() {
    //     $('#myTable').DataTable();
    // });
</script>

<script>
    $(document).on('click', ".importButton", function() {
        new swal({
            title: "Import Data",
            html: "<div class='card'><div class='card-body'><div class='row'><div class='col-md-12'>" +
                "<a href='<?= SITE_URL . 'public/admin/rates_sample.csv' ?>' download><h4>Download sample file</h4></a>" +
                "</div>" +
                "<div class='col-md-12'>" +
                "<form action='<?= ADMIN_URL . 'property/import-rates' ?>' method='post' enctype='multipart/form-data'>" +
                "<div class='form-group'>" +
                "<label for='file' class='form-label'>Upload file :</label>" +
                "<input type='file' id='file' name='file' class='form-control' required  oninvalid='this.setCustomValidity(`Please Enter Rates CSV File`) oninput='this.setCustomValidity('')'/>" +
                "</div>" +
                "<div class='btn-group'>" +
                "<input type='submit' name='importSubmit' class='btn btn-primary' value='Upload' style='display: unset;'/>" +
                "</div>" +
                "</form>" +
                "</div>" +
                "</div>" +
                "</div>" +
                "</div>",
        });
    });
    $(document).ready(function() {
        // var date = new Date();
        // date.setDate(date.getDate());
        $('#startDate').datepicker({
            format: 'yyyy-mm-dd',
            // startDate: date,
            todayHighlight: 'TRUE',
            autoclose: true,
        }).on('changeDate', function(ev) {
            $('.start_date').val($('#startDate').data().datepicker.getFormattedDate('yyyy-mm-dd'));
        });

        $('#endDate').datepicker({
            format: 'yyyy-mm-dd',
            // startDate: date,
            todayHighlight: 'TRUE',
            autoclose: true,
        }).on('changeDate', function() {
            $('.end_date').val($('#endDate').data().datepicker.getFormattedDate('yyyy-mm-dd'));
        });



        // var disableDates = ["19-11-2024", "16-11-2024", "17-11-2024", "27-12-2024"];
        // $('.datepicker').datepicker({
        //     format: 'mm/dd/yyyy',
        //     beforeShowDay: function(date) {
        //         dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
        //         if (disableDates.indexOf(dmy) != -1) {
        //             return false;
        //         } else {
        //             return true;
        //         }
        //     }
        // });
    });
</script>

<link rel="stylesheet" type="text/css" href="<?= SITE_URL ?>public/admin/assets/vendor/sweetalert2/dist/sweetalert2.min.css">
<script src="<?= SITE_URL ?>public/admin/assets/vendor/sweetalert2/dist/sweetalert2.min.js"></script>
<script>
    function deleteData(_this) {
        let url = $(_this).attr('data-href');
        new swal({
            title: 'Are you sure?',
            text: "It will permanently deleted !",
            type: 'danger',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, delete it!'
        }).then((result) => {
            if (result.isConfirmed) {
                window.location.href = url;
            } else if (result.isDenied) {}
        })
    }
</script>