var gyna = function () {
    /*$("#diagnosis3 option[value='2']").text('new text');
     $("#diagnosis3").select2("destroy");
     $("#diagnosis3").select2();*/
    var gyna_fun = function () {
        //global hosturl
        var hosturl = $('#hosturl').val();
        var patientid = $('#patientId').val();

        $('#gyna_add').click(function () {

            $.post(hosturl + '/controllers/gyna.php?ac=newrow&id=' + patientid, function (data) {
                $('#gyna').prepend(data);

                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");
                }
            });


        });

        $iDsArray = $('#iDsArray').val();
        if ($iDsArray != '')
        {
            $iDsArray = $iDsArray.split(',');
            for ($i = 0; $i < $iDsArray.length; $i++)
            {
                diagnosis_itr = $iDsArray[$i];

                $('#diagnosis' + diagnosis_itr).select2({
                    width: '270px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        $thisitr = $(this).attr('data-id');
                        // return '<input class="form-control" id="newregTerm' + $thisitr + '" value="' + term + '"><a href="#" id="addregnew' + $thisitr + '" data-itr="' + $thisitr + '" class="btn btn-default addnewselect">Add</a>';

                    }
                });
            }
        }

        /*$(document).ready(function () {
         
         $('.datepicker').datepicker({
         format: "yy/mm/dd",
         })
         //Listen for the change even on the input
         //.change(dateChanged)
         .on('changeDate', dateChanged);
         });
         function dateChanged(ev) {*/
        $('body').on('change', '.datepicker', function () {
            var value = $(this).val();//$(this).data('datepicker').getFormattedDate('yy/mm/dd');//choosen date
            var id = $(this).attr("data-id");
            var colName = $(this).attr("data-colName");
            var tableName = $(this).attr("data-tableName");
            //var value = $(this).val();
            var controllName = $('#controllName').val();
            var patientId = $('#patientId').val();

            if (colName == "gynadate" || colName == "gynalmp") {
                $.post(hosturl + '/controllers/' + controllName + '.php?ac=update', {id: id, value: value, tableName: tableName, colName: colName, patientId: patientId}, function (res)
                {

                });
            }
        });
        /*------------------------- edit,del diagnosis -------------------------------*/

        $('.all').on('change', '.diagnosis', function ()
        {
            /*
             var thisvalue = $(this).val();
             if (thisvalue == '-2')
             {
             $(this).select2("val", "");
             $.post(hosturl + '/controllers/gyna.php?ac=alldiagnosis', function (data)
             {
             $("#alldatadiv").html(data);
             $("#newitem_name").val("");
             $("#newitem_id").val("");
             $("#edit_model").modal('show');
             });
             }*/
        });
        $(".clikMe").live('click', function ()
        {
            var id = $(this).attr('data-id');
            var name = $(this).text();
            $("#newitem_name").val(name);
            $("#newitem_id").val(id);
        });
        $("#editmodel_submit").live('click', function () {
            var value = $("#newitem_name").val();
            var id = $("#newitem_id").val();
            var itr = $(this).attr('data-itr');
            if (id > 0)
            {
                $.post(hosturl + '/controllers/gyna.php?ac=update', {tableName: 'diagnosis', id: id, colName: 'name', value: value}, function (data) {
                    $(".diagnosis option[value='" + id + "']").text(value);
                    $('#diagnosis' + itr).select2('val', id);
                    $('#edit_model').modal('hide');

                    $.post(hosturl + '/controllers/gyna.php?ac=update', {tableName: 'gyna', id: itr, colName: 'diagnosisid', value: id}, function (data) {
                    });
                });
            } else
            {
                alert('choose element to edit first !');
            }
        });


        $('body').on('shown.bs.modal', '#add_model', function () {
            $('#diagnosisName').focus();
        });



//////////////// Diagnosis Multiple Select
        $('.updateDiagnosis').live('change', function ()
        {
            var id = $(this).attr("data-id");
            var colName = $(this).attr("data-colName");
            var tableName = $(this).attr("data-tableName");
            //var value = $(this).val();
            var controllName = $('#controllName').val();
            var patientId = $('#patientId').val();
            var dated = 0;

            var realvalues = new Array(); //storing the selected values inside an array
            var lastvalue;
            var slcttable;
            if (colName == "diagnosisid")
            {
                $('#diagnosis' + id + ' :selected').each(function (i, selected) {
                    realvalues[i] = $(selected).val();
                    lastvalue = $(selected).val();
                });
                slcttable = "diagnosis";
            } else
            {
                $('#complaint' + id + ' :selected').each(function (i, selected) {
                    realvalues[i] = $(selected).val();
                    lastvalue = $(selected).val();
                });
                slcttable = "complaint";
            }
            //var lastvalue = realvalues[realvalues.length-1];

            if (lastvalue == -2)
            {
                var selectId = $(this).attr('id');
                $("#" + selectId + " option[value='-2']").prop("selected", false);
                $("#" + selectId + "").select2();
                $.post(hosturl + '/controllers/gyna.php?ac=alldiagnosis', {colName: colName}, function (data)
                {
                    $("#alldatadiv").html(data);
                    $("#newitem_name").val("");
                    $("#newitem_id").val("");
                    $("#slctTbl").val(slcttable);
                    $("#edit_model").modal('show');
                    $("#edtSlct").attr("data-tableName", slcttable);
                    realvalues.pop();
                });
            }
            if ($.inArray("-1", realvalues) != -1)
            {
                var selectId = $(this).attr('id');
                $("#" + selectId + " option[value='-1']").prop("selected", false);
                $("#" + selectId + "").select2();
                $("#add_model").modal('show');
                realvalues.pop();
                $("#diagnosisName").val('');
                $("#diagnosisName").attr("data-select", selectId);
                $("#diagnosisName").attr("data-tablename", slcttable);
                $("#diagnosisName").attr("data-id", id);



            }
            if (lastvalue != '-1' && lastvalue != '-2' && $.inArray("-1", realvalues) == -1)
            {
                $.ajax({
                    url: hosturl + '/controllers/' + controllName + '.php?ac=update',
                    data: {
                        value: JSON.stringify(realvalues),
                        id: id,
                        tableName: tableName,
                        colName: colName,
                        patientId: patientId,
                        dated: dated
                    },
                    type: 'POST',
                    success: function (data) {

                    }
                });

            }
        });


        $(".addDiagnosisName").live('change', function () {
            var hosturl = $('#hosturl').val();
            var selectId = $(this).attr('data-select');
            var table = $(this).attr('data-tablename');
            var id = $(this).attr("data-id");
            var newitem = $(this).val();
            var select;

            $.post(hosturl + '/controllers/gyna.php?ac=adddiagnosis', {diagnosisName: newitem, id: id, table: table}, function (data) {

                $('.' + table + ' > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                $('#' + selectId + ' option[value="' + Number(data) + '"]').attr('selected', true);
                $('select').select2();
                //$('#diagnosis' + $thisitr).select2('val', Number(data)); // select the new term
            });
        });

        $("#addnewRow").live('click', function () {
            var tablename = $(this).attr('data-tableName');
            var selectname = $(this).attr('data-selectClass');
            var dtcount = $("#dtcount").val();
            dtcount++;
            $.post(hosturl + "/controllers/gyna.php?ac=addNewRow", {tablename: tablename}, 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" class="form-control updateElementSelect" data-selectClass="' + tablename + '"  data-select2="yes" data-id="' + dataid + '" data-colName="name" data-tableName="' + tablename + '" value=""/>\
                                                </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>');
            });
            $("#dtcount").val(dtcount);
        });

        /*----------------------------------------------------------------------------*/



        /*$(".js-data-example-ajax").select2({
         ajax: {
         url: "https://api.github.com/search/repositories",
         dataType: 'json',
         delay: 250,
         data: function (params) {
         return {
         q: params.term, // search term
         page: params.page
         };
         },
         processResults: function (data, page) {
         // parse the results into the format expected by Select2.
         // since we are using custom formatting functions we do not need to
         // alter the remote JSON data
         return {
         results: data.items
         };
         },
         cache: true
         },
         escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
         minimumInputLength: 1,
         templateResult: formatRepo, // omitted for brevity, see the source of this page
         templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
         });*/

        /*var deletemsg = function () {
         //to open model for accept del
         $('.deletemsg').click(function () {
         var itemid = $(this).attr('data-id');
         var urlgo = $(this).attr('data-link');
         bootbox.dialog({
         message: "Do you want to delete this item?",
         title: "Delete confirmation",
         buttons: {
         danger: {
         label: "Cancel",
         className: "btn-danger"
         },
         main: {
         label: "Delete",
         className: "btn-primary",
         callback: function () {
         //do something else
         window.location.href = hosturl + "/controllers/" + urlgo + ".php?ac=del&id=" + itemid;
         }
         }
         }
         }).find('.modal-content').addClass('modal-sm');
         });
         }*/


    }














    return {
        //main function to initiate the module
        init: function () {
            if ($('#controllName').val() == "gyna")
            {
                gyna_fun();
            }

        }

    };

}();
