<!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/layui/css/my.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>
    <style>img {
        height: 25px;
    }</style>

</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto my-header">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">订单编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="orderId" class="layui-input" placeholder="请输入订单编号">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">收货人</label>
                    <div class="layui-input-block">
                        <input type="text" name="receiverName" class="layui-input" placeholder="请输入收货人姓名">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">收货电话</label>
                    <div class="layui-input-block">
                        <input type="text" name="receiverPhone" class="layui-input" placeholder="请输入收货人手机号">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">订单状态</label>
                    <div class="layui-input-block">
                        <select name="orderState" lay-filter="orderState" lay-search>
                            <option value="1">待发货</option>
                            <option value="2">待收货</option>
                            <option value="3">租赁中</option>
                            <option value="4">已完成</option>
                        </select>
                    </div>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-button-btn" lay-submit lay-filter="The-search" id="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>
</div>

<script type="text/html" id="toolbarUtil">
    <div class="layui-btn-container">
        <!--        <button class="layui-btn layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除-->
        <!--        </button>-->
        <!--        <button class="layui-btn layui-btn" lay-event="export"><i class="layui-icon layui-icon-release"></i>导出</button>-->
    </div>
</script>

<script type="text/html" id="orderState">
    {{#if (d.orderState == 0) { }}
    <span>待支付</span>
    {{# }else if(d.orderState == 1){ }}
    <span>待发货</span>
    {{# }else if(d.orderState == 2){ }}
    <span>待收货</span>
    {{# }else if(d.orderState == 3){ }}
    <span>租赁中</span>
    {{# }else if(d.orderState == 4){ }}
    <span>已完成</span>
    {{# } }}
</script>

<script type="text/html" id="headLink">
    {{#if (d.headLink !=null && d.headLink !='') { }}
    <img src="{{d.headLink }}">
    {{# }else if(d.headLink ==null || d.headLink ==''){ }}
    <span>无</span>
    {{# } }}
</script>

<!--<script type="text/html" id="goodsSpec">-->
<!--    {{# layui.each(d.orderDetailGoodsList, function(index, item){ }}-->
<!--    &lt;!&ndash;    <span>商品名称:{{item.goodsName}};规格:{{item.goodsSpecName}};数量:{{item.goodsNum}}</span><span style="color: red">|</span>&ndash;&gt;-->
<!--    <span>{{item.goodsName}}|{{item.goodsSpecName}}|{{item.goodsNum}}</span>-->
<!--    {{#if (d.orderDetailGoodsList.length>index+1) { }}-->
<!--    <span style="color: red">;</span>-->
<!--    {{# } }}-->
<!--    {{# }); }}-->
<!--</script>-->

<script type="text/html" id="table-handle">
    <i class="layui-icon layui-icon-form" lay-event="orderDetail" title="订单详情"></i>
    <i class="layui-icon layui-icon-edit" lay-event="edit" title="编辑"></i>
    {{#if (d.orderState == 0) { }}
    {{# }else if(d.orderState == 1 && d.deliveryId != 7){ }}
    <i class="layui-icon layui-icon-ok-circle" lay-event="deliver" title="发货"></i>
    {{# }else if(d.orderState == 2 && d.isLease != 1){ }}
    <i class="layui-icon layui-icon-cart" lay-event="receiving" title="已完成"></i>
    {{# }else if(d.orderState == 2 && d.isLease == 1 ){ }}
    <i class="layui-icon layui-icon-date" lay-event="lease" title="租赁开始"></i>
    {{# }else if(d.orderState == 3&& d.isLease == 1){ }}
    <i class="layui-icon layui-icon-cart" lay-event="refund" title="退还押金"></i>
    {{# }else if(d.deliveryId == 7){ }}
    <i class="layui-icon layui-icon-cart" lay-event="receiving" title="已完成"></i>
    {{# } }}
    <i class="layui-icon layui-icon-delete" lay-event="del" title="删除"></i>
</script>

<script>
    var field = {"orderId": "", orderState: '[[${orderState}]]'};
    layui.use(['table', 'laydate', '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: 'getList'
                , where: {"orderState": '[[${orderState}]]'}
                , method: 'post'
                , height: 500
                , toolbar: '#toolbarUtil' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter']
                , cols: [
                    [
                        {type: 'checkbox', fixed: "left"}
                        , {field: 'orderId', width: 200, title: '订单编号', fixed: "left"}
                        , {field: 'orderState', width: 100, title: '订单状态', toolbar: '#orderState'}
                        , {field: 'goodsNum', width: 100, title: '商品总数'}
                        // , {field: 'transportAmount', width: 100, title: '运费'}
                        , {field: 'totalAmount', width: 100, title: '总金额'}
                        , {field: 'payAmount', width: 100, title: '已付金额'}
                        , {field: 'payTime', width: 200, title: '支付时间'}
                        //, {field: '', width: 350, title: '商品规格', toolbar: '#goodsSpec'}
                        , {field: 'orderDetails', width: 350, title: '订单明细'}
                        , {field: 'deliveryName', width: 160, title: '配送方式'}
                        , {field: 'receiverName', width: 100, title: '收货人姓名'}
                        , {field: 'receiverPhone', width: 150, title: '收货人手机号'}
                        , {field: 'receiverAddress', width: 300, title: '收货地址'}
                        , {field: 'orderRemarks', width: 200, title: '备注'}
                        , {field: 'trackingName', width: 120, title: '快递名称'}
                        , {field: 'trackingNum', width: 200, title: '快递单号'}
                        , {field: 'trackingCost', width: 80, title: '快递费用'}
                        // , {field: 'nickName', width: 200, title: '用户昵称'}
                        // , {field: 'headLink', width: 200, title: '用户头像', toolbar: '#headLink'}
                        , {field: 'createTime', width: 200, title: '下单时间'}
                        , {field: '', width: 130, title: '操作', toolbar: '#table-handle', fixed: "right"}
                    ]
                ]
                , cellMinWidth: 20 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                , skin: 'line ' //表格风格 line （行边框风格）row （列边框风格）nob （无边框风格）
                , even: true    //隔行换色
                , limit: 20 //每页默认显示的数量
                , method: 'post'  //提交方式
                , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['count', 'prev', 'page', 'next', 'skip', 'limit'] //自定义分页布局
                    , curr: 1 //设定初始在第 5 页
                    , groups: 10 //只显示 1 个连续页码
                }
                , limits: [10, 20, 50, 100]  //每页条数的选择项，默认：[10,20,30,40,50,60,70,80,90]。

            });

            //头工具栏事件
            table.on('toolbar(tableId)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'export':
                        var href = "exportOrder?orderState=[[${orderState}]]";
                        if (field.orderId) {
                            href = href + "&orderId=" + field.orderId;
                        }
                        location.href = href;
                        break;
                    case 'del':
                        var data = checkStatus.data;
                        var id = '';
                        if (data.length > 0) {
                            for (var j = 0; j < data.length; j++) {
                                id = id + "," + data[j].orderId;
                                if (data[j].orderState == 1) {
                                    layer.msg("不能删除已支付的订单");
                                    return false;
                                } else if (data[j].orderState == 2) {
                                    layer.msg("不能删除已发货的订单");
                                    return false;
                                }
                            }
                        } else {
                            layer.msg("请至少选择一条数据!");
                            return false;
                        }
                        layer.confirm('确定彻底删除选中的数据吗?', function (index) {
                            $.ajax({
                                url: "del",
                                data: {"id": id},
                                type: "POST",
                                dataType: "json",
                                success: function (result) {
                                    if (result.code == 100) {
                                        layer.msg(result.msg, {icon: 6, time: 1000}, function () {
                                            layer.close(index);
                                            table.reload('tableId');
                                        });
                                    } else {
                                        layer.msg(result.msg, {icon: 5});
                                    }
                                }
                            });
                        });
                        break;
                    // case 'deliver':
                    //     var data = checkStatus.data;
                    //     var id = '';
                    //     if (data.length > 0) {
                    //         for (var j = 0; j < data.length; j++) {
                    //             id = id + "," + data[j].orderId;
                    //         }
                    //     } else {
                    //         layer.msg("请至少选择一条数据!");
                    //         return false;
                    //     }
                    //     layer.confirm('选中的订单确定发货吗?', function (index) {
                    //         $.ajax({
                    //             url: "deliver",
                    //             data: {"id": id},
                    //             type: "POST",
                    //             dataType: "json",
                    //             success: function (result) {
                    //                 if (result.code == 10001) {
                    //                     layer.msg(result.msg, {icon: 6, time: 1000}, function () {
                    //                         layer.close(index);
                    //                         table.reload('tableId');
                    //                     });
                    //                 } else {
                    //                     layer.msg(result.msg, {icon: 5});
                    //                 }
                    //             }
                    //         });
                    //     });
                    //     break;
                }
                ;
            });

            //监听行工具事件
            table.on('tool(tableId)', function (obj) {
                var data = obj.data;
                if (obj.event === 'deliver') {
                    var id = obj.data.orderId;
                    var w = ($(window).width() * 0.75);
                    var h = ($(window).height() * 0.75);
                    layer.open({
                        type: 2
                        , title: '发货确认'
                        , content: 'toDeliver?orderId=' + id
                        , area: [w + 'px', h + 'px']
                        , fix: false //不固定
                        , maxmin: true
                        , shadeClose: true
                        , shade: 0.4
                        , btn: ['确认', '取消']
                        , yes: function (index, layero) {
                            var body = layer.getChildFrame('body', index); //得到iframe页面层的BODY
                            var iframeBtn = body.find('#saveBtn');//得到iframe页面层的提交按钮
                            iframeBtn.click();//模拟iframe页面层的提交按钮点击
                        }
                    });
                } else if (obj.event === 'edit') {
                    var id = obj.data.orderId;
                    var w = ($(window).width() * 0.95);
                    var h = ($(window).height() * 0.95);
                    layer.open({
                        type: 2
                        , title: '修改订单'
                        , content: 'orderDetailEdit?orderId=' + id
                        , area: [w + 'px', h + 'px']
                        , fix: false //不固定
                        , maxmin: true
                        , shadeClose: true
                        , shade: 0.4
                        , btn: ['确认', '取消']
                        , yes: function (index, layero) {
                            var body = layer.getChildFrame('body', index); //得到iframe页面层的BODY
                            var iframeBtn = body.find('#saveBtn');//得到iframe页面层的提交按钮
                            iframeBtn.click();//模拟iframe页面层的提交按钮点击
                        }
                    });
                }else if (obj.event === 'orderDetail') {
                    var id = obj.data.orderId;
                    var w = ($(window).width() * 0.95);
                    var h = ($(window).height() * 0.95);
                    layer.open({
                        type: 2
                        , title: '订单详情'
                        , content: 'orderDetail?orderId=' + id
                        , area: [w + 'px', h + 'px']
                        , fix: false //不固定
                        , maxmin: true
                        , shadeClose: true
                        , shade: 0.4
                        , btn: ['关闭']
                        // , yes: function (index, layero) {
                        //     var body = layer.getChildFrame('body', index); //得到iframe页面层的BODY
                        //     var iframeBtn = body.find('#saveBtn');//得到iframe页面层的提交按钮
                        //     iframeBtn.click();//模拟iframe页面层的提交按钮点击
                        // }
                    });
                }
                else if (obj.event === 'del') {
                    var id = obj.data.orderId;
                    layer.confirm('确定彻底删除选中的数据吗?', function (index) {
                        $.ajax({
                            url: "del",
                            data: {"id": id},
                            type: "POST",
                            dataType: "json",
                            success: function (result) {
                                if (result.code == 100) {
                                    layer.msg(result.msg, {icon: 6, time: 1000}, function () {
                                        layer.close(index);
                                        table.reload('tableId');
                                    });
                                } else {
                                    layer.msg(result.msg, {icon: 5});
                                }
                            }
                        });
                    });
                } else if (obj.event === 'receiving') {
                    var id = obj.data.orderId;
                    layer.confirm(obj.data.orderDetails + '确定完成订单吗?', function (index) {
                        $.ajax({
                            url: "receiving",
                            data: {"orderId": id, "orderState": 4},
                            type: "POST",
                            dataType: "json",
                            success: function (result) {
                                if (result.code == 100) {
                                    layer.msg(result.msg, {icon: 6, time: 1000}, function () {
                                        layer.close(index);
                                        table.reload('tableId');
                                    });
                                } else {
                                    layer.msg(result.msg, {icon: 5});
                                }
                            }
                        });
                    });
                } else if (obj.event === 'lease') {
                    var id = obj.data.orderId;
                    layer.confirm(obj.data.orderDetails + '确定开始租赁?', function (index) {
                        $.ajax({
                            url: "receiving",
                            data: {"orderId": id, "orderState": 3},
                            type: "POST",
                            dataType: "json",
                            success: function (result) {
                                if (result.code == 100) {
                                    layer.msg(result.msg, {icon: 6, time: 1000}, function () {
                                        layer.close(index);
                                        table.reload('tableId');
                                    });
                                } else {
                                    layer.msg(result.msg, {icon: 5});
                                }
                            }
                        });
                    });
                } else if (obj.event === 'refund') {
                    var id = obj.data.orderId;
                    var w = ($(window).width() * 0.95);
                    var h = ($(window).height() * 0.95);
                    layer.open({
                        type: 2
                        , title: '退还押金'
                        , content: 'toRefund?orderId=' + id
                        , area: [w + 'px', h + 'px']
                        , fix: false //不固定
                        , maxmin: true
                        , shadeClose: true
                        , shade: 0.4
                        , btn: ['保存', '关闭']
                        , yes: function (index, layero) {
                            var body = layer.getChildFrame('body', index); //得到iframe页面层的BODY
                            var iframeBtn = body.find('#saveBtn');//得到iframe页面层的提交按钮
                            iframeBtn.click();//模拟iframe页面层的提交按钮点击
                        }
                    });
                }
            });
        }
    );

</script>

</body>

</html>