@using PageAdmin.Common;using PageAdmin.ORM;using PageAdmin.Utils;
@{
    dynamic fieldData = ViewBag.FieldData;
    int fieldId = fieldData.id;
    string fieldType = fieldData.field_type;
    string mimeTypes = "image/jpg,image/jpeg,image/png,image/gif";
    if (fieldType == "files")
    {
        mimeTypes = "*/*";
    }
    string field = fieldData.name;
    string parentTable = fieldData.table_name;
    int fileNumLimit = fieldData.max_num;
    string attachmentTable = ViewBag.AttachmentTable;
}

<style type="text/css">
    body {
        background: transparent;
    }

    .thumbnail {
        max-width: 100px;
        height: 30px;
    }
</style>
<div>
    <table border=0 cellpadding=0 cellspacing=0 class="tb-head form-inline form-group-sm">
        <tr>
            <td>
                <div class="control-group">
                    <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)" onclick="Add()">添加</a></li>
                            <li class="divider"></li>
                            <li><a href="javascript:void(0)" class="ui-updateMultiple" data-custom-params="url:'@Url.Action("UpdateList")?table=@attachmentTable',callBack:'LoadData',successMsg:'更新成功!'">更新</a></li>
                            <li class="divider"></li>
                            <li><a href="javascript:void(0)" class="ui-deleteMultiple" data-custom-params="url:'@Url.Action("Delete")?table=@attachmentTable',callBack:'LoadData'">删除</a></li>
                        </ul>
                    </div>
                </div>
            </td>
        </tr>
    </table>
    <table border=0 cellpadding=0 cellspacing=0 class="table table-bordered 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>
                <td align="center">文件</td>
                <td align="center">文件名</td>
                @*<td align="center">简介</td>*@
                <td align="center">大小</td>
                <td align="center">序号</td>
                <td align="center">删除</td>
            </tr>
        </thead>
        <tr class="item list-template-item" id="item_{id}">
            <td align="center"><input type="checkbox" class="checked-item" value="{id}"></td>
            @if (fieldType == "images")
            {
                <td align="center">
                    <a href="{path}" target="_blank"><img src="{thumbnail}" class="thumbnail" border="0" /></a>
                </td>
            }
            else
            {
                <td align=left>
                    <a href="#" target="_blank" data-path="{path}" class="path">
                        <span class="glyphicon glyphicon-paperclip"></span>
                    </a>
                </td>
            }

            <td align="center"><input type="text" name="title" id="title" maxlength="100" value="{title}" class="form-control form-control-sm"></td>
            @*<td align="center"><textarea name="content" id="content" style="height:50px;width:95%" class="form-control"></textarea></td>*@
            <td align="center">{filesize}kb</td>
            <td align="center">
                <input type="text" name="xuhao" id="xuhao" class="ui-listXuhao form-control  form-control-sm"  data-table="@attachmentTable" data-id="{id}" value='{xuhao}' style="width:50px" maxlength="3" />
            </td>
            <td align="center">
                <input type="hidden" name="guid" value="{guid}" />
                <input type="hidden" name="id" value="{id}" />
                <a class="btn btn-default btn-sm ui-deleteSingle" data-custom-params="url:'@Url.Action("Delete")?table=@attachmentTable',callBack:'LoadData'">删除</a>
            </td>
        </tr>
    </table>
</div>

<script type="text/javascript">
    var hasError=false;
    var field = "@field";
    var $parentObj = parent.$("#" + field);
    var $parentGuid = parent.$("#guid");
    var $parentId = parent.$("#id");
    if ($parentObj.length == 0 || $parentGuid.length == 0 && $parentObj==0)
    {
        $(".main").html("父级对象缺少必须的参数");
        hasError=true;
    }

    var parentGuid=$parentGuid.val();
    var parentId=$parentId.val();
    if(parentGuid.length<32)
    {
        $(".main").html("父级页面的guid参数错误");
        hasError=true;
    }
    if(!IsInt(parentId))
    {
        $(".main").html("父级页面的id参数错误");
        hasError=true;
    }
    //判断适合和父级页面域名一样
    if(parent.location==self.location)
    {
        $(".main").html("无效的请求！");
        hasError=true;
    }
    if (document.domain!=parent.document.domain) {
        $(".main").html("非法请求！");
        hasError=true;
    }
    var $tb_list = $("#tb_list");
    var itemsNum = 0;
    var ajaxUrl = "@Url.Action("GetList")?parentTable=@parentTable&parentField=@field&parentGuid="+parentGuid+"&parentId="+parentId;

    var parentobj = parent.document.getElementById(field);
    var IFrameBox = window.parent.document.getElementById("iframe_" + field + "_box");
    var IFrame = window.parent.document.getElementById("iframe_" + field);
    var parentAddBtn = window.parent.document.getElementById("addBtn_" + field);

    function AutoHeight() {
        if (IFrameBox == null) {
            //$("body").html("无效参数!");
        }
        IFrameBox.style.display = "";
        var scrollHeight = parseInt(document.body.scrollHeight);
        var offsetHeight = parseInt(document.body.offsetHeight);
        var height;
        var isIE = document.all && window.external;
        if (isIE) {
            height = scrollHeight;
        }
        else {
            height = scrollHeight < offsetHeight ? scrollHeight : offsetHeight;
        }
        if (itemsNum == 0) {
            IFrameBox.style.display = "none";
            parentAddBtn.style.display = "";
        }
        else {
            IFrameBox.style.display = "";
            parentAddBtn.style.display = "none"
        }
        IFrame.style.height = height + "px";
        parentobj.value = itemsNum; //设置父级页面字段值为已经上传的文件个数
    }

    function LoadData() {
        $tb_list.ListBind({url:ajaxUrl, callBack: "LoadedInit",showFooter:false});
    }
    function LoadedInit()
    {
        itemsNum = $tb_list.find("tr.item").length;
        AutoHeight();
        $path = $(".path");
        $path.each(function () {
            var $this = $(this);
            var path = $this.attr("data-path");
            var arrPath = path.split("/");
            $this.attr("href", path);
            $this.children().eq(0).text(arrPath[arrPath.length - 1]);
        });
    }
    if(!hasError)
    {
        LoadData();
    }

    //判断还可以发布的数量
    var fileNumLimit = @fileNumLimit; //文件总数限制
    var cannums = parseInt(fileNumLimit) - parseInt($(parentobj).val());

    function Add() {
        var cannums = parseInt(fileNumLimit) - itemsNum;
        if (cannums <= 0 && fileNumLimit != "0") {
            Msg("最多只能发布" + fileNumLimit + "个文件！");
            return;
        }
        parentAddBtn.click();
    }

    function Edit(id)
    {
        var url="@Url.Action("index","UploadPanel",new { area = "" })?objid=@field&delid="+id;
        var dialogParams={title:"修改文件",area: ["70%","400px"],url:url,target:"self"};
        parent.IDialog(dialogParams);
    }

</script>