<?php
if (!function_exists('callout')) {
    require(dirname(__FILE__) . "/../../Includes/config.php");
    require(dirname(__FILE__) . "/../../System/Autoloader.php");
    require(dirname(__FILE__) . "/../../Includes/autoloader.php");
}


if (isset($_SERVER['REQUEST_URI'])) {
    $url = explode('/', $_SERVER['REQUEST_URI']);
    $IsEdit = ($url[count($url) - 1]) == "LeaveCalendar";
} else {
    $IsEdit = "edit";
}
if (isset($_POST["Employee_ID"]) && isset($_POST['EditMode'])) {
    $LeaveAnnounce = new Announcement();

    $IsEdit = $_POST['EditMode'];
    session_start();
    $logged_id = Session::getUserID();
    array_push($_POST, $logged_id);
    if ($IsEdit == 0) {
        $Check = $LeaveAnnounce->setData($_POST)->checkExistLeave();
        if (is_array($Check)) {
            die(json_encode($Check));
        } else {
            if ($Check)
                die("success");
            else
                die("error");
        }
    }
    else {

        $RET = $LeaveAnnounce->setData($_POST)->EditAssignLeave();
        if ($RET)
            die("success");
        else
            die("error");
    }
}
if (isset($_POST['Employee_ID'])) {
    $LeaveAnnounce = new Announcement;
    $RET = $LeaveAnnounce->setData($_POST)->createAssignLeave();
    if ($RET)
        die("success");
    else
        die("error");
}
if (isset($_GET["GetLeaveID"])) {
    $LeaveAnnounce = new Announcement;
    $Content = $LeaveAnnounce->viewLeaveByID($_GET["GetLeaveID"]);
    die($Content ? json_encode($Content) : "");
}
//include("xhr/calendar.php");
?>
<style>
    .input-group-addon.complete {
        background-color:#4c0870;
        color: #ffffff;
    }
    .HideStatus,.hide{
        display: none !important;    
    }
    /*    .datepicker{z-index:0 !important;}*/
</style>
<link href="<?php echo THEME_PATH; ?>plugins/bootstrap-timepicker/css/bootstrap-timepicker.min.css" rel="stylesheet" type="text/css"/>
<script src="<?php echo THEME_PATH; ?>plugins/bootstrap-timepicker/js/bootstrap-timepicker.min.js"></script>
<div class="modal fade stick-up " id="AssignLeave" tabindex="-1" role="dialog" aria-hidden="true" style="    width: 50%;
     margin: 0 auto;">
    <div class="modal-dialog ">
        <div class="modal-content">
            <div class="modal-header clearfix text-left">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-14"></i></button>
                <h5><span class="semi-bold"><?php echo CommonLanguage::getMessage($IsEdit ? 'edit_assign_leave' : 'assign_leave'); ?>  </span></h5>
            </div>
            <div class="modal-body">
                    <!--                <input type="text" id="LeaveID"/>-->
                <form id="AssignLeavePostForm" method="POST" onsubmit="return false">
                    <div class="row">
                        <div class="col-md-12" style="margin:8px;margin-bottom:10px;">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <div class="input-group m-b"><span class="input-group-btn">
                                            <button type="button" class="btn btn-success" style="width:0 " ><i class="fa fa-user"style="color:#fff;margin-left:-7px"></i> </button> </span> 
                                        <select name="Employee_ID"  id="Employee_AssignLeave" class="full-width" data-init-plugin="select2">
<?php foreach ($absence_employees as $employee): ?>
                                                <option value="<?php echo $employee['Employee_ID']; ?>"><?php echo $employee['Employee_Name']; ?></option>
<?php endforeach; ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-3" style="margin-top:-25px">
                                <label><?php print CommonLanguage::getMessage('from_time'); ?></label>
                                <input type="text" value="12:00 AM" id="AssignLeave_Start_Time" data-format="hh:mm:ss" name="AssignLeave_Start_Time" placeholder="From Time" class="form-control">
                            </div>
                            <div class="col-md-3"  style="margin-top:-25px">
                                <label><?php print CommonLanguage::getMessage('to_time'); ?></label>
                                <input type="text" value="12:00 AM" id="AssignLeave_End_Time" data-format="hh:mm:ss" name="AssignLeave_End_Time" placeholder="From Time" class="form-control">
                            </div>
                        </div>
                        <div class="col-md-12" style="margin-left:8px;margin-bottom:10px;">
                            <div class="col-md-6">
                                <div class="input-group ">
                                    <span class="input-group-addon complete">
                                        <i class="fa fa-calendar"></i>
                                    </span>
                                    <input type="text"  placeholder="<?php print CommonLanguage::getMessage('From'); ?> " id="AssignLeave_From_Date" name="AssignLeave_From_Date" class="form-control " >
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="input-group ">
                                    <span class="input-group-addon complete">
                                        <i class="fa fa-calendar"></i>
                                    </span>
                                    <input type="text"  placeholder="<?php print CommonLanguage::getMessage('To'); ?> " id="AssignLeave_To_Date" name="AssignLeave_To_Date" class="form-control " >
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12" style="margin-left:8px;margin-bottom:10px;">
                            <div class="col-md-6">
                                <div class="input-group ">
                                    <span class="input-group-addon complete">
                                        <i class="fa fa-pencil"></i>
                                    </span>
                                    <input type="text"  placeholder="<?php print CommonLanguage::getMessage('reason'); ?> " id="AssignLeave_Reason_ID" name="AssignLeave_Reason_ID" class="form-control " required>
                                </div>
                            </div>
                            <div class="col-md-6 HideStatus Status">
                                <div class="form-group">
                                    <div class="input-group m-b"><span class="input-group-btn">
                                            <button type="button" class="btn btn-success" style="width:0 " ><i class="fa fa-user"style="color:#fff;margin-left:-7px"></i> </button> </span> 
                                        <select name="LeaveRequest_Status"  id="LeaveRequest_Status" class="full-width" data-init-plugin="select2">
                                            <option value="0"><?php print CommonLanguage::getMessage('planned'); ?></option>
                                            <option value="1"><?php print CommonLanguage::getMessage('declined'); ?></option>
                                            <option value="2"><?php print CommonLanguage::getMessage('approved'); ?></option>
                                            <!--                                            <option value="3">Done</option>-->
                                            <option value="4"><?php print CommonLanguage::getMessage('deleted'); ?></option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <br>
                        <div class="col-md-12 " style="margin-left:10px;margin-top:10px">
                            <div class="col-md-4 pull-right">
                                <button class="btn btn-default" id="CloseAssignLeave"   > <?php print CommonLanguage::getMessage('Close'); ?> </button>
                                <button class="btn btn-success " id="SubmitAssignLeave"  > <?php print CommonLanguage::getMessage('Save'); ?>   </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<div class="modal fade stick-up " id="ModalToAssignByForce" tabindex="-1" role="dialog" aria-hidden="true" style="    width: 50%;
     margin: 0 auto;">
    <div class="modal-dialog ">
        <div class="modal-content">
            <div class="modal-header clearfix text-left">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-14"></i></button>
                <h5><span class="semi-bold"> Assign Leave By Force </span></h5>
            </div>
            <div class="modal-body">
                <form>
                    <div class="row">
                        <div class="col-md-12 ">
                            <div class="col-md-12" style="margin-top: 20px;">
                                <p><strong class="Name"></strong> aleready have submitted leave with common duration .</p>
                                <p>Do you want to proceed?</p>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-success" id="ConfirmAssign" style="width:auto">Confirm</button>
            </div>
        </div>
    </div>
</div>
<script>
    var Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>;
    var IsEdit = <?php echo $IsEdit ? "true" : "false"; ?>, CurrentID;
    $(function () {
        $('#AssignLeave_Start_Time,#AssignLeave_End_Time').timepicker({showMeridian: false, defaultTime: false})
        $("#AssignLeave_From_Date,#AssignLeave_To_Date").datepicker({autoclose: true}).on('show', function (ev) {
            ev.stopPropagation();
        })
        $("body").addClass("NoSearch")
        $("body .search-link[data-toggle='search']").html("<i class='pg-search'></i>Click here to <span class='bold'>search</span>")
        if (IsEdit) {
            $(".Status").removeClass("HideStatus")
            $("#AssignLeave").on("show.bs.modal", function () {
                CurrentID = $("#LeaveID").val();
                $.ajax({
                    url: "Views/HRM/AssignLeave.php?GetLeaveID=" + CurrentID,
                    dataType: "json",
                    complete: function (v, vv) {
                        if (vv == "success") {
                            if (v.responseJSON.length > 0) {
                                var Content = v.responseJSON[0];
                                $("#Employee_AssignLeave").val(Content.Employee_ID).select2()
                                $("#AssignLeave_Start_Time").val(Content.AssignLeave_Start_Time)
                                $("#AssignLeave_End_Time").val(Content.AssignLeave_End_Time)
                                $("#AssignLeave_From_Date").val(Content.AssignLeave_From_Date)
                                $("#AssignLeave_To_Date").val(Content.AssignLeave_To_Date)
                                $("#AssignLeave_Reason_ID").val(Content.AssignLeave_Reason)
                                $("#LeaveRequest_Status").val(Content.LeaveRequest_Status).select2()
                            }
                        }
                    }
                })
            })
        }
        //        $("#AssignLeave_To_Date").datepicker({autoclose:true}).on("changeDate",function(){ })
        $("#SubmitAssignLeave").on("click", function (e) {
            $("#AssignLeaveValidationAlert").remove();
            var AlertBox = $('<div id="AssignLeaveValidationAlert" class="WizardValidationAlert alert"></div>');
            AlertBox.insertAfter($("#AssignLeave").find(".modal-dialog .modal-header").find("h5"))
            $("#AssignLeaveIsBusy").remove();
            $("#AssignLeave").append('<div id="AssignLeaveIsBusy" style="position:absolute;left:0;right:0;top:0px;height:100%;background:rgba(255,255,255,0.9);z-index:9999;opacity:0"><img class="image-responsive-height demo-mw-50" src="img/demo/progress.svg" alt="Progress" style="position: absolute;left: 50%;top: 50%;margin: -25px 0 0 -25px;" /></div>');
            $("#AssignLeaveIsBusy").animate({opacity: 1});
            var Data = {
                Employee_ID: $("#Employee_AssignLeave").val(),
                AssignLeave_Reason: $("#AssignLeave_Reason_ID").val(),
                AssignLeave_From_Date: $("#AssignLeave_From_Date").val(),
                AssignLeave_To_Date: $("#AssignLeave_To_Date").val(),
                AssignLeave_Start_Time: $("#AssignLeave_Start_Time").val(),
                AssignLeave_End_Time: $("#AssignLeave_End_Time").val(),
                EditMode: IsEdit ? 1 : 0
            }
            if (IsEdit) {
                Data.Leave_ID = CurrentID;
                Data.LeaveRequest_Status = $("#LeaveRequest_Status").val()
            }
            $.ajax({
                url: "Views/HRM/AssignLeave.php",
                method: "POST",
                data: Data,
                complete: function (v1, v2) {
                    if (v2 == 'success' && v1.responseText == 'success') {
                        if (IsEdit) {
                            $("body").append($('<div id="ajaxLoaderModal" style="position:fixed;left:0;right:0;top:0px;height:100%;background:rgba(255,255,255,0.4);z-index:999"><img class="image-responsive-height demo-mw-50" src="img/demo/progress.svg" alt="Progress" style="position: absolute;left: 50%;top: 50%;margin: -25px 0 0 -25px;" /></div>'))
                            var Name = $(".select2-chosen").html();
                            var Status = $("#s2id_LeaveRequest_Status .select2-chosen").html();
                            $("#lblfromTime").empty().html(Data['AssignLeave_Start_Time'])
                            $("#lbltoTime").empty().html(Data['AssignLeave_End_Time'])
                            $("#txtEventName,.event-title").empty().html(Data['AssignLeave_Reason'])
                            $("#txtEventEmployeeName").empty().html(Name)
                            $("#txtEventStatus").empty().html(Status)
                            setTimeout(function () {
                                $('#calendar').load(window.location.pathname);
                                $("#ajaxLoaderModal").remove()
                            }, 5000)
                        }
                        $("#AssignLeaveIsBusy").remove();
                        $("#AssignLeaveValidationAlert").addClass("alert-success").empty().html(Submitted_Successfully);
                        setTimeout(function () {
                            $("#AssignLeave").modal('hide')
                            $("#AssignLeaveValidationAlert").hide()
                        }, 1000)
                    } else {
                        var Row = $.parseJSON(v1.responseText);
                        var name = '';
                        for (var i = 0; i < Row.length; i++) {
                            name += Row[i]['Employee_Name'] + ', '
                        }
                        $("#ModalToAssignByForce").find(".Name").append(name)
                        $("#AssignLeaveValidationAlert").addClass("alert-danger").empty().html(Failed_Submit);
                        $("#ModalToAssignByForce").modal('show');
                    }
                    $("#AssignLeaveIsBusy").remove();
                }
            });
        });
        $("#AssignLeave input").val("");
    })
    $("#CloseAssignLeave").on("click", function () {
        $("#AssignLeave input").val("");
        $("#AssignLeave").modal("hide");
    })
    $("#ConfirmAssign").on("click", function () {
        var AlertBox = $('<div id="AssignLeaveValidation" class="WizardValidationAlert alert"></div>');
        AlertBox.insertAfter($("#ModalToAssignByForce").find(".modal-dialog .modal-header").find("h5"))
        var Data = {
            Employee_ID: $("#Employee_AssignLeave").val(),
            AssignLeave_Reason: $("#AssignLeave_Reason_ID").val(),
            AssignLeave_From_Date: $("#AssignLeave_From_Date").val(),
            AssignLeave_To_Date: $("#AssignLeave_To_Date").val(),
            AssignLeave_Start_Time: $("#AssignLeave_Start_Time").val(),
            AssignLeave_End_Time: $("#AssignLeave_End_Time").val()
        }
        $.ajax({
            url: "Views/HRM/AssignLeave.php",
            method: "POST",
            data: Data,
            complete: function (v1, v2) {
                if (v2 == "success") {
                    $("#AssignLeaveValidation").addClass("alert-success").empty().html(Submitted_Successfully);
                    setTimeout(function () {
                        $("#AssignLeave").modal('hide')
                        $("#ModalToAssignByForce").modal('hide')
                        $("#AssignLeaveValidation,#AssignLeaveValidationAlert").hide()
                    }, 1000)
                } else {
                    $("#AssignLeaveValidation").addClass("alert-danger").empty().html(Failed_Submit);
                }
            }
        })
    })
</script>