let url = readWebConfig();
let token = bearertoken();
let pdpa = document.getElementById('pdpaEditor')

 getdb();
$(document).ready(function () {

    /*$(document).on('click', '#btnSave', function () {       
        Swal.fire({
            //title: "Are you sure you want to stock in with zero cost?",
            text: "Are you sure you want save changes?",
            icon: "warning",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Yes"
        }).then((result) => {
            if (result.isConfirmed) {
                putPDPAConsent(pdpa.innerHTML)
                Swal.fire({
                    position: "center",
                    icon: "success",
                    text: "PDPA Consent successfully change",
                    showConfirmButton: false,
                    timer: 1500
                }).then((result) => {

                });
            } else {
                pdpa.innerHTML = getPDPA();
            }
            return;
        });       
    });

    $(document).on('click', '#btnClear', function () {
        Swal.fire({
            //title: "Are you sure you want to stock in with zero cost?",
            text: "Are you sure you want clear PDPA consent?",
            icon: "warning",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Yes"
        }).then((result) => {
            if (result.isConfirmed) {
                pdpa.innerHTML = "";              
            } else {
                //pdpa.innerHTML = getPDPA();
            }
            return;
        });     
    });
*/
    
    
});

function bearertoken() {
    let token;
    var settings = {
        "url": url + "/Users/authenticate",
        "method": "POST",
        "async":false,
        "timeout": 0,
        "headers": {
            "Content-Type": "application/json"
        },
        "data": JSON.stringify({
            "username": "test",
            "password": "test"
        }),
    };

    $.ajax(settings).done(function (response) {
        token = response['token'];
    });
    return token;    
}

function putPDPAConsent(db,data) {    
    var settings = {
        "url": url + "/api/ICRMSetting/UpdatepdpaContends",
        "method": "PUT",
        "timeout": 0,
        "async":false,
        "headers": {
            "Content-Type": "application/json",
            "Authorization": token
        },
        "data": JSON.stringify({
            "id": "1",
            "pdpacontent": data,
            "DbName": db
        }),
    };

    $.ajax(settings).done(function (response) {
        //console.log(response);
    }).fail(function (jqXHR, textStatus, errorThrown) {
        console.log(errorThrown)
        console.log("error(putPDPAConsetn)");
    });



    //$.ajax({
    //    type: "POST",
    //    url: "PDPAContends.aspx/putPDPAConsent",
    //    async: false,
    //    contentType: 'application/json;charset=utf8',
    //    data: "{'data':'" + data + "'}",
    //    beforeSend: function () {
    //        //$('#modalspinner').modal('show');
    //    },
    //    complete: function (result) {
    //        //modalLoad.style.display = "none";

    //    },
    //    dataType: 'json',
    //    error: function (jqXHR, textStatus, errorThrown) {
    //        console.log(errorThrown);
    //    },
    //    success: function (result) {
    //        //location.reload();
    //        //$('#modalspinner').modal('hide');
    //        //sessionStorage.setItem('category', result.d);
    //    }
    //});
}

function getPDPA(db) {
    let pdpa = document.getElementById('pdpaEditor')
   
    var settings = {
        "url": url + "/api/" + db + "/ICRMSetting/GetpdpaContends/1",
        "method": "GET",
        "timeout": 0,
        "async": false,
        "headers": {
            "Authorization": token
        },
    };
    $.ajax(settings).done(function (response) {        
        pdpa.innerHTML = (JSON.parse(response)['pdpacontent'])               
    });
    return pdpa;    
}