@using PageAdmin.Utils;
@using PageAdmin.Common;
@{
    int fieldId = ViewBag.FieldId;
    int parentId = ViewBag.ParentId;
    string parentGuid = ViewBag.parentGuid;
    string subTable = ViewBag.Table;
    CurrentUser currentUser = ViewBag.CurrentUser;
    string actionName = this.ViewContext.RouteData.Values["action"].ToString();
    IEnumerable<dynamic> fieldDataList = Html.GetFieldList(subTable, actionName, currentUser, (FromEnum)ViewBag.From, true);
    string from = Request.QueryString["_from"];
    string guid = Html.Guid();
}
@if (fieldDataList.Count() > 0)
{
    <script src="~/Incs/UEditor/ueditor.config.js" type="text/javascript"></script>
    <script src="~/Incs/UEditor/ueditor.all.min.js" type="text/javascript"></script>
    <div class="main">
        <form method="post" id="_form" class="form-group-sm form ui-validForm" enctype="multipart/form-data" data-custom-params="beforeSubmit:'BeforeSubmit'" data-source="@Model">
            @{
                string beforeSubmitJs = "";
                var data = new ViewDataDictionary();
                data.Add("currentUser", currentUser);
                data.Add("guid", guid);
                data.Add("from", from);
                data.Add("i", 0);
                int i = 0;
                foreach (var row in fieldDataList)
                {
                    data["i"] = i;
                    beforeSubmitJs += row.BeforeSubmitJs;
                    Html.RenderPartial("~/Areas/E/Views/CustomForm/FormPartial.cshtml", (object)row, data);
                    i++;
                }
            }
            <div class="form-group">
                <div class="controls">
                    @Html.AntiForgeryToken()
                    <input type="hidden" name="_fieldId" value="@fieldId" />
                    <input type="hidden" name="ParentId" id="ParentId" value="@parentId" />
                    <input type="hidden" name="ParentGuid" id="ParentGuid" value="@parentGuid " />
                    <input type="hidden" name="Id" id="Id" value="0" />
                    <input type="hidden" name="Guid" id="Guid" value="@guid" />
                    <input type="hidden" name="_SubtableType" id="_SubtableType" value="multiple" />
                    <input type="hidden" name="_AttachmentFields" id="_AttachmentFields" value="@Html.GetAttachmentFields(subTable)" />
                    <button type="button" class="btn btn-sm  btn-primary ui-pagePost" data-custom-params="url:'@Url.ActionUrl()',callBack:'PostBack'">提交</button>
                    <button type="button" class="btn btn-sm btn-default ui-closeDialog">关闭</button>
                </div>
            </div>
        </form>

    </div>
    <script type="text/javascript">
    var hasError=false;
    var $parentGuid = parent.$("#parentGuid");
    if ($parentGuid.length == 0) {
        $(".main").html("父页面缺少parentGuid对象");
        hasError = true;
    }
    $("#ParentGuid").val($parentGuid.val());


    //判断是否和父级页面域名一样
    if (parent.location == self.location) {
        $(".main").html("无效的请求！");
        hasError = true;
    }
    if (document.domain != parent.document.domain) {
        $(".main").html("非法请求！");
        hasError = true;
    }

    var id = Request("id");
    if (id!="")
    {
        $("#_form").FormBind({ dataSourceFrom: "attr" })
    }
    function BeforeSubmit() {
         @Html.Raw(beforeSubmitJs)
        return true;
    }

    function PostBack()
    {
        PostRefreshBack();
    }
    </script>
}