@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;
    dynamic parentFieldData = ViewBag.FieldData;
    IEnumerable<dynamic> fieldDataList = Html.GetFieldList(subTable, "list", currentUser, (FromEnum)ViewBag.From);
    string parentField = StringHelper.FirstCharUpper(parentFieldData.Name);
    int fileNumLimit = parentFieldData.MaxNum;
    string order = parentFieldData.Orderby;
    int pageSize = parentFieldData.Pagesize;
    string from = Request.QueryString["_from"];
}
<div class="main">
    <table border=0 cellpadding=0 cellspacing=0 class="tb-head form-inline form-group-sm">
        <tr>
            <td>
                @if (currentUser.RoleId > 0)
                {
                    <div class="btn-group left">
                        <button type="button" class="btn btn-default btn-sm ui-selectInvert">反选</button>
                        <button type="button" class="btn btn-default dropdown-toggle btn-sm" data-toggle="dropdown" aria-expanded="false">
                            菜单
                            <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu" role="menu">
                            <li><a href="javascript:void(0)" class="ui-dialog" data-custom-params="title:'添加',url:'@Url.Action("Add")?parentGuid=@parentGuid&parentId=@parentId&_fieldId=@fieldId&_from=@from',width:'95%',height:'95%',beforeExecute:'CheckLimitNum'">添加</a></li>
                            <li class="divider"></li>
                            <li><a href="javascript:void(0)" class="ui-deleteMultiple" data-custom-params="url:'@Url.Action("Delete")',eachCallBack:'LoadData'">删除</a></li>
                        </ul>
                    </div>
                }
                else
                {
                    <a href="javascript:void(0)" class="ui-dialog btn btn-primary btn-sm " data-custom-params="title:'添加',url:'@Url.Action("Add")?parentGuid=@parentGuid&parentId=@parentId&_fieldId=@fieldId&_from=@from',width:'95%',height:'95%',beforeExecute:'CheckLimitNum'"><i class="glyphicon glyphicon-plus"></i>添加</a>
                }
            </td>

            <td align="right">
                <input type="hidden" id="_from" name="_from" class="ui-queryString" value="@from">
                <input type="hidden" id="_fieldId" name="_fieldId" class="ui-queryString" value="@fieldId">
                <input type="hidden" id="parentId" name="parentId" class="ui-queryString" value="@parentId">
                <input type="hidden" name="parentGuid" id="parentGuid" class="ui-queryString" value="@parentGuid" />
                <input type="hidden" id="_pagesize" name="_pagesize" class="ui-queryString" value="@pageSize">
                @if (currentUser.RoleId > 0)
                {
                    <select id="order" class="ui-queryString form-control">
                        <option value="">排序方式</option>
                        <option value="id desc">按id↓</option>
                        <option value="id asc">按id↑</option>
                        @Html.Partial("~/Areas/E/Views/CustomForm/OrderFieldPartial.cshtml", fieldDataList)
                    </select>
                    <select id="_field" name="_field" class="btn-xs ui-queryString ui-setName form-control">
                        <option value="username">用户名</option>
                        @Html.Partial("~/Areas/E/Views/CustomForm/SearchFieldPartial.cshtml", fieldDataList)
                    </select>
                    <input type="text" id="_keyword" name="_keyword" class="ui-queryString ui-keyword form-control" style="width:120px" placeholder="输入关键词">
                    <input type="button" value="确定" class="btn btn-default btn-sm ui-queryString-search">}
            </td>

        </tr>
    </table>
    <table border=0 cellpadding=0 cellspacing=0 class="table table-bordered form-inline form-group-sm" id="tb_list">
        <thead>
            <tr>
                <td align="center" style="width:40px"><input type="checkbox" title="选择" class="ui-selectInvert" data-custom-params="isReverse:false"></td>
                @foreach (var item in fieldDataList)
                {
                    <td align="center">@item.Remark</td>
                }
                <td align="center">发布时间</td>
                <td align="center">发布人</td>
                <td align="center" width="150px">管理</td>
            </tr>
        </thead>
        <tr class="item list-template-item" id="item_{id}">
            <td align="center"><input type="checkbox" value="{id}" class="checked-item id_{id}"></td>
            @foreach (var item in fieldDataList)
            {
                int listWords = item.ListWords;
                string listWidth = item.ListWidth;
                if (listWidth != "")
                {
                    listWidth = "style=\"width:" + listWidth + ";\"";
                }
                string id = item.Id.ToString();
                string field = item.Name;
                string fieldType = item.FieldType;
                string valueType = item.ValueType;
                string align = "center";
                if (valueType == "nvarchar" || valueType == "ntext")
                {
                    if (fieldType != "image")
                    {
                        align = "left";
                    }
                }
                @*if (fieldType == "subtable")
                        {
                            string subTable = ViewBag.Table + "_" + field;
                            <td align="@align">
                                <button type="button" class="btn btn-default btn-sm ui-dialog" data-custom-params='title:"@(item.remark)-管理",url:"@Url.Action("Index","SubTableData")?table=@subTable&field_id=@fieldId&parent_guid={guid}&parent_id={id}&objId=@(field)_{id}&from=admin",width:"@item.win_width",height:"@item.win_height",target:"self"'>管理(<span id="@(field)_{id}" class="small">{@field}</span>)</button>
                            </td>
                        }
                        else if (fieldType == "files" || fieldType == "images")
                        {
                            string attTable = ViewBag.Table + "_" + field;
                            <td align="@align">
                                <button type="button" class="btn btn-default btn-sm ui-dialog" data-custom-params='title:"@(item.remark)-管理",url:"@Url.Action("Admin","AttachmentData")?table=@attTable&field_id=@fieldId&parent_guid={guid}&parent_id={id}&objId=@(field)_{id}&fieldType=@(fieldType)&from=admin",width:"@item.win_width",height:"@item.win_height",target:"self"'>管理(<span id="@(field)_{id}" class="small">{@field}</span>)</button>
                            </td>
                    }*@
                <td align="@align" @Html.Raw(listWidth)>
                    @if (new string[] { "images", "files", "subtable" }.Contains(fieldType))
                    {
                        <span class="sp_@field sp_values" data-value-params="id:{id},fieldId:@fieldId,name:'@field',fieldType:'@fieldType',valueType:'@valueType',listWords:'@listWords'"><i class="fa fa-spinner fa-spin"></i></span>
                    }
                    else
                    {
                        <span class="sp_@field sp_values" data-value-params="id:{id},fieldId:@fieldId,name:'@field',fieldType:'@fieldType',valueType:'@valueType',listWords:'@listWords'">{@field}</span>
                    }
                </td>
            }
            <td align="center">{thedate}</td>
            <td align="center">{username}</td>
            <td align="center">
                <input type="hidden" name="id" value="{id}" />
                <input type="hidden" name="_fieldId" value="@fieldId" />
                <input type="hidden" name="parentId" value="@parentId" />
                <div class="btn-group btn-group-sm">
                    <button type="button" class="btn btn-default btn-sm ui-dialog" data-custom-params='title:"@ViewBag.TableRemark-修改",url:"@Url.Action("Edit")?id={id}&parentid={parentId}&parentGuid={parentGuid}&_fieldId=@fieldId&_from=@from&",width:"95%",height:"95%",target:"self"'>修改</button>
                    <button type="button" class="btn btn-default btn-sm ui-deleteSingle" data-custom-params="url:'@Url.Action("Delete")?table=@subTable',callBack:'LoadData'">删除</button>
                </div>
            </td>
        </tr>
    </table>
</div>
<script type="text/javascript">
    var hasError = false;
    var fileNumLimit = @fileNumLimit;
    var recordCount = 0;
    var field = "@parentField";

    var $parentGuid = parent.$("#Guid");
    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;
    }

    function init() {
        var $sp_values = $(".sp_values")
        $sp_values.each(function () {
            var $this = $(this);
            var thisHtml = $this.html();
            var params = $this.attr("data-value-params")
            params = ExtendObject(params);
            var parentId = params.id;
            var listWords = parseInt(params.listWords);
            var valueType = params.valueType;
            var fieldType = params.fieldType;
            var field = params.name;
            var fieldId = params.fieldId;
            if (fieldType == "subtable") {
                $.get("/E/SubData/Count", { _fieldId: fieldId, parentId: parentId }, function (data) { $this.text(data) });
            }
            else if (fieldType == "images" || fieldType == "files") {
                $.get("/E/AttachmentsData/Count", { _fieldId: fieldId, parentId: parentId }, function (data) { $this.text(data) });
            }
            else if (params.fieldType == "image" && thisHtml != "") {
                $this.html("<a href=\"" + thisHtml + "\" target=\"_blank\"><img src=\"" + thisHtml + "\" class=\"ui-thumbnail thumbnail\"></a>");
            }
            else if (valueType == "nvarchar" || valueType == "ntext" || valueType == "datetime") {
                if (thisHtml.length > listWords) {
                    if (field == "title") {
                        $this.html(thisHtml.Left(listWords) + "...");
                    }
                    else {
                        $this.html(thisHtml.Left(listWords));
                    }
                }
            }
        });
        SetParent();
    }

    function SetParent() {

        recordCount = $(".lb_recordcount").attr("data-recordcount");
        var $parentFieldObj = parent.$("#" + field);
        var $parentFieldLbObj = parent.$("#_" + field + "_recordCount");
        if ($parentFieldObj.length > 0) {
            $parentFieldObj.val(recordCount);
            if ($parentFieldLbObj.length > 0) {
                $parentFieldLbObj.text(recordCount);
            }
        }
    }

    var $tbList = $("#tb_list");
    var url = "@Url.Action("PageListData")"

    function LoadData() {
        $tbList.ListBind({ url: url, callBack: "LoadedInit"});
    }
    function LoadedInit() {
        init();
    }
    if (!hasError) {
        LoadData();
    }

    function CheckLimitNum()
    {
        if (fileNumLimit == 0)
        {
            return true;
        }
        else if (recordCount >= fileNumLimit)
        {
            Alert("最多只能发布" + fileNumLimit + "条数据！");
            return false;
        }
    }

</script>