<?php include 'session.php';
include("db/db.php");
setUserSignOut() ?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">

    <?php include 'header.php'; ?>
    <style type="text/css">
        .pickdropbody {
            position: relative;
        }

        .picknordrop {
            display: none;
        }

        .pick {
            position: absolute;
            top: 5%;
            right: 2%;
            display: block;
            content: url(images/pick.png);
            width: 100px;
            height: 80px;
        }

        .drop {
            position: absolute;
            top: 5%;
            right: 2%;
            display: block;
            content: url(images/drop.png);
            width: 100px;
            height: 80px;
        }

        .bg-read,
        .bg-read h2,
        .bg-read small {
            background-color: #9E9E9E !important;
            color: #FFFFFF !important;
        }

        .card .header .header-dropdown i {
            color: #FFFFFF !important;
        }

        .card .body {
            padding: 5px 30px;
        }

        .error {
            color: #FF0000;
        }
    </style>
</head>


<body class="theme-red" onload=display_ct5();>

    <?php include 'nav.php'; ?>

    <section class="content">

        <div id="excursion_add_form" class="col-lg-10 col-md-12 col-sm-12 col-xs-12">

            <div class="card">
                <div id="form_headar" class="header">
                    <h2 id="ex_head"> Add New Excursion
                    </h2>
                </div>
                <br />

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-12">
                            <div class="form-group form-float form-group">
                                <div class="form-line">
                                    <label> Reference No </label>
                                    <input id="ref_no" name="ref_no" type="text" class="form-control" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-12">
                            <div class="form-group form-float form-group">
                                <div class="form-line">
                                    <label> CE No </label>
                                    <input id="ehi_no" name="ehi_no" type="text" class="form-control" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-12">
                            <div class="form-group form-float form-group">
                                <div class="form-line">
                                    <label> Client Name </label>
                                    <input id="name" name="name" type="text" class="form-control" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-12">
                            <div class="form-group form-float form-group">
                                <div class="form-line">
                                    <label> Agent Name </label>
                                    <input id="agent_name" name="agent_name" type="text" class="form-control" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-12">
                            <div class="form-group form-float form-group">
                                <div class="form-line">
                                    <label> Company </label>
                                    <select class="form-control show-tick" id="company" name="company">
                                        <?php
                                        $sql_2 = "select *
                                  FROM
                                    company
                                  WHERE
                                    val=1";
                                        $result_2 = mysqli_query($db, $sql_2);
                                        while ($row_2 = mysqli_fetch_array($result_2)) {
                                            echo '<option value="' . $row_2['company_name'] . '">' . $row_2['company_name'] . '</option>';
                                        }
                                        ?>
                                        <!-- <option value="0">CMD</option> -->

                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-4">
                            <div class="form-group form-float form-group">
                                <div class="form-line">
                                    <label> Adult </label>
                                    <select id="adult" name="adult">
                                        <option value="0">0</option>
                                        <option value="1">1</option>
                                        <option value="2">2</option>
                                        <option value="3">3</option>
                                        <option value="4">4</option>
                                        <option value="5">5</option>
                                        <option value="6">6</option>
                                        <option value="7">7</option>
                                        <option value="8">8</option>
                                        <option value="9">9</option>
                                        <option value="10">10</option>
                                        <option value="11">11</option>
                                        <option value="12">12</option>
                                        <option value="13">13</option>
                                        <option value="14">14</option>
                                        <option value="15">15</option>
                                        <option value="16">16</option>
                                        <option value="17">17</option>
                                        <option value="18">18</option>
                                        <option value="19">19</option>
                                        <option value="20">20</option>
                                        <option value="21">21</option>
                                        <option value="22">22</option>
                                        <option value="23">23</option>
                                        <option value="24">24</option>
                                        <option value="25">25</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-4">
                            <div class="form-group form-float form-group">
                                <div class="form-line">
                                    <label> Child </label>
                                    <select id="child" name="child">
                                        <option value="0">0</option>
                                        <option value="1">1</option>
                                        <option value="2">2</option>
                                        <option value="3">3</option>
                                        <option value="4">4</option>
                                        <option value="5">5</option>
                                        <option value="6">6</option>
                                        <option value="7">7</option>
                                        <option value="8">8</option>
                                        <option value="9">9</option>
                                        <option value="10">10</option>
                                        <option value="11">11</option>
                                        <option value="12">12</option>
                                        <option value="13">13</option>
                                        <option value="14">14</option>
                                        <option value="15">15</option>
                                        <option value="16">16</option>
                                        <option value="17">17</option>
                                        <option value="18">18</option>
                                        <option value="19">19</option>
                                        <option value="20">20</option>
                                        <option value="21">21</option>
                                        <option value="22">22</option>
                                        <option value="23">23</option>
                                        <option value="24">24</option>
                                        <option value="25">25</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-4">
                            <div class="form-group form-float form-group">
                                <div class="form-line">
                                    <label> Infant </label>
                                    <select id="infant" name="infant">
                                        <option value="0">0</option>
                                        <option value="1">1</option>
                                        <option value="2">2</option>
                                        <option value="3">3</option>
                                        <option value="4">4</option>
                                        <option value="5">5</option>
                                        <option value="6">6</option>
                                        <option value="7">7</option>
                                        <option value="8">8</option>
                                        <option value="9">9</option>
                                        <option value="10">10</option>
                                        <option value="11">11</option>
                                        <option value="12">12</option>
                                        <option value="13">13</option>
                                        <option value="14">14</option>
                                        <option value="15">15</option>
                                        <option value="16">16</option>
                                        <option value="17">17</option>
                                        <option value="18">18</option>
                                        <option value="19">19</option>
                                        <option value="20">20</option>
                                        <option value="21">21</option>
                                        <option value="22">22</option>
                                        <option value="23">23</option>
                                        <option value="24">24</option>
                                        <option value="25">25</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-12">
                            <div class="form-group">
                                <div class="form-line">
                                    <input id="ticket_no" name="ticket_no" type="text" placeholder="Ticket No" class="form-control" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-12">
                            <div class="form-group">
                                <div class="form-line">
                                    <input name="room_no" id="room_no" type="text" placeholder="Room No" class="form-control" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-12">
                            <div class="form-group">
                                <div class="form-line">
                                    <input id="excursion_date" name="excursion_date" type="text" class="form-control" placeholder="Please choose Excursion Date">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-12 position-relative">
                            <label for="hotel_name" class="form-label fw-bold">Hotel Name</label>
                            <input type="text" id="hotel_name" class="form-control" placeholder="Type hotel name..." autocomplete="off">

                            <input type="hidden" id="hotel" name="hotel">
                            <div id="hotelList" class="list-group"></div>
                        </div>
                    </div>
                </div>

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-12">
                            <label> Select Excursion </label>
                            <select id="excursion" name="excursion" class="form-control show-tick">
                                <option value="0">Select Excursion</option>
                                <!-- <option value="0">Select Excursion</option> -->
                                <?php
                                // $sql_2 = "select *
                                //           FROM
                                //             excursion_names
                                //           WHERE
                                //             val=1";
                                // $result_2 = mysqli_query($db, $sql_2);
                                // while ($row_2 = mysqli_fetch_array($result_2)) {
                                //     echo '<option value="' . $row_2['id'] . '">' . $row_2['excursion_name'] . '</option>';
                                // }
                                ?>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-12">
                            <label> Transfer Type </label>
                            <select id="transfer_type" name="transfer_type" class="form-control show-tick">
                                <!--                            <option value="0">Select</option>-->
                                <option value="0">Group</option>
                                <option value="1">Individual</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-12">
                            <div class="form-group">
                                <div class="form-line">
                                    <input name="pickup_time" id="pickup_time" type="text" placeholder="Please Select PickUp Time" class="form-control" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-12">
                            <div class="form-group form-float form-group">
                                <div class="form-line">
                                    <input id="amount" name="amount" type="text" placeholder="Excursion Amount" class="form-control" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="body">
                    <div class="row clearfix">
                        <div class="col-sm-12">
                            <div class="form-group form-float form-group">
                                <div class="form-line">
                                    <input id="remark" name="remark" type="textarea" placeholder="Remark" class="form-control" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <input type="hidden" id="add_excursion" name="add_excursion" value="add" />

                <!-- button -->
                <div class=" body">
                    <div class="">
                        <button id="btn_add_excursion" type="button" class="btn bg-red btn-block btn-lg waves-effect" style="padding: 24px 16px; border-radius: 4px; font-size: 16px;">
                            Add Excursion
                        </button>
                    </div>
                </div>

            </div>

        </div>

        <?php include 'main.php'; ?>
        <?php include 'search.php'; ?>

    </section>

    <div id="result"></div>

</body>

<?php include 'footer.php'; ?>

<script type="text/javascript">
    $(document).ready(function() {

        var aa;
        var today = new Date();
        var h = today.getHours();
        var m = today.getMinutes();
        var s = today.getSeconds();
        if (s < 10) {
            s = "0" + s;
        }
        if (m < 10) {
            m = "0" + m;
        }

        aa = h;

        $("#excursion_add_form").show();
        $("#main").hide();
        $("#search_results").hide();
        $("#search").hide();

        if (aa >= '18') {
            $('#excursion_date').bootstrapMaterialDatePicker({
                format: 'dddd DD MMMM YYYY',

                minDate: moment().add('d', 2).toDate(),
                clearButton: true,
                weekStart: 1,
                time: false

            });
        } else {

            $('#excursion_date').bootstrapMaterialDatePicker({
                format: 'dddd DD MMMM YYYY',

                minDate: moment().add('d', 1).toDate(),
                clearButton: true,
                weekStart: 1,
                time: false

            });
        }

        $('#pickup_time').bootstrapMaterialDatePicker({
            date: false,
            format: 'HH:mm',
            shortTime: true
        });

        $("#ref_no").keyup(function() {
            $('#ehi_no').val('');
            $('#name').val('');
            $('#agent_name').val('');
            $('#adult').val('').change().selectpicker('refresh');
            $('#child').val('').change().selectpicker('refresh');
            $('#infant').val('').change().selectpicker('refresh');
            // $('#hotel').val(item.hotel).change();

            var ref_no = $('#ref_no').val();
            var url = 'get_data.php';

            var EHIRefData = new FormData();
            EHIRefData.append('ref_no', ref_no);

            // GET EHI DETAILS
            $.ajax({
                url: url,
                method: 'POST',
                processData: false,
                contentType: false,
                cache: false,
                dataType: 'json',
                data: EHIRefData,
                success: function(data) {
                    if (data != null) {
                        //consloe.log(data);
                        $.each(data, function(index, item) {
                            // $('#ref_no').val(item.ref_no);

                            $('#ehi_no').val(item.ehi_no);
                            $('#name').val(item.name);
                            $('#agent_name').val(item.agent_name);
                            $('#adult').val(item.adult).change().selectpicker('refresh');
                            $('#child').val(item.child).change().selectpicker('refresh');
                            $('#infant').val(item.infant).change().selectpicker('refresh');
                            // $('#hotel').val(item.hotel).change();
                        });

                        $('.datepicker').bootstrapMaterialDatePicker({
                            format: 'dddd DD MMMM YYYY',
                            clearButton: true,
                            weekStart: 1,
                            time: false
                        });
                    } else {

                    }
                },

                error: function() {

                }
            });

            // $(".second").val($(".first").val());
        });

        $("#btn_add_excursion").on("click", function(event) {

            var ref_no = $('#ref_no').val();
            var ehi_no = $('#ehi_no').val();
            var name = $('#name').val();
            var agent_name = $('#agent_name').val();
            //var company = $('#company option:selected').text();
            var company = $('#company').val();
            var adult = $('#adult').val();
            var child = $('#child').val();
            var infant = $('#infant').val();
            var ticket_no = $('#ticket_no').val();
            var room_no = $('#room_no').val();
            var excursion_date = $('#excursion_date').val();
            var excursion = $('#excursion').val();
            var transfer_type = $('#transfer_type').val();
            var pickup_time = $('#pickup_time').val();
            var hotel = $('#hotel').val();
            var amount = $('#amount').val();
            var remark = $('#remark').val();
            var add_excursion = $('#add_excursion').val();

            //alert("dddd "+transfer_type);
            // alert(ref_no+'-/-'+ehi_no+'-/-'+name+'-/-'+adult+'-/-'+child+'-/-'+ticket_no+'-/-'+room_no+'-/-'+excursion_date+'-/-'+excursion+'-/-'+hotel+'-/-'+amount);

            var url = 'get_data.php';
            var addExcursionData = new FormData();

            addExcursionData.append('ref_no', ref_no);
            addExcursionData.append('ehi_no', ehi_no);
            addExcursionData.append('name', name);
            addExcursionData.append('agent_name', agent_name);
            addExcursionData.append('company', company);
            addExcursionData.append('adult', adult);
            addExcursionData.append('child', child);
            addExcursionData.append('infant', infant);
            addExcursionData.append('ticket_no', ticket_no);
            addExcursionData.append('room_no', room_no);
            addExcursionData.append('excursion_date', excursion_date);
            addExcursionData.append('excursion', excursion);
            addExcursionData.append('transfer_type', transfer_type);
            addExcursionData.append('pickup_time', pickup_time);
            addExcursionData.append('hotel', hotel);
            addExcursionData.append('amount', amount);
            addExcursionData.append('remark', remark);
            addExcursionData.append('add_excursion', add_excursion);

            var today = new Date();
            today.setHours(today.getHours(), today.getMinutes(), today.getSeconds());

            var today_1 = new Date();
            today_1.setHours(13, 0, 0, 0);

            var today_2 = new Date();
            today_2.setHours(0, 0, 0, 0);
            var tomorrow1 = moment(today_2).add('days', 1);
            var tomorrow = Date.parse(tomorrow1);

            var chk_date1 = new Date(excursion_date);
            chk_date1.setHours(0, 0, 0, 0);
            var chk_date = Date.parse(chk_date1);

            //alert(transfer_type);
            //document.write(tomorrow+'//////'+today_2+'//////'+chk_date);
            //hasitha - 2021-01-01 remove || (ticket_no === '') and commect bellow
            if ((ref_no === '') || (ehi_no === '') || (excursion_date === '') || (room_no === '') || (adult === '') || (child === '') || (infant === '') || (amount === '') || (transfer_type === '2') || (excursion === '0') || (hotel === '0')) {
                alert('Please check the form');
            } else {
                // if (today > today_1) {
                // if (chk_date === tomorrow) {
                //   alert('You cannot add excursions for tomorrow after 12:00PM..!');
                // } else {
                //addExcursionDetails(url, addExcursionData);
                // }
                //} else {
                // addExcursionDetails(url, addExcursionData);
                //}
                addExcursionDetails(url, addExcursionData);
            }
        });

        $("#btn-search").click(function(event) {

            $('#search_results').empty();

            var ref_no_se = $('#ref_no_se').val();
            var ticket_no_se = $('#ticket_no_se').val();
            var ehi_no_se = $('#ehi_no_se').val();
            var excursion_se = $('#excursion_se').val();
            var excursion_name_se = $('#excursion_se option:selected').text();
            var user_se = $('#user_se').val();
            var excursion_date_se = $('#excursion_date_se').val();
            var user_role = $('#user_role').val();
            // alert('d');
            //alert('/-'+excursion_date_se+'/'+ref_no_se+'/'+ticket_no_se+'/'+ehi_no_se+'/'+excursion_se+'/'+user_se);

            var url = 'get_data.php';
            var ExcursionSearchData = new FormData();

            ExcursionSearchData.append('ref_no_se', ref_no_se);
            ExcursionSearchData.append('ticket_no_se', ticket_no_se);
            ExcursionSearchData.append('ehi_no_se', ehi_no_se);
            ExcursionSearchData.append('excursion_se', excursion_se);
            ExcursionSearchData.append('excursion_name_se', excursion_name_se);
            ExcursionSearchData.append('user_se', user_se);
            ExcursionSearchData.append('excursion_date_se', excursion_date_se);

            if (user_role === '3') {
                if ((ref_no_se === '') && (ticket_no_se === '') && (excursion_se === '0') && (ehi_no_se === '') && (excursion_date_se === '')) {
                    alert('Please check the form');
                } else {
                    searchExcursions(url, ExcursionSearchData);
                }
            } else if (user_role === '1') {
                if ((ref_no_se === '') && (ticket_no_se === '') && (excursion_se === '0') && (ehi_no_se === '') && (excursion_date_se === '') && (user_se === '0')) {
                    alert('Please check the form');
                } else {
                    searchExcursions(url, ExcursionSearchData);
                }
            }


        });

        $("#load-search-icon, #load-search-icon-m, #load-search-ref").click(function(event) {
            event.preventDefault();

            $("#excursion_add_form").hide();
            $("#search_results").hide();
            $("#main").hide();
            $("#search").show();
            $('.overlay').fadeOut();
            $('body').removeClass('overlay-open');

            $('#ref_no_se').val('');
            $('#ticket_no_se').val('');
            $('#ehi_no_se').val('');
            $('#excursion_se').val('').change().selectpicker('refresh');
            $('#excursion_date_se').val('');
            $('#user_se').val('').change().selectpicker('refresh');

        });

        $("#load-home").click(function(event) {
            event.preventDefault();

            $("#excursion_add_form").show();
            $("#search").hide();
            $("#search_results").hide();
            $("#main").hide();
            $('.overlay').fadeOut();
            $('body').removeClass('overlay-open');

            $('#ref_no').val('').attr("disabled", false);
            $('#ehi_no').val('').attr("disabled", false);
            $('#name').val('').attr("disabled", false);
            $('#agent_name').val('').attr("disabled", false);
            $('#company').val('').change().selectpicker('refresh');
            $('#adult').val('').change().selectpicker('refresh');
            $('#child').val('').change().selectpicker('refresh');
            $('#infant').val('').change().selectpicker('refresh');
            $('#ticket_no').val('');
            $('#room_no').val('');
            $('#excursion_date').val('');
            $('#hotel').val('').change().selectpicker('refresh');
            $('#excursion').val('').change().selectpicker('refresh');
            $('#transfer_type').val('').change().selectpicker('refresh');
            $('#pickup_time').val('');
            $('#amount').val('');
            $('#remark').val('');
            $('#add_excursion').val('add');

            $("div#form_headar h2").html("Add New Excursion");

            $('#btn_update_excursion').attr('id', 'btn_add_excursion');
            $('#btn_add_excursion').html("Add Excursion");
            $("#ex_head").html("Add Excursion");

        });


        $("#excursion, #transfer_type,#currency_type,#adult,#child").change(function() {
            event.preventDefault();
            var ref_no = $('#ref_no').val();
            var adult = $('#adult').val();
            var child = $('#child').val();
            var infant = $('#infant').val();
            var hotel = $('#hotel').val();
            var excursion = $('#excursion').val();
            var transfer_type = $('#transfer_type').val();
            var currency_type = $('#currency_type').val();
            var excursion_hotel_rate = 1;


            //alert(adult+'/'+hotel+'/'+child+'/'+infant+'/'+excursion+'/'+transfer_type);
            var url = 'get_data.php';
            var ExcursionHotelRateData = new FormData();

            ExcursionHotelRateData.append('adult', adult);
            ExcursionHotelRateData.append('child', child);
            ExcursionHotelRateData.append('infant', infant);
            ExcursionHotelRateData.append('hotel', hotel);
            ExcursionHotelRateData.append('excursion', excursion);
            ExcursionHotelRateData.append('transfer_type', transfer_type);
            ExcursionHotelRateData.append('currency_type', currency_type);
            ExcursionHotelRateData.append('excursion_hotel_rate', excursion_hotel_rate);

            if ((hotel != '0') && (excursion != '0') && (transfer_type != '2') && (adult != '0')) {
                // alert(adult+'/'+hotel+'/'+child+'/'+infant+'/'+excursion+'/'+transfer_type);
                GetExcursionHotelRate(url, ExcursionHotelRateData);
            }

        });

        $("#hotel_name").change(function() {
            event.preventDefault();
            $('#amount').val('0');
            // $('#amount').prop('disabled', false);
            var hotelid = $('#hotel').val();
            var transfer_type = $('#transfer_type').val();

            var url = 'get_data.php';
            var HotelcityData = new FormData();

            HotelcityData.append('hotelid', hotelid);
            HotelcityData.append('transfer_type', transfer_type);

            //getExcirsionFromHotel();

            if ((hotelid != '0')) {
                GetExcursionlist(url, HotelcityData);
            }

        });


    });
</script>
<script>
    $(document).ready(function() {

        $('#hotel_name').keyup(function() {
            var query = $(this).val();
            if (query != '') {
                $.ajax({
                    url: "get_hotels.php",
                    method: "POST",
                    data: {
                        query: query
                    },
                    success: function(data) {
                        $('#hotelList').fadeIn();
                        $('#hotelList').html(data);
                    }
                });
            } else {
                $('#hotelList').fadeOut();
            }
        });

        // Dropdown එකේ hotel එකක් select කළාම
        $(document).on('click', '.hotel_item', function() {
            var hotelName = $(this).text();
            var hotelId = $(this).data('id'); // data-id attribute එකෙන් id එක ගන්නවා
            $('#hotel_name').val(hotelName);
            $('#hotel').val(hotelId);
            $('#hotelList').fadeOut();
        });
    });
</script>

</html>