@using PageAdmin.Utils
@{
    dynamic permissionsSet = ViewBag.PermissionsSet;
    CurrentUser user = ViewBag.CurrentUser;
    int tableId = ViewBag.TableId;
    string table = ViewBag.Table;
    string action = ViewContext.RouteData.Values["action"].ToString().ToLower();
    int id = StringHelper.Format<int>(Request.QueryString["id"]);
    IEnumerable<dynamic> fieldData = Html.GetFieldList((string)ViewBag.Table, action, user, PageAdmin.Common.FromEnum.Member);
    IEnumerable<dynamic> subFieldData = fieldData.Where(c => c.FieldType == "subtable");
    ICollection<int> subtableIframeIdList = new List<int>();
    var guid = Html.Guid();
    int historyColumnId = StringHelper.Format<int>(Request.QueryString["columnId"]);
    var columnTree = Html.GetColumnTree(tableId, user, PageAdmin.Common.FromEnum.Member);
}
<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" id="main" v-cloak>
    <el-tabs v-model="activeName" type="card" v-on:tab-click="handleTabClick">
        <el-tab-pane label="内容设置" name="1">
            <el-form label-width="150px" label-position="left" size="medium">
                <el-form-item label="所属栏目*" v-show="ignoreColumn==false">
                    <ui-cascader v-model="formData.ColumnId" placeholder="请选择栏目"  style="width:50%"  :options="@JsonHelper.JsonParse(columnTree)" :props="{ expandTrigger: 'hover',multiple:false }" v-validate="{data:formData.ColumnId,dataType:'*',min:1,nullMsg:'请选择栏目!',ignore:ignoreColumn}"></ui-cascader>
                    <span class="help-block"></span>
                </el-form-item>
                @{
                    string beforeSubmitJs = "";
                    var data = new ViewDataDictionary();
                    data.Add("from", PageAdmin.Common.FromEnum.Member);
                    data.Add("currentUser", user);
                    data.Add("guid", guid);
                    data.Add("fieldPrefix", "formData");
                    data.Add("i", 0);
                    int i = 0;
                    if (fieldData != null)
                    {
                        foreach (var row in fieldData.Where(c => c.FieldType != "subtable"))
                        {
                            data["i"] = i;
                            beforeSubmitJs += row.BeforeSubmitJs;
                            Html.RenderPartial("~/Areas/E/Views/CustomForm/FormPartial.cshtml", (object)row, data);
                            i++;
                        }
                    }
                }
                @if (permissionsSet.CanSetSubmitState == 1)
                {
                    <el-form-item label="信息状态" v-if="formData.Id==0 || currentState<0">
                        <el-radio-group v-model="formData.State">
                            <el-radio :label="1">送审</el-radio>
                            <el-radio :label="-1" v-if="currentState==-1">已退回</el-radio>
                            <el-radio :label="-2">草稿</el-radio>
                        </el-radio-group>
                        <span class="help-block"></span>
                    </el-form-item>
                }
            </el-form>
        </el-tab-pane>
        @foreach (var fieldItem in subFieldData)
            {
            subtableIframeIdList.Add(fieldItem.Id);
            <el-tab-pane label="@fieldItem.Remark" name="subtable_@(fieldItem.Id)" style="width:100%;height:100%">
                <iframe data-src="/E/SubData/@(fieldItem.SubTableType=="single"?"set":"index")/?parentguid=@guid&parentId=@id&fieldId=@fieldItem.Id&isTabIframe=1" id="subtableIframe@(fieldItem.Id)" name="subtableIframe@(fieldItem.Id)" style="width:100%" frameborder="0"></iframe>
            </el-tab-pane>
        }
    </el-tabs>
    <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>
        <el-button onclick="closeSelf(parent.loadData)">返回</el-button>
    </div>
</div>

<script type="text/javascript">
    var action = "@action";
    var postUrl = "@Url.ActionUrl()";
    var currentState = 1;
    var formData = @Html.RawModel((string)Model);
    formData.Guid = "@guid";
    if ("@action"=="add")
    {
        formData.State = 1;
        //刷新后保存上次的栏目id，避免再次选择
        var historyColumnId = @historyColumnId;
        if (historyColumnId!=0){
            formData.ColumnId = historyColumnId ;
        }
    }
    currentState = formData.State;
    //提交前执行
    function beforeSubmitExecute() {
        @Html.Raw(beforeSubmitJs)
        return true;
    }
      var vue= new Vue({
          el: "#main",
          data: {
              currentState: currentState,
              activeName: "1",
              formData: formData
          },
         computed: {
              ignoreColumn: function ()
              {
                  return @(columnTree.Count())<=0;
              }
          },
         methods: {
             handleTabClick: function (tab, event) {
                 var activeName = this.activeName;
                 if (activeName.indexOf("subtable_") >= 0) {
                     var $iframe = document.getElementById("subtableIframe" + this.activeName.replace("subtable_", ""));
                     if ($iframe == null) {
                         return;
                     }
                     else {
                         $iframe.height = document.documentElement.clientHeight - 100 - 30;
                     }
                     if (!isNullOrEmpty($iframe.src)) { return; }
                     var dataSrc = $iframe.getAttribute("data-src");
                     $iframe.src = dataSrc;
                 }
             },
             postSubmit: function () {
                 var _this = this;
                  var validated = this.validateForm({ beforeSubmit: function () { return beforeSubmitExecute() } });//表单验证
                  if (validated) {
                      validated = validateIframe();//验证附属表
                  }
                  else {
                     this.activeName = "1";
                  }
                  if (validated) {
                      var callback = function () {
                          _this.submit({ url: postUrl, data: formData, success: function () { callBack() } });
                      }
                      postAllForm(callback);
                  }
              },
          },
    });

    function callBack() {
            if (action == "add") {
                location.href = "@Url.ActionUrl("Add")?table=@table&columnId=" +formData.ColumnId;
            }
      }

    var subtableIframeIds = @Html.Raw(JsonHelper.JsonParse(subtableIframeIdList));//附属表框架集合
      ///检测iframe中的输入
      function validateIframe() {
          for (var i = 0; i < subtableIframeIds.length; i++) {
              var $iframe = document.getElementById("subtableIframe" + subtableIframeIds[i]);
              if ($iframe == null) { continue; }
              if ($iframe.contentWindow.vue == undefined) { continue; }
              var ifameValidate = $iframe.contentWindow.vue.validate;
              if (ifameValidate != undefined) {
                  var activeName = "subtable_" + subtableIframeIds[i];
                  var result = ifameValidate();
                  if (!result) {
                      vue.activeName = activeName;
                      return result;
                  }
              }
          }
          return true;
      }
      var subPostIdx = 0;
      //提交子框架中的表单
      function postAllForm(mainCallback) {
          if (subPostIdx == subtableIframeIds.length) {
              mainCallback();
              subPostIdx = 0;
              return;
          }
          var $iframe = document.getElementById("subtableIframe" + subtableIframeIds[subPostIdx]);
          var executing = false;
          if ($iframe != null && $iframe.contentWindow.submit != undefined) {
                  executing = true;
                  var callback = function () {
                      subPostIdx++;
                      postAllForm(mainCallback);
                  }
                  $iframe.contentWindow.submit(callback); //回调继续提交下一个
          }
          if (!executing) {//没有submit方法则继续下一个
              subPostIdx++;
              postAllForm(mainCallback)
          }
      }
</script>
