
<div class="current-location">
    <ul>
        <li class="left"><i class="glyphicon glyphicon-home"></i><a href="javascript:void(0)" onclick="location.href=location.href">插件安装</a></li>
        <li class="right"></li>
    </ul>
</div>
<div class="main">
    <form class=" form ui-formBind" method="post" id="_form">
        <div class="form-group">
            <label class="control-label">安装方式</label>
            <div class="controls radio">
                <label class="radio-inline">
                    <input type="radio" name="Source" id="Source" value="ExtractCode" class="ui-show-hide" showObjs=".sp-ExtractCode" hideObjs=".sp-Package" checked>插件提取码
                </label>
                <label class="radio-inline">
                    <input type="radio" name="Source" id="Source" value="Package" class="ui-show-hide" showObjs=".sp-Package" hideObjs=".sp-ExtractCode">插件安装包
                </label>
                <span class="help-block"></span>
            </div>
        </div>

        <div class="form-group sp-ExtractCode">
            <label class="control-label">提取码</label>
            <div class="controls">
                <input type="text" maxlength="100" class="form-control form-control-md" placeholder="请输入插件提取码" id="ExtractCode" name="ExtractCode" data-validate="minLength:4,minLengthErrorMsg:'提取码填写错误！',dataType:'*',nullMsg:'请填写提取码',ignoreParentHidden:true" />
                <span class="help-block"></span>
            </div>
        </div>


        <div class="form-group sp-Package">
            <label class="control-label">安装包</label>
            <div class="controls  form-inline">
                <div class="input-group input-group-sm">
                    <input type="text" maxlength="50" name="Package" id="Package" readonly data-validate="dataType:'*',nullMsg:'选择要上传的插件压缩包',ignoreParentHidden:true" class="form-control form-control-sm ui-uploadPanel" data-upload-params="fileType:'file',savePath:'/Upload/Temp/',fileName:'',fileSingleSizeLimit:'0',fileNumLimit:'1',extensions:'zip',mimeTypes:'application/x-zip-compressed',rename:false,deleteRepeat:true,sortByDate:false,sortByFileType:false,saveToAttachmentData:false" />
                </div>
                    <span class="help-block">只能上传.zip格式的安装包,文件名必须和插件名保持一致</span>
            </div>
        </div>


        <div class="form-group" id="btn-box">
            <label class="control-label"></label>
            <div class="controls">
                <input type="hidden" id="AppType" name="AppType" value="plugin" />
                <input type="hidden" id="Guid" name="Guid" value="@Html.Guid()" />
                @Html.AntiForgeryToken()
                <button type="button" class="btn btn-sm btn-primary ui-pagePost" data-custom-params="url:'@Url.Action()',callBack:'CallBack',successMsg:''" id="bt_submit">提交</button>
                <div class="form-control-static">
                    <ul id="process"></ul>
                    <span class="help-block"></span>
                </div>
                <span class="help-block"></span>
            </div>
        </div>
    </form>

    <div class="alert alert-warning alert-dismissible" role="alert">
        <strong>说明：</strong>
        <p>1、如果从官方获取了插件提取码，可以输入提取码在线安装，请确保网络连接正常。</p>
        <p>2、如果采用.zip压缩安装包，请确保压缩包中有必须的配置文件，详情参考PageAdmin官方的插件开发说明。</p>
        <p>3、使用插件必须先在系统设置中填写好AccessKeyId和AccessKeySecret参数。</p>
    </div>

</div>

<script type="text/javascript">
    var $btn = $("#bt_submit");
    var $process = $("#process");
    var $package = $("#Package");
    var guid = $("#Guid").val();
    var url = "@Url.Action("GetProgress")?guid=" + guid +"&sleep=1000";
    function CallBack(data)
    {
        var state = data.State;
        var msg = data.Msg;
        if (state == 1)
        {
            Loading();
            $btn.hide();
            WriteProcess("");
            WriteProcess("提交请求中...");
            GetProgress();
        }
        else
        {
            Alert(msg);
        }
    }

    function GetProgress()
    {
        var hasError = false;
        Ajax({ url: url }, function (data) {
            data=JsonParse(data);
            var state = data.State;
            var msg = data.Msg;
            if (state == 1)
            {
                WriteProcess("安装成功，请到插件管理中开启插件即可使用!");
                CloseLoading();
                $btn.attr("disabled", "disabled").text("安装完毕!").show();
            }
            else
            {
                switch (msg)
                {
                    case "":
                        break;
                    case "runSql":
                        WriteProcess("运行脚本文件...");
                        break;
                    case "runSqlOk":
                        WriteProcess("脚本文件运行完毕");
                        break;
                    case "downFile":
                        WriteProcess("文件下载中...");
                        break;
                    case "downFileOk":
                        WriteProcess("文件下载完毕");
                        break;
                    case "unCommpress":
                        WriteProcess("文件解压中...");
                        break;
                    default:
                        hasError = true;
                        $package.DeleteUploadedFile();
                        WriteProcess("<span class=\" text-danger\">出错:" + msg + "</span>");
                        CloseLoading();
                        $btn.show();
                        break;
                }
                if (!hasError)
                {
                    setTimeout(function () { GetProgress(); }, 1000);
                }

            }

        }, function (httpRequest, textStatus)
        {
            alert("http" + httpRequest.status + "错误:" + httpRequest.responseText + "/" + textStatus);
            $btn.show();
        });
    }
        //写进度
    function WriteProcess(msg)
    {
        if (msg == "")
        {
            $process.html("");
        }
        else
        {
            $process.append("<li>" + msg + "<li>");
        }

    }
</script>
