<!--
< ?php if (empty($Annoucements[0])) { ?>
<div class="alert alert-danger" role="alert">
    <button class="close" data-dismiss="alert"></button>
    <strong>Danger: </strong>you can not edit this announcement! 
    <br>
    This announcement is not available.
</div>
< ?php die(); } ?>
-->


<link href="<?php echo LIBS_URL; ?>summernote/summernote.css" rel="stylesheet">
<script src="<?php echo LIBS_URL; ?>summernote/summernote.min.js"></script>


<div class="col-md-12">
    <div class="panel panel-white">
        <div class="panel-heading clearfix">
            <h4 class="panel-title"><i class="fa fa-comment"></i> <?php print CommonLanguage::getMessage('new_annoucement'); ?></h4>
            <div id="SuccessAlert"></div>
        </div>
        <div class="panel-body">

            <input type = "hidden" id="Post_Time" value="" onload="clocktime()" />
            <div class="form-group col-md-12">
                <div class="row">
                    <div class="control-label col-md-2"><?php echo $Announcement_Type; ?>
                        <h5><?php echo CommonLanguage::getMessage('type') ?><span class="red-star">*</span></h5>
                        <select id="type_announce" name="privacy__type" class="full-width document_publish_type"  data-init-plugin="select2">
                            <?php echo CommonFunctions::getUserPrivacyTypes($Announcement_Type) ?>
                        </select>
                    </div>
                    <div class="control-label col-md-10 m-t-50 ">
                        <div class="department_priv hidden"><input value='<?= $Announcement_Value ?>' data-select-name='<?= json_encode($privacy_value_name) ?>'  type="hidden" class="full-width " id="department_priv"/></div>
                        <div class="private_sup hidden"><input value='<?= $Announcement_Value ?>' data-select-name='<?= json_encode($privacy_value_name) ?>'  type="hidden" class="full-width " id="private_sup"/></div>
                        <div class="emp_priv_grp hidden"><input value='<?= $Announcement_Value ?>' data-select-name='<?= json_encode($privacy_value_name) ?>'  type="hidden" class="full-width " id="emp_priv_grp"/></div>
                        <div class="emp_grp hidden"><input value='<?= $Announcement_Value ?>' data-select-name='<?= json_encode($privacy_value_name) ?>'  type="hidden"  class="full-width " id="emp_grp"/></div>
                    </div>
                </div>
            </div>

            <!--            <div class="col-md-12 m-b-5">
                            <div class="col-md-6">
                                <label>< ?php print CommonLanguage::getMessage('type_annouce') ?></label><span class="red-star">*</span>
                                <select class="full-width type_announce" id="type_announce" data-init-plugin="select2">
                                    <option value='1' < ?php echo $Announcement_Type == 1 ? 'selected' : '' ?>>< ?php print CommonLanguage::getMessage('public') ?></option>
                                    <option value='2' < ?php echo $Announcement_Type == 2 ? 'selected' : '' ?>>< ?php print CommonLanguage::getMessage('department') ?></option>
                                    <option value='3' < ?php echo $Announcement_Type == 3 ? 'selected' : '' ?>>< ?php print CommonLanguage::getMessage('employees') ?></option>
            
                                </select>
                            </div>
            
            
                            <div  class=" col-md-6 hidden department">
                                <label>< ?php print CommonLanguage::getMessage('department'); ?></label><span class="red-star">*</span>
                                <input type="hidden" name="Branch_Name"  id="Branch_Name"  class="Branch_Name full-width" required value="< ?= $Branch_ID ?>" >
                            </div>
                            <div  class=" col-md-6 hidden employee">
                                <label>< ?php print CommonLanguage::getMessage('employees'); ?></label><span class="red-star">*</span>
                                <input type="hidden" name="employee_id"  id="employee_id"  class="employee_id full-width" required value="< ?= $Employee_ID ?>" /> 
                            </div>
            
                        </div>-->

            <div class="col-md-12">

                <div class="col-md-6 form-group">
                    <label><?php print CommonLanguage::getMessage('title'); ?></label><span class="red-star">*</span>
                    <input type="text" id="Annoucement_Title" value="<?php echo $Title; ?>"	class="form-control" placeholder="<?php print CommonLanguage::getMessage('title'); ?>" /> <br>
                </div>
                <div class="col-md-6 form-group">
                    <label><?php print CommonLanguage::getMessage('subtitle'); ?></label>
                    <input type="text" id="Annoucement_Subtitle" value="<?php echo $Subtitle; ?>" class="form-control" placeholder="<?php print CommonLanguage::getMessage('subtitle'); ?>" /> <br>
                </div>


            </div>

            <div class="row">


                <div class="col-md-12 form-group">


                    <label><?php print CommonLanguage::getMessage('content'); ?></label><span class="red-star">*</span><br>
                    <div class="col-md-12  summernote-wrapper m-b-10">
                        <div id="summernote" name="Content"><?php echo $Content; ?></div>
                    </div>



                    <div class="col-md-4 pull-right">
                        <button id="submitAnnoucement" class=" btn btn-success btn-lg" >
                            <?php print CommonLanguage::getMessage('Save'); ?>
                        </button>
                    </div>

                </div>
            </div>

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

<script>
    var Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
            Announcement_Type = <?php echo json_encode($Announcement_Type); ?>;
    window.onload = clocktime();
  var pubID = <?php echo $PubID; ?>;

    $(document).ready(function () {

//        if (Announcement_Type == 2) {
//            $('.department').removeClass('hidden')
//
//        }
//        if (Announcement_Type == 3) {
//            $('.employee').removeClass('hidden')
//
//        }
//        build_select2($('.Branch_Name'), 'xhr/searchDepartment', 1);
//        build_select2($('.employee_id'), 'xhr/employeeSearch', 1);
//        $('#Branch_Name').select2('val', [])
//        $('#employee_id').select2('val', [])
//        $('#Branch_Name').data().select2.updateSelection([< ?php echo $Branch_Name; ?>]);
//        $('#employee_id').data().select2.updateSelection([< ?php echo $Employee_Name; ?>]);
        document.onselectstart = function () {
            return true
        };
        build_select2($('#department_doc_id'), 'HRMAjax/searchDepartment');
        build_select2($('#department_priv'), 'HRMAjax/searchDepartment', 1);
        build_select2($('#private_sup'), 'HRMAjax/searchDepartment', 1);
        build_select2($('#emp_priv_grp'), 'xhr/CommonSearch?col_id=Employee_ID&col_name=Employee_Name&table=view_users', 1);
        build_select2($('#emp_grp'), 'xhr/groupSearch', 1);
        $('#type_announce').on('change', function () {
            changeeselect($(this));

        });

//        $('#type_announce').on('change', function () {
//            var value = $(this).val();
//
//            if (value == 1)
//                $('.department,.employee').addClass('hidden')
//            if (value == 2) {
//                $('.department').removeClass('hidden')
//                $('.employee').addClass('hidden')
//            }
//            if (value == 3) {
//                $('.employee').removeClass('hidden')
//                $('.department').addClass('hidden')
//            }
//
//        })
      

        if (pubID > 0) {
            $('#type_announce').trigger('change');
            changeeselect($('#type_announce'));
        }
        $('#summernote').summernote({
            height: 150,
            toolbar: [
                ['style', ['style']],
                ['font', ['bold', 'italic', 'underline', 'clear']],
                ['fontname', ['fontname']],
                ['color', ['color']],
                ['para', ['ul', 'ol', 'paragraph']],
                ['height', ['height']],
                ['table', ['table']],
                ['insert', ['link', 'picture', 'hr']]
            ],
            focus: true
        });
        $(".summernote-wrapper").on('click', "[data-event = showImageDialog],[data-event=showLinkDialog]", function () {
            $(".modal-backdrop.in").remove()

        })
//        $('.note-editable').on('keypress',function(e){
//            console.log(e.keyCode)
//            if(e.which == 32){
//                return true;
//            }
//        })
        $('#submitAnnoucement').on('click', function () {

            if ($('#Annoucement_Title').val() == '' || $('#summernote').code() == '' || $('#type_announce').val() == '---') {
                $("#SuccessAlert").attr("class", "alert alert-danger").html(Required_Fields)
                $(document.body).animate({scrollTop: 0})
                return;
            }
            $("#SuccessAlert").attr("class", "").html("")
            var Data = new FormData();
            Data.append("Announcement_Title", $('#Annoucement_Title').val())
            Data.append("Announcement_Subtitle", $('#Annoucement_Subtitle').val())
            Data.append("Announcement_Type", $('#type_announce').val())
            Data.append("Announcement_Value", $('input[name="privacy__value"]').val())
            Data.append("Post_Time", $('#Post_Time').val()),
                    Data.append("Announcement_Content", $('#summernote').code())
//
//            if ($('#type_announce').val() == 2) {
//
//                Data.append("Branch_ID", $('#Branch_Name').val());
//            }
//            if ($('#type_announce').val() == 3) {
//
//                Data.append("Employee_ID", $('#employee_id').val());
//            }
            if (pubID > 0)
                Data.append("Announcement_ID", pubID);
            $("#AnnouncmentLoader").removeClass('hidden');



            $.ajax({
                url: "HRMAjax/NewAnnouncementRequest",
                method: 'POST',
                data: Data,
                processData: false,
                contentType: false,
                complete: function (v1, v2) {

                    if (v2 == "success" && v1.responseText == "sent") {
                        $("#SuccessAlert").attr("class", "alert alert-success").html(Submitted_Successfully)
                        if (pubID > 0) {
//                            $("#SuccessAlert").html(Submitted_Successfully)
                        } else {
//                            $("#SuccessAlert").html(Submitted_Successfully)
                            $("#Annoucement_Title,#Annoucement_Subtitle").val("");
                            $('#summernote').code("");
                        }
                        $(document.body).animate({scrollTop: 0})
                    } else {
                        $("#SuccessAlert").attr("class", "alert alert-danger").html(v1.responseText)
                    }
                    $("#AnnouncmentLoader").addClass('hidden');
                }
            });
        });
    });
    function addZero(i) {
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    }
    function clocktime() {
        var date = new Date();
        var hours = addZero(date.getHours());
        var minutes = addZero(date.getMinutes());
        var seconds = addZero(date.getSeconds());
        var ampm = hours >= 12 ? 'pm' : 'am';
        var clock = hours + ":" + minutes + ":" + seconds + " " + ampm + " ";
        $("#Post_Time").val(clock)
    }
    function changeeselect(who) {
        var item = who.val();
        (item == 'dep' ? ($('.department_priv').removeClass('hidden'), $('#department_priv').attr('name', "privacy__value")) : ($('.department_priv').addClass('hidden'), $('#department_priv').removeAttr('name')));
        (item == 'admin_dep' ? ($('.private_sup').removeClass('hidden'), $('#private_sup').attr('name', "privacy__value")) : ($('.private_sup').addClass('hidden'), $('#private_sup').removeAttr('name')));
        (item == 'usr' ? ($('.emp_priv_grp').removeClass('hidden'), $('#emp_priv_grp').attr('name', "privacy__value")) : ($('.emp_priv_grp').addClass('hidden'), $('#emp_priv_grp').removeAttr('name')));
        (item == 'grp' ? ($('.emp_grp').removeClass('hidden'), $('#emp_grp').attr('name', "privacy__value")) : ($('.emp_grp').addClass('hidden'), $('#emp_grp').removeAttr('name')));
        if (pubID > 0) {
            $('.selectitem >').find('input').not('.' + item).val('');
            $('.selectitem >').find('input').not('.' + item).attr('data-select-name', '');
        }
    }
</script>