var hosturl = $('#hosturl').val();
var controllName = $('#controllName').val();
if (controllName == "heart_reports") {
    $(document).ready(function () {
        $(".slctWAdd").select2({
            width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var elemId = $(this).attr("id");
                var id = $(this).attr("data-id");
                return "<input class='form-control' id='newSlctTerm' value='" + term + "'>\n\
                               <a href='#' id='addNewSlctRow' data-elemId='" + elemId + "'  data-id='" + id + "'   \n\
                                  class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewSlctRow', function () {
            /* add the new term */
            var newTerm = $('#newSlctTerm').val();
            var id = $(this).attr("data-id");
            var elemId = $(this).attr("data-elemId");
            $.post(hosturl + "/controllers/heart_reports.php?ac=addNewRecord", {
                selectId: elemId, name: newTerm, id: id,
                mainTable: $('#' + elemId + '').attr('data-tableName'),
                table: $('#' + elemId + '').attr('data-colName')
            }, function (data) {
                $('#' + elemId + ' > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newTerm + '</option>');
                $('#' + elemId + '').select2('val', Number(data)); // select the new term
                $('#' + elemId + '').select2('close'); // close the dropdown
            });
        });

        //////////////// presentHistory Multiple Select
        $('.updatePresentHistory').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 patientId = $('#patientId').val();
            var dated = 0;

            var realvalues = new Array(); //storing the selected values inside an array
            var lastvalue;
            var slcttable;
            // if (colName == "present_history") {
            $('#' + colName + ' :selected').each(function (i, selected) {
                realvalues[i] = $(selected).val();
                lastvalue = $(selected).val();
            });
            slcttable = colName;
            // }
            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/heart_reports.php?ac=allPatientHistory', { 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();
                $("#presentHistoryName").val('');
                $("#presentHistoryName").attr("data-select", selectId);
                $("#presentHistoryName").attr("data-tablename", slcttable);
                $("#presentHistoryName").attr("data-mainTable", tableName);
                $("#presentHistoryName").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,
                        multi: 1
                    },
                    type: 'POST',
                    success: function (data) {

                    }
                });

            }
        });


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

            $.post(hosturl + '/controllers/heart_reports.php?ac=addNewRecord', { name: newitem, id: id, table: table, mainTable: mainTable, type: 'multi' }, function (data) {
                if (selectId == 'diagnosis')
                    $('.' + table + ' > .addDiagnosis').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                else
                    $('.' + table + ' > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                
                $('#' + selectId + ' option[value="' + Number(data) + '"]').attr('selected', true);
                $('select').select2();
            });
        });

        $("#addnewRow").live('click', function () {
            var tablename = $(this).attr('data-tableName');
            var selectname = $(this).attr('data-selectClass');
            var dtcount = $("#dtcount").val();
            dtcount++;
            $.post(hosturl + "/controllers/heart_reports.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="delrowHeart fa fa-times btn red fleft" data-colName="conditions" data-link="heart_reports" data-tableName="' + tablename + '" data-fn="delPresentHistory" data-id="' + dataid + '" ></a>\
                                            </td>\
                                        </tr>');
            });
            $("#dtcount").val(dtcount);
        });

        $(".delrowHeart").live('click', function () {
            var itemid = $(this).attr('data-id');
            var urlgo = $(this).attr('data-link');
            var fngo = $(this).attr('data-fn');
            var tableName = $(this).attr('data-tableName');
            var colName = $(this).attr('data-colName');
            var tr = $(this).parent().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/' + urlgo + '.php?ac=' + fngo, {
                                id: itemid, colName: colName, tableName: tableName
                            }, function (res) {
                                if (res == 1) {
                                    tr.remove();
                                    $("#newitem_name").val("");
                                    $("#newitem_id").val("");
                                    $('#' + tableName).children('option[value = ' + itemid + ']').remove();
                                    $('select').select2();
                                } else {
                                    alert("!!!!!!!!!!!!! some thing wrong here");
                                }
                            });
                        }
                    }
                }
            }).find('.modal-content').addClass('modal-sm');
        });

        $("body").on('click', '.assessmentFormPrint', function () {
            var id = $(this).attr("data-id");
            var fn = $(this).attr("data-fn");
            $.post(hosturl + '/controllers/heart_reports.php?ac=' + fn, { id: id }, function (res) {
                $("#forPrint").html(res);
                $.print("#forPrint");
                $("#forPrint").html("");
            });
        });

        $(".allupdateselect").on('change', function () {
            // Do something wi.on('change', function () {
            var thisvalue = $(this).val();
            var tableName = $(this).attr('select-tableName');
            var name = $(this).attr('name');
            var pervious = $(this).attr('data-val');
            var patientid = $("#patientId").val();
            var rowoftableid = $(this).attr('data-id');
            var maintableName = $(this).attr('data-tableName');
            if (thisvalue == '-2') {


                $('#' + name + '').select2('val', pervious);
                $.post(hosturl + '/controllers/heart_reports.php?ac=editselect', { tableName: tableName, name: name, rowoftableid: rowoftableid, patientid: patientid, maintableName: maintableName }, function (data) {
                    $("#newitem_name").val('');
                    $("#newitem_id").val('');
                    $("#alldatadiv").html(data);
                    $("#edit_model").modal('show');
                });
            }

        })

        $("body").on('change', ".updateCheckbox", function () {
            var id = $(this).attr("data-id");
            var colName = $(this).attr("data-colName");
            var tableName = $(this).attr("data-tableName");
            var value;
            var thisId = $(this).attr('id');

            if ($(this).is(":checked")) {
                var value = 1;
            } else {
                var value = 0;
            }
            $.post(hosturl + '/controllers/' + controllName + '.php?ac=update', { id: id, value: value, tableName: tableName, colName: colName, patientId: patientId }, function (res) {
            });
        });

        $("body").on('click', '#addNewMed', function () {
            var id = $(this).attr("data-id");
            var itr = $('#newMedItr').val();
            var tableName = $(this).attr('data-tableName');
            itr++;
            $('#newMedItr').val(itr);
            $.post(hosturl + '/controllers/heart_reports.php?ac=addNewMed', { id: id, itr: itr, tableName: tableName }, function (res) {
                $("#newMedDiv").append(res);
            });
        });

        $("body").on('click', '#addCarePlan', function () {
            var id = $(this).attr("data-id");
            var itr = $('#carePlanItr').val();
            var tableName = $(this).attr('data-tableName');
            itr++;
            $('#carePlanItr').val(itr);
            $.post(hosturl + '/controllers/heart_reports.php?ac=addCarePlan', { id: id, itr: itr, tableName: tableName }, function (res) {
                $("#carePlanDiv").append(res);
            });
        });

        $(".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/case_summary.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();
            });
        });
        $('.updateDiagnosis').live('change', function () {
            var id = $(this).attr("data-id");
            var colName = $(this).attr("data-colName");
            var tableName = $(this).attr("data-tableName");
            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 == "diagnosis") {
                $('#diagnosis :selected').each(function (i, selected) {
                    realvalues[i] = $(selected).val();
                    lastvalue = $(selected).val();
                });
                slcttable = "diagnosis";
            }
            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/ovumpickupembryotrans.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,
                        multi: 1,
                    },
                    type: 'POST',
                    success: function (data) {

                    }
                });
            }
        });

        $(".datetimepicker").datetimepicker({
            autoclose: true
        });
        $(".datepicker").datepicker({
            autoclose: true
        });
        $(".timepicker").timepicker({
            autoclose: true
        });
        $(".bp_mask").inputmask("999/999", {
            autoUnmask: true
        });
    });
}