@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset layui-hide" id="searchField">
            <div>
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label required">新闻类别:</label>
                            <div class="layui-input-block">
                                <select id="CategoryId" name="CategoryId" lay-filter="CategoryId" lay-search>
                                    <option value="">请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">关键字:</label>
                            <div class="layui-input-inline">
                                <input type="text" id="txt_keyword" name="txt_keyword" autocomplete="off" class="layui-input" placeholder="">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container" id="toolbar">
                <button id="NF-add" name="NF-add" authorize class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon">&#xe654;</i>新增</button>
                <button id="NF-edit" name="NF-edit" authorize class="layui-btn layui-btn-sm layui-btn-warm layui-hide" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</button>
                <button id="NF-delete" name="NF-delete" authorize class="layui-btn layui-btn-sm layui-btn-danger layui-hide" lay-event="delete"> <i class="layui-icon">&#xe640;</i>删除</button>
                <button id="NF-details" name="NF-details" authorize class="layui-btn layui-btn-sm layui-btn-normal layui-hide" lay-event="details"> <i class="layui-icon">&#xe60b;</i>查看</button>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a id="NF-edit" authorize class="layui-btn layui-btn-xs  layui-btn-warm" lay-event="edit">修改</a>
            <a id="NF-delete" authorize class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
            <a id="NF-details" authorize class="layui-btn layui-btn-xs layui-btn-normal" lay-event="details">查看</a>
        </script>
    </div>
</div>

<script>
    layui.use(['jquery', 'form', 'table', 'common', 'commonTable'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            commonTable = layui.commonTable,
            common = layui.common;
        //权限控制(js是值传递)
        currentTableBar.innerHTML = common.authorizeButtonNew(currentTableBar.innerHTML);
        toolbarDemo.innerHTML = common.authorizeButtonNew(toolbarDemo.innerHTML);
        wcLoading.close();
        commonTable.rendertable({
            elem: '#currentTableId',
            id: 'currentTableId',
            url: '/ContentManage/ArticleNews/GetGridJson',
            filter: {
                clearFilter: false,
            },
            cols: [[
                { type: "checkbox", width: 50, fixed: 'left'  },
                { field: 'F_CategoryName', title: '新闻类别', width: 120, filter: true },
                { field: 'F_Title', title: '标题', width: 160, filter: true },
                {
                    field: ' F_ImgUrl ', title: '封面', width: 60,
                    templet: function (d) {
                        if (!!d.F_ImgUrl) {
                            return '<img src="' + d.F_ImgUrl + '" style="height: 25px; width: 25px; line- height: 25 px!important;" onclick="previewImg(this)">'

                        }
                        else {
                            return '';
                        }
                    }
                },
                { field: 'F_Zhaiyao', title: '摘要', minWidth: 220, filter: true },
                { field: 'F_SortCode', title: '排序', width: 80, filter: true },
                {
                    field: 'F_IsTop', title: '置顶', width: 80, filter: true,
                    templet: function (d) {
                        if (d.F_IsTop == true) {
                            return "<span class='layui-btn layui-btn-normal layui-btn-xs'>是</span>";
                        } else {
                            return "<span class='layui-btn layui-btn-warm layui-btn-xs'>否</span>";
                        }
                    }
                },
                {
                    field: 'F_IsHot', title: '热门', width: 80, filter: true,
                    templet: function (d) {
                        if (d.F_IsHot == true) {
                            return "<span class='layui-btn layui-btn-normal layui-btn-xs'>是</span>";
                        } else {
                            return "<span class='layui-btn layui-btn-warm layui-btn-xs'>否</span>";
                        }
                    }
                },
                { field: 'F_Click', title: '点击次数', width: 120, filter: true },
                { field: 'F_Source', title: '来源', width: 120, filter: true },
                { field: 'F_Author', title: '作者', width: 120, filter: true },
                {
                    field: 'F_CreatorTime', title: '创建时间', width: 160, sort: true, filter: { type: 'date[yyyy/MM/dd HH:mm:ss]' }
                },
                { field: 'F_CreatorUserId', title: '创建人', width: 120, filter: true, hide: true },
                { title: '操作', width: 160, toolbar: '#currentTableBar', align: "center", fixed: 'right'  }
            ]]
        });
        $(function () {
            initControl();
            form.render();
        });
        function initControl() {
            //绑定数据源
            //类型为下拉框时
            $("#CategoryId").bindSelect({
                url: "/ContentManage/ArticleCategory/GetListJson",
                id: "F_Id",
                text: "F_FullName",
                search: true,
            });
        }
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            commonTable.reloadtable({
                elem: 'currentTableId',
                curr: 1,
                where: { CategoryId: data.field.CategoryId, keyword: data.field.txt_keyword }
            });
            return false;
        });
        //行点击事件监听，控制按钮显示
        var oneList = ["NF-edit", "NF-details"];//选择1条显示
        var morerList = ["NF-delete"];//选中1条以上显示
        commonTable.tableRowClick("checkbox", "currentTableFilter", "currentTableId", oneList, morerList);
        //toolbar监听事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            var data = table.checkStatus('currentTableId').data;
            var id = data.length > 0 ?data[0].F_Id:null;
            if (obj.event === 'add') {  // 监听添加操作
                common.openNewTabByIframe({
                    title: "添加文章",
                    href: "/ContentManage/ArticleNews/Form",
                });
            }
            else if (obj.event === 'delete') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                var ids = [];
                for (var i = 0; i < data.length; i++) {
                    ids.push(data[i].F_Id);
                }
                common.deleteForm({
                    url: "/ContentManage/ArticleNews/DeleteForm",
                    param: { keyValue: ids.join(',') },
                    success: function () {
                        common.reload('data-search-btn');
                    }
                });
            }
            else if (obj.event === 'edit') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                if (data.length > 1) {
                    common.modalMsg("只能选择一条编辑", "warning");
                    return false;
                }
                common.openNewTabByIframe({
                    title: "编辑文章",
                    href: "/ContentManage/ArticleNews/Form?keyValue=" + id,
                });
            }
            else if (obj.event === 'details') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                if (data.length > 1) {
                    common.modalMsg("只能选择一条查看", "warning");
                    return false;
                }
                common.openNewTabByIframe({
                    title: "查看文章",
                    href: "/ContentManage/ArticleNews/Details?keyValue=" + id,
                });
            }
            else if (obj.event === 'TABLE_SEARCH') {
                var _that = $("#searchField");
                if (_that.hasClass("layui-hide")) {
                    _that.removeClass('layui-hide');
                } else {
                    _that.addClass('layui-hide');
                }
                table.resize();
            }
            return false;
        });
        //toolrow监听事件
        table.on('tool(currentTableFilter)', function (obj) {
            var id = obj.data.F_Id;
            if (obj.event === 'delete') {
                common.deleteForm({
                    url: "/ContentManage/ArticleNews/DeleteForm",
                    param: { keyValue: id },
                    success: function () {
                        obj.del();
                    }
                });
            }
            else if (obj.event === 'edit') {
                common.openNewTabByIframe({
                    title: "编辑文章",
                    href: "/ContentManage/ArticleNews/Form?keyValue=" + id,
                });
            }
            else if (obj.event === 'details') {
                common.openNewTabByIframe({
                    title: "查看文章",
                    href: "/ContentManage/ArticleNews/Details?keyValue=" + id,
                });
            }
            return false;
        });
    });
    function previewImg(obj) {
        var img = new Image();
        img.src = obj.src;
        var height = (!!img.height ? img.height:100) + 50; //获取图片高度
        var width = (!!img.width ? img.width : 100); //获取图片宽度
        if (height > 500) { height = 500; };
        if (width > 500) { width = 500; };
        var imgHtml = "<img src='" + obj.src + "' style=\"height: " + (height - 43) + "px; width: " + width + "px; line-height: 25px!important;\"/>";
        //弹出层
        layer.open({
            type: 1,
            shade: 0.8,
            offset: 'auto',
            area: [width + 'px', height + 'px'],
            shadeClose: true,//点击外围关闭弹窗
            scrollbar: false,//不现实滚动条
            title: "图片预览", //不显示标题
            content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function () {
                //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
            }
        });
    }
</script>
