
$(document).ready(function () {
    LoadBranch();
    LoadScreen();
    //LoadOperation();
    FieldTypeValue();  
    check_previlige_tabs(17)
    if ($('#hiddenRole_id').val() == 2) {
       IpOperationsLoad();
    }

    OperationServive();
    FormNameLoad();
});
function check_previlige_tabs(formid) {
    debugger;
    var RightData = {
        Stringtype: formid,
        Dml_Indicator: "CTABR"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserRight/User_rights_privileges',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(RightData),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {

                }
                else {
                    var j = 0

                    //$.each(data1.Table, function (i, emp1) {
                    //    $("[data-id=" + emp1.tab_id + "]").removeClass('hidden');
                    //})

                    $.each(data1.Table1, function (i, emp) {
                        if (emp.status == 0) {
                            $("[data-id=" + emp.role_rights_view + "]").addClass('hidden');
                        }
                        else if (emp.status == 1) {
                            if (j == 0) {
                                if (emp.role_rights_view == 'IOD_V1') {
                                    $('#OparationDetailsTab').click();

                                }
                               


                            }
                            j = j + 1;
                        }
                    });
                }
            }
        }
    });
}


function LoadBranch() {
    debugger;
    //Only KinderSurge
    //var dml = ''
    //if (parseInt($('#hiddenRole_id').val()) == 1) {
    //    dml = 'DLBRAN'
    //}
    //else {
    //    dml = 'LBRAN'
    //}

    var subdepartment = {
        Dml_Indicator: "LBRAN",
        //Dml_Indicator: dml,
        name: $('#HiddenDoctorID').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    var default_opt = '<option value="0">All</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#ddBranch').append(SubDepartment);
                        $('#ddDetailsBranch').append(SubDepartment);
                        $('#ddBranch').change();
                        $('#ddDetailsBranch').change();
                    }
                    else {
                        $('#ddBranch').append(default_opt + SubDepartment);
                        $('#ddDetailsBranch').append(default_opt + SubDepartment);
                    }


                    //if ($('#hiddenRole_id').val() == 1) {
                    //    $('#ddBranch').val($('#HospitalBranchID').val());


                    //    $('#ddDetailsBranch').val($('#HospitalBranchID').val());

                    //    IpOperationsLoad();
                    //    OperationServive();

                    //    LoadOperation();
                    //    FormNameLoad();
                    //}
                    //else 
                    if ($('#hiddenRole_id').val() != 2) {
                        $('#ddBranch').val($('#HospitalBranchID').val());
                        $('#ddBranch').attr('disabled', true);


                        $('#ddDetailsBranch').val($('#HospitalBranchID').val());
                        $('#ddDetailsBranch').attr('disabled', true);

                        IpOperationsLoad();
                        OperationServive();

                        LoadOperation();
                        FormNameLoad();
                    }
                }
            }
            else {
                console.log(data1);
            }

        }
    });
}

// Operation Start //

$('#ddBranch').change(function () {
    debugger;
    IpOperationsLoad();
    //Medway all branch concept
    //OperationServive();
    //FormNameLoad();
});

$('#ddDetailsBranch').change(function () {
    $('#ddSelectScreen').val('');
    $('#ddScreenName').val('');
    $('#ddSelectParameter').val('');
    $('#ddSParameterName').val('');
    $('#ddSPsurgery').val(null).trigger('change');
    $('#tblParameter tbody').empty();
    $('#ddsurgery').empty();
    $('#tblSubParameter tbody').empty();
    ParameterCancell();
    //LoadOperation();
    RelationalCancell();
});

//$('#ddSelectScreen').change(function () {
//    LoadOperation();
//});


function IpOperationsLoad() {
    debugger;
    $('#shade_slot').show();
    $("#ddOperationStatus").removeClass('disabled');
    $("#ddServiceName").removeClass('disabled');
    var Operationsdata = {
        Dml_Indicator: "OPL",
        branch_id: $('#ddBranch').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Operationsdata),
        success: function (data) {
            var data2 = JSON.parse(data);
            //  $('#tblOperation tbody').empty();    
            $('#Oparation_body').empty();
            $('#tblOperation').dataTable().fnClearTable();
            $('#tblOperation').dataTable().fnDestroy();
            if (jQuery.isEmptyObject(data2.data)) {
            
            }
            else {

                if (data2.msg == "Success") {
                    

                    debugger;
                    var data1 = data2.data;

                    if (data1.Table.length > 0) {
                        
                        var tr = "", Del = "", Edit = "", active_inactive = "";
                        for (var i = 0; i < data1.Table.length; i++) {
                            debugger;

                            if (data1.Table[i].Operationtatus == "InActive") {
                                //Edit = "<a href='javascript:void(0)' data-toggle='tooltip' data-placement='bottom' title='Edit' class='btn btn-edit btn-xs'onclick=EditOperations('" + data1.Table[i].ip_operation_id + "','" + data1.Table[i].operation_Active + "','" + data1.Table[i].branch_id + "');><i class='fa fa-pencil' aria-hidden='true'></i></a>";
                                //active_inactive = "<a href='javascript:void(0)' data-toggle='tooltip' data-placement='bottom' title='Make Active!' class='btn btn-blue btn-xs' onclick=DeledeOperations('" + data1.Table[i].ip_operation_id + "','active','" + data1.Table[i].operation_Active + "');><i class='fa fa-check-square-o' aria-hidden='true'></i></a>";

                                Edit = "<a href='javascript:void(0)' title='Edit' class='edit_icon'onclick=EditOperations('" + data1.Table[i].ip_operation_id + "','" + data1.Table[i].operation_Active + "','" + data1.Table[i].branch_id + "');><i class='fa fa-pencil' aria-hidden='true'></i></a>";
                                active_inactive = "<a href='javascript:void(0)' title='Make Active!' class='active_icon' onclick=DeledeOperations('" + data1.Table[i].ip_operation_id + "','active','" + data1.Table[i].operation_Active + "');><i class='fa fa-check-square-o' aria-hidden='true'></i></a>";
                                if (data1.Table[i].operation_Active == 1) {
                                    Del = "";
                                }
                                else {
                                   // Del = "<a href='javascript:void(0)' data-toggle='tooltip' data-placement='bottom' title='Delete' class='btn deleteicon-btn btn-xs' onclick=DeledeOperations('" + data1.Table[i].ip_operation_id + "','OPDEL','" + data1.Table[i].operation_Active + "');><i class='fa fa-trash' aria-hidden='true'></i></a>";
                                    Del = "<a href='javascript:void(0)'  title='Delete' class='delete_icon' onclick=DeledeOperations('" + data1.Table[i].ip_operation_id + "','OPDEL','" + data1.Table[i].operation_Active + "');><i class='fa fa-trash-o' aria-hidden='true'></i></a>";
                                }
                            }
                            else {
                                //Edit = "<a href='javascript:void(0)' data-toggle='tooltip' data-placement='bottom' title='Edit' class='btn btn-edit btn-xs'onclick=EditOperations('" + data1.Table[i].ip_operation_id + "','" + data1.Table[i].operation_Active + "','" + data1.Table[i].branch_id + "');><i class='fa fa-pencil' aria-hidden='true'></i></a>";
                                //active_inactive = "<a href='javascript:void(0)' data-toggle='tooltip' data-placement='bottom' title='Make In Active!' class='btn btn-blue btn-xs' onclick=DeledeOperations('" + data1.Table[i].ip_operation_id + "','inactive','" + data1.Table[i].operation_Active + "');><i class='fa fa-minus-square-o' aria-hidden='true'></i></a>";

                                Edit = "<a href='javascript:void(0)' title='Edit' class='edit_icon'onclick=EditOperations('" + data1.Table[i].ip_operation_id + "','" + data1.Table[i].operation_Active + "','" + data1.Table[i].branch_id + "');><i class='fa fa-pencil' aria-hidden='true'></i></a>";
                                active_inactive = "<a href='javascript:void(0)'  title='Make In Active!' class='inactive_icon' onclick=DeledeOperations('" + data1.Table[i].ip_operation_id + "','inactive','" + data1.Table[i].operation_Active + "');><i class='fa fa-minus-square-o' aria-hidden='true'></i></a>";

                                if (data1.Table[i].operation_Active == 1) {
                                    Del = "";
                                }
                                else {
                                   //Del = "<a href='javascript:void(0)' data-toggle='tooltip' data-placement='bottom' title='Delete' class='btn deleteicon-btn btn-xs' onclick=DeledeOperations('" + data1.Table[i].ip_operation_id + "','OPDEL','" + data1.Table[i].operation_Active + "');><i class='fa fa-trash' aria-hidden='true'></i></a>";
                                    Del = "<a href='javascript:void(0)' title='Delete' class='delete_icon' onclick=DeledeOperations('" + data1.Table[i].ip_operation_id + "','OPDEL','" + data1.Table[i].operation_Active + "');><i class='fa fa-trash-o' aria-hidden='true'></i></a>";
                                }
                            }

                            var act_inact = '', other_info='';
                            if (data1.Table[i].Operationtatus == "Active") {
                                act_inact = '<span class="label label-success">Active</span>';
                            }
                            else if (data1.Table[i].Operationtatus == "InActive") {
                                act_inact = '<span class="label label-warning">Inactive</span>';
                            }

                            if (data1.Table[i].Operationtatus == "InActive") {
                                //tr = tr + '<tr><td>' + data1.Table[i].row_num + '</td><td>' + data1.Table[i].branch_name + '</td><td class="tbl-name">' + data1.Table[i].ip_operation_name + '</td><td>' + data1.Table[i].service_name + '</td><td>' + act_inact + '</td><td>' + Del + "" + active_inactive + '</td></tr>';

                                //other_info = Del + "" + active_inactive;
                                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + Del + '</li><li>' + active_inactive + '</li></div></ul></div>';
                            }
                            else {
                                //tr = tr + '<tr><td>' + data1.Table[i].row_num + '</td><td>' + data1.Table[i].branch_name + '</td><td class="tbl-name">' + data1.Table[i].ip_operation_name + '</td><td>' + data1.Table[i].service_name + '</td><td>' + act_inact + '</td><td>' + Edit + " " + Del + "" + active_inactive + '</td></tr>';
                                //other_info = Edit + " " + Del + "" + active_inactive;
                                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + Edit + '</li><li>' + Del + '</li><li>' + active_inactive + '</li></div></ul></div>';

                            }

                            tr = tr + '<tr><td>' + data1.Table[i].row_num + '</td><td>' + data1.Table[i].branch_name + '</td><td class="tbl-name">' + data1.Table[i].ip_operation_name + '</td><td>' + data1.Table[i].service_name + '</td><td>' + data1.Table[i].Form_Name + '</td><td>' + act_inact + '</td><td>' + other_info + '</td></tr>';
                        }

                        $('#tblOperation tbody').append(tr);
                        
                        $('#tblOperation').DataTable();

                    }

                }
                else {
                    console.log(data);
                }
            }
            $('#shade_slot').hide();
        },
        error: function (data) {
            //toastr.warning('Operations Not Loaded');
        }
    });
}

function EditOperations(val, operation_active, branch_id) {

    $('#ddBranch').val(branch_id);
    if (parseInt($('#hiddenRole_id').val()) == 2)
    {
        FormNameLoad();    
    }
    

    if (operation_active == 1) {
        $("#ddOperationStatus").addClass('disabled');
        $("#ddServiceName").addClass('disabled');
    }
    else {
        $("#ddOperationStatus").removeClass('disabled');
        $("#ddServiceName").removeClass('disabled');
    }

    $('#add_operation_form').show();
    $('#add_new_operation').hide();
    $("#HOperationBtnType").val("OperationEdit");
    $("#HOperationId").val(val);

    var Parameter = {
        Dml_Indicator: "OPEdit",
        operatio_id: $("#HOperationId").val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Parameter),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    if (data1.Table.length > 0) {
                        debugger;
                        //$("#ddBranch").val(data1.Table[0].branch_id);
                        
                        $("#txtOperationName").val(data1.Table[0].ip_operation_name);
                        $("#ddOperationStatus").val(data1.Table[0].Operationtatus);

                        if (parseInt($('#hiddenRole_id').val()) == 2)
                        {
                            OperationServive(data1.Table[0].service_id);
                        }
                        else
                        {
                            $("#ddServiceName").val(data1.Table[0].service_id);
                        }

                        $('#ddFormName').val('').change();
                        if (data1.Table[0].formid != '' && data1.Table[0].formid != null) {
                            debugger;
                            var s = data1.Table[0].formid.split(',');
                            $('#ddFormName').val(s).trigger('change');

                        }

                    }
                    
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

function DeledeOperations(val, Dml_Indicator,operation_map) {
    debugger;
    $("#HOperationId").val(val);

    if (parseInt(operation_map) == 1)
    {
        toastr.warning("Some Patient admitted under this Operation. Unable to In-Activate.",
           {
              progressBar: true,
              allowHtml: true,
              timeOut: 0,
           });
        return false;
    }

    var status = "";
    if (Dml_Indicator == 'active') {
        status = "Are You Sure, You want to Activate the Operation?";
    }
    else if (Dml_Indicator == 'inactive') {
        status = "Are You Sure, You want to In-activate the Operation?";
    }
    else if (Dml_Indicator == 'OPDEL') {

        status = "'Are You Sure, You want to Delete the Operation?'";
    }
    toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button> <button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", status,
      {
          closeButton: true,
          progressBar: true,
          allowHtml: true,
          timeOut: 0,

          onShown: function (toast) {
              debugger
              $("#confirmationRevertYes").click(function () {
                  debugger

                  var Parameter = {
                      //Dml_Indicator: "OPDEL",
                      Dml_Indicator: Dml_Indicator,
                      operatio_id: $("#HOperationId").val(),
                  }

                  $.ajax({
                      async: true,
                      type: 'POST',
                      url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
                      contentType: 'application/json; charset=UTF-8',
                      data: JSON.stringify(Parameter),
                      success: function (data) {
                          debugger
                          var data2 = JSON.parse(data);
                          if (jQuery.isEmptyObject((data2.data))) {

                          }
                          else {

                              if (data2.msg == "Success") {
                                  var data1 = data2.data;

                                  if (Dml_Indicator == 'OPDEL') {
                                      toastr.success('Operation Deleted Successfully',
                                          {
                                              //closeButton: false,
                                              progressBar: true,
                                              allowHtml: true,
                                              timeOut: 0,
                                          });
                                  }
                                  else if (Dml_Indicator == 'active') {
                                      toastr.success("Activated Successfully",
                                      {
                                          progressBar: true,
                                          allowHtml: true,
                                          timeOut: 0,
                                      });
                                  }
                                  else {
                                      toastr.success("In-activated Successfully",
                                      {
                                      progressBar: true,
                                      allowHtml: true,
                                      timeOut: 0,
                                  });
                                  }

                                  IpOperationsLoad();

                              }
                              else {
                                  console.log(data);
                              }
                          }
                      }
                  });

              });
          }
      });

}

$('#btnAddOperation').click(function () {
    
    if ($.trim($('#ddBranch').val()) == 0) {
        $('#PddBranch').show();
        $('#PddBranch').fadeOut(8000);
        $('#PddBranch').html('Select Branch Name');
        $('#ddBranch').focus();
        $('#ddBranch').val(0);
        return false;
    }

    if ($.trim($('#txtOperationName').val()) == "") {
        $('#PtxtOperationName').show();
        $('#PtxtOperationName').fadeOut(8000);
        $('#PtxtOperationName').html('Enter Operation Name');
        $('#txtOperationName').focus();
        $('#txtOperationName').val('');
        return false;
    }

    if ($.trim($('#ddServiceName').val()) == "") {
        $('#PddServiceName').show();
        $('#PddServiceName').fadeOut(8000);
        $('#PddServiceName').html('Select Service Name');
        $('#ddServiceName').focus();
        $('#ddServiceName').val('');
        return false;
    }

    var FormName_id = '';

    if ($('#ddFormName').val() == '')
    {
        FormName_id = '';
    }
    else
    {
        FormName_id = $('#ddFormName').val().toString();
    }


    $('#btnAddOperation').hide();
    var Parameterdata = {
        Dml_Indicator: "OPINUP",
        Operation_Status: $('#ddOperationStatus').val(),
        ip_operation_name: $('#txtOperationName').val(),
        operatio_id: $("#HOperationId").val(),
        branch_id: $('#ddBranch').val(),
        service_id: $('#ddServiceName').val(),
        formid: FormName_id
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Parameterdata),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;
                    $('#btnAddOperation').show();
                    if ($("#HOperationBtnType").val() == "OperationEdit") {
                        toastr.success("Operation Updated Successfully");
                        LoadOperation();

                    }
                    else {
                        toastr.success("Operation Added Successfully");
                    }
                    IpOperationsLoad();

                    OperationCancell();

                }
                else {
                    console.log(data);
                }
            }
        }
    });
});

$("#BtnOperationCan").click(function () {
    OperationCancell();
});

function OperationCancell() {
    $("#txtOperationName").val("");
    $("#ddOperationStatus").val(1);
    $("#HOperationBtnType").val("");
    $("#HOperationId").val("");
    $('#ddServiceName').val('');
    $("#ddOperationStatus").removeClass('disabled');
    $("#ddServiceName").removeClass('disabled');
    $('#ddFormName').val('').change();
}

function OperationServive(id) {

    var Operationdata = {
        Dml_Indicator: "SMS",
        //branchid: $('#ddBranch').val()//Medway all branch concept
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/AdmitPatient_Ins_Upd_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Operationdata),
        success: function (data) {
            $('#ddServiceName').empty();
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    var operation_service;

                    var default_opt = '<option value="">--Select--</option>';
                    $.each(data1, function (i, emp) {

                        operation_service = operation_service + '<option value="' + emp.master_service_id + '">' + emp.service_name + '</option>';

                    });
                    $('#ddServiceName').append(default_opt + operation_service);

                    if(id!='' ||id!=null || id!=0)
                    {
                        $('#ddServiceName').val(id);
                    }


                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

// Operation End //

// Parameters functions Start //

function LoadOperation() {

    var surgery_type='';

    if(parseInt($('#ddSelectScreen').val())==5)
    {
        surgery_type=1;
    }
    else{
        surgery_type=0;
    }

    var Operationdata = {
        Dml_Indicator: "AOPC",
        branchid: $('#ddDetailsBranch').val(),
        ip_surgery_type: surgery_type
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/AdmitPatient_Ins_Upd_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Operationdata),
        success: function (data) {
            $('#ddsurgery').empty();
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    var operation;
                    $('#ddsurgery').empty();
                    $.each(data1, function (i, emp) {

                        operation = operation + '<option value="' + emp.ip_operation_id + '">' + emp.ip_operation_name + '</option>';

                    });
                    $('#ddsurgery').append(operation);
                    //$('#ddSPsurgery').append(operation);

                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

$('.select2 - selection__choice__remove').click(function () {
    var triggeredLIElement = $(this).parent();
    triggeredLIElement.remove();
});

function LoadScreen() {
    var Beddata = {

        Dml_Indicator: "LSN"
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Beddata),
        success: function (data) {
            var data2 = JSON.parse(data)
            if (jQuery.isEmptyObject(data2.data)) {

            }
            else {
                ;
                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    var SelScreen = '';
                    var default_opt = '<option value="">--Select--</option>';
                    if (data1.Table.length > 0) {
                        for (var i = 0; i < data1.Table.length; i++) {
                            SelScreen = SelScreen + '<option value="' + data1.Table[i].ip_operation_screen_id + '">' + data1.Table[i].ip_screen_name + '</option>';
                        }
                        $('#ddSelectScreen').html(default_opt + SelScreen);
                        $('#ddScreenName').html(default_opt + SelScreen);
                        $('#ddScreenName').attr("disabled", true);
                    }


                }
                else {
                    console.log(data);
                }
            }
        },
        error: function (data) {
            toastr.warning('Unable to Load the Screen Name');
        }
    });
}

$('#ddSelectScreen').change(function () {

    LoadOperation();
    LoadSectionTable();
});

function LoadSectionTable() {
    var Beddata = {
        Dml_Indicator: "PT",
        screen_name: $("#ddSelectScreen").val(),
        branch_id: $("#ddDetailsBranch").val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Beddata),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    var SelParameter = '';
                    if (data1.Table.length > 0) {
                        $('#tblParameter tbody').empty();
                        var tr = "";
                        for (var i = 0; i < data1.Table.length; i++) {

                            Edit = "<a href='javascript:void(0)' class='btn btn-edit btn-xs' onclick=loadParameter('" + data1.Table[i].parameter_id + "');><i class='fa fa-pencil' aria-hidden='true'></i></a>";
                            Del = "<a href='javascript:void(0)'  class='btn deleteicon-btn btn-xs' onclick=DeledeParameter('" + data1.Table[i].parameter_id + "');><i class='fa fa-trash' aria-hidden='true'></i></a>";
                            ChangeOrder = "<a href='javascript:void(0)' class='btn btn-primaryuse btn-xs' onclick=ChangeParameterOrder('" + data1.Table[i].parameter_id + "');>Change Order</a>";

                            tr = tr + '<tr><td>' + data1.Table[i].row_num + '</td><td>' + data1.Table[i].session_name + '</td><td>' + data1.Table[i].ip_operation_name + '</td><td>' + data1.Table[i].session_order + '</td><td>' + data1.Table[i].Parameterstatus + '</td><td>' + ChangeOrder + " " + Edit + " " + Del + '</td></tr>';

                            //Parameter Dropdown //
                            if (data1.Table[i].Parameterstatus == "Active") {
                                var default_opt = '<option value="">--Select--</option>';
                                SelParameter = SelParameter + '<option value="' + data1.Table[i].parameter_id + '">' + data1.Table[i].session_name + '</option>';

                            }

                        }

                        $('#tblParameter tbody').append(tr);

                        $('#ddSelectParameter').html(default_opt + SelParameter);
                        $('#ddSParameterName').html(default_opt + SelParameter);
                    }
                    else {
                        $('#tblParameter tbody').empty();
                    }
                    $("#ddScreenName").val($("#ddSelectScreen").val());

                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

function ChangeParameterOrder(val) {

    $("#HParameterID").val(val);
    $("#HPBtnType").val("ChangeOrder");

    EditorChangeOrder();
}

function loadParameter(val) {

    $("#HParameterID").val(val);
    $("#HPBtnType").val("EditParameter");

    EditorChangeOrder();
}

function EditorChangeOrder() {
    if ($("#HPBtnType").val() == "ChangeOrder") {
        $('.PDisable').prop('readonly', true);
        $('.PDisable').attr("disabled", true);
    }
    else {
        $('.PDisable').prop('readonly', false);
        $('.PDisable').attr("disabled", false);
    }

    var Parameter = {
        Dml_Indicator: "CPO",
        parameter_id: $("#HParameterID").val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Parameter),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    if (data1.Table.length > 0) {
                        $.each(data1.Table, function (i, emp) {

                            $("#txtParametersName").val(emp.session_name),
                            $("#ddScreenName").val(emp.screen_id),
                            $("#txtDisplayOrder").val(emp.session_order),
                            $("#ddParametersStatus").val(emp.Parameterstatus),
                            $("#HParaOrderID").val(emp.session_order)
                        });
                    }

                    if (data1.Table1.length > 0) {
                        if ($('#ddsurgery').select2() != null)
                            $('#ddsurgery').select2('destroy');

                        var abc = "";
                        $.each(data1.Table1, function (i, emp) {
                            abc = abc + '<li class="select2-selection__choice" title="' + emp.ip_operation_name + '"><span class="select2-selection__choice__remove" role="presentation">×</span>' + emp.ip_operation_name + '</li>';
                            $('#ddsurgery option').each(function (index, value) {

                                if (emp.ip_operation_name == value.innerText) {
                                    $(this).prop("selected", true);
                                }
                            });
                        });
                        $('ul.select2-selection__rendered').append(abc);
                        $('#ddsurgery').select2();

                        //Clear clear Sub Parameter Surgery column //
                        $('#ddSPsurgery').val(null).trigger('change');
                        $('#ddRelational').val(null).trigger('change');
                        //$('#ddConditionName').val(null).trigger('change');
                        //$('#ddCSubParameterName').val(null).trigger('change');
                    }


                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

function ParameterCancell() {
    $('.PDisable').prop('readonly', false);
    $('.PDisable').attr("disabled", false);
    $("#txtParametersName").val("");
    //$("#ddScreenName").val("");
    $("#txtDisplayOrder").val("");
    $("#ddParametersStatus").val(1);
    $("#HParameterID").val("");
    $("#HPBtnType").val("");
    //$('#ddsurgery').empty();
    //$('#ddsurgery').select2('destroy');
    $('#ddsurgery').val(null).trigger('change'); // Clear current selected item from surgery control
}

function DeledeParameter(val) {

    $("#HParameterID").val(val);

    var Parameter = {
        Dml_Indicator: "PDEL",
        parameter_id: val
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Parameter),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    toastr.success("Section Deleted");
                    LoadSectionTable();

                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

$("#BtnParaCancell").click(function () {
    ParameterCancell();
});

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

    var MultiOperationID = $('#ddsurgery').val();
    var OperationID = "";

    for (var i = 0; i < MultiOperationID.length; i++) {
        if (OperationID == "") {
            OperationID = MultiOperationID[i];
        }
        else {
            OperationID = OperationID + "," + MultiOperationID[i];
        }

    }

    if ($.trim($('#ddDetailsBranch').val()) == 0) {
        $('#PddDetailsBranch').show();
        $('#PddDetailsBranch').fadeOut(8000);
        $('#PddDetailsBranch').html('Select Branch Name');
        $('#ddDetailsBranch').focus();
        $('#ddDetailsBranch').val(0);
        return false;
    }

    if ($.trim($('#ddScreenName').val()) == "") {
        $('#PddSelectScreen').show();
        $('#PddSelectScreen').fadeOut(8000);
        $('#PddSelectScreen').html('Select Screen Name');
        $('#ddSelectScreen').focus();
        $('#ddSelectScreen').val('');
        return false;
    }

    if ($.trim($('#txtParametersName').val()) == "") {
        $('#PtxtParametersName').show();
        $('#PtxtParametersName').fadeOut(8000);
        $('#PtxtParametersName').html('Enter Parameter Name');
        $('#txtParametersName').focus();
        $('#txtParametersName').val('');
        return false;
    }

    if ($.trim($('#txtDisplayOrder').val()) == "") {
        $('#PtxtDisplayOrder').show();
        $('#PtxtDisplayOrder').fadeOut(8000);
        $('#PtxtDisplayOrder').html('Enter Display Order');
        $('#txtDisplayOrder').focus();
        $('#txtDisplayOrder').val('');
        return false;
    }

    if (OperationID == "") {
        $('#Pddsurgery').show();
        $('#Pddsurgery').fadeOut(8000);
        $('#Pddsurgery').html('Select Surgery Name');
        return false;
    }

    var DMLIndicator = "";

    if ($("#HPBtnType").val() == "ChangeOrder") {
        DMLIndicator = "UPO";

        if ($.trim($("#HParaOrderID").val()) == $.trim($('#txtDisplayOrder').val())) {

            toastr.warning("Same Display Order.\n Please Change Display Order or Click Cancel");
            return false;
        }
    }
    else {
        DMLIndicator = "PIN";
    }



    var Parameterdata = {
        Dml_Indicator: DMLIndicator,
        parameters_name: $('#txtParametersName').val(),
        screen_name: $('#ddScreenName').val(),
        display_order: $('#txtDisplayOrder').val(),
        ip_operation_name: OperationID,
        parameters_status: $("#ddParametersStatus").val(),
        parameter_id: $("#HParameterID").val(),
        branch_id: $('#ddDetailsBranch').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Parameterdata),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    if ($("#HPBtnType").val() == "ChangeOrder") {
                        toastr.success("Section Display Order Changed Successfully");
                    }
                    else if ($("#HPBtnType").val() == "EditParameter") {
                        toastr.success("Section Details Updated Successfully");
                    }
                    else {
                        toastr.success("Section Added Successfully");
                    }

                    LoadSectionTable();
                    ParameterCancell();

                }
                else {
                    console.log(data);
                }
            }
        }
    });
});

//Parameters function End //

//Sub parameter start //

$('#ddFieldValues').change(function () {

    debugger;

    if ($.trim($('#ddFieldValues').val()) == "1") {
        $("#HideLength").hide();
        $(".HideMaxMin").show();

        $("#txtLength").val("");
    }
    else if ($.trim($('#ddFieldValues').val()) == "") {
        $("#HideLength").hide();
        $(".HideMaxMin").hide();

        $("#txtMaxValue").val("");
        $("#txtMinValue").val("");
        $("#txtLength").val("");
    }
    else {
        $("#HideLength").show();
        $(".HideMaxMin").hide();
        $("#txtMaxValue").val("");
        $("#txtMinValue").val("");
    }

});

$('#ddSelectParameter').change(function () {

    LoadParametersTable();
});

function LoadParametersTable() {
    var Beddata = {
        Dml_Indicator: "SUBP",
        screen_name: $("#ddSelectScreen").val(),
        parameter_id: $("#ddSelectParameter").val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Beddata),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {
                $("#ddSParameterName").val($("#ddSelectParameter").val());
                $('#ddSParameterName').attr("disabled", true);
            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    var SelParameter = '';
                    if (data1.Table.length > 0) {
                        $('#tblSubParameter tbody').empty();
                        var tr = "";
                        for (var i = 0; i < data1.Table.length; i++) {

                            Edit = "<a href='javascript:void(0)' class='btn btn-edit btn-xs' onclick=loadSubParameter('" + data1.Table[i].MasterSubPara_ID + "');><i class='fa fa-pencil-square-o' aria-hidden='true'></i></a>";
                            Del = "<a href='javascript:void(0)'  class='btn deleteicon-btn btn-xs' onclick=DeledeSubParameter('" + data1.Table[i].MasterSubPara_ID + "');><i class='fa fa-trash' aria-hidden='true'></i></a>";
                            ChangeOrder = "<a href='javascript:void(0)'  class='btn btn-primaryuse btn-xs' onclick=ChangeSubParameterOrder('" + data1.Table[i].MasterSubPara_ID + "');>Change Order</a>";

                            tr = tr + '<tr><td>' + data1.Table[i].row_num + '</td><td>' + data1.Table[i].SubParameter + '</td><td>' + data1.Table[i].ip_operation_name + '</td><td>' + data1.Table[i].Column_Type + '</td><td>' + data1.Table[i].DisplayOrder + '</td><td>' + data1.Table[i].unit_values + '</td><td>' + data1.Table[i].Field_Value + '</td><td>' + data1.Table[i].Max_Value + '</td><td>' + data1.Table[i].Min_Value + '</td><td>' + data1.Table[i].Error_Message + '</td><td>' + data1.Table[i].tooltip + '</td><td>' + data1.Table[i].Field_Length + '</td><td>' + ChangeOrder + " " + Edit + " " + Del + '</td></tr>';

                        }

                        $('#tblSubParameter tbody').append(tr);

                    }
                    else {
                        $('#tblSubParameter tbody').empty();
                    }
                    $("#ddSParameterName").val($("#ddSelectParameter").val());
                    $('#ddSParameterName').attr("disabled", true);

                    if (data1.Table1.length > 0) {
                        var Sparameter_column = "";
                        $('#ddCSubParameterName').empty();
                        var default_opt = '<option value=""></option>';
                        $.each(data1.Table1, function (i, emp) {

                            Sparameter_column = Sparameter_column + '<option value="' + emp.ip_intra_operative_findings_id + '">' + emp.SubParameter + '</option>';

                        });
                        $('#ddCSubParameterName').append(default_opt + Sparameter_column);
                    }

                    if (data1.Table2.length > 0) {
                        var Relational = "";
                        $('#ddRelational').empty();
                        //var default_opt = '<option value=""></option>';
                        $.each(data1.Table2, function (i, emp) {

                            Relational = Relational + '<option value="' + emp.ip_intra_operative_findings_id + '">' + emp.SubParameter + '</option>';

                        });
                        $('#ddRelational').append(Relational);
                    }

                    if (data1.Table3.length > 0) {
                        $('#tblRelational tbody').empty();
                        var tr = "";
                        for (var i = 0; i < data1.Table3.length; i++) {

                            //Edit = "<a href=# class='label label-success'onclick=loadRelational('" + data1.Table3[i].ip_intra_operative_findings_id + "');>Edit</a>";
                            Del_Rel = "<a href='javascript:void(0)'  class='btn deleteicon-btn btn-xs' onclick=DeledeRelational('" + data1.Table3[i].ip_intra_operative_findings_id + "');><i class='fa fa-trash' aria-hidden='true'></i></a>";
                            //ChangeOrder = "<span class='label label-success'><a href=#  onclick=ChangeSubParameterOrder('" + data1.Table3[i].MasterSubPara_ID + "');>Change Order</span></a>";

                            tr = tr + '<tr><td>' + data1.Table3[i].row_num + '</td><td>' + data1.Table3[i].SubParameter + '</td><td>' + data1.Table3[i].CheckBox_condition_Value + '</td><td>' + data1.Table3[i].relational_column + '</td><td>' + Del_Rel + '</td></tr>';

                        }

                        $('#tblRelational tbody').append(tr);

                    }
                    else {
                        $('#tblRelational tbody').empty();
                    }


                    if (data1.Table4.length > 0) {
                        var operation;
                        $('#ddSPsurgery').empty();
                        $.each(data1.Table4, function (i, emp) {

                            operation = operation + '<option value="' + emp.ip_operation_id + '">' + emp.ip_operation_name + '</option>';
                        });

                        $('#ddSPsurgery').append(operation);

                        $('#ddSPsurgery').val(null).trigger('change');
                        if ($('#ddSPsurgery').select2() != null)
                            $('#ddSPsurgery').select2('destroy');

                        var abc = "";
                        $.each(data1.Table4, function (i, emp) {
                            abc = abc + '<li class="select2-selection__choice" title="' + emp.ip_operation_name + '"><span class="select2-selection__choice__remove" role="presentation">×</span>' + emp.ip_operation_name + '</li>';
                            $('#ddSPsurgery option').each(function (index, value) {

                                if (emp.ip_operation_name == value.innerText) {
                                    $(this).prop("selected", true);
                                }
                            });
                        });
                        $('ul.select2-selection__rendered').append(abc);
                        $('#ddSPsurgery').select2();


                        //Clear clear Sub Parameter Surgery column //
                        $('#ddsurgery').val(null).trigger('change');
                        $('#ddRelational').val(null).trigger('change'); 
                        //$('#ddConditionName').val(null).trigger('change');
                        //$('#ddCSubParameterName').val(null).trigger('change');
                    }

                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

$('#ddCSubParameterName').change(function () {

    var SPdata = {
        Dml_Indicator: "CSPL",
        sub_parameters_id: $("#ddCSubParameterName").val(),
        parameter_id: $("#ddSelectParameter").val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(SPdata),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    if (data1.Table.length > 0) {
                        var Condition_column = "";
                        $('#ddConditionName').empty();
                        var default_opt = '<option value=""></option>';
                        $.each(data1.Table, function (i, emp) {

                            Condition_column = Condition_column + '<option value="' + emp.Subparmeter + '">' + emp.Subparmeter + '</option>';

                        });
                        $('#ddConditionName').append(default_opt + Condition_column);
                    }

                }
                else {
                    console.log(data);
                }
            }
        }
    });
});

$("#BtnConditionCancell").click(function () {
    RelationalCancell();
});

function RelationalCancell() {
    $("#ddCSubParameterName").val(null).trigger('change');
    $("#ddConditionName").val(null).trigger('change');
    $('#ddRelational').val(null).trigger('change'); // Clear current selected item from surgery control
}

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

    var MultiRelational = $('#ddRelational').val();
    var RelationalID = "";

    for (var i = 0; i < MultiRelational.length; i++) {
        if (RelationalID == "") {
            RelationalID = MultiRelational[i];
        }
        else {
            RelationalID = RelationalID + "," + MultiRelational[i];
        }

    }

    if ($.trim($('#ddCSubParameterName').val()) == "") {
        $('#PddCSubParameterName').show();
        $('#PddCSubParameterName').fadeOut(8000);
        $('#PddCSubParameterName').html('Select Sub Parameter Name');
        $('#ddCSubParameterName').focus();
        $('#ddCSubParameterName').val('');
        return false;
    }
    if ($.trim($('#ddConditionName').val()) == "") {
        $('#PddConditionName').show();
        $('#PddConditionName').fadeOut(8000);
        $('#PddConditionName').html('Select Condition colmn Name');
        $('#ddConditionName').focus();
        $('#ddConditionName').val('');
        return false;
    }

    if ($.trim($('#ddRelational').val()) == "") {
        $('#PddRelational').show();
        $('#PddRelational').fadeOut(8000);
        $('#PddRelational').html('Select Relational Name');
        $('#ddRelational').focus();
        $('#ddRelational').val('');
        return false;
    }


    var SubParadata = {
        Dml_Indicator: "CSPUP",
        parameter_id: $("#ddSelectParameter").val(),
        sub_parameters_id: $("#ddCSubParameterName").val(),
        relational_column_id: RelationalID,
        condition_Value: $("#ddConditionName").val()

    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(SubParadata),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    toastr.success("Values Updated successfully");
                    var SelParameter = '';
                    if (data1.Table.length > 0) {
                        $('#tblRelational tbody').empty();
                        var tr = "";
                        for (var i = 0; i < data1.Table.length; i++) {

                            //Edit = "<a href=# class='label label-success'onclick=loadRelational('" + data1.Table3[i].ip_intra_operative_findings_id + "');>Edit</a>";
                            //Del = "<span class='label label-danger'><a href=#  onclick=DeledeSubParameter('" + data1.Table3[i].MasterSubPara_ID + "');>Delete</span></a>";
                            //ChangeOrder = "<span class='label label-success'><a href=#  onclick=ChangeSubParameterOrder('" + data1.Table3[i].MasterSubPara_ID + "');>Change Order</span></a>";

                            tr = tr + '<tr><td>' + data1.Table[i].row_num + '</td><td>' + data1.Table[i].SubParameter + '</td><td>' + data1.Table[i].CheckBox_condition_Value + '</td><td>' + data1.Table[i].relational_column + '</td></tr>';

                        }

                        $('#tblRelational tbody').append(tr);

                    }
                    else {
                        $('#tblRelational tbody').empty();
                    }
                    RelationalCancell();

                }
                else {
                    console.log(data);
                }
            }
        }
    });
});

function loadRelational(val) {

    var Parameter = {
        Dml_Indicator: "SPRC",
        sub_parameters_id: val,
        parameter_id: $("#ddSelectParameter").val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Parameter),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    if (data1.Table.length > 0) {
                        $.each(data1.Table, function (i, emp) {

                            $("#ddCSubParameterName").val(emp.ip_intra_operative_findings_id),
                            $("#ddConditionName").val(emp.CheckBox_condition_Value)
                        });
                    }

                    if (data1.Table1.length > 0) {
                        if ($('#ddRelational').select2() != null)
                            $('#ddRelational').select2('destroy');

                        var abc = "";
                        $.each(data1.Table1, function (i, emp) {
                            abc = abc + '<li class="select2-selection__choice" title="' + emp.SubParameter + '"><span class="select2-selection__choice__remove" role="presentation">×</span>' + emp.SubParameter + '</li>';
                            $('#ddRelational option').each(function (index, value) {

                                if (emp.SubParameter == value.innerText) {
                                    $(this).prop("selected", true);
                                }
                            });
                        });
                        $('ul.select2-selection__rendered').append(abc);
                        $('#ddRelational').select2();

                        //Clear clear Parameter Surgery column //
                        $('#ddsurgery').val(null).trigger('change');
                        $('#ddSPsurgery').val(null).trigger('change');
                        //$('#ddConditionName').val(null).trigger('change');
                        //$('#ddCSubParameterName').val(null).trigger('change');
                    }

                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

function FieldTypeValue() {

    var FTypeValue = [];
    $('#txtFieldTypeValue').tagEditor({
        delimiter: ',',
        initialTags: [],
        placeholder: '',
        maxLength: 250,
        autocomplete: {
            delay: 0,
            position: {
                collision: 'flip'
            },
            source: FTypeValue
        },
        clickDelete: false,
        beforeTagSave: beforeTagSavecb,
        beforeTagDelete: beforeTagDeletecb,
        onChange: onChangecb
    });

    function onChangecb(field, editor, tags) {
    }
    function beforeTagSavecb(field, editor, tags, tag, val) {
    }
    function beforeTagDeletecb(field, editor, tags, val) {
        return true;
    }
    $('#divtxtFieldTypeValue > ul').append('<li style="width:1px" class="ui-sortable-handle">&nbsp;</li>');

    $("#HideFieldTypeValue").hide();
}

$('#ddFieldType').change(function () {

    if ($.trim($('#ddFieldType').val()) == "R") {
        $("#HideFieldTypeValue").show();
    }
    else {
        $("#HideFieldTypeValue").hide();
    }

    if ($.trim($('#ddFieldType').val()) == "R" || $.trim($('#ddFieldType').val()) == "YN") {
        $(".RCHideField").hide();
    }
    else {
        $(".RCHideField").show();
        $("#HideLength").hide();
        $(".HideMaxMin").hide();
    }

});

function ChangeSubParameterOrder(val) {
    $("#HSubParaID").val(val);
    $("#HPBtnType").val("ChangeSubPOrder");

    SubParaEditorChangeOrder();
}

function loadSubParameter(val) {
    SubParameterCancell();
    $("#HSubParaID").val(val);
    $("#HPBtnType").val("EditSubParameter");

    SubParaEditorChangeOrder();
}

function SubParaEditorChangeOrder() {
    if ($("#HPBtnType").val() == "ChangeSubPOrder") {
        $("#txtSubPDisplayOrder").focus();
        $('.SubPDisable').prop('readonly', true);
        $('.SubPDisable').attr("disabled", true);
        //document.getElementById("txtFieldTypeValue").readOnly = true;
    }
    else {
        $('.SubPDisable').prop('readonly', false);
        $('.SubPDisable').attr("disabled", false);
    }

    var Parameter = {
        Dml_Indicator: "SPEC",
        sub_parameters_id: $("#HSubParaID").val(),
        parameter_id: $("#ddSelectParameter").val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Parameter),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    if (data1.Table.length > 0) {
                        $.each(data1.Table, function (i, emp) {

                            $("#txtSubParaName").val(emp.SubParameter),
                            $("#ddSParameterName").val(emp.Parameters_ID),
                            $("#ddFieldType").val(emp.Column_Type),
                            //$("#txtFieldTypeValue").val(emp.Column_Type_values),
                            $("#txtUnits").val(emp.unit_values),
                            $("#ddFieldValues").val(emp.Field_Value),
                            $("#txtSubPDisplayOrder").val(emp.DisplayOrder),
                            $("#txtMaxValue").val(emp.Max_Value),
                            $("#txtMinValue").val(emp.Min_Value),
                            $("#txtErrorMessage").val(emp.Error_Message),
                            $("#txtTooltip").val(emp.tooltip),
                            $("#txtLength").val(emp.Field_Length),
                            $("#ddSubParaStatus").val(emp.SubParameter_Status),
                            $("#HParaOrderID").val(emp.DisplayOrder),
                            $('#ddFieldValues').change();
                        });
                    }

                    if (data1.Table1.length > 0) {
                        $('#ddSPsurgery').val(null).trigger('change');
                        if ($('#ddSPsurgery').select2() != null)
                            $('#ddSPsurgery').select2('destroy');

                        var abc = "";
                        $.each(data1.Table1, function (i, emp) {
                            abc = abc + '<li class="select2-selection__choice" title="' + emp.ip_operation_name + '"><span class="select2-selection__choice__remove" role="presentation">×</span>' + emp.ip_operation_name + '</li>';
                            $('#ddSPsurgery option').each(function (index, value) {

                                if (emp.ip_operation_name == value.innerText) {
                                    $(this).prop("selected", true);
                                }
                            });
                        });
                        $('ul.select2-selection__rendered').append(abc);
                        $('#ddSPsurgery').select2();

                        //Clear clear Parameter Surgery column //
                        $('#ddsurgery').val(null).trigger('change');
                        $('#ddRelational').val(null).trigger('change');
                        //$('#ddConditionName').val(null).trigger('change');
                        //$('#ddCSubParameterName').val(null).trigger('change');
                    }
                    debugger;
                    if ($.trim($('#ddFieldType').val()) == "R") {
                        if (data1.Table2.length > 0) {
                            $("#HideFieldTypeValue").show();
                            for (var i = 0; i < data1.Table2.length; i++) {
                                var FieldTypeValue = '';
                                if (data1.Table2[i].Column_Type_values != '' && data1.Table2[i].Column_Type_values != null) {
                                    FieldTypeValue = data1.Table2[i].Column_Type_values;
                                    $('#txtFieldTypeValue').tagEditor('addTag', FieldTypeValue);
                                }
                            }

                        }
                    }

                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

function SubParameterCancell() {
    $('.SubPDisable').prop('readonly', false);
    $('.SubPDisable').attr("disabled", false);
    $("#HPBtnType").val("");
    $("#txtSubParaName").val("");
    $("#HSubParaID").val("");
    $("#ddFieldType").val("");
    $("#HParaOrderID").val("");
    //$("#txtFieldTypeValue").val("");
    //$('#txtFieldTypeValue').tagEditor("");
    $("#txtUnits").val("");
    $("#ddFieldValues").val("");
    $("#txtSubPDisplayOrder").val("");
    $("#txtErrorMessage").val("");
    $("#txtTooltip").val("");
    $("#txtMaxValue").val("");
    $("#txtMinValue").val("");
    $("#txtLength").val("");
    $('#ddFieldValues').change();
    $("#ddSubParaStatus").val(1);
    $("#HideFieldTypeValue").hide();
    $('#ddSPsurgery').val(null).trigger('change');// Clear current selected item from surgery control
    //$('#ddCSubParameterName').val(null).trigger('change');

    var tags = $('#txtFieldTypeValue').tagEditor('getTags')[0].tags;
    for (i = 0; i < tags.length; i++) { $('#txtFieldTypeValue').tagEditor('removeTag', tags[i]); }
}

function DeledeSubParameter(val) {

    $("#HSubParaID").val(val);

    var Parameter = {
        Dml_Indicator: "SPDEL",
        sub_parameters_id: val,
        parameter_id: $("#ddSelectParameter").val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Parameter),
        success: function (data) {

            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    toastr.success("Parameter Deleted Successfully");
                    LoadParametersTable();

                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

function DeledeRelational(val) {

    var Parameter = {
        Dml_Indicator: "REDEL",
        sub_parameters_id: val,
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Parameter),
        success: function (data) {

            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    toastr.success("Condition Column Deleted Successfully");
                    LoadParametersTable();

                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

$("#BtnSubParaCancell").click(function () {
    SubParameterCancell();
});

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

    var MultiOperationID = $('#ddSPsurgery').val();
    var OperationID = "";

    for (var i = 0; i < MultiOperationID.length; i++) {
        if (OperationID == "") {
            OperationID = MultiOperationID[i];
        }
        else {
            OperationID = OperationID + "," + MultiOperationID[i];
        }

    }

    if ($.trim($('#txtSubParaName').val()) == "") {
        $('#PtxtSubParaName').show();
        $('#PtxtSubParaName').fadeOut(8000);
        $('#PtxtSubParaName').html('Enter Sub Parameter Name');
        $('#txtSubParaName').focus();
        $('#txtSubParaName').val('');
        return false;
    }
    if ($.trim($('#ddSParameterName').val()) == "") {
        $('#PddSParameterName').show();
        $('#PddSParameterName').fadeOut(8000);
        $('#PddSParameterName').html('Select Parameter Name');
        $('#ddSParameterName').focus();
        $('#ddSParameterName').val('');
        return false;
    }

    if ($.trim($('#ddFieldType').val()) == "") {
        $('#PddFieldType').show();
        $('#PddFieldType').fadeOut(8000);
        $('#PddFieldType').html('Select Field Type');
        $('#ddFieldType').focus();
        $('#ddFieldType').val('');
        return false;
    }

    if ((($.trim($('#ddFieldType').val()) == "R") && $('#txtFieldTypeValue').val() == "")) {
        $('#PtxtFieldTypeValue').show();
        $('#PtxtFieldTypeValue').fadeOut(8000);
        $('#PtxtFieldTypeValue').html('Enter Field Type Value');
        $('#txtFieldTypeValue').focus();
        $('#txtFieldTypeValue').val('');
        return false;
    }

    if ($.trim($('#txtSubPDisplayOrder').val()) == "") {
        $('#PtxtSubPDisplayOrder').show();
        $('#PtxtSubPDisplayOrder').fadeOut(8000);
        $('#PtxtSubPDisplayOrder').html('Enter Display Order');
        $('#txtSubPDisplayOrder').focus();
        $('#txtSubPDisplayOrder').val('');
        return false;
    }

    if (OperationID == "") {
        $('#PddSPsurgery').show();
        $('#PddSPsurgery').fadeOut(8000);
        $('#PddSPsurgery').html('Select Surgery Name');
        return false;
    }

    var DMLIndicator = "";

    if ($("#HPBtnType").val() == "ChangeSubPOrder") {
        DMLIndicator = "USPO";

        if ($.trim($("#HParaOrderID").val()) == $.trim($('#txtDisplayOrder').val())) {

            toastr.warning("Same Display Order.\n Please Change Display Order or Click Cancel");
            return false;
        }
    }
    else {
        DMLIndicator = "SUBPIN";
    }

    debugger;
    var FieldType_value = "";

    if ($("#txtFieldTypeValue").val() != "" && $("#txtFieldTypeValue").val() != null) {
        FieldType_value = $("#txtFieldTypeValue").val();
    }
    else {
        FieldType_value = "";
    }


    var SubParadata = {
        Dml_Indicator: DMLIndicator,
        sub_parameters_name: $('#txtSubParaName').val(),
        parameter_id: $("#ddSParameterName").val(),
        sub_parameters_id: $("#HSubParaID").val(),
        screen_name: $('#ddSelectScreen').val(),
        display_order: $('#txtSubPDisplayOrder').val(),
        ip_operation_name: OperationID,
        parameters_status: $("#ddSubParaStatus").val(),
        field_type: $("#ddFieldType").val(),
        units: $("#txtUnits").val(),
        field_values: $("#ddFieldValues").val(),
        max_value: $("#txtMaxValue").val(),
        min_value: $("#txtMinValue").val(),
        error_message: $("#txtErrorMessage").val(),
        tooltip: $("#txtTooltip").val(),
        length: $("#txtLength").val(),
        field_type_value: FieldType_value,

    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(SubParadata),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    if ($("#HPBtnType").val() == "ChangeSubPOrder") {
                        toastr.success("Parameter Display Order Changed Successfully");
                    }
                    else if ($("#HPBtnType").val() == "EditSubParameter") {
                        toastr.success("Parameter Details Updated Successfully");
                    }
                    else {
                        toastr.success("Parameter Added Successfully");
                    }

                    LoadParametersTable();
                    SubParameterCancell();

                }
                else {
                    console.log(data);
                }
            }
        }
    });
});

// Sub parameter End //


function FormNameLoad() {
    debugger
    var Operationdata = {
        Dml_Indicator: "FN",
        //branch_id: $('#ddBranch').val(),//Medway all branch concept
        relational_column_id: $('#hiddenRole_id').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPOperationSessions_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Operationdata),
        success: function (data) {
            $('#ddFormName').empty();
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    var FormName;

                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {

                        FormName = FormName + '<option value="' + emp.Formid + '">' + emp.Form_Name + '</option>';

                    });
                    $('#ddFormName').append(default_opt + FormName);


                }
                else {
                    console.log(data);
                }
            }
        }
    });
}
