/*Data  loading  start here*/

let _userInfo = atob(document.cookie.split('=')[1]);
let userInfo = JSON.parse(_userInfo).result;
let sessionId = userInfo.sessionId;

let usrinfoHTML = `<li><a class="dropdown-item" href="javascript:void(0);">${userInfo.name}</a></li>
<li><a class="dropdown-item" href="javascript:void(0);">Cloud Settings</a></li>
<li><a class="dropdown-item" href="javascript:void(0);">Help</a></li>
                    <li><a class="dropdown-item" href="javascript:void(0);">Logoff</a></li>`;
$('.icn-circle').text(userInfo.name.charAt(0).toUpperCase())
$('.uindtls').html(usrinfoHTML)

// List Page loading
getData('apis/v4/unibase/reminders/platform/getunreadreminders/ReminderType/6/PageSize/20').then(function(result) {
    let response = JSON.parse(result.result);
    if (response.length != 0) { // Notifcations
        let bdyUI = '<div class="accordion" id="notifi-list">';
        for (var i = 0; i < response.length; i++) {
            bdyUI += `<li class="item-li" id="${response[i].ReminderId}" data-ref="${response[i].RedirectOptions.RefId}" data-installedAppId="${response[i].RedirectOptions.InstalledAppId}" data-formId="${response[i].RedirectOptions.CreateFormId}">
                        <div class="accordion-header" id="accHeader-${response[i].ReminderId}">
                            <div class="notifiListItms accordion-button collapsed" data-bs-toggle="collapse"  data-bs-target="#collapse-${response[i].ReminderId}" aria-expanded="false" aria-controls="collapseOne">
                                <div class="row m-0 w-100 p-0 ">
                                    <div class="col-1 ">
                                        <input class="form-check-input mx-1" type="checkbox" value="" id="flexCheckDefault">
                                    </div>
                                    <div class="col-4 text-truncate">
                                        <span class="mx-1">${response[i].Subject}</span>
                                    </div>
                                    <div class="col-5 text-truncate">
                                        <span class="mx-1">${response[i].Message}</span>
                                    </div>
                                    <div class="col-2 text-truncate">
                                        <span class="mx-1">${formatLocal(response[i].ReminderDate)}</span>
                                    </div>
                                </div>    
                            </div>
                            <div id="collapse-${response[i].ReminderId}"  class="accordion-collapse collapse" aria-labelledby="accHeader-${response[i].ReminderId}" data-bs-parent="#notifi-list">
                                <div class="accordion-body" id="accBody${response[i].ReminderId}">
                                    
                                </div>
                            </div>
                        </div>
                    </li>`;
        }
        bdyUI += '</div>'
        $('.bdy-ul').append(bdyUI);
        $('.notifiListItms').on('click', function() {
            let _this = $(this).parents('.item-li');
            let obj = { "FromDate": "2022/11/30", "InstalledAppId": Number($(_this).attr('data-installedAppId')), "RecordId": Number($(_this).attr('data-ref')), "ToDate": "2022/11/30" };
            //let obj = { "FromDate": "2022/11/30", "InstalledAppId": 102530270000109, "RecordId": 102533490346298, "ToDate": "2022/11/30" };
            getDataObj('apis/v4/unibase/platform/widgets/getdefaultwidget', obj, true, "POST").then(function(response) {
                let res = JSON.parse(response.result),
                    html = '';
                if (response.message == '200' && response.errors == null) {
                    html = `<ul class="p-0 m-0 d-flex flex-row">`;
                    if (res.AppConfigurations != null && res.AppConfigurations.length > 0) {
                        for (var k = 0; k < res.AppConfigurations.length; k++) {
                            html += `<li class="mx-2 btn-outline-primary btn btn-sm">${res.AppConfigurations[k].SettingName}</li>`;
                        }
                    } else {
                        html += `<li class="p-1 my-0 alert alert-danger" role="alert">
                            App Configurations Not configure
                        </li>`;
                    }
                    if (res.Stages != null) {
                        for (var x = 0; x < res.Stages.length; x++) {
                            if (res.Stages[x].IsDefault) {
                                html += `<li class="ml-auto">
                                            <a class="btn btn-sm btn-secondary dropdown-toggle" href="javascript:void(0)" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                                            ${res.Stages[x].StageName}
                                            </a>
                                            <ul class="dropdown-menu">`;
                            }
                        }
                        for (var i = 0; i < res.Stages.length; i++) {
                            if (!res.Stages[i].IsDefault) {
                                html += `<li><a class="dropdown-item" href="javascript:void(0);">${res.Stages[i].StageName}</a></li>`;
                            }
                        }
                        html += '</ul></li>'
                    } else {
                        html += `<li class="p-1 my-0 alert alert-danger ml-auto" role="alert">
                                No Stages
                            </li>`;
                    }
                    html += `</ul>`;
                } else {
                    html += `<div class="alert alert-danger" role="alert">
                                ${response.message} ${response.errors}
                            </div>`
                }
                $(_this).find('.accordion-body').html(html);
            });
        })
    }
})
getData('apis/v4/unibase/reminders/platform/getunreadreminders/ReminderType/1/PageSize/20').then(function(tskResp) {
    let response = JSON.parse(tskResp.result)
    if (response.length != 0) {
        let _tskhtml = '<div class="accordion" id="task-list">';
        for (var i = 0; i < response.length; i++) {
            _tskhtml += `<li class="item-li" id="${response[i].ReminderId}" data-ref="${response[i].RedirectOptions.RefId}" data-installedAppId="${response[i].RedirectOptions.InstalledAppId}" data-formId="${response[i].RedirectOptions.CreateFormId}">
                            <div class="accordion-header" id="accHeader-${response[i].ReminderId}">
                                <div class="tskListItms accordion-button collapsed" data-bs-toggle="collapse"  data-bs-target="#collapse-${response[i].ReminderId}" aria-expanded="false" aria-controls="collapseOne">
                                    <div class="row m-0 w-100 p-0 ">
                                        <div class="col-1 ">
                                            <input class="form-check-input mx-1" type="checkbox" value="" id="flexCheckDefault">
                                        </div>
                                        <div class="col-4 text-truncate">
                                            <span class="mx-1">${response[i].Subject}</span>
                                        </div>
                                        <div class="col-5 text-truncate">
                                            <span class="mx-1">${response[i].Message}</span>
                                        </div>
                                        <div class="col-2 text-truncate">
                                            <span class="mx-1">${formatLocal(response[i].ReminderDate)}</span>
                                        </div>
                                    </div>    
                                </div>
                                <div id="collapse-${response[i].ReminderId}"  class="accordion-collapse collapse" aria-labelledby="accHeader-${response[i].ReminderId}" data-bs-parent="#notifi-list">
                                    <div class="accordion-body" id="accBody${response[i].ReminderId}">
                                        <ul class="p-0 m-0 d-flex flex-row justify-content-between">
                                            <li class="mx-2 btn-outline-success btn btn-sm">Confirm</li>
                                            <li class="mx-2 btn-outline-danger btn btn-sm">Reject</li>
                                            <li class="mx-2 btn-outline-primary btn btn-sm">Close</li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </li>`;
        }
        _tskhtml += '</div>'
        $('.bdy-ul').append(_tskhtml);
    }
});

/*Data  loading  start here*/
$('.mnNvIcn').click(function() {
    $('.lft-nav-bar').toggleClass('lft-nav-bar-Wrap');
    $('.ctr-lst-pge').toggleClass('lft-nav-bar-Wrap');
});