$(document).ready(function () {
    debugger;
    LoadApprovalList()
})
//Load Approval List
function LoadApprovalList() {
    var DoctorData = {
        UserId: $('#hiddenUser_id').val(),
        DmlIndicator:'L'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserRight/LoadApprovalList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data2 = JSON.parse(data);
            console.log(data2)
            if (jQuery.isEmptyObject((data2.data))) {
            }
            else {
                debugger;
                $('#tbl_approval tbody').empty();
                $('#tbl_approval').dataTable().fnClearTable();
                $('#tbl_approval').dataTable().fnDraw();
                $('#tbl_approval').dataTable().fnDestroy();
                if (data2.msg == "Success") {
                    var data1 = data2.data;
                    var tr = '', hide = '', status = '', view = '', approve = '', reject = '';
                    $.each(data1, function (i, emp) {
                        if (emp.IsApproved == 1 || emp.IsApproved == 2) {
                            status = '<span class="label label-success">Approved</span>'
                        }
                        else if (emp.IsApproved == 3) {
                            status = '<span class="label label-danger">Declined</span>'
                        }
                        //else {
                        //    status = '<span class="label label-warning">Pending</span>'
                        //}
                        else {
                            view = '<a title="View" onclick=\'Redirect(' + JSON.stringify(emp) + ')\'>View</a>';
                            approve = '<a title="Approve" onclick=\'IsApprove(' + JSON.stringify(emp) + ',1)\'>Approve</a>';
                            reject = '<a title="Reject" onclick=\'IsApprove(' + JSON.stringify(emp) + ',3)\'>Reject</a>';
                            status = '<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="overflowmenu"><li>' + view + '</li><li>' + approve + '</li><li>' + reject + '</li></div></ul></div>';
                        }
                        tr = tr + '<tr><td>' + (i + 1) + '</td><td>' + emp.ApprovalCode + '</td><td>' + emp.EntityName + '</td><td>' + emp.CreatedAt1 + '</td><td>' + status + '</td></tr>'
                    })
                    $('#tbl_approval tbody').append(tr);
                    datatable('tbl_approval', 'tbl_approval1', 'Legal');
                }
            }
        }
    });
}
//Redirect To Respective Page based on dynamic url
function Redirect(datas) {
    debugger;
    var DoctorData = {
        EntityTransactionId: datas.EntityTransactionId,
       DmlIndicator: 'DL',
       ApprovalTable: datas.ApprovalTable,
       ApprovalColumn:datas.ApprovalColumn
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserRight/LoadApprovalList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data1= JSON.parse(data);
            console.log(data1)
            if (jQuery.isEmptyObject((data1.data))) {
            }
            else {
                debugger;

                if (data1.msg == 'Success') {
                    data1 = data1.data.Table;
                    if (data1.length > 0)
                    {
                        let Obj = data1[0];
                        let paramArray = datas.ApprovalParameter.split(',');
                        let paramDataArr=[]
                        Object.keys(Obj).forEach((key) => {
                            let val = paramArray.filter((value) => key.includes(value));
                            if (val.length > 0) {
                                let objWithKey = {};
                                objWithKey[key] = Obj[key];
                                paramDataArr.push(objWithKey)
                            }
                        });
                        if (paramDataArr.length > 0) {
                            for (var i = 0; i < paramDataArr.length; i++) {
                                let objval = paramDataArr[i];
                                Object.keys(objval).forEach((key) => {
                                    let keys = '{{' + key + '}}'
                                    datas.ApprovalURL=datas.ApprovalURL.replace(keys, objval[key])
                                })
                                
                            }
                           // window.location.href = datas.ApprovalURL
                            window.open(datas.ApprovalURL, '_blank');
                        }
                    }
                    
                }
               
                
            }
        }
    });
    //window.location.href = data.ApprovalURL + '&EntityTransactionId=' + data.EntityTransactionId + '&DeptRoleId=' + data.DepRoleId


}
//Approve/Reject
function IsApprove(data, type) {
    debugger
   
    var DoctorData = {
        EntityTransactionId: data.EntityTransactionId,
        EntityOperationId: data.EntityOperationId,
        DeptRoleId: data.DepRoleId,
        DmlIndicator: (type == 1) ? 'AA' : 'AR',
        ApprovalColumn: data.ApprovalColumn,
        ApprovalTable: data.ApprovalTable,
        IsApproved: type
    }
     $.ajax({
        async: true,
        type: 'POST',
        url: '/UserRight/LoadApprovalList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {
            }
            else {
                debugger;
                $('#tbl_approval tbody').empty();
                if (data2.msg == "Success") {

                    (type == 1) ? toastr.success('Approved Successfully') : toastr.success('Rejected Successfully')
                    LoadApprovalList();
                }
                
            }
        }
    });

}