/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 * Amr in 16-11-2015
 */

var hosturl = $('#hosturl').val();

$(document).ready(function () {

    $("#awval").validate();
    $(".awval").validate();
    $(".today").datepicker("setDate", "0");
    $(".tomoro").datepicker("setDate", "+1");
    $(".datepicker").datepicker();
    $.datepicker.setDefaults({
        dateFormat: "yy/mm/dd",
        changeMonth: true,
        changeYear: true
    });
    $(".datepicker").datepicker({
        appendText: "(yy/mm/dd)"
    });

    $('[data-toggle="popover"]').popover();

    var minxhit = $(window).height();
    var mihitx = minxhit + "px";
    $("#pageminheight").css("min-height", mihitx);
/////////////////////////

//$(window).scroll(function () {
//    if ($(this).scrollTop() > 370) {
//        $('.opsrisk').addClass('fixed');
//        var fulwdz = $('.col-md-12').width();
//        var fullwidth = fulwdz + "px";
//        $(".fixed").css("width", fullwidth);
//    } else {
//        $('.opsrisk').removeClass('fixed');
//    }
//});


    $(".btn-group").hover(function () {
        $(".btn-group").removeClass("open");
        $(this).addClass("open");
    });
    $("*").click(function () {
        $(".btn-group").removeClass("open");
    });
    $(".clinick.invz .form-group.form-md-line-input.has-info").focusin(function () {
        $(this).children(".input-group-addon").css("z-index", "-500");
    });
    $(".clinick.invz .form-group.form-md-line-input.has-info").focusout(function () {
        $(this).children(".input-group-addon").css("z-index", "500");
    });
    $(".amrpop,input[type='text']:not(.datepicker):not(.notPop),textarea:not(.notPop)").hover(function () {
        var pop_value = $(this).val();
        $(this).attr({
            "data-toggle": "popover",
            "data-trigger": "hover",
            "data-placement": "bottom",
            "data-content": pop_value
        });
        $(this).popover("show");
    });
    $(window).load(function () {
        $('input[type="search"]').attr("placeholder", "Search ..");
    });
    $(".focus-search-index").click(function () {
        $(this).hide();
        $(".xhide").hide();
        $(".indexsearchbox").css("z-index", "1");
    });
    $(document).keyup(function (e) {
        $(".focus-search-index").hide();
        //$(".xhide").hide();
        $(".indexsearchbox").css("z-index", "1");
    });


    var check_heigt_and_make_new_class = $(".check_heigt_and_make_new_class").height();
    if (check_heigt_and_make_new_class > 400) {
        $(".check_heigt_and_make_new_class").addClass("overflow_scroll");
    } else {
        $(".check_heigt_and_make_new_class").removeClass("overflow_scroll");
    }


    /***********/


//
//    $("input").hover(function () {
//        var xlabel = $(this).parent('#awval div').first().children('label').html();
//        $(this).parent('#awval div').first().children('label').hide()
//        $(this).attr("placeholder", xlabel);
//    });
//
//    $("input").hover(function () {
//        var xlabel = $(this).parent('#div_exmination div').first().children('label').html();
//        $(this).parent('#div_exmination div').first().children('label').hide()
//        $(this).attr("placeholder", xlabel);
//    });
//
//
//    $("input").hover(function () {
//        var xlabel = $(this).parent('.invz div').first().children('label').html();
//        $(this).parent('.invz div').first().children('label').hide()
//        $(this).attr("placeholder", xlabel);
//    });
//
//
//    $("input").hover(function () {
//        var xlabel = $(this).parent('.ng-pristine div').first().children('label').html();
//        $(this).parent('.ng-pristine div').first().children('label').hide()
//        $(this).attr("placeholder", xlabel);
//    });
//
//


    /***************************************************************/

    var showNum = 10;
    var showScrollLines = (showNum * 31) + 23;
    $(".showNum").height(showScrollLines);

    var showNumGyna = 10;
    var showScrollLines = (showNumGyna * 31) + 43;
    $(".showNumGyna").height(showScrollLines);


    /***********/


});
$(document).ready(function () {
    $('.menu_role_item').change(function () {

        let check_val = $(this).val();
        let role_id = $('input[name=roleid]').val();
        let menu_id = $(this).attr('data-menuid');
        $.post(hosturl + '/controllers/_role.php?ac=updateMenuSetting', {roleid: role_id, menuid: menu_id, menurolecheck: check_val}, function (result) {
            //alert(result);
        });
    });

    $('.menu_user_item').change(function () {

        let check_val = $(this).val();
        let user_id = $('input[name=userid]').val();
        let menu_id = $(this).attr('data-menuid');
        $.post(hosturl + '/controllers/_member.php?ac=updateMenuUserSetting', {userid: user_id, menuid: menu_id, menurolecheck: check_val}, function (result) {
            //alert(result);
        });
    });

    $('.control_prop_role_item').change(function () {

        let check_val = $(this).val();
        let role_id = $('input[name=roleid]').val();
        let control_prop_id = $(this).attr('data-controlpropid');
        $.post(hosturl + '/controllers/_role.php?ac=updateControlSetting', {roleid: role_id, controllpropid: control_prop_id, proprolecheck: check_val}, function (result) {
            //alert(result);
        });
    });

    $('.btn_role_item').change(function () {

        let check_val = $(this).val();
        let role_id = $('input[name=roleid]').val();
        let btn_id = $(this).attr('data-btnid');
        $.post(hosturl + '/controllers/_role.php?ac=updateBtnSetting', {roleid: role_id, btn_id: btn_id, btncheck: check_val}, function (result) {
            //alert(result);
        });
    });
});

$(document).ready(function () {

    $('.remove_patient_tank_cell').click(function () {
        let cell_id = $(this).attr('data-id');
        let tank_id = $(this).attr('data-tank-id');
        $.post(hosturl + '/controllers/tanks.php?ac=removePatientCell', {cell_id: cell_id}, function (result) {
            if (Boolean(result) === true) {
                window.location.href = hosturl + "/controllers/tanks.php?ac=cells&id=" + tank_id;
            }
        });
    });

    $('#lab_tank_id').change(function () {
        let tank_id = $(this).val();
        $.post(hosturl + '/controllers/tanks.php?ac=tankLabCells', {tank_id: tank_id}, function (result) {
            $('#tankCellsContainer').html(result);
        });
    });

    let selected_name_group_lab = '';
    $('body').on('change', '.last_cell_tank', function (e) {
        let select_group = $(this).attr('data-name');
        if (selected_name_group_lab == '' || select_group == selected_name_group_lab) {
            selected_name_group_lab = select_group;
            $('#up_level_name').val($(this).attr('data-up-level'));
            $('#cell_name').val($(this).attr('data-cell-name'));
        } else {
            $(this).prop("checked", false);
            alert('You can not choose another cell');
        }

        // check if empty
        if ($('input[data-name="' + selected_name_group_lab + '"]:checked').length <= 0) {
            selected_name_group_lab = '';
            $('#up_level_name').val(0);
            $('#cell_name').val(0);
        }

    });

    $('#save_tank_lab_cell_patient').click(function () {
        // create form data
        let formData = new FormData();
        formData.append('lab_tank_id', $('#lab_tank_id').val());
        formData.append('up_level_name', $('#up_level_name').val());
        formData.append('cell_name', $('#cell_name').val());
        formData.append('lab_patient_id', $('input[name="lab_patient_id"]').val());
        // get all checked cells with name=last_cell[]
        let checked_cells = $('input[name="last_cell[]"]:checked');
        let cells = [];
        checked_cells.each(function (index, element) {
            cells.push($(element).val());
        });
        formData.append('last_cell', cells);
        // get input color #color_picker_cell and append it to form data
        let $choosenColor = $('#color_picker_cell').val();
        formData.append('cell_color', $choosenColor);

        
        // get input tank txt #tank_txt and append it to form data
        let $tank_txt = $('#tank_txt').val();
        formData.append('tank_txt', $tank_txt);

        const control_name = $("#control_name").val();
        const control_id = $("#control_id").val();
        formData.append('control_name', control_name);
        formData.append('control_id', control_id);
        if(control_name == 'embryo-lab') {
            const assignAt = $('[name="date_freezing"]').val();
            formData.append('assignAt', assignAt);
        }else if(control_name == 'sperm-freezing') {
            const assignAt = $('[name="sysdate"]').val();
            formData.append('assignAt', assignAt);
        }



        // check if no cells selected and color
        if (cells.length <= 0 || $choosenColor == '') {
            bootbox.dialog({
                message: "Please choose at least one cell and color",
                title: "Warning",
                // add class to bootbox
                className: 'warning',
                buttons: {
                    danger: {
                        label: "Ok",
                        className: "btn-success w-100",
                    }
                }
            });
            return false;
        }

        // send request
        $.ajax({
            url: hosturl + '/controllers/tanks.php?ac=saveTankLabCell',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function (result) {
                if (Boolean(result)) {
                    $('#addTankLocation').modal('toggle');
                    location.reload();
                } else {
                    bootbox.dialog({
                        message: "Something bad happened , check again after refresh",
                        title: "Error",
                        // add class to bootbox
                        className: 'error',
                        buttons: {
                            danger: {
                                label: "Ok",
                                className: "btn-danger w-100",
                            }
                        }
                    });
                }
            },
            error: function (error) {
                console.log(error);
            }
        });
    })
});