<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<style>
   .modal-body textarea {
       resize: none;
   }
   .modal-body button {
       margin-left: 0 !important;
   }
   .form_tips {
       color: #999;
       font-size: 12px;
   }
   p img {
       max-width: 100%;
       max-height: 100%;
       margin: 10px 0;
   }
   label {
       font-weight: bolder;
   }
</style>

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
    <h4 class="modal-title">已完成测试任务上传比对文件</h4>
</div>
<div class="modal-body">
    <form id="userForm" method="post">
        <input type="hidden" name="taskId" value="${taskId}">
        <input type="hidden" name="channelId" value="${channelId}">
        <div class="form_group">
            <label class="form_label col-xs-3">任务流水号：</label>
            <div class="col-xs-9">${taskId}</div>
        </div>
        <div class="form_group">
            <label class="form_label col-xs-3">通道：</label>
            <div class="col-xs-9">${channelId}</div>
        </div>
        <div class="form_group">
            <label class="form_label col-xs-3">比对文件上传：</label>
            <div class="col-xs-9">
                <input type="file" style="display: none" class="form_control" name="uploadFile" id="uploadFile" accept="text/*,application/json" onchange="uploadExtendFile()"/>
                <button type="button" class="btn btn_blue" onclick="selectExtendFile()">选择文件</button>
                <input type="text" class="form_control" name="extendFile" id="extendFile" readonly style="width: 200px" >
                <br>
                <span class="form_tips">请注意筛选短信数据,不要添加与本次测试内容无关的内容,以免出现误判</span>
            </div>
        </div>
        <div class="form_group">
            <label class="form_label col-xs-3">比对文件格式</label>
            <div class="col-xs-9">
                <label><input type="radio" name="fileType" value="0" checked> 360手机助手导出文件 </label>
                <label><input type="radio" name="fileType" value="1"> Deku APP导出文件 </label>
                <br>
                <span class="form_tips">请确保选择的类型正确,两种文件格式不一样,类型不对会解析错误</span>
            </div>
        </div>
    </form>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn_blue" onclick="startCompare()">开始比对</button>
    <button type="button" class="btn default" data-dismiss="modal">关闭</button>
</div>

<script>
    $(function () {
        $('.singleSelect').select2();
    });

    function selectExtendFile() {
        $("#uploadFile").click()
    }

    function downloadDemoFile() {
        window.open('<%=request.getContextPath()%>/channel_batch_test/download_demo_file.html')
    }

    function uploadExtendFile() {
        const formData = new FormData();
        const file = $("#uploadFile")[0].files[0];
        if (!file) {
            Dialog.alertError("请选择文件");
            return;
        }
        const channelId = $("input[name=channelId]").val();
        formData.append("extendFile", file, file.name);
        formData.append("channelId", channelId);
        formData.append("filePrefix", "compare");
        $.ajax({
            url: '<%=request.getContextPath()%>/channel_batch_test/upload_extend_file.html',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function (res) {
                if (res.code === 0) {
                    $("#extendFile").val(res.data);
                } else {
                    Dialog.alertError(res.msg);
                    $("#extendFile").val("");
                    $("#uploadFile").val("");
                }
            },
            error: function () {
                Dialog.alertError("上传文件失败");
                $("#extendFile").val("");
                $("#uploadFile").val("");
            }
        })
    }

    function startCompare() {
        const extendFile = $("#extendFile").val();
        if (!extendFile) {
            Dialog.alertError("请上传比对文件");
            return;
        }
        const fileType = $("input[name=fileType]:checked").val();
        if (!fileType) {
            Dialog.alertError("请选择文件类型");
            return;
        }
        const taskId = $("input[name=taskId]").val()
        const channelId = $("input[name=channelId]").val();
        const requestParams = {
            compareFile: extendFile,
            taskId: taskId,
            channelId: channelId,
            fileType: fileType
        }
        $.ajax({
            url: '<%=request.getContextPath()%>/channel_batch_test/startCompare.html',
            type: 'POST',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            dataType: 'json',
            data: requestParams,
            success: function (data) {
                if (data.code === 0) {
                    Dialog.alertSucc("比对任务开始,请稍后刷新列表查看");
                    setTimeout(() => {
                        window.location.reload();
                    }, 1500)
                } else {
                    Dialog.alertError(data.msg);
                }
            },
            error: function () {
                Dialog.alertError("比对任务失败,请求出错");
            }
        })

    }
</script>
