<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ApplyApproval.aspx.cs" Inherits="Approval_ApplyApproval" ValidateRequest="false" meta:resourcekey="Page" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../libs/fontawesome/css/all.min.css" >
    <link rel="stylesheet" href="../libs/kendoui/2023.3.1114/styles/bootstrap-3.css" />
    <script type="text/javascript" src="../libs/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="../libs/bootstrap/js/bootstrap.min.js"></script>
    <script src="../libs/kendoui/2023.3.1114/mjs/kendo.upload.js" type="module"></script>
</head>
<body>
    <script type="text/javascript">
        var malwareInfected = false;        // 악성 코드 감염 여부

        $(document).ready(function () {
            Dialog.setMessage('<asp:Literal runat="server" Text="<%$ Resources:Dialog, Title%>" />', '<asp:Literal runat="server" Text="<%$ Resources:Dialog, OK%>" />', '<asp:Literal runat="server" Text="<%$ Resources:Dialog, Cancel%>" />');
            Dialog.setUseBrowserConfirm(<%=AppSetting.UseBrowserConfirm%>);

            if ("<%=WebUser.CurrentUser.UiLanguage%>" == "ko") kendo.culture("ko-KR");
            kendo.ui.Upload.fn._supportsDrop = function () { return false; }    // 드래그&드롭 업로드 기능 off
            $("#files").kendoUpload({
                multiple: true,
                async: {
                    saveUrl: "../Common/UploadChunk.ashx",
                    removeUrl: "../Common/UploadRemove.ashx",
                    chunkSize: 2 * 1024 * 1024,
                    autoUpload: true
                },
                // 업로드 시작하기 전 발생
                upload: function (request) {
                    // 파라미터 추가
                    request.data = { checkMalware: true };
                    $("#acceptToolButton").attr("disabled", true);
                },
                // 업로드 완료 시 발생
                complete: function (e) {
                    $("#acceptToolButton").attr("disabled", false);
                },
                // 오류 응답 시 발생: 악성 코드 검출 시 발생
                error: function (e) {
                    try {
                        const cmResult = JSON.parse(e.XMLHttpRequest.responseText);
                        if (cmResult.infected) {
                            malwareInfected = true;
                            Dialog.alert(String.format('<asp:Literal runat="server" Text="<%$ Resources:, Msg04%>" />', cmResult.malware));
                        }
                    } catch { }
                }
            });
        });

        function Accept() {
            // 결재 문서를 승인하시겠습니까?
            Dialog.confirm({
                content: '<asp:Literal runat="server" Text="<%$ Resources:, Msg01%>" />',
                ok: function () {
                    var upload = $("#files").data("kendoUpload")
                    if (malwareInfected) {
                        // 감염 이력이 있을 경우 모든 첨부를 제거한다.
                        upload.clearAllFiles();
                        malwareInfected = false;
                    }
                    var files = upload.getFiles();
                    if (files.length > 0) {
                        var fileInfoList = [];
                        for (var i = 0; i < files.length; i++) {
                            var file = files[i];
                            var fileInfo = {
                                Uid: file.uid,
                                Name: file.name,
                                Extension: file.extension,
                                ContentType: file.rawFile.type,
                                Size: file.size
                            };
                            fileInfoList.push(fileInfo);
                        }
                        $("#JsonUploadFilesHidden").val(JSON.stringify(fileInfoList));                        
                    }
                    __doPostBack("AcceptButton", "");
                }
            });
        }

        function GoBack() {
            history.back();
        }

        function GotoList() {
            location.href = "List.aspx?type=<%=AppFolderDataSource.APPROVAL_FOLDERID%>";
        }
    </script>

    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <CompositeScript>
                <Scripts>
                    <asp:ScriptReference Path="~/libs/tabs/common.js" />
                    <asp:ScriptReference Path="~/libs/tabs/dialog.js" />
                </Scripts>
            </CompositeScript>
        </asp:ScriptManager>

        <div class="container-fluid">
            <div class="row" id="toolBar" data-spy="affix">
                <div class="col-xs-12" style="padding-top:2px;padding-bottom:2px;">
                    <button type="button" id="acceptToolButton" class="btn btn-primary" onclick="Accept()"><span class="fa-regular fa-circle-check"></span> <%=_approveButtonText%></button>
                    <span>&nbsp;</span>
                    <button type="button" id="gobackToolButton" class="btn btn-primary" onclick="GoBack();"><span class="fa-solid fa-xmark"></span></button>
                </div>
            </div>
        </div>

        <div class="container-fluid" style="padding-top:40px">
            <asp:UpdatePanel ID="AjaxPanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
                <ContentTemplate>
                    <asp:Panel ID="MsgBox" runat="server" Visible="false">
                        <asp:Label ID="MsgBoxLabel" runat="server" Text="Label"></asp:Label>
                        <div>
                            <button type="button" id="gotoListButton" class="btn btn-warning" onclick="GotoList();">
                                <asp:Localize ID="Localize3" runat="server" meta:resourcekey="L03">목록으로</asp:Localize>
                            </button>
                        </div>                        
                    </asp:Panel>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="AcceptButton" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>
            <div class="form-group">
                <label for="AcceptTextBox"><asp:Localize ID="Localize1" runat="server" meta:resourcekey="L01">추가 코멘트</asp:Localize>:</label>
                <textarea class="form-control" rows="10" id="AcceptTextBox" runat="server"></textarea>
            </div>
            <div class="form-group">
                <label for="FileUpload1"><asp:Localize ID="Localize2" runat="server" meta:resourcekey="L02">파일</asp:Localize>:</label>
                <input type="file" name="files" id="files" />
                <asp:HiddenField ID="JsonUploadFilesHidden" runat="server"></asp:HiddenField>
            </div>
        </div>

        <div style="display:none">
            <asp:LinkButton ID="AcceptButton" runat="server" onclick="AcceptButton_Click" /> 
        </div>
    </form>
</body>
</html>
