<script type="text/javascript">
    function btn_reshard_OnClick()
    {
        document.getElementById("btn_reshard").value = "in progress...";
        document.getElementById("btn_reshard").disabled = true;
        var php2 = {};
        var reshard_count = document.getElementById("reshard_count");
        php2["count"] = reshard_count.value;
        var shard_min = document.getElementById("shard_min");
        php2["shard_min"] = shard_min.value;
        var shard_max = document.getElementById("shard_max");
        php2["shard_max"] = shard_max.value;
        koolajax.callback(reshard_start(JSON.stringify(php2)), onDoneStart);
    }

    function onDoneCheck(s)
    {
        var event = JSON.parse(s);
        if (event.is_finished === true) {
            document.getElementById("btn_reshard").value = "Reshard";
            document.getElementById("btn_reshard").disabled = false;
        } else {
            koolajax.callback(task_check(event.id_task), onDoneCheck);
        }
        document.getElementById("status_reshard").innerHTML = event.report;
    }

    function onDoneStart(s)
    {
        if (s.trim() === "duplication") {
            document.getElementById("btn_reshard").value = "Reshard";
            document.getElementById("btn_reshard").disabled = false;
            document.getElementById("status_reshard").innerHTML = "duplication";
        } else {
            document.getElementById("status_reshard").innerHTML = s.trim();
            koolajax.callback(task_check(s.trim()), onDoneCheck);
        }
    }
</script>

<?php
$form_manager = new KoolForm("reshard_form");
$form_manager->scriptFolder = $KoolControlsFolder . "/KoolForm";
$form_manager->styleFolder = "sunset";
$form_manager->DecorationEnabled = true;

$reshard_count_new = $form_manager->AddControl(new KoolNumericTextBox("reshard_count"));
$reshard_count_new->Value = 2;
$reshard_shard_min = $form_manager->AddControl(new KoolNumericTextBox("shard_min"));
$reshard_shard_min->Value = 0;
$reshard_shard_max = $form_manager->AddControl(new KoolNumericTextBox("shard_max"));
$reshard_shard_max->Value = 65533;

$koolajax->scriptFolder = $KoolControlsFolder . "/KoolAjax";
$koolajax->enableFunction("reshard_start");
$koolajax->enableFunction("task_check");

$form_manager->Init();

$reshard_actions = "<div>new_parts_count</div>";
$reshard_actions .= $reshard_count_new->Render() . "<br/>";
$reshard_actions .= "<div>shard_min</div>";
$reshard_actions .= $reshard_shard_min->Render() . "<br/>";
$reshard_actions .= "<div>shard_max</div>";
$reshard_actions .= $reshard_shard_max->Render() . "<br/>";
$reshard_actions .= "<input id=\"btn_reshard\" type=\"button\" value=\"Reshard\" onclick=\"btn_reshard_OnClick()\" />";
$reshard_actions .= "<div id=\"status_reshard\">reshard info</div>";

function reshard_start($info) {
    $json = json_decode($info);
    $shard_min = $json->{"shard_min"};
    $shard_max = $json->{"shard_max"};
    $count = $json->{"count"};
    global $ctype_cgi;
    global $service_cgi;
    read_reply_from_dm("reshard?config_type=default&ctype=$ctype_cgi&service=$service_cgi&shard_min=$shard_min&shard_max=$shard_max&new_parts_count=$count", $result);
    return $result;
}

include("check_task.php");
?>