<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="java.io.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ include file="/jsp/common/include/localemessage.jsp" %>
<%
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Expires", "0");
%>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <jsp:include page="/jsp/common/include/css.jsp"/>
    <jsp:include page="/jsp/common/include/script.jsp"/>
    <style>
        .mask-email  { color:#1a73e8; font-weight:bold; }
        .mask-phone  { color:#d93025; font-weight:bold; }
        .mask-digits { color:#9334e6; font-weight:bold; }
        .msg-detail { white-space:pre-wrap; word-break:break-all; min-height:80px; line-height:1.6; }
    </style>
    <script language="javascript">
        var url = '<c:url value="/onl/apim/messagerequest/messageRequestMan.json" />';
        var url_view = '<c:url value="/onl/apim/messagerequest/messageRequestMan.view" />';
        var key = '${param.id}';

        function detail() {
            if (!key) return;
            $.ajax({
                type: "POST", url: url, dataType: "json",
                data: {cmd: 'DETAIL', id: key},
                success: function (data) {
                    var codeText = (data.messageCodeName || '') + (data.messageCode ? ' (' + data.messageCode + ')' : '');
                    $('#messageCodeName').text(codeText);
                    $('#subject').text((data.subject && data.subject !== '') ? data.subject : (data.messageCode || ''));
                    $('#messageType').text(data.messageType || '');
                    $('#requestStatus').text(data.requestStatus || '');
                    $('#username').text(data.username || '');
                    $('#email').text(data.email || '');
                    $('#phone').text(data.phone || '');
                    $('#messengerId').text(data.messengerId || '');
                    $('#umsUid').text(data.umsUid || '');
                    $('#eaiInterfaceId').text(data.eaiInterfaceId || '');
                    $('#serviceId').text(data.serviceId || '');
                    $('#requestDate').text(data.requestDateFull || '');
                    $('#sentDate').text(data.sentDateFull || '');
                    // 서버에서 마스킹+escape 된 안전 HTML
                    $('#messageHtml').html(data.messageHtml || '');

                    // PENDING 건만 '실패 처리' 노출 (권한 제어는 buttonControl 이 담당)
                    if (data.requestStatus !== 'PENDING') {
                        $('#btn_fail').hide();
                    }
                },
                error: function (e) { alert(e.responseText); }
            });
        }

        $(document).ready(function () {
            var returnUrl = getReturnUrlForReturn();

            buttonControl();
            detail();

            $('#btn_fail').click(function () {
                if (!confirm('이 건을 FAILED(발송 실패) 상태로 변경하시겠습니까?')) return;
                $.ajax({
                    type: "POST", url: url, dataType: "json",
                    data: {cmd: 'UPDATE_STATUS', id: key},
                    success: function () {
                        alert("변경되었습니다.");
                        detail();
                    },
                    error: function (e) { alert(e.responseText); }
                });
            });

            $('#btn_previous').click(function () { goNav(returnUrl); });
        });
    </script>
</head>
<body>
<div class="right_box">
    <div class="content_top">
        <ul class="path"><li><a href="#">${rmsMenuPath}</a></li></ul>
    </div>
    <div class="content_middle">
        <div class="search_wrap">
            <button type="button" class="cssbtn" id="btn_fail" level="W" status="DETAIL" style="background-color:#dc3545;border-color:#dc3545;color:#fff;"><i class="material-icons">report</i> 실패 처리</button>
            <button type="button" class="cssbtn" id="btn_previous" level="R" status="DETAIL,NEW"><i class="material-icons">arrow_back</i> <%= localeMessage.getString("button.previous") %></button>
        </div>
        <div class="title" id="title">메세지 발송 내역 상세<span class="tooltip">개인정보는 마스킹되어 표시됩니다.</span></div>
        <table class="table_row" cellspacing="0">
            <colgroup>
                <col style="width:12%"/><col style="width:38%"/>
                <col style="width:12%"/><col style="width:38%"/>
            </colgroup>
            <tr>
                <th>메세지 코드</th>
                <td colspan="3"><span id="messageCodeName"></span></td>
            </tr>
            <tr>
                <th>제목</th>
                <td colspan="3"><span id="subject"></span></td>
            </tr>
            <tr>
                <th>유형</th>
                <td><span id="messageType"></span></td>
                <th>상태</th>
                <td><span id="requestStatus"></span></td>
            </tr>
            <tr>
                <th>수신자명</th>
                <td><span id="username"></span></td>
                <th>메신저 ID</th>
                <td><span id="messengerId"></span></td>
            </tr>
            <tr>
                <th>이메일</th>
                <td><span id="email"></span></td>
                <th>휴대폰</th>
                <td><span id="phone"></span></td>
            </tr>
            <tr>
                <th>요청 시간</th>
                <td><span id="requestDate"></span></td>
                <th>발송 시간</th>
                <td><span id="sentDate"></span></td>
            </tr>
            <tr>
                <th>EAI 인터페이스 ID</th>
                <td><span id="eaiInterfaceId"></span></td>
                <th>서비스 ID</th>
                <td><span id="serviceId"></span></td>
            </tr>
            <tr>
                <th>UMS UID</th>
                <td colspan="3"><span id="umsUid"></span></td>
            </tr>
            <tr>
                <th>메세지 내용</th>
                <td colspan="3"><div id="messageHtml" class="msg-detail"></div></td>
            </tr>
        </table>
    </div>
</div>
</body>
</html>
