<%@ Page Title="Chunk Retry" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" %>
<%@ Register Assembly="AjaxUploader" Namespace="AjaxUploader.Controls" TagPrefix="au" %>

<asp:Content ID="TitleContent" ContentPlaceHolderID="TitleContent" runat="server">Chunk Retry - AjaxUploader v5.0 Demos</asp:Content>
<asp:Content ID="HeaderContent" ContentPlaceHolderID="HeaderTitle" runat="server">Chunk Retry</asp:Content>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

<div class="demo-card">
    <div class="demo-card-header">Automatic Retry on Chunk Failure</div>
    <div class="demo-card-body">
        <p class="demo-description">
            When a chunk fails to upload due to a network error, AjaxUploader automatically retries
            the failed chunk. Configure the number of retry attempts and delay between retries.
        </p>

        <au:AjaxFileUpload ID="Uploader1" runat="server"
            EnableChunkedUpload="true"
            ChunkSize="1048576"
            MaxRetries="3"
            RetryDelay="2000" />

        <div class="mb-3 mt-3">
            <label class="form-label fw-bold">Max Retries</label>
            <select id="retryCount" class="form-select" style="width:auto;display:inline-block;">
                <option value="0">0 (No retry)</option>
                <option value="1">1</option>
                <option value="3" selected>3</option>
                <option value="5">5</option>
            </select>
        </div>

        <div id="log" class="mt-3 p-2 bg-light border rounded" style="max-height:160px;overflow-y:auto;font-size:.85rem;"></div>

        <div class="code-panel">
            <pre><code>&lt;au:AjaxFileUpload ID="Uploader1" runat="server"
    EnableChunkedUpload="true"
    ChunkSize="1048576"
    MaxRetries="3"
    RetryDelay="2000" /&gt;

// JavaScript API approach
AjaxUploader.create('#uploader', {
    uploadUrl: '<%= ResolveUrl("~/ajaxupload.axd/chunk") %>',
    chunked: true,
    chunkSize: 1 * 1024 * 1024,
    retries: 3, // retry failed chunks up to 3 times
    onTaskProgress: function(task) {
        log('Chunk progress: ' + Math.round(task.percent) + '%');
    },
    onTaskError: function(task, error) {
        log('Failed after retries: ' + error);
    }
});</code></pre>
        </div>
    </div>
</div>

</asp:Content>

<asp:Content ID="ScriptsContent" ContentPlaceHolderID="ScriptsContent" runat="server">
<script>
    var logEl = document.getElementById('log');
    function log(msg) {
        var div = document.createElement('div');
        div.textContent = '[' + new Date().toLocaleTimeString() + '] ' + msg;
        logEl.appendChild(div);
        logEl.scrollTop = logEl.scrollHeight;
    }

    var uploader = AjaxUploader.getInstance('Uploader1');
    if (uploader) {
        uploader.on('taskStart', function(task) {
            log('Started: ' + task.fileName);
        });
        uploader.on('taskProgress', function(task) {
            log('Progress: ' + task.fileName + ' ' + Math.round(task.percent) + '%');
        });
        uploader.on('taskError', function(task, error) {
            log('Error: ' + task.fileName + ' - ' + error);
        });
        uploader.on('taskComplete', function(task) {
            log('Completed: ' + task.fileName);
        });
    }

    document.getElementById('retryCount').addEventListener('change', function() {
        if (uploader) {
            uploader.setOptions({ retries: parseInt(this.value) });
            log('Retries set to ' + this.value);
        }
    });
</script>
</asp:Content>
