<?php
$ticketing_fields_hide = smc_session()['default_setting']['maintenance_fields_to_hide'] != '' ? explode(",", smc_session()['default_setting']['maintenance_fields_to_hide']) : [];
//echo '<pre>';
//print_r($ticketing_fields_hide);

//print_r($ticketing_status);

//die('ok');
//$t=$edit_arr['main']['ticketing_reply_id'];
//echo "<script>alert('$t');</script>";
?>
<link href="<?php echo LIBS_URL; ?>summernote/summernote.css" rel="stylesheet">
<script src="<?php echo LIBS_URL; ?>summernote/summernote.min.js"></script>
<style>
    .fa {
        cursor: pointer;
    }
</style>
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print $title; ?></div>
        </div>
        <div class="panel-body">
            <div class="row"><?php echo Forms::AlertDiv('ticketing_alert'); ?></div>
            <div hidden id="hide" class="row">
                <div class="col-md-12">
                    <div class="alert">
                        <p id="alert_id"></p>
                    </div>
                </div>
            </div>

            <form method="POST" id="ticketing_form" onsubmit="return false">
                <input type="hidden" id="ticketing_id" name="ticketing_id" value="<?php echo $ticketing_id; ?>" />
                <div class="row form-group">
                    <div class="col-md-3 ">
                        <label><?php echo CommonLanguage::getMessage('id'); ?>
                            <span class="red-star">*</span>
                        </label>
                        <input type="text" name="event_id" id="event_id" class="form-control" value="<?php echo $ticketing_id  == 0 ? '' :  $edit_arr['main']['event_id']; ?>" required />
                    </div>
                    <div class="col-md-3 ">
                        <label><?php echo CommonLanguage::getMessage('event_name'); ?>
                            <span class="red-star">*</span>
                        </label>
                        <input type="text" name="event_name" id="event_name" class="form-control" value="<?php echo $ticketing_id  == 0 ? '' :  $edit_arr['main']['event_name']; ?>" required />
                    </div>
                    <div class="col-md-3">
                        <label><?php echo CommonLanguage::getMessage('event_book_type'); ?>
                            <span class="red-star">*</span>

                        </label>
                        <input type="hidden" name="event_book_type_id" id="event_book_type_id" class="full-width" value="<?php echo $ticketing_id  == 0 ? '' :  $edit_arr['main']['event_book_type_id']; ?>" data-select-name="<?php echo $ticketing_id  == 0 ? '' :  $edit_arr['main']['book_name']; ?>" required />
                    </div>

                    <div class="col-md-3 ">
                        <label><?php echo CommonLanguage::getMessage('book_time'); ?>
                            <span class="red-star">*</span>
                        </label>
                        <input type="number" min='1' name="book_time" id="book_time" class="form-control" value="<?php echo $ticketing_id  == 0 ? 1 :  $edit_arr['main']['book_time']; ?>" required />
                    </div>
                </div>

                <div class="row form-group">

                    <div class="col-md-3">
                        <label><?php echo CommonLanguage::getMessage('from_date'); ?>
                            <span class="red-star">*</span>
                        </label>
                        <input type="text" name="from_date" id="from_date" class="form-control" value="<?php echo $ticketing_id == 0 ? date('Y-m-d') : $edit_arr['main']['from_date']; ?>" required />
                    </div>
                    <div class="col-md-3">
                        <label><?php echo CommonLanguage::getMessage('to_date'); ?>
                            <span class="red-star">*</span>
                        </label>
                        <input type="text" name="to_date" id="to_date" class="form-control" value="<?php echo $ticketing_id == 0 ? date('Y-m-d') : $edit_arr['main']['to_date']; ?>" required />
                    </div>
                    <div class="col-md-3">
                        <label><?php echo CommonLanguage::getMessage('from_time'); ?>
                            <span class="red-star">*</span>
                        </label>
                        <input type="text" name="from_time" id="from_time" class="form-control" value="<?php echo $ticketing_id == 0 ? date('Y-m-d') : $edit_arr['main']['from_time']; ?>" required />
                    </div>
                    <div class="col-md-3">
                        <label><?php echo CommonLanguage::getMessage('to_time'); ?>
                            <span class="red-star">*</span>
                        </label>
                        <input type="text" name="to_time" id="to_time" class="form-control" value="<?php echo $ticketing_id == 0 ? date('Y-m-d') : $edit_arr['main']['to_time']; ?>" required />
                    </div>

                </div>

                <div class="row form-group">
                    <div class="col-md-3">
                        <label><?php echo CommonLanguage::getMessage('event_break_type'); ?>
                            <span class="red-star">*</span>

                        </label>
                        <input type="hidden" name="event_break_type_id" id="event_break_type_id" class="full-width" value="<?php echo $ticketing_id  == 0 ? '' :  $edit_arr['main']['event_break_type_id']; ?>" data-select-name="<?php echo $ticketing_id  == 0 ? '' :  $edit_arr['main']['break_type']; ?>" required />
                    </div>

                    <div class="col-md-3 ">
                        <label><?php echo CommonLanguage::getMessage('event_break_time'); ?>
                            <span class="red-star">*</span>
                        </label>
                        <input type="number" name="event_break_time" min='1' value="1" id="event_break_time" class="form-control" value="<?php echo $ticketing_id  == 0 ? '' :  $edit_arr['main']['event_break_time']; ?>" required />
                    </div>

                </div>
                <div hidden class="row form-group">

                    <label><?php echo CommonLanguage::getMessage('off_day'); ?>
                </div>
                <div hidden class="row form-group">

                    <div class="col-md-1">
                        <input type="checkbox" value="1" value="1" name="off_day" id="off_day1" /> <label>Monday</label>
                    </div>
                    <div class="col-md-1">
                        <input type="checkbox" value="2" value="2" name="off_day" id="off_day2" /> <label>Tuesday</label>
                    </div>

                    <div class="col-md-1">
                        <input type="checkbox" value="4" value="4" name="off_day" id="off_day3" /> <label>Thursday</label>
                    </div>
                    <div class="col-md-1">
                        <input type="checkbox" value="5" value="5" name="off_day" id="off_day4" /> <label>Friday</label>
                    </div>
                    <div class="col-md-1">
                        <input type="checkbox" value="6" value="6" name="off_day" id="off_day5" /> <label>Saturday</label>
                    </div>
                    <div class="col-md-1">
                        <input type="checkbox" value="7" value="7" name="off_day" id="off_day6" /> <label>Sunday</label>
                    </div>
                    <div class="col-md-2">
                        <input type="checkbox" value="3" value="3" name="off_day" id="off_day7" /> <label>Wednesday</label>
                    </div>

                </div>





            </form>
            <div class="row">
                <div class="col-md-3 pull-right">
                    <button id="submit_event" class=" btn btn-complete pull-right"><?php echo CommonLanguage::getMessage('Save'); ?></button>
                </div>
            </div>

            <?php echo Forms::ModalLoader('ticketing_loader'); ?>
        </div>
    </div>
</div>

<script>
    $(function() {

        build_select2($("#assigned_user_id"), "xhr/searchEmployeeUsers");
        build_select2($("#depot_id"), "xhr/depotSearch");
        build_select2($("#payment_account_id"), 'xhr/accIDSearch?type=pay');
        build_select2($("#currency_id"), 'xhr/CurrencySearch');
        build_select2($("#customer_id"), 'xhr/customerSearch');
        build_select2($("#event_break_type_id"), 'xhr/commonSearch?table=tbl_event_break_type&col_id=ID&col_name=break_type');
        build_select2($("#event_book_type_id"), 'xhr/commonSearch?table=tbl_event_book_type&col_id=ID&col_name=book_name');
        build_select2($("#ticketing_station_id"), 'xhr/commonSearch?table=tbl_maintenance_supporter&col_id=maintenance_supporter_id&col_name=maintenance_supporter_name');
        build_select2($("input.product_id"), ("xhr/ProductSearch?ticketing_price=1"));
        build_select2($("input.uom_id"), "xhr/productuomSearch?pid=&ticketing_price=1");



        $('#from_date').datepicker({
            autoclose: true
        });
        $('#to_date').datepicker({
            autoclose: true
        });
        $('#from_time').timepicker({
            showMeridian: false
        });
        $('#to_time').timepicker({
            showMeridian: false
        });

        $("#to_date,#from_date").on("change", function() {
            var fromDate = $('#from_date').val();
            var toDate = $('#to_date').val();
            // console.log('fromDate: '+fromDate);
            // console.log('to_date: '+toDate);
            if (fromDate > toDate) {
                alert_massage('From date must be less than To Date', false);
                $("#submit_event").prop('disabled', true);
            } else {

                $("#submit_event").prop('disabled', false);

            }

        });
        $("#to_time,#from_time").on("change", function() {
            var fromTime = $('#from_time').val();
            var toTime = $('#to_time').val();
            console.log('fromTime: ' + fromTime);
            console.log('toTime: ' + toTime);
            const [hours1, minutes1] = fromTime.split(':');

            const [hours2, minutes2] = toTime.split(':');
            const date1 = new Date(2022, 0, 1, +hours1, +minutes1);
            const date2 = new Date(2022, 0, 1, +hours2, +minutes2);


            if (date1.getTime() > date2.getTime()) {
                alert_massage('From time must be less than To time', false);
                $("#submit_event").prop('disabled', true);
            } else if (date2.getTime() > date1.getTime()) {
                alert_massage('', true);
                $("#submit_event").prop('disabled', false);
            } else {
                alert_massage('From time must be less than To time', false);
                $("#submit_event").prop('disabled', true);
            }
            // if (fromTime > toTime) {
            // } else {

            //     $("#submit_event").prop('disabled', false);

            // }

        });

        $("#submit_event").on("click", function() {
            if ($('#event_name').val().length <= 0 || $('#event_book_type_id').val() == '') {
                AddAlert("#ticketing_alert", "<?php echo CommonLanguage::getMessage('Required_Fields'); ?>", "danger");
                $(document.body).animate({
                    scrollTop: 0
                }, 800)
                return;
            }

            var Data = new FormData(document.getElementById("ticketing_form"));
            Data.append('event_id', $('#event_id').val());
            Data.append('event_book_type_id', $('#event_book_type_id').val());
            Data.append('book_time', $('#book_time').val());
            Data.append('event_name', $('#event_name').val());

            Data.append('from_date', $('#from_date').val());
            Data.append('to_date', $('#to_date').val());
            Data.append('from_time', $('#from_time').val());
            Data.append('to_time', $('#to_time').val());
            Data.append('event_break_type_id', $('#event_break_type_id').val());
            Data.append('event_break_time', $('#event_break_time').val());
            var off_day = [];
            for (let index = 0; index < 8; index++) {
                if ($('#off_day' + index).is(":checked"))
                    off_day.push($('#off_day' + index).val());
            }



            Data.append('off_day', off_day);


            $("#ticketing_loader").removeClass('hidden');
            $.ajax({
                url: "TicketingAjax/submit_event",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function(jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            $("#submit_event").prop('disabled', true);
                        }
                        $(document.body).animate({
                            scrollTop: 0
                        }, 800)
                        AddAlert("#ticketing_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#ticketing_loader").addClass('hidden');
                }
            });
        });





    });

    function alert_massage(massage, hide) {
        if (hide)
            $("#hide").hide();
        else
            $("#hide").show();
        $("#alert_id").text(massage);
    }
</script>