<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>后台管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="shortcut icon" th:href="@{/images/logo.jpg}"/>
    <link rel="stylesheet" th:href="@{/js/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/js/layui/css/admin.css}">
    <link rel="stylesheet" th:href="@{/js/upload_img/css/index.css}">
    <script type="text/javascript" th:src="@{/js/jquery-3.3.1.min.js}"></script>
    <script th:src="@{/js/layui/layui.js}" charset="utf-8"></script>
    <script th:src="@{/js/layui/layui.all.js}" charset="utf-8"></script>

    <style>
    </style>

</head>
<body>
<div class="layui-form" style="padding: 20px 0 0 0;">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-block">
                        <input type="text" name="sname" class="layui-input" placeholder="请输入商品名称">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-button-btn" lay-submit lay-filter="The-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn">
                        </i>
                    </button>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-button-btn"
                            onclick="javascript:location.replace(location.href);">
                        <i class="layui-icon layui-icon-refresh-3 layuiadmin-button-btn">
                        </i>
                    </button>
                </div>
            </div>

        </div>
    </div>
    <div class="layui-card-body">
        <table class="layui-hide" id="tableId" lay-filter="tableId"></table>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" id="btn-ok">确定</button>
            <button class="layui-btn layui-btn-primary" id="btn-cancel">取消</button>
        </div>
    </div>
</div>

<script type="text/html" id="sshortpic">
    {{#if (d.sshortpic !=null && d.sshortpic !='') { }}
    <img src="{{d.sshortpic }}">
    {{# }else if(d.sshortpic ==null || d.sshortpic ==''){ }}
    <span>无</span>
    {{# } }}
</script>
<script type="text/html" id="istatus">
    {{#if (d.istatus == 3) { }}
    <span>已下架</span>
    {{# }else if(d.istatus == 1){ }}
    <span>在售</span>
    {{# } }}
</script>

<!--<script type="text/javascript" th:src="@{/js/upload_img/js/updateimg.js}"></script>-->

<script>
    var field;

    layui.use(['table', 'form'], function () {
        var form = layui.form, table = layui.table;
        //监听搜索
        form.on('submit(The-search)', function (data) {
            field = data.field;
            //执行重载
            table.reload('tableId', {
                where: field
                , page: {
                    curr: 1
                }
            });
        });
        table.render({
            elem: '#tableId'
            , url: 'getProductList'
            , where: field
            , method: 'post'
            , contentType: "application/json" // 内容编码， json格式
            , height: 350
            , defaultToolbar: ['filter']
            , cellMinWidth: 20 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , skin: 'line ' //表格风格 line （行边框风格）row （列边框风格）nob （无边框风格）
            , even: true    //隔行换色
            , limit: 10 //每页默认显示的数量
            , page: true
            , request: {
                pageName: "curpage", // 页码的参数名称，默认：page
                limitName: "pagesize" // 每页数据量的参数名，默认：limit
            }
            , response: {
                statusName: "code", // 规定数据状态的字段名称，默认：code
                statusCode: 100, // 规定成功的状态码，默认：0
                msgName: "msg", // 规定状态信息的字段名称，默认：msg
                countName: "count", // 规定数据总数的字段名称，默认：count
                dataName: "data" // 规定数据列表的字段名称，默认：data
            }

            , parseData: function (res) { // res 即为原始返回的数据
                return {
                    "code": res.code, // 解析接口状态
                    "msg": res.msg, // 解析提示文本
                    "count": res.data.pageinfo.totalRecords, // 解析数据长度
                    "data": res.data.items // 解析数据列表
                };
            }

            , limits: [10, 20, 50, 100]  //每页条数的选择项，默认：[10,20,30,40,50,60,70,80,90]。
            , cols: [
                [
                    {type: 'checkbox', field: 'id'},
                    {field: 'id', title: '编码'},
                    {field: 'sshortpic', title: '头图', toolbar: '#sshortpic'},
                    {field: 'sname', title: '名称'},
                    {field: 'catalogName', title: '分类'},
                    {field: 'doldprice', title: '原价'},
                    {field: 'dsaleprice', title: '售价'},
                    {field: 'istatus', title: '状态', toolbar: '#istatus'},
                ]
            ]
        });

        var selectedData = []; // 保存选中的数据
        $('#btn-ok').on('click', function() {
            table.checkStatus('tableId').data.forEach(function(item){
                selectedData.push(item);
            });
            // 获取父页面的 window 对象
            var parentWindow = window.parent;
            // 使用 postMessage 发送数据到父页面
            parentWindow.postMessage(selectedData, '*');
            // parent.layui.trigger('selectedData', selectedData);
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        });

        $('#btn-cancel').on('click', function() {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        });
    });

</script>
</body>

</html>