<!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="sorderno" class="layui-input" placeholder="请输入订单编号">
                    </div>
                </div>



                <div class="layui-inline">
                    <label class="layui-form-label">订单状态</label>
                    <div class="layui-input-block">
                        <select name="istatus" lay-filter="istatus" lay-search>
                            <option value="">请选择</option>
                            <option value="10">待付款</option>
                            <!--<option value="20">待发货</option>-->
                            <option value="30">待收货</option>
                            <option value="90">已完成</option>
                            <option value="100">已取消</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="table-handle">
    <i class="layui-icon layui-icon-form" lay-event="orderDetail" title="订单详情"></i>
    <!--    101退款申请中 102待退货 103待退款 104退款成功 105退款拒绝-->
    {{#if (d.orderRefund.refundStatus == 1 && d.orderRefund.refundType == 1) { }}
    <i class="layui-icon layui-icon-ok" lay-event="pass" title="退款申请通过"></i>
    <i class="layui-icon layui-icon-close" lay-event="refuse" title="退款申请拒绝"></i>
    {{# }else if(d.orderRefund.refundStatus == 4){ }}
    <i class="layui-icon layui-icon-component" lay-event="receive" title="退款已收货"></i>
    {{# }else if(d.orderRefund.refundStatus == 1 && d.orderRefund.refundType == 2){ }}
    <i class="layui-icon layui-icon-ok" lay-event="pass" title="仅退款申请通过"></i>
    <i class="layui-icon layui-icon-close" lay-event="refuse" title="仅退款申请拒绝"></i>
    {{# } }}
</script>

<script>
    var field = {};
    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: 'findPageRefundList'
                , where: field
                , method: 'post'
                ,contentType: "application/json" // 内容编码， json格式
                , height: 500
                , toolbar: '#toolbarUtil' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter']
                , cellMinWidth: 20 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                , skin: 'line ' //表格风格 line （行边框风格）row （列边框风格）nob （无边框风格）
                , even: true    //隔行换色
                , limit: 20 //每页默认显示的数量
                , 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.desc, // 解析提示文本
                        "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', fixed: "left"}
                        , {field: 'sorderno', width: 200, title: '订单编号', fixed: "left"}
                        , {field: 'groupBuyId', width: 200, title: '活动编码'}
                        , {field: 'groupBuyName', width: 200, title: '活动标题'}
                        ,{title: '用户信息',
                            width: 100,
                            templet: function(d) {
                                var str = "";
                                if(d.customer){
                                    if (d.customer.swximgurl) {
                                        str += "<img style='width: 18px' src=" + d.customer.swximgurl + ">&nbsp;";
                                    }
                                    if (d.customer.swxnick) {
                                        str += "<span style=''>" + d.customer.swxnick ? d.customer.swxnick : '' + "</span>"
                                    }
                                }
                                return str;
                            }
                        }
                        ,{
                            title: "订单金额",
                             width: 150,
                            templet: function(d) {
                                var str = "";
                                str += '金额:<span style="color:red;"> '+(d.dactmoney ? d.dactmoney : 0)+ '</span>&nbsp';
                                str += '积分:<span style="color:red;"> '+(d.dactscore ? d.dactscore : 0)+ '</span>';
                                str += '</div>';
                                return str ? str : "";
                            }
                        }
                        , {width: 150, title: '退款金额',
                            templet: function(d) {
                                var str = "";
                                str += '<span> '+(d.orderRefund.refundMoney ? d.orderRefund.refundMoney : 0)+ '</span>&nbsp';
                                str += '</div>';
                                return str ? str : "";
                            }
                        }
                        ,{
                        title: "订单状态",
                        width: 200,
                        templet: function(d) {

                            var str = "";
                            str += '<div>';
                            var itype = ""; //0:未支付1:支付成功2:支付失败
                            if (d.ipaystatus == 0) itype = "<span class='layui-badge layui-bg-gray'>未付</span>";
                            if (d.ipaystatus == 1) itype = "<span class='layui-badge'>支付成功</span>";
                            if (d.ipaystatus == 2) itype = "<span class='layui-badge layui-bg-gray'>支付失败</span>";
                            str += ' <span>' + itype + '</span>';
                            //订单状态 10:待付款 20:待发货 30:待收货 90:已完成 99:已拒绝/关闭 100:取消
                            var status = "";
                            if (d.istatus == 10) status = "<span class='layui-badge layui-bg-orange'>待付款</span>";
                            if (d.istatus == 20) status = "<span class='layui-badge layui-bg-orange'>待发货</span>";
                            if (d.istatus == 30) status = "<span class='layui-badge layui-bg-orange'>待收货</span>";
                            if (d.istatus == 90) status = "<span class='layui-badge layui-bg-green'>已完成</span>";
                            if (d.istatus == 99) status = "<span class='layui-badge layui-bg-gray'>交易关闭</span>";
                            if (d.istatus == 100) status = "<span class='layui-badge layui-bg-black'>已取消</span>";
                            str += ' <span>' + status + '</span>';
                            // 101退款申请中 102待退货 103待退款 104退款成功
                            var refundStatus = "";
                            if (d.orderRefund.refundStatus == 1 && d.orderRefund.refundType == 1) refundStatus = "<span class='layui-badge layui-bg-red'>退款申请</span>";
                            if (d.orderRefund.refundStatus == 4) refundStatus = "<span class='layui-badge layui-bg-orange'>待退货</span>";
                            if (d.orderRefund.refundStatus == 6) refundStatus = "<span class='layui-badge layui-bg-orange'>待退款</span>";
                            if (d.orderRefund.refundStatus == 5) refundStatus = "<span class='layui-badge layui-bg-green'>退款成功</span>";
                            if (d.orderRefund.refundStatus == 3) refundStatus = "<span class='layui-badge layui-bg-cyan'>退款拒绝</span>";
                            if (d.orderRefund.refundStatus == 1 && d.orderRefund.refundType == 2) refundStatus = "<span class='layui-badge layui-bg-red'>仅退款申请</span>";
                            str += ' <span>' + refundStatus + '</span>';
                            str += '</div>';
                            return str ? str : "";
                            }
                         }
                        , {field: 'tordertime', width: 100, title: '下单日期'}
                        , {field: 'sremark',  width: 100,title: '备注'}
                        , {width: 100, title: '退款时间',
                            templet: function(d) {
                                var str = "";
                                str += '<span> '+(d.orderRefund.refundSucTime ? d.orderRefund.refundSucTime : "")+ '</span>&nbsp';
                                str += '</div>';
                                return str ? str : "";
                            }
                        }
                        ,{
                            width: 100,
                            title: "退款图片",
                            align: "center",
                            templet: function(d) {
                                return d.orderRefund.refundImg ? '<div><img id="zi' + d.id + '" src="' + d.orderRefund.refundImg +
                                    '" onmouseover="bigImg(this)" onmouseout="smallImg()" onclick="tolook(src)" style="height:20px;"></div>' :
                                    '';
                            }
                        }
                        , {width: 100, title: '退款理由',
                            templet: function(d) {
                                var str = "";
                                str += '<span> '+(d.orderRefund.refundReason ? d.orderRefund.refundReason : "")+ '</span>&nbsp';
                                str += '</div>';
                                return str ? str : "";
                            }
                        }
                        , {field: 'couriernumber', width: 120, title: '退款快递单号'}
                        , {field: 'scusaddr', width: 100, title: '地址'}
                        , {field: 'scuname', width: 100, title: '联系姓名'}
                        , {field: 'scuphone', width: 100, title: '联系电话'}
                        , {field: 'sprovince', width: 100, title: '省份'}
                        , {field: 'scity', width: 100, title: '城市'}
                        , {field: 'sregion', width: 100, title: '区县'}
                        // , {field: 'scensus', width: 100, title: '户籍'}
                        , {field: '', width: 130, title: '操作', toolbar: '#table-handle', fixed: "right"}
                    ]
                ]

            });

            //头工具栏事件
            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.id;
                    layer.confirm( '确定接受订单吗?', function (index) {

                        var param = {};
                        param.id = id;
                        param.istatus = 30;

                        $.ajax({
                            url: "updateFwStatus",
                            data: JSON.stringify(param),
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json;charset=utf-8",
                            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 === '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.id;
                    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.id;
                    layer.confirm( '确定完成订单吗?', function (index) {

                        var param = {};
                        param.id = id;
                        param.istatus = 90;

                        $.ajax({
                            url: "updateOkStatus",
                            data: JSON.stringify(param),
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json;charset=utf-8",
                            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页面层的提交按钮点击
                        }
                    });
                } else if (obj.event === 'pass') {
                    var id = obj.data.id;
                    layer.confirm('确定审核通过吗?', function (index) {

                        var param = {};
                        param.id = id;
                        $.ajax({
                            url: "refundApplicationPass",
                            data: JSON.stringify(param),
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json;charset=utf-8",
                            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 === 'refuse') {
                    var id = obj.data.id;
                    layer.confirm( '确定审核拒绝吗?', function (index) {
                        var param = {};
                        param.id = id;
                        $.ajax({
                            url: "refundApplicationRefuse",
                            data: JSON.stringify(param),
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json;charset=utf-8",
                            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 === 'receive') {
                    var id = obj.data.id;
                    layer.confirm( '确定已收到退回的货物吗? 确定后将进行退款！', function (index) {

                        var param = {};
                        param.id = id;
                        $.ajax({
                            url: "receive",
                            data: JSON.stringify(param),
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json;charset=utf-8",
                            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});
                                }
                            }
                        });
                    });
                }
            });
        }
    );


    function tolook(e) {
        window.open(e);
    }

    /**
     * 鼠标移入图片时, 提示弹框 (放大的图片)
     */
    function bigImg(obj){
        var imgUrl = obj.getAttribute("src");
        var imgid = obj.getAttribute("id");

        layer.tips('<img src="' + imgUrl + '"style="width:120px; height: 120rpx;">', "#" + imgid, {
            tips: [1, ''],
            tipsMore: false,
            area: ['150px', '140px'],
            time: 20000
        });
    }

    /**
     * 鼠标移出图片时, 取消弹框
     */
    function smallImg(){
        layer.closeAll("tips");
    }

</script>

</body>

</html>