var drug_file = function () {

    //at drug cotroller
    $('#drugname_search').live('keyup', function () {
        var hosturl = $('#hosturl').val();
        var drugname_search = $('#drugname_search').val();
        $.post(hosturl + "/patienttreatmentajax/searchbydrugname", {drugname_search: drugname_search}, function (data) {
            $('#new_search_res').html(data);
            $('#pag_res').css('display', 'none');
        });

    });
    $('.catnew_show').live('change', function () {
        var hosturl = $('#hosturl').val();
        var catid = $(this).val();
        $.post(hosturl + "/patienttreatmentajax/searchbycatid", {catid: catid}, function (data) {
            $('#new_search_res').html(data);
            $('#pag_res').css('display', 'none');
        });
    });

    /*======================== add new cat in drug controller ==================================================*/
    $('.catnew').live('change', function () {
        //alert($(this).val());
        if ($(this).val() == -2)
        {
            $('#cat_model').modal('show');
        }
        if ($(this).val() == -3)
        {
            $("#catedit_name").val("");
            $('#editcat_model').modal('show');
        }
        var row_itr = this.id.substr(5);
        $("#cat_submit").one('click', function () {
            //alert(row_itr);
            var hosturl = $('#hosturl').val();
            var catname = $('#catname').val();
            $.post(hosturl + '/patienttreatmentajax/addcat', {row_itr: row_itr, catname: catname}, function (data) {
                $("#cat_form")[0].reset();
                $('#cat_model').modal('hide');
                $('#catdiv' + row_itr).html(data);
                $("#catid" + row_itr).select2();

            });
        });
    });
    var editcat = function ()
    {
        var hosturl = $('#hosturl').val();
        $(".catnew").live('change', function () {
            var regularity_val = $(this).val();
            if (regularity_val == -3) {
                $.post(hosturl + '/patienttreatmentajax/getallcat', function (data) {
                    $("#allcat").html(data);
                });
            }
        });

        $(".cl_choosename").live('click', function ()
        {
            var id = $(this).attr('data-id');
            var name = $(this).text();
            $('#cat_edit_sumbit').removeAttr('disabled');
            $("#catedit_name").val(name);
            $("#catedit_id").val(id);
        });
        $("#cat_edit_sumbit").live('click', function () {
            var regname = $("#catedit_name").val();
            var regid = $("#catedit_id").val();
            var itr_drug = document.getElementById('itr_drug').value;
            var new_itr = itr_drug - 1;
            //alert(itr_drug);
            $.post(hosturl + '/patienttreatmentajax/updatecat', {regid: regid, regname: regname, itr_drug: itr_drug}, function (data) {
                $("#catdiv" + itr_drug).html(data);
                $('#editcat_model').modal('hide');
                $("#catid" + itr_drug).select2();
                $("#catid" + itr_drug).select2("data", {id: regid, text: regname});

            });
        });
    }
    $('.delcat').live('click', function () {
        var id = $(this).attr('data-id');
        var hosturl = $('#hosturl').val();
        var itr_drug = document.getElementById('itr_drug').value;
        //alert(itr_drug);
        $.post(hosturl + "/patienttreatmentajax/delcat", {id: id}, function (data) {
            if (data > 0)
            {
                $('#cat_edit_sumbit').attr('disabled', 'disabled');
                $.post(hosturl + '/patienttreatmentajax/getallcat', function (data) {
                    $("#allcat").html(data);
                    $.post(hosturl + '/patienttreatmentajax/querycat', {itr_drug: itr_drug}, function (data) {
                        //alert(data);
                        $("#catdiv" + itr_drug).html(data);
                        //$('#editcat_model').modal('hide');
                        $("#catid" + itr_drug).select2();
                    });
                });
            }
        });
    });
    /*======================== add new drug in drug controller ==================================================*/
    $('.dragchange_div').live('change', function () {
        var row_itr = this.id.substr(10);
        //alert(row_itr);
        if ($('#drugnameid' + row_itr).val() == -2)
        {
            $('#drugnamemodel').modal('show');
        }
        if ($('#drugnameid' + row_itr).val() == -3)
        {
            $("#drugname_name").val("");
            $('#editdrug_model').modal('show');
        }
        var itr_drug = $('#itr_drug').val();

        var addnewdrugname = 0;
        $(".dragchange_div").each(function () {

            if ($(this).val() == -1)
            {
                addnewdrugname = 0;
            } else {
                addnewdrugname = 1;
            }
        });
        if (addnewdrugname == 1) {
            itr_drug++;

            $("#treat_addnew").append('<div class="row">'

                    + ' <div class="col-md-3">'
                    + ' <div class="form-group">'
                    + ' <label class="col-md-4 control-label"> Group </label>'
                    + ' <div class="col-md-8">'
                    + '<div id="catdiv' + itr_drug + '" style="width:166px;">'
                    + '  <select class="form-control catnew select2me" name="catid' + itr_drug + '" id="catid' + itr_drug + '" >'
                    + $('#allcat_div').html()
                    + '</select>'
                    + ' </div>'
                    + ' </div>'
                    + '  </div>'
                    + '  </div>'

                    + ' <div class="col-md-3">'
                    + ' <div class="form-group">'
                    + ' <label class="col-md-4 control-label"> Drug Name</label>'
                    + ' <div class="col-md-8">'
                    + '   <div id="drugnamediv' + itr_drug + '">'
                    + '  <select class="form-control dragchange_div select2me" name="drugnameid' + itr_drug + '" id="drugnameid' + itr_drug + '" >'
                    + $('#alldrug_div').html()
                    + '</select>'
                    + ' </div>'
                    + ' </div>'
                    + '  </div>'
                    + '  </div>'

                    + ' <div class="col-md-2">'
                    + ' <div class="form-group">'
                    + ' <label class="col-md-4 control-label">Form</label>'
                    + ' <div class="col-md-8">'
                    + '   <div id="drugtypediv' + itr_drug + '">'

                    + '  <select class="form-control drugtypeid" name="drugtypeid' + itr_drug + '" id="drugtypeid' + itr_drug + '" >'
                    + $('#allDrugtype_div').html()
                    + '</select>'

                    + ' </div>'
                    + ' </div>'
                    + '  </div>'
                    + '  </div>'

                    + ' <div class="col-md-3">'
                    + ' <div class="form-group">'
                    + ' <label class="col-md-4 control-label">Use</label>'
                    + ' <div class="col-md-8">'
                    + '   <div id="drugdosediv' + itr_drug + '">'

                    + '  <select class="form-control drugdoseid_div makeeggwithfont" name="drugdoseid' + itr_drug + '" id="drugdoseid' + itr_drug + '" >'
                    + $('#alluses_div').html()
                    + '</select>'

                    + ' </div>'
                    + ' </div>'
                    + '  </div>'
                    + '  </div>'
                    + ' </div>'
                    );

            $("#drugnameid" + itr_drug).select2();
            $("#drugtypeid" + itr_drug).select2();
            $("#drugdoseid" + itr_drug).select2();
            $("#catid" + itr_drug).select2();
        }
        $('#itr_drug').val(itr_drug);
        itr_drug++;

        $("#adddrugname_submit").one('click', function () {
            var hosturl = $('#hosturl').val();
            var drugname = $('#drugname').val();
            var hidden_itr_type = row_itr;
            // alert(hidden_itr_type);
            if ($('#drugname_form').validate().form()) {
                $.post(hosturl + '/patienttreatmentajax/adddrugs', {hidden_itr_type: hidden_itr_type, drugname: drugname}, function (data) {
                    $("#drugname_form")[0].reset();
                    if (data == '1')
                    {
                        document.getElementById("drugname").style.backgroundColor = "yellow";
                        document.getElementById("RepeatedName").style.display = "";
                        document.getElementById("RepeatedName").style.color = "red";
                        $("#drugname").val('');
                    }
                    else
                    {
                        if (data != '2')
                        {
                            //alert(data);
                            document.getElementById("RepeatedName").style.display = "none";
                            $("#drugnamediv" + hidden_itr_type).html(data);
                            $('#drugnamemodel').modal('hide');
                            $adddrugId = $('#adddrugId').val();
                            $adddrugnam = $('#adddrugnam').val();
                            $("#drugnameid" + hidden_itr_type).select2();
                            //$("#drugnameid" + hidden_itr_type).select2("data", {id: $adddrugId, text: $adddrugnam});
                        }
                        else
                        {
                            alert("يرجى ادخال البيانات بشكل صحيح");
                        }

                    }
                });
            }
        });

        $(this).change(function () {
            return false;
        });

    });

    var editdrug = function ()
    {
        var hosturl = $('#hosturl').val();
        $(".dragchange_div").live('change', function () {
            var regularity_val = $(this).val();
            if (regularity_val == -3) {
                $.post(hosturl + '/patienttreatmentajax/getalldrug', function (data) {
                    $("#alldrug").html(data);
                });
            }
        });

        $(".cl_choosename").live('click', function ()
        {
            var id = $(this).attr('data-id');
            var name = $(this).text();

            $('#drugname_edit_sumbit').removeAttr('disabled');
            $("#drugname_name").val(name);
            $("#drugname_id").val(id);
        });
        $("#drugname_edit_sumbit").live('click', function () {
            var regname = $("#drugname_name").val();
            var regid = $("#drugname_id").val();
            var itr_drug = document.getElementById('itr_drug').value;
            var newitr = itr_drug - 1;
            $.post(hosturl + '/patienttreatmentajax/updatedrugname', {regid: regid, regname: regname, itr_drug: newitr}, function (data) {
                $("#drugnameid" + newitr).html(data);
                $('#editdrug_model').modal('hide');
                $("#drugnameid" + newitr).select2();
                $("#drugnameid" + newitr).select2("data", {id: regid, text: regname});

            });
        });
    }
    $('.deldrugname').live('click', function () {
        var id = $(this).attr('data-id');
        var hosturl = $('#hosturl').val();
        var itr_drug = document.getElementById('itr_drug').value;
        var newitr = itr_drug - 1;

        $.post(hosturl + "/followupajax/deldrugname", {id: id}, function (data) {
            alert(data);
            if (data > 0)
            {
                $('#drugname_edit_sumbit').attr('disabled', 'disabled');
                $.post(hosturl + '/patienttreatmentajax/getalldrug', function (data) {
                    $("#alldrug").html(data);
                    $.post(hosturl + '/patienttreatmentajax/queryalldrug', {newitr: newitr}, function (data) {
                        $("#drugnamediv" + newitr).html(data);
                        //$('#editdrug_model').modal('hide');
                        $("#drugnameid" + newitr).select2();
                    });
                });
            }
        });
    });

    /*======================== add new Form in drug controller  drugtypeidupdate===================نوع الدواء===============================*/
    $('.drugtypeid').live('change', function () {
        var drugtype_val = $(this).val();
        //alert(drugtype_val);
        if (drugtype_val == -2) {
            $('#drugetypemodel').modal('show');
        }
        if (drugtype_val == -3) {
            $("#drugtype_name").val("");
            $('#edittypdrug_model').modal('show');
        }
        var hidden_itr_type = this.id.substr(10); //document.getElementById('itr_drug').value;
        //alert(hidden_itr_type);
        $("#adddrugtype_submit").one('click', function () {
            var hosturl = $('#hosturl').val();
            var drugtype = $('#drugtype').val();
            if ($('#drugtype_form').validate().form()) {
                //alert(hidden_itr_type);
                $.post(hosturl + '/followupajax/adddrugstype', {hidden_itr_type: hidden_itr_type, drugtype: drugtype}, function (data) {
                    alert(data);
                    $("#drugtype_form")[0].reset();
                    if (data == '1')
                    {
                        document.getElementById("drugtype").style.backgroundColor = "yellow";
                        document.getElementById("RepeatedNamex").style.display = "";
                        document.getElementById("RepeatedNamex").style.color = "red";
                        $("#drugtype").val('');
                    }
                    else
                    {
                        if (data != '2')
                        {
                            document.getElementById("RepeatedNamex").style.display = "none";

                            $("#drugtypediv" + hidden_itr_type).html(data);
                            $('#drugetypemodel').modal('hide');

                            $adddrugtypeId = $('#adddrugtypeId').val();
                            $adddrugtypenam = $('#adddrugtypenam').val();
                            $("#drugtypeid" + hidden_itr_type).select2();
                            //$("#drugtypeid" + hidden_itr_type).select2("data", {id: $adddrugtypeId, text: $adddrugtypenam});
                        }
                        else
                        {
                            alert("يرجى ادخال البيانات بشكل صحيح");
                        }

                    }
                });
            }
        });
    });

    var editdrugtype = function ()
    {
        var hosturl = $('#hosturl').val();
        $(".drugtypeid").live('change', function () {
            var regularity_val = $(this).val();
            if (regularity_val == -3) {
                $.post(hosturl + '/patienttreatmentajax/getalldrugtype', function (data) {
                    $("#alldrugtype").html(data);
                });
            }
        });
        //$("#edit_type_form")[0].reset();
        $(".cl_choosename").live('click', function ()
        {
            var id = $(this).attr('data-id');
            var name = $(this).text();
            $('#type_edit_sumbit').removeAttr('disabled');
            $("#drugtype_name").val(name);
            $("#drugtype_id").val(id);
        });
        $("#type_edit_sumbit").click(function () {
            var regname = $("#drugtype_name").val();
            var regid = $("#drugtype_id").val();

            var itr_drug = document.getElementById('itr_drug').value;
            var newitr = itr_drug;
            $.post(hosturl + '/patienttreatmentajax/updatedrugtype', {regid: regid, regname: regname, itr_drug: newitr}, function (data) {
                $("#drugtypediv" + itr_drug).html(data);
                $('#edittypdrug_model').modal('hide');
                $("#drugtypeid" + itr_drug).select2();
                $("#drugtypeid" + itr_drug).select2("data", {id: regid, text: regname});

            });
        });

    }


    $('.deldrugtype').live('click', function () {
        var id = $(this).attr('data-id');
        var hosturl = $('#hosturl').val();
        var itr_drug = document.getElementById('itr_drug').value;
        var newitr = itr_drug;

        $.post(hosturl + "/patienttreatmentajax/deldrugtype", {id: id}, function (data) {
            if (data > 0)
            {
                $('#type_edit_sumbit').attr('disabled', 'disabled');
                $.post(hosturl + '/patienttreatmentajax/getalldrugtype', function (data) {
                    $("#alldrugtype").html(data);
                    $.post(hosturl + '/patienttreatmentajax/queryalldrugtype', {itr_drug: itr_drug}, function (data) {
                        //alert(data);
                        $("#drugtypediv" + itr_drug).html(data);
                        //$('#edittypdrug_model').modal('hide');
                        $("#drugtypeid" + itr_drug).select2();
                    });
                });
            }
        });
    });


    //-------------------------------------------------------------------------------------------------------------

    $('.get_use_form').live('change', function () {
        var id = $(this).val();
        var row_itr = this.id.substr(10);
        var drugid = $('#drugnameid' + row_itr).val();
        var name = $('#drugnameid' + row_itr).val();
        //alert(name);
        var hosturl = $('#hosturl').val();
        $.post(hosturl + "/followupajax/showuseform", {id: id, row_itr: row_itr, drugid: drugid, name: name}, function (data) {
            $('#new_use_form' + row_itr).html(data);
            $("#drugdoseid" + row_itr).select2();
        });
    });

    $('.get_use_form_update').live('change', function () {

        var id = $(this).val();
        var row_itr = this.id.substr(16);

        var drugid = $('#drugnameidupdate' + row_itr).val();

        var name = $('#drugnameidupdate' + row_itr).val();

        //alert(name);
        var hosturl = $('#hosturl').val();
        $.post(hosturl + "/followupajax/showuseformupdate", {id: id, row_itr: row_itr, drugid: drugid, name: name}, function (data) {

            $('#new_use_form_update' + row_itr).html(data);
            $("#drugdoseidupdate" + row_itr).select2();
        });
    });

    /*======================== add new Use in drug controller =================طريقه الاستخدام================================*/
    $(".drugdoseid_div").live('change', function () {
        var hidden_itr_type = this.id.substr(10);
        var new_val = $(this).val();
        if (new_val == -2) {
            $('#drugedrosemodel').modal('show');
        }
        if (new_val == -3) {
            $("#dose_name").val("");
            $('#edituse_model').modal('show');
        }
        $("#adddose_submit").one('click', function () {
            //alert(hidden_itr_type);
            var hosturl = $('#hosturl').val();
            var drugdose = $('#drugdose').val();
            if ($('#drugdose_form').validate().form()) {

                $.post(hosturl + '/patienttreatmentajax/adddrugsdose', {drugdose: drugdose, hidden_itr_type: hidden_itr_type}, function (data) {
                    $("#drugdose_form")[0].reset();
                    if (data == '1')
                    {
                        document.getElementById("drugdose").style.backgroundColor = "yellow";
                        document.getElementById("RepeatedNamey").style.display = "";
                        document.getElementById("RepeatedNamey").style.color = "red";
                        $("#drugdose").val('');
                    }
                    else
                    {
                        if (data != '2')
                        {//alert(data);
                            document.getElementById("RepeatedNamey").style.display = "none";

                            $("#drugdosediv" + hidden_itr_type).html(data);
                            $('#drugedrosemodel').modal('hide');
                            $("#drugdoseid" + hidden_itr_type).select2();

                            $adddrugdoseId = $('#adddrugdoseId').val();
                            $adddrugdosenam = $('#adddrugdosenam').val();

                            //$("#drugdoseid"+hidden_itr_type).select2("data", {id: $('#adddrugdoseId').val(), text: $('#adddrugdosenam').val()});

                        }
                        else
                        {
                            alert("يرجى ادخال البيانات بشكل صحيح");
                        }

                    }

                    //}
                });
            }

            $(this).click(function (e) {
                return false;
            });
        });
    });
    var editdrugdose = function ()
    {
        var hosturl = $('#hosturl').val();
        $(".drugdoseid_div").live('change', function () {
            var regularity_val = $(this).val();
            if (regularity_val == -3) {
                $.post(hosturl + '/patienttreatmentajax/getalldose', function (data) {
                    $("#alldose").html(data);
                });
            }
        });
        $(".cl_choosename").live('click', function ()
        {
            var id = $(this).attr('data-id');
            var name = $(this).text();
            //var regid = $("#chooseregular" + id).val();
            $('#dose_edit_sumbit').removeAttr('disabled');
            $("#dose_name").val(name);
            $("#dose_id").val(id);
        });
        $("#dose_edit_sumbit").click(function () {
            var regname = $("#dose_name").val();
            var regid = $("#dose_id").val();
            var itr_drug = document.getElementById('itr_drug').value;
            var newitr = itr_drug;
            $.post(hosturl + '/patienttreatmentajax/updatedrugdose', {regid: regid, regname: regname, itr_drug: newitr}, function (data) {
                //alert(data);
                $("#drugdosediv" + itr_drug).html(data);
                $('#edituse_model').modal('hide');
                $("#drugdoseid" + itr_drug).select2();
                $("#drugdoseid" + itr_drug).select2("data", {id: regid, text: regname});

            });
        });
    }
    $('.deldrugdose').live('click', function () {
        var id = $(this).attr('data-id');
        var hosturl = $('#hosturl').val();
        var itr_drug = document.getElementById('itr_drug').value;
        var newitr = itr_drug;
        $.post(hosturl + "/patienttreatmentajax/deldrugdose", {id: id}, function (data) {
            if (data > 0)
            {
                $('#dose_edit_sumbit').attr('disabled', 'disabled');
                $.post(hosturl + '/patienttreatmentajax/getalldose', function (data) {
                    $("#alldose").html(data);
                    $.post(hosturl + '/patienttreatmentajax/queryalldose', {newitr: newitr}, function (data) {
                        //alert(data);
                        $("#drugdosediv" + newitr).html(data);
                        //$('#edituse_model').modal('hide');
                        $("#drugdoseid" + newitr).select2();
                    });
                });
            }
        });
    });
    /*======================================================Gyna visit===============================================================*/
    $('#drugname_search').live('keyup', function () {
        var hosturl = $('#hosturl').val();
        var drugnamesearch = $('#drugname_search').val();
        $.post(hosturl + '/followupajax/search', {drugnamesearch: drugnamesearch}, function (data) {
            $("#alldrug").html(data);
        });
    });

    $('#drugtype_search').live('keyup', function () {
        var hosturl = $('#hosturl').val();
        var drugtypesearch = $('#drugtype_search').val();
        $.post(hosturl + '/followupajax/searchtype', {drugtypesearch: drugtypesearch}, function (data) {
            alert(data);
            $("#alldrugtype").html(data);
        });
    });


    $('.dragchange_div_model').live('change', function () {
        var row_itr = this.id.substr(10);
        $hosturl = $('#hosturl').val();
        var itr_drug = $('#itr_drug').val();
        var addnew = 0;
        $(".dragchange_div_model").each(function () {

            if ($(this).val() == -1)
            {
                addnew = 0;
            } else {
                addnew = 1;
            }
        });
        if (addnew == 1) {//to go
            itr_drug++;

            $("#treat_addnew").append('<div class="row">'

                    + '<div class="col-md-3" >'
                    + '<div class="form-group" >'
                    + '<label class="col-md-2 control-label" > Group </label>'
                    + '<div class="col-md-8" >'
                    + ' <select class="form-control grouphere_drugs" name="grouphere' + itr_drug + '" id="grouphere' + itr_drug + '" data-itr="' + itr_drug + '" >'
                    + $('#allcats_div').html()
                    + '</select>'
                    + '</div>'
                    + '</div>'
                    + '</div>'


                    + ' <div id="catloaddrugs' + itr_drug + '"><div class="col-md-3" style=" margin-left: -3%; ">'
                    + ' <div class="form-group">'
                    + ' <label class="col-md-2 control-label"> Drug</label>'
                    + ' <div class="col-md-8">'
                    + '<div id="drugnamediv' + itr_drug + '">'
                    + '  <select class="form-control dragchange_div_model select2me" name="drugnameid' + itr_drug + '" id="drugnameid' + itr_drug + '" >'
                    + $('#alldrug_div').html()
                    + '</select>'
                    + ' </div>'
                    + ' </div>'
                    + '  </div>'
                    + '  </div></div>'
                    + '<div id="new_form' + itr_drug + '">'

                    + ' <div class="col-md-2" style=" margin-left: -3%; ">'
                    + ' <div class="form-group">'
                    + ' <label class="col-md-3 control-label">Form</label>'
                    + ' <div class="col-md-9">'
                    + '<div id="drugtypediv' + itr_drug + '">'
                    + '  <select class="form-control select2me drugtypeid" name="drugtypeid' + itr_drug + '" id="drugtypeid' + itr_drug + '" >'
                    + $('#allDrugtype_div').html()
                    + '</select>'
                    + ' </div>'
                    + ' </div>'
                    + '  </div>'
                    + '  </div>'

                    + ' <div class="col-md-3">'
                    + ' <div class="form-group">'
                    + ' <label class="col-md-2 control-label">Use</label>'
                    + ' <div class="col-md-10">'
                    + '  <select style="width: 173.2%;" class="form-control drugdoseid_div" name="drugdoseid' + itr_drug + '" id="drugdoseid' + itr_drug + '" >'
                    + $('#alluses_div').html()
                    + '</select>'
                    + ' </div>'
                    + '  </div>'
                    + '  </div>'

                    + '  </div>'
                    + ' </div>'
                    );
            $("#drugnameid" + itr_drug).select2({
                minimumInputLength: 2
            });
            $("#drugtypeid" + itr_drug).select2();
            $("#drugdoseid" + itr_drug).select2();
            $("#grouphere" + itr_drug).select2();
        }
        var id = $(this).val();
        if (id == -2) {
            $('#drugnamemodel').modal('show');
            $('#adddrugname_submit_follow').one('click', function () {
                var hosturl = $('#hosturl').val();
                var drugname = $('#drugname').val();
                var hidden_itr_type = document.getElementById('itr_drug').value;
                var newitr = hidden_itr_type - 1;
                $.post(hosturl + '/followupajax/adddrugs', {hidden_itr_type: newitr, drugname: drugname}, function (data) {
                    //alert(data);
                    $("#drugname_form")[0].reset();
                    if (data == '1')
                    {
                        document.getElementById("drugname").style.backgroundColor = "yellow";
                        document.getElementById("RepeatedName").style.display = "";
                        document.getElementById("RepeatedName").style.color = "red";
                        $("#drugname").val('');
                    }
                    else
                    {
                        if (data != '2')
                        {
                            //alert(data);
                            document.getElementById("RepeatedName").style.display = "none";

                            $("#drugnamediv" + newitr).html(data);
                            $('#drugnamemodel').modal('hide');
                            $adddrugId = $('#adddrugId').val();
                            $adddrugnam = $('#adddrugnam').val();
                            $("#drugnameid" + newitr).select2();
                            //$("#drugnameid" + newitr).select2("data", {id: $adddrugId, text: $adddrugnam});
                        }
                        else
                        {
                            alert("يرجى ادخال البيانات بشكل صحيح");
                        }

                    }
                });
                //}

            });
        }
        if (id == -3) {
            $('#editdrug_model').modal('show');
        }

        $.post($hosturl + "/followupajax/showdrugdetails", {iitemd: id, row_itr: row_itr}, function (data) {
            $('#new_form' + row_itr).html(data);
            $("#drugtypeid" + row_itr).select2();
            $("#drugdoseid" + row_itr).select2();
        });

        $('#itr_drug').val(itr_drug);
        itr_drug++;

        /*  $(this).change(function () {
         return false;
         });*/
    });
    var editdrug_follow = function ()
    {
        var hosturl = $('#hosturl').val();
        $(".dragchange_div_model").live('change', function () {
            var regularity_val = $(this).val();
            if (regularity_val == -3) {
                $.post(hosturl + '/followupajax/getalldrug', function (data) {
                    //alert(data);
                    $("#alldrug").html(data);
                });
            }
        });
        $(".cl_choosename").live('click', function ()
        {
            var id = $(this).attr('data-id');
            var name = $(this).text();
            $('#drugname_edit_sumbit_follow').removeAttr('disabled');
            $("#drug_edit_name").val(name);
            $("#drug_edit_id").val(id);
        });
        $("#drugname_edit_sumbit_follow").live('click', function () {
            var regname = $("#drug_edit_name").val();
            var regid = $("#drug_edit_id").val();
            var itr_drug = document.getElementById('itr_drug').value;
            var newitr = itr_drug - 1;
            $.post(hosturl + '/patienttreatmentajax/updatedrugname', {regid: regid, regname: regname, itr_drug: newitr}, function (data) {
                $("#drugnameid" + newitr).html(data);
                $('#editdrug_model').modal('hide');
                $("#drugnameid" + newitr).select2();
                // $("#drugnameid" + newitr).select2("data", {id: regid, text: regname});

            });
        });
    }

    $('.dragchange_div_model').select2({
        minimumInputLength: 2
    });
    $('.dragchange_div_modelupdate').live('change', function () {
        var row_itr = this.id.substr(16);
        $hosturl = $('#hosturl').val();
        var itr_drug = $('#itr_drugupdate').val();
        var addnew = 0;
        $(".dragchange_div_modelupdate").each(function () {

            if ($(this).val() == -1)
            {
                addnew = 0;
            } else {
                addnew = 1;
            }
        });
        if (addnew == 1) {//to go
            itr_drug++;
            $("#treat_addnewupdate").append('<div class="row">'

                    + '<div class="col-md-3" >'
                    + '<div class="form-group" >'
                    + '<label class="col-md-2 control-label" > Group </label>'
                    + '<div class="col-md-8" >'
                    + ' <select class="form-control grouphere_drugs_edit" name="grouphere' + itr_drug + '" id="grouphere' + itr_drug + '" data-itr="' + itr_drug + '" >'
                    + $('#allcats_div').html()
                    + '</select>'
                    + '</div>'
                    + '</div>'
                    + '</div>'


                    + ' <div id="catloaddrugsupdate' + itr_drug + '"><div class="col-md-3" style=" margin-left: -3%; ">'

                    + ' <div class="form-group">'
                    + ' <label class="col-md-2 control-label"> Drug</label>'
                    + ' <div class="col-md-8">'
                    + '<div id="drugnamediv' + itr_drug + '">'
                    + '  <select class="form-control dragchange_div_modelupdate select2me" name="drugnameidupdate' + itr_drug + '" id="drugnameidupdate' + itr_drug + '" >'
                    + $('#alldrug_div').html()
                    + '</select>'
                    + ' </div>'
                    + ' </div>'
                    + '  </div>'
                    + '  </div>'
                    + '  </div>'
                    + '<div id="new_formupdate' + itr_drug + '">'

                    + ' <div class="col-md-2" style=" margin-left: -3%; ">'
                    + ' <div class="form-group">'
                    + ' <label class="col-md-3 control-label">Form</label>'
                    + ' <div class="col-md-9">'
                    + '<div id="drugtypediv' + itr_drug + '">'
                    + '  <select class="form-control select2me drugtypeidupdate" name="drugtypeidupdate' + itr_drug + '" id="drugtypeidupdate' + itr_drug + '" >'
                    + $('#allDrugtype_div').html()
                    + '</select>'
                    + ' </div>'
                    + ' </div>'
                    + '  </div>'
                    + '  </div>'

                    + ' <div class="col-md-4">'
                    + ' <div class="form-group">'
                    + ' <label class="col-md-2 control-label">Use</label>'
                    + ' <div class="col-md-10">'
                    + '  <select class="form-control drugdoseid_div" name="drugdoseidupdate' + itr_drug + '" id="drugdoseidupdate' + itr_drug + '" >'
                    + $('#alluses_div').html()
                    + '</select>'
                    + ' </div>'
                    + '  </div>'
                    + '  </div>'

                    + '  </div>'
                    + ' </div>');
            $("#drugnameidupdate" + itr_drug).select2({
                minimumInputLength: 2
            });
            $("#drugtypeidupdate" + itr_drug).select2();
            $("#drugdoseidupdate" + itr_drug).select2();
            $("#grouphere" + itr_drug).select2();
        }
        var id = $(this).val();
        if (id == -2) {
            $('#drugnamemodel').modal('show');
            $('#adddrugname_submit_follow').one('click', function () {
                var hosturl = $('#hosturl').val();
                var drugname = $('#drugname').val();
                var hidden_itr_type = document.getElementById('itr_drug').value;
                var newitr = hidden_itr_type - 1;
                $.post(hosturl + '/followupajax/adddrugs', {hidden_itr_type: newitr, drugname: drugname}, function (data) {
                    //alert(data);
                    $("#drugname_form")[0].reset();
                    if (data == '1')
                    {
                        document.getElementById("drugname").style.backgroundColor = "yellow";
                        document.getElementById("RepeatedName").style.display = "";
                        document.getElementById("RepeatedName").style.color = "red";
                        $("#drugname").val('');
                    }
                    else
                    {
                        if (data != '2')
                        {
                            //alert(data);
                            document.getElementById("RepeatedName").style.display = "none";
                            $("#drugnamediv" + newitr).html(data);
                            $('#drugnamemodel').modal('hide');
                            $adddrugId = $('#adddrugId').val();
                            $adddrugnam = $('#adddrugnam').val();
                            $("#drugnameid" + newitr).select2();
                            //$("#drugnameid" + newitr).select2("data", {id: $adddrugId, text: $adddrugnam});
                        }
                        else
                        {
                            alert("يرجى ادخال البيانات بشكل صحيح");
                        }

                    }
                });
                //}

            });
        }
        if (id == -3) {
            $('#editdrug_model').modal('show');
        }

        $.post($hosturl + "/followupajax/showdrugdetailsupadte", {iitemd: id, row_itr: row_itr}, function (data) {

            $('#new_formupdate' + row_itr).html(data);
            $("#drugtypeidupdate" + row_itr).select2();
            $("#drugdoseidupdate" + row_itr).select2();
        });
        $('#itr_drugupdate').val(itr_drug);
        itr_drug++;
        /*  $(this).change(function () {
         return false;
         });*/
    });

    $('.dragchange_div_modelupdate').select2({
        minimumInputLength: 2
    });

    var eupdatetrdrug_follow = function ()
    {
        var hosturl = $('#hosturl').val();
        $(".dragchange_div_modelupdate").live('change', function () {
            var regularity_val = $(this).val();
            if (regularity_val == -3) {
                $.post(hosturl + '/followupajax/getalldrug', function (data) {
                    //alert(data);
                    $("#alldrug").html(data);
                });
            }
        });
        $(".cl_choosename").live('click', function ()
        {
            var id = $(this).attr('data-id');
            var name = $(this).text();
            $('#drugname_edit_sumbit_follow').removeAttr('disabled');
            $("#drug_edit_name").val(name);
            $("#drug_edit_id").val(id);
        });
        $("#drugname_edit_sumbit_follow").live('click', function () {
            var regname = $("#drug_edit_name").val();
            var regid = $("#drug_edit_id").val();
            var itr_drug = document.getElementById('itr_drug').value;
            var newitr = itr_drug - 1;
            $.post(hosturl + '/patienttreatmentajax/updatedrugname', {regid: regid, regname: regname, itr_drug: newitr}, function (data) {
                $("#drugnameid" + newitr).html(data);
                $('#editdrug_model').modal('hide');
                $("#drugnameid" + newitr).select2();
                // $("#drugnameid" + newitr).select2("data", {id: regid, text: regname});

            });
        });
    };



    /*======================================== Drug model in gyna visit========================================================*/
    $('#treatment_follow').live('click', function () {
        var hosturl = $('#hosturl').val();
        $.post(hosturl + "/followupajax/addpatienttreatment", $('#treatment_form_follow').serialize(), function (data) {
            //alert(data);
            if (data > 0)
            {
                $('#patientdrugs_model').modal('hide');
            }
        });

    });

    $('.print_treat_follow').live('click', function () {
        var id = $(this).attr('data-id');
        var hosturl = $('#hosturl').val();
        if ($(this).attr('data-fdate') !== "")
        {
            var fdate = $(this).attr('data-fdate');
        }
        else
        {
            var fdate = document.getElementById('followdate').value;
        }
        // alert('fdate ' + fdate);
        $.post(hosturl + "/printajax/print", {fdate: fdate}, function (data) {
            //  alert(data);
            window.open(hosturl + "/print/" + id + "/" + data, '_blank');

        });
        $('#patientdrugs_model').modal('hide');
        $('#patientdrug').css('display', 'none');
        $('#show_btn').css('display', 'block');
    });
    /*=====================================================Drug model in antenatel=========================================================================*/
    $('#treatment_antenal_add').live('click', function () {
        var hosturl = $('#hosturl').val();
        $.post(hosturl + "/antenalvisitajax/addpatienttreatment", $('#treatment_form_antenal').serialize(), function (data) {

            //alert(data);
            if (data > 0)
            {
                $('#patientdrugs_model').modal('hide');
                //$('#edit_patientdrug').css('display','block');
                //$('#patientdrug').css('display','none');
            }
        });

    });



    return {
        //main function to initiate the theme
        init: function () {
            if ($('#itr_drug').length > 0) {

                //drug controller
                editcat();
                editdrug();
                editdrugtype();
                editdrugdose();

                //follow up
                editdrug_follow();
                eupdatetrdrug_follow();
            }
        }
    };
}();
