@{
    string guid = Html.Guid();
    CurrentUser user = ViewBag.CurrentUser;
}
<script src="~/Incs/UEditor/ueditor.forAdmin.config.js" type="text/javascript"></script>
<script src="~/Incs/UEditor/ueditor.all.min.js" type="text/javascript"></script>
<div class="main" id="main" v-cloak>
    <el-form ref="form" label-width="150px" label-position="left" size="medium" >
        <el-form-item label="站点标识" prop="Remark">
            <el-input v-model="Remark" v-validate="{data:Remark,dataType:'*',nullMsg:'请填写站点标识!'}"></el-input>
            <span class="help-block validate-tips-box">仅用于后台识别</span>
        </el-form-item>
        <el-form-item label="站点名称">
            <el-input v-model.trim="Name" maxlength="50" v-validate="{data:Name,dataType:'*',nullMsg:'请填写站点名称!'}"></el-input>
            <span class="help-block validate-tips-box">默认作为站点的标题后缀</span>
        </el-form-item>
        <el-form-item label="默认缓存方案" style="width:600px">
            <el-input-convert placeholder="选择缓存方案" :clearable="true" v-model="HttpcacheSolutionId" :convert-url="'@Url.ActionUrl("getName","HttpCacheSolution")?id='+HttpcacheSolutionId">
                <el-button type="button" v-on:click="dialog({title:'选择站点缓存方案',url:'@Url.ActionUrl("Select","HttpCacheSolution")?setData=parent.vue.HttpcacheSolutionId',width:'660px',height:'95%',target:'_self'})">选择</el-button>
            </el-input-convert>
            <span class="help-block" id="_ValidateTips_HttpcacheSolutionId">设置后会作用于站点的所有栏目，也可以在栏目管理中独立设置。</span>
        </el-form-item>

        <el-form-item label="访问目录">
            <el-input v-model.trim="Directory" maxlength="50" v-validate="{data:Directory,dataType:'string',nullMsg:'请填写访问目录！',errorMsg:'访问目录填写错误'}" v-on:change="NeedRestart=1"></el-input>
            <span class="help-block validate-tips-box">如果网站未绑定域名，则用主域名+访问目录形式访问,<span class="text-danger">修改后必须重启进程才生效。</span></span>
        </el-form-item>
        <el-form-item label="绑定域名">
            <el-input v-model.trim="Domain" v-on:change="NeedRestart=1"></el-input>
            <span class="help-block validate-tips-box">如：http://www.domain.cn，留空则用主域名+目录访问网站,如有采用非80端口，必须带上端口,<span class="text-danger">修改后必须重启进程才生效。</span></span>
        </el-form-item>
        <el-form-item label="模板目录">
            <ui-select v-model.trim="TemplateDirectory" :options="TemplateDirectoryList" :clearable="false" :multiple="false" style="width:100%;" maxlength="100" v-validate="{data:TemplateDirectory,dataType:'*',nullMsg:'请选择模板目录!'}" v-on:change="NeedRestart=1"></ui-select>
            <span class="help-block validate-tips-box">模板目录在/Templates目录下，填写或修改时请确认目录是否存在{{NeedRestart}}</span>
        </el-form-item>
        <el-form-item label="Gzip压缩">
            <el-switch v-model="Compression" active-text="开启" :active-value="1" :inactive-value="0"></el-switch>
            <span class="help-block">压缩可减少带宽消耗和提升访问速度，如果服务器的IIS中已开启了Gzip压缩，网站中就不需要再开启。</span>
        </el-form-item>
        @{
            string beforeSubmitJs = "";
            var data = new ViewDataDictionary();
            data.Add("from", PageAdmin.Common.FromEnum.Admin);
            data.Add("currentUser", user);
            data.Add("guid", guid);
            data.Add("i", 0);
            int i = 0;
            var fieldData = Html.GetFieldList("pa_site", "Add", user);
            if (fieldData != null)
            {
                foreach (var row in fieldData)
                {
                    data["i"] = i;
                    beforeSubmitJs += row.BeforeSubmitJs;
                    Html.RenderPartial("~/Areas/E/Views/CustomForm/FormPartial.cshtml", (object)row, data);
                    i++;
                }
            }
        }
    </el-form>
    <div class="footer-submit-bar" style="padding-left:170px">
        <el-button type="primary" v-on:click="postSubmit">提交保存</el-button>
        <el-button onclick="closeSelf()">关闭</el-button>
    </div>
</div>

<script type="text/javascript">

        //提交前执行
    function beforeSubmitExecute() {
        @Html.Raw(beforeSubmitJs)
        return true;
    }
    //绑定数据
    var formData = @Html.RawModel((string)Model);
    formData.Guid = "@guid";
    formData.NeedRestart = 0;

    var id = "@Request.QueryString["id"]";
    if (id == "") {
        formData.Compression = 0;
    }
    formData.TemplateDirectoryList = [];
    var vue = new Vue({
        el: "#main",
        data: formData,
        methods: {
            postSubmit: function () {
                var _this = this;
                var validated = _this.validateForm({ beforeSubmit: function () { return beforeSubmitExecute() } });//jquery表单验证
                if (validated) {
                    this.submit({
                        url: "@Url.ActionUrl()", data: formData, success: function () {
                            PostBack();
                            if (_this.NeedRestart == 1)
                            {
                                _this.restartApplication();
                                _this.NeedRestart = 0;//复原
                            }
                        },
                    });
                }
            },
            restartApplication: function () {
                var url = "@Url.ActionUrl("RestartApplication","default")";
                var vue = this;
                vue.ajax({
                    type: "post", async: true, url: url,showSuccessMsg:false, success: function () {
                        //vue.$message.success("网站已重启成功！");
                    }
                });
            },
        },
    });
    function PostBack() {
        if (id == "") {
            location.href = location.href;
        }
        parent.loadData();
    }
    //加载数据
    function loadData()
    {
        vue.ajax({
            url: "@Url.ActionUrl("TemplatesDirectoryList", "TemplateManager")",
            data: {},
            success: function (responseData) {
                if (Array.isArray(responseData))
                {
                    var templateDirectoryList = [];
                    responseData.forEach(function (item, index) {
                        templateDirectoryList.push({ value: item.Name, label: item.Name });
                    })
                    formData.TemplateDirectoryList = templateDirectoryList;
                }
            }
        });
    }
    loadData();
</script>