<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" trimDirectiveWhitespaces="true" %>
<!doctype html>
<html>
<head>
	<title></title>
	<meta charset="utf-8">
	<meta http-equiv="x-ua-compatible" content="ie=edge">
	<meta name="description" content="">
	<meta name="viewport" content="width=device-width, initial-scale=1">
    
    <script src="https://code.jquery.com/jquery-3.6.4.min.js" integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8=" crossorigin="anonymous"></script>
</head>

<body>
    index.jsp<br><br>
    CRUD example<br><br><br><br>
    ---------------------------------------<br>
    <button id="h_insert_1">insert</button><br>
    ---------------------------------------<br>
    <div id="h_select_1">select1</div>
    <div id="h_select_2">select2</div>
    <div id="h_select_3">select3</div>
    <div id="h_select_4">select4</div>
    <div id="h_select_5">select5</div>
    ---------------------------------------<br>
    <span id="h_update_1">update</span><br>
    ---------------------------------------<br>
    <p id="h_delete_1">delete</p>
</body>
<script>

var add_data = function(test_data) {
    var data = {
        test_data: test_data
    }

    //var url = "/api/add_data.do";
    var url = "https://apis.naver.com/cafe-web/cafe-home/v2/popular/section/home";
    $.ajax({
        type: "GET",
        url: url,
        data: data,
        success: function (resp) {
            if (resp.errCode != 0) {
                alert("Error : " + resp.errCode);
                return;
            }

            //...
        },
        error: function (request, status, error) {
            alert("<Request Error>\nstatus = " + request.status + "\nresponseText = " + request.responseText + "\nerror = " + error);
        }
    });
}
//th code
var account_data = function() {

    var url = "/api/get-all-account.do";
    $.ajax({
        type: "POST",
        url: url,
        success: function (resp) {
            if (resp.errCode != 0) {
                alert("Error : " + resp.errCode);
                return;
            }

            alert("data = " + JSON.stringify(resp.data));

            //...
        },
        error: function (request, status, error) {
            alert("<Request Error>\nstatus = " + request.status + "\nresponseText = " + request.responseText + "\nerror = " + error);
        }
    });
}

var add_account_data = function() {
    var params = {
            userId: "testId",
            userName: "test123",
            userPw: "1234",
            email: "testEmail@naver.com",
            firstName : "test_first_name",
            lastName : "test_last_name",
            nickName : "test_nick_name"

        };
        $.ajax({
                type: "POST",
                url: "/api/add-account.do",
                contentType: "application/json; charset=UTF-8",
                data: JSON.stringify(params),
                success: function(resp) {
                    if (resp.errCode != 0) {
                        alert("Error : " + resp.errCode);
                        return;
                    }

                    alert("테스트 계정 추가 성공!\n" + JSON.stringify(resp.data));
                    console.log("서버 응답:", resp);
                },
                error: function(request, status, error) {
                    alert("<Request Error>\nstatus = " + request.status +
                          "\nresponseText = " + request.responseText +
                          "\nerror = " + error);
                }
            });
    }

var get_data = function(id, url) {
    var data = {
        id: id
    }

    $.ajax({
        type: "POST",
        url: url,
        data: data,
        success: function (resp) {
            if (resp.errCode != 0) {
                alert("Error : " + resp.errCode);
                return;
            }

            if (resp.data == null || resp.data.length == 0) {
                alert("no data");
                return;
            }

            alert("data = " + JSON.stringify(resp.data));
            //...
        },
        error: function (request, status, error) {
            alert("<Request Error>\nstatus = " + request.status + "\nresponseText = " + request.responseText + "\nerror = " + error);
        }
    });
}

var modify_data = function() {
    var params = {
            gid: 36,
            userPw: "9999",
            firstName: "길동",
            lastName: "홍",
            nickName: "수정된닉",
            email: "update@test.com",
            state: 1
        };

    var url = "/api/modify-account.do";
    $.ajax({
        type: "POST",
        url: url,
        contentType: "application/json; charset=UTF-8",
        data: JSON.stringify(params),
        success: function (resp) {
            if (resp.errCode != 0) {
                alert("Error : " + resp.errCode);
                return;
            }
              alert("성공: " + resp.data.message);
        },
        error: function (request, status, error) {
            alert("<Request Error>\nstatus = " + request.status + "\nresponseText = " + request.responseText + "\nerror = " + error);
        }
    });
}

var delete_data = function() {
    var params = {
        userId: "testId"
    }

    var url = "/api/remove-account.do";
    $.ajax({
        type: "POST",
        url: url,
        contentType: "application/json; charset=UTF-8",
        data: JSON.stringify(params),
        success: function (resp) {
            if (resp.errCode != 0) {
                alert("Error : " + resp.errCode);
                return;
            }

            alert("데이터 삭제 성공 " + resp.data.message);
        },
        error: function (request, status, error) {
            alert("<Request Error>\nstatus = " + request.status + "\nresponseText = " + request.responseText + "\nerror = " + error);
        }
    });
}

$("#h_insert_1").click(function(){
    alert("h_insert_1");
    add_data(Math.random());
});

$("#h_select_1").click(function(){
    alert("account_data");
    account_data();
});

$("#h_select_2").click(function(){
    alert("add_account_data");
    add_account_data();
});

$("#h_select_3").click(function(){
    alert("modify_data");
    modify_data();
});

$("#h_select_4").click(function(){
    alert("h_select_4");
    get_data('1', "/api/get_data4.do");
});

$("#h_select_5").click(function(){
    alert("h_select_5");
    get_data('1', "/api/get_data5.do");
});

$("#h_update_1").click(function(){
    alert("h_update_1");
    modify_data(1, Math.random());
});

$("#h_delete_1").click(function(){
    alert("h_delete_1");
    delete_data();
});
</script>

</html>
