var spermfreezing = function () {

    var spermfreezingReport = function () {


        $(".bp_location2").inputmask("(**,**,**,**)", {
            autoUnmask: true
        });


        var hosturl = $('#hosturl').val();
        var patientId = $("#patientId").val();
        //freezing media
        $("#frmedia").select2({
            width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                return "<input class='form-control' id='newMediaTerm' value='" + term + "'><a href='#' id='addNew' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNew', function () {
            /* add the new term */
            var media = $('#newMediaTerm').val();
            $.post(hosturl + "/controllers/spermfreezing.php?ac=addmedia", { media: media }, function (data) {
                $('.frmedia > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + media + '</option>');
                //$('<option value="' + Number(data) + '" >' + education + '</option>').appendTo('#education');
                $('#frmedia').select2('val', Number(data)); // select the new term
                $("#frmedia").select2('close'); // close the dropdown
            });
        });
        //freezing protocol
        $("#protocol").select2({
            width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                return "<input class='form-control' id='newProtocolTerm' value='" + term + "'><a href='#' id='addNew' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNew', function () {
            /* add the new term */
            var protocol = $('#newProtocolTerm').val();
            $.post(hosturl + "/controllers/spermfreezing.php?ac=addprotocol", { protocol: protocol }, function (data) {
                $('.protocol > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + protocol + '</option>');
                //$('<option value="' + Number(data) + '" >' + education + '</option>').appendTo('#education');
                $('#protocol').select2('val', Number(data)); // select the new term
                $("#protocol").select2('close'); // close the dropdown
            });
        });
        $(".frmedia").live('change', function () {
            // Do something wi.on('change', function () {
            var colName = $(this).attr("data-colName");
            var tableName = $(this).attr("select-tableName");
            var thisvalue = $(this).val();
            if (thisvalue == '-2') {

                $.post(hosturl + '/controllers/spermfreezing.php?ac=editModal', { tableName: tableName, colName: colName }, function (data) {
                    $("#frmedia").select2("val", "");

                    $("#newitem_name").val('');
                    $("#newitem_id").val('');
                    $("#alldatadiv").html(data);
                    $("#edit_model").modal('show');
                });
            }

        })
        $(".protocol").on('change', function () {
            // Do something wi.on('change', function () {
            var colName = $(this).attr("data-colName");
            var tableName = $(this).attr("select-tableName");
            var thisvalue = $(this).val();
            if (thisvalue == '-2') {

                $.post(hosturl + '/controllers/spermfreezing.php?ac=editModal', { tableName: tableName, colName: colName }, function (data) {
                    $("#protocol").select2("val", "");
                    $("#newitem_name").val('');
                    $("#newitem_id").val('');
                    $("#alldatadiv").html(data);
                    $("#edit_model").modal('show');
                });
            }

        })

        //////////////////////////////////////////////// add new row in the modal ////////////////////////////////////////////////
        $("#addnewRow").live('click', function () {
            var tablename = $(this).attr('data-tableName');
            var selectname = $(this).attr('data-selectClass');

            var colName = $("#colName").val();

            $.post(hosturl + "/controllers/spermfreezing.php?ac=addNewRow", { tablename: tablename, colName: colName }, function (data) {

                var dataid = Number(data);
                $('.' + selectname + ' > .deleteoptincompo').before('<option value="' + dataid + '" ></option>');
                $("#dtAddTbl").prepend('<tr>\
                                            <td style="width: 100%">\
                                                <a href="#" >\
                                                    <input type="text" id="newitem_name" class="form-control updateElementSelect" data-selectClass="' + tablename + '"  data-select2="yes" data-id="' + dataid + '" data-colName="name" data-tableName="' + tablename + '" value="" name="newamount_name"/>\
                                                </a>\
                                            </td>\
                                            <td>\
                                                <a class="delrow fa fa-times btn red fleft" data-colName="conditions" data-link="gyna" data-tableName="' + tablename + '" data-fn="deldiagnosis" data-id="' + dataid + '" ></a>\
                                            </td>\
                                        </tr>');
            });

        });
        // edit the new row of modal
        $("#newitem_name").live('change', function () {

            var value = $(this).val();
            var id = $(this).attr("data-id");

            var tablename = $("#tablename").val();
            var patientid = $("#patientid").val();
            var colName = $("#colName").val();


            if (id != '') {
                $.post(hosturl + '/controllers/spermfreezing.php?ac=updateModal', { tablename: tablename, colName: colName, id: id, value: value, patientid: patientid }, function (data) {
                    if (tablename === "freezingmedia") {
                        $(".frmedia option[value='" + id + "']").text(value);
                        $('#frmedia').select2('val', id);
                        //                        $('#edit_model').modal('hide');
                    }
                    else if (tablename === "freezingprotocol") {
                        $(".protocol option[value='" + id + "']").text(value);
                        $('#protocol').select2('val', id);
                        //                        $('#edit_model').modal('hide');
                    }
                });
            }
        });
        ///////////////////////////////////////////////////// delete row from modal and database and select2////////////////////////

        $('.deleteModal').live('click', function () {
            var id = $(this).attr('data-id');

            var tablename = $("#tablename").val();
            var tr = $(this).parent();
            bootbox.dialog({
                message: "Do you want to delete this item?",
                title: "Delete confirmation",
                buttons: {
                    danger: {
                        label: "Cancel",
                        className: "btn-primary"
                    },
                    main: {
                        label: "Delete",
                        className: "btn-danger",
                        callback: function () {

                            $.post(hosturl + '/controllers/spermfreezing.php?ac=delRowsommedel', { id: id, tableName: tablename }, function (res) {

                                if (res == 1) {
                                    tr.parent().remove();
                                    if (tablename === "freezingmedia") {
                                        $(".frmedia option[value='" + id + "']").remove();

                                    }
                                    else if (tablename === "freezingprotocol") {
                                        $(".protocol option[value='" + id + "']").remove();

                                    }
                                }
                                else {
                                    alert("!!!!!!!!!!!!! some thing wrong here");
                                }
                            });


                        }
                    }
                }
            }).find('.modal-content').addClass('modal-sm');
        });





        ///////////////////////////////////////////////// print reports         ///////////////////////////////////////////////
        $(".printReportOfFreezing").live('click', function () {
            var id = $(this).attr('data-intId');
            var datapatientId = $(this).attr('data-patientId');
            $.post(hosturl + '/controllers/spermfreezing.php?ac=getprint', { id: id, patientid: datapatientId }, function (data) {
                $("#printDiv").html(data);
                $.print("#printDiv");
                $("#printDiv").html('');
            });



        });
        $(".printReportOfembryology").live('click', function () {
            var id = $(this).attr('data-intId');
            var datapatientId = $(this).attr('data-patientId');
            $.post(hosturl + '/controllers/embryology.php?ac=getprint', { id: id, patientid: datapatientId }, function (data) {
                $("#printDiv").html(data);
                $.print("#printDiv");
                $("#printDiv").html('');
            });



        });

        $('body').on('click', '#tankLocationBtn', function () {
            $.post(hosturl + '/controllers/spermfreezing.php?ac=tankLocation', function (data) {
                $("#tankLocationDiv").html();
                $("#tankLocationDiv").html(data);
                $("#tankLocationModal").modal('show');
            });
        });

        $('body').on('click', '#tankSettingsBtn', function () {
            $.post(hosturl + '/controllers/embryoslab.php?ac=tankSetting', function (data) {
                $("#tankSettingDiv").html();
                $("#tankSettingDiv").html(data);
                $("#tankSettingModal").modal('show');
            });
        });

        $('body').on('click', '.tankCanBtn', function () {
            var embryosLabId = $("#embryoslab").val();
            var preview = $("#preview").val();
            var location = $(this).attr('data-location');
            var no = $(this).attr('data-no');

            $.post(hosturl + '/controllers/embryoslab.php?ac=tankCan'
                , { embryosLabId: embryosLabId, location: location, no: no, preview: preview }, function (data) {
                    if (no < 10) {
                        $("#upperCellsDiv").html('');
                        $("#lowerCellsDiv").html('');
                        $("#upperCellsDiv").html(data);
                    } else {
                        $("#upperCellsDiv").html('');
                        $("#lowerCellsDiv").html('');
                        $("#lowerCellsDiv").html(data);
                    }
                });
        });

        $("body").on('click', '.emptyCell', function () {
            $(this).removeClass('emptyCell');
            $(this).addClass('reserveMeCell');
        });

        $("body").on('click', '.reserveMeCell', function () {
            $(this).removeClass('reserveMeCell');
            $(this).addClass('emptyCell');
        });

        $('body').on('click', '#saveTankLocation', function () {
            $("#saveTankLocation").attr('disabled', true);
            var embryosLabId = $("#embryoslab").val();
            var location = $("#location").val();
            var no = $("#no").val();
            var selectedCells = new Array();
            var cellData = new Array();
            $('.reserveMeCell').each(function () {
                cellData = [];
                cellData[0] = $('#location').val();
                cellData[1] = $('#no').val();
                cellData[2] = $(this).attr('data-row');
                cellData[3] = $(this).attr('data-col');
                cellData[4] = $(this).attr('data-inner');

                selectedCells.push(cellData);
            });

            $.post(hosturl + '/controllers/embryoslab.php?ac=saveTankLocation',
                {
                    location: location, no: no, embryosLabId: embryosLabId
                    , selectedCells: JSON.stringify(selectedCells)
                }, function (data) {
                    alert(data);
                    $("#saveTankLocation").attr('disabled', false);
                    loadLocationCells(embryosLabId);
                });
        });


        $('body').on('click', '.tankLocationSpan', function () {
            var location = $(this).attr('data-location');
            var no = $(this).attr('data-no');
            var specTabId = '#tabUpper';
            if (no > 9) {
                specTabId = '#tabLower';
            }
            var specCanId = '#' + location + '' + no;

            $.post(hosturl + '/controllers/embryoslab.php?ac=tankLocation'
                , { specTabId: specTabId, specCanId: specCanId }, function (data) {
                    $("#tankLocationDiv").html();
                    $("#tankLocationDiv").html(data);
                    $("#tankLocationModal").modal('show');
                });
        });

        $('body').on('click', '#tankLocationPreviewBtn', function () {
            $.post(hosturl + '/controllers/embryoslab.php?ac=tankLocation', { preview: 1 }, function (data) {
                $("#tankLocationDiv").html();
                $("#tankLocationDiv").html(data);
                $("#tankLocationModal").modal('show');
            });
        });


        function loadLocationCells(embryosLabId) {
            $.post(hosturl + '/controllers/embryoslab.php?ac=tankLocationDisplay'
                , { embryosLabId: embryosLabId }, function (data) {
                    $("#cellsContainer").html('');
                    $("#cellsContainer").html(data);
                });
        }

    }
    return {
        //main function to initiate the theme
        init: function () {

            if ($("#spermfreez").length > 0) {
                spermfreezingReport();
            }
        }
    };
}();