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('');
            });



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

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