@{
    CurrentUser user = ViewBag.CurrentUser;
    IEnumerable<dynamic> fieldData = Html.GetFieldList((string)ViewBag.Table, "list", user, PageAdmin.Common.FromEnum.Member);
    int tableId = ViewBag.TableId;
    dynamic permissions = ViewBag.Permissions;
}
<style>
    .item-isOnline-0 td, item-isExpire-1 td {
        color: #999
    }

        .item-isOnline-0 td a, item-isExpire-1 td a {
            color: #999
        }
</style>
<div class="current-location">
    <ul>
        <li class="left"><i class="glyphicon glyphicon-home"></i><a href="javascript:void(0)" onclick="location.href=location.href">@ViewBag.TableRemark</a></li>
        <li class="right"><a href="javascript:void(0)" onclick="LoadData()" title="刷新"><span class="glyphicon glyphicon-refresh"></span></a></li>
    </ul>
</div>

<div class="main">
    <ul class="navTabs">
        <li data-tab-params="tabName:'全部',tabValue:'',tabValueObjName:'#state'"></li>
        <li data-tab-params="tabName:'已审核',tabValue:'1',tabValueObjName:'#state'"></li>
        <li data-tab-params="tabName:'待审核',tabValue:'0',tabValueObjName:'#state'"></li>
        <li data-tab-params="tabName:'退回',tabValue:'-1',tabValueObjName:'#state'"></li>
        @*<li data-tab-params="tabName:'草稿',tabValue:'-2',tabValueObjName:'#state'"></li>*@
    </ul>
    @*<table class="table table-bordered">
        <tr>
            <td class="leftNav @(ViewBag.Table)-leftNav">
                <div class="leftNav-body">
                    <ul id="columnList">
                        <li class="current"  is_final="1" data-columnId="">所有栏目<i class="fa fa-fw fa-angle-down"></i></li>
                        <li class="item list-template-item parent_{parentGuid}" id='item_{id}' data-columnId="{id}"  data-parentId="{parentId}" data-id="{id}" data-isFinal="{isFinal}">
                            {space}<span class="node node_{isFinal}"></span><span>{name}</span><i class="fa fa-fw fa-angle-down"></i>
                        </li>
                    </ul>
                </div>
            </td>
            <td>*@

    <table border=0 cellpadding=0 cellspacing=0 class="tb-head form-inline form-group-sm" id="tbHead">
        <tr>
            @if (permissions.CanAdd == 1)
            {
                <td>
                    <button class="ui-dialog btn btn-sm btn-primary btn-add hidden" data-custom-params="title:'添加',url:'@Url.Action("add")?table=@ViewBag.Table'"><i class="glyphicon glyphicon-plus"></i>添加</button>
                </td>
            }
            <td align="right">
                <div class="controls form-inline form-group-sm">
                    <input type="hidden" id="_custom_where" name="_custom_where" class="ui-queryString" />
                    <input type="hidden" id="state" name="state" class="ui-queryString" value="" />
                    <select name="columnId" id="columnId" class="form-control ui-queryString" data-custom-params="isTree:true" data-source="@Html.GetColumnListJsonByUser(tableId,user,PageAdmin.Common.FromEnum.Member)">
                        <option value="">所属栏目</option>
                    </select>
                    <select name="_attr" id="_attr" class="form-control ui-queryString hidden">
                        <option value="">信息属性</option>
                        <option value="isGood">推荐</option>
                        <option value="isHot">热门</option>
                        <option value="isTop">置顶</option>
                        @*<option value="notOnline">待上线</option>*@
                        <option value="isExpire">已到期</option>
                        @*<option value="notReply">未回复</option>
                            <option value="hasThumbnail">带缩略图</option>*@
                    </select>
                    <select id="order" class="ui-queryString form-control hidden">
                        <option value="">排序方式</option>
                        <option value="id desc">按id↓</option>
                        <option value="id asc">按id↑</option>
                        @Html.Partial("~/Areas/E/Views/CustomForm/OrderFieldPartial.cshtml", fieldData)
                        <option value="clicks asc">按点击↑</option>
                        <option value="clicks desc">按点击↓</option>
                    </select>
                    <select id="s_field" name="s_field" class="btn-xs ui-setName form-control">
                        @Html.Partial("~/Areas/E/Views/CustomForm/SearchFieldPartial.cshtml", fieldData)
                    </select>
                    <input type="text" placeholder="搜索关键词" class="form-control ui-keyword ui-queryString">
                    <button type="button" class="btn btn-default btn-sm ui-queryString-search">搜索</button>
                </div>
            </td>
        </tr>
    </table>
    <table border=0 cellpadding=0 cellspacing=0 class="table table-bordered" id="tb_list">
        <thead>
            <tr>
                @foreach (var item in fieldData)
                {
                    <td align="center">@item.Remark</td>
                }

                <td align="center">属性</td>
                <td align="center" @if (permissions.CanEdit == 0 && permissions.CanDelete == 0) { <text> class="hidden" </text> }>操作</td>
            </tr>
        </thead>
        <tr class="item list-template-item item-isOnline-{isOnline} item-isExpire-{isExpire}" id="item_{id}">
            @foreach (var item in fieldData)
            {
                int listWords = item.ListWords;
                int fieldId = item.Id;
                string field = item.Name;
                string fieldType = item.FieldType;
                string valueType = item.ValueType;
                string align = "center";
                string remark = item.Remark;
                if (valueType == "nvarchar" || valueType == "text")
                {
                    if (fieldType != "image")
                    {
                        align = "left";
                    }
                }
                <td align="@align">
                    @if (field == "title")
                    {<text>
                            [<span class="sp_column_id sp_values" data-custom-params="fieldType:'text',valueType:'nvarchar',listWords:'10'">{columnId}</span>]
                    </text>
                }
                    @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>
                    }
                    @if (field == "title")
                    {
                        <span class="glyphicon glyphicon-eye-open ui-infoPreview" onclick="Preview('@ViewBag.Table','{id}')" title="预览"></span>
                    }

                </td>
            }
            <td align="center">
                <div class="dropdown dropdown-hover">
                    <a class="dropdown-toggle" data-toggle="dropdown">
                        <span class="sp_state" data-state="{state}"></span>
                        <span class="caret"></span>
                    </a>
                    <div class="dropdown-menu item-detail pull-right" role="menu">
                        <table class="table" style="width:350px">
                            <tr>
                                <td>相关属性：<span class="sp__attr" data-attr-params="publish_from:'{publishFrom}',has_thumbnail:'{hasThumbnail}',is_good:'{isGood}',is_hot:'{isHot}',is_top:'{isTop}',is_online:'{isOnline}',is_expire:'{isExpire}'"></span></td>
                            </tr>
                            <tr>
                                <td>点击：{clicks}</td>
                            </tr>
                        </table>
                    </div>
                </div>


            </td>
            <td align="center" @if (permissions.CanEdit == 0 && permissions.CanDelete == 0) { <text> class="hidden" </text> }>
                <input type="hidden" name="id" id="id" value="{id}" />
                <div class="btn-group btn-group-sm">
                    @if (permissions.CanEdit > 0)
                    {
                        <button type="button" data-custom-params="title:'修改',url:'@Url.Action("Edit")?table=@ViewBag.Table&id={id}',beforeExecute:'EditBefore'" data-state="{state}" class="btn btn-default btn-sm ui-dialog">修改</button>
                    }    @if (permissions.CanDelete > 0)
                    {
                        <button type="button" data-custom-params="url:'@Url.Action("Delete")?table=@(ViewBag.Table)',callBack:'LoadData',beforeExecute:'DeleteBefore'" data-state="{state}" class="btn btn-default btn-sm ui-deleteSingle">删除</button>
                    }
                </div>
            </td>
        </tr>
    </table>
    @*</td>
        </table>*@
</div>
<script src="~/Areas/Member/Js/infoData.js"></script>
<script type="text/javascript">
    $(".navTabs").children().InitTab(function (){LoadData()}); //初始化选项卡，并设置回调函数为LoadData()
    var maxNum =@permissions.MaxNum;
    var firstLoad = 0;
    function LoadedInit() {
        var $spColumnId = $(".sp_column_id");
        $spColumnId.ConvertText({ emptyStr: "-" }, $("#columnId").attr("data-source"))
        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));
                    }
                }
            }
        });
        var $sp_state = $(".sp_state");
        $sp_state.each(function () {
            var $this = $(this);
            var state = $this.attr("data-state");
            var stateName = "";
            switch (state) {
                case "1":
                    if (stateName == "") {
                        stateName = "已审";
                        $this.addClass("text-success");

                    }
                    break;
                case "0":
                    if (stateName == "") {
                        stateName = "待审";
                        $this.addClass("text-warning");
                    }
                    break;
                case "-1":
                    if (stateName == "") {
                        stateName = "退稿";
                        $this.addClass("text-light");
                    }
                    break;

                case "-2":
                    stateName = "草稿";
                    $this.addClass("text-muted");
                    break;
            }
            $this.html(stateName)
        });

        var $sp__attr = $(".sp__attr");
        $sp__attr.each(function () {
            var $this = $(this);
            var attrs = "";
            var params = $this.attr("data-attr-params")
            params = ExtendObject(params);
            if (params.has_thumbnail == "1") {
                attrs += "<span title='带缩略图'>[图]</span> &nbsp;"
            }
            if (params.is_good != "0") {
                attrs += "<span title='推荐信息'>[" + params.is_good + "级推荐]</span> &nbsp;"
            }
            if (params.is_hot != "0") {
                attrs += "<span title='热门信息'>[" + params.is_hot + "级热门]</span> &nbsp;"
            }
            if (params.is_top != "0") {
                attrs += "<span title='置顶信息'>[" + params.is_top + "级置顶]</span> &nbsp;"
            }
            if (params.is_online == "0") {
                attrs += "<span title='待上线信息'>[待上线]</span> &nbsp;"
            }
            if (params.is_expire == "1") {
                attrs += "<span title='过期信息'>[已到期]</span> &nbsp;"
            }
            if (attrs == "") {
                attrs = "";
            }
            $this.html(attrs);
        });
        $(".dropdown-hover").DropdownHover();
        if (firstLoad == 0)
        {
            var $btnAdd = $(".btn-add");
            if (maxNum == 0) {
                $btnAdd.removeClass("hidden");
            }
            else if (maxNum > 0) {
                $lbRecordcount = $(".lb_recordcount");
                var currentNum = $lbRecordcount.attr("data-recordcount");
                if (currentNum < maxNum) {
                    $btnAdd.removeClass("hidden");
                }
            }
        }
        firstLoad = 1;
    }

    function LoadData() {
        var ajaxUrl = "@Url.Action("PageListData")?table=@ViewBag.Table";
        $("#tb_list").ListBind({ url: ajaxUrl, callBack: "LoadedInit" });
    }
    LoadData();

    function LoadColumn() {
        $("#columnId").SelectBind({ dataSourceFrom: "attr" });
    }
    LoadColumn();


    function EditBefore(thisObj)
    {
        var canEdit = @permissions.CanEdit;
        var state = thisObj.attr("data-state");
        if (canEdit == 1 && state == 1)
        {
            Alert("对不起，当前信息已通过审核，您没有权限修改！")
            return false;
        }
        //return true;
    }

    function DeleteBefore(thisObj)
    {
        var canDelete = @permissions.CanDelete;
        var state = thisObj.attr("data-state");
        if (canDelete == 1 && state == 1) {
            Alert("对不起，当前信息已通过审核，您没有权限删除！")
            return false;
        }
        //return true;
    }
    //function RegLeftMenu() {
    //    var $columnListItem = $("#columnList").children("li");
    //    $columnListItem.click(function () {
    //        var $this = $(this);
    //        var isFinal = $this.attr("is_final")
    //        var columnId = $this.attr("data-columnId");
    //        $this.addClass("current").siblings().removeClass("current");
    //        $("#column_id").val(columnId);
    //        LoadData();
    //    });
    //}

</script>
