<title>退款单表</title>
<!--当前位置开始-->
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <script type="text/html" template lay-done="layui.data.updateMainBreadcrumb();">
        </script>
    </div>
</div>
<!--当前位置结束-->
<style>
    /* 重写样式 */
</style>
<script type="text/html" template lay-type="Post" lay-url="{{ layui.setter.apiUrl }}Api/CoreCmsBillRefund/GetIndex" lay-done="layui.data.done(d);">
    <div class="layui-form coreshop-search-form">
        <div class="layui-form-item">

            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="refundId" id="refundId" placeholder="请输入退款单号" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="aftersalesId" id="aftersalesId" placeholder="请输入售后单号" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="userId" id="userId" placeholder="请输入用户昵称" class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <div class="layui-input-inline">
                    <select name="type" id="type" lay-filter="conditionCode">
                        <option value="">请选择单据类型</option>
                        {{# layui.each(d.data.paymentsResourceTypes, function(index, item){ }}
                        <option value="{{ item.value }}">{{ item.description }}</option>
                        {{# }); }}
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <select name="status" id="status" lay-filter="conditionCode">
                        <option value="">请选择状态</option>
                        {{# layui.each(d.data.refundStatus, function(index, item){ }}
                        <option value="{{ item.value }}">{{ item.description }}</option>
                        {{# }); }}
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <select name="paymentCode" id="paymentCode" lay-filter="conditionCode">
                        <option value="">请选择退款方式</option>
                        {{# layui.each(d.data.paymentCode, function(index, item){ }}
                        <option value="{{ item.value }}">{{ item.description }}</option>
                        {{# }); }}
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="LAY-app-CoreCmsBillRefund-search"><i class="layui-icon layui-icon-search"></i>筛选</button>
            </div>
        </div>
    </div>
</script>
<div class="table-body">
    <table id="LAY-app-CoreCmsBillRefund-tableBox" lay-filter="LAY-app-CoreCmsBillRefund-tableBox"></table>
</div>

<script type="text/html" id="LAY-app-CoreCmsBillRefund-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="selectExportExcel"><i class="layui-icon layui-icon-add-circle"></i>选择导出</button>
        <button class="layui-btn layui-btn-sm" lay-event="queryExportExcel"><i class="layui-icon layui-icon-download-circle"></i>查询导出</button>
    </div>
</script>
<script type="text/html" id="LAY-app-CoreCmsBillRefund-tableBox-bar">
    {{#  if(d.status === 1){ }}
    <a class="layui-btn layui-btn-xs" lay-event="audit">退款</a>
    {{#  } }}
    {{#  if(d.status === 3){ }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="reaudit">再次退款</a>
    {{#  } }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">明细</a>
</script>

<script>
    var indexData;
    var debug = layui.setter.debug;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d); }

        indexData = d.data;
        layui.use(['index', 'table', 'laydate', 'util', 'coreHelper'],
            function () {
                var $ = layui.$
                    , admin = layui.admin
                    , table = layui.table
                    , form = layui.form
                    , laydate = layui.laydate
                    , setter = layui.setter
                    , coreHelper = layui.coreHelper
                    , util = layui.util
                    , view = layui.view;

                laydate.render({
                    elem: '#searchTime-CoreCmsBillRefund-createTime',
                    type: 'datetime',
                    range: '到',
                });
                //重载form
                form.render();
                var searchwhere;
                //监听搜索
                form.on('submit(LAY-app-CoreCmsBillRefund-search)',
                    function (data) {
                        var field = data.field;
                        searchwhere = field;
                        //执行重载
                        table.reload('LAY-app-CoreCmsBillRefund-tableBox', { where: field });
                    });
                //数据绑定
                table.render({
                    elem: '#LAY-app-CoreCmsBillRefund-tableBox',
                    url: layui.setter.apiUrl + 'Api/CoreCmsBillRefund/GetPageList',
                    method: 'POST',
                    toolbar: '#LAY-app-CoreCmsBillRefund-toolbar',
                    defaultToolbar: ['filter', 'print', 'exports'],
                    height: 'full-189',//无面包屑127,搜索框189,1行62
                    page: true,
                    limit: 30,
                    limits: [10, 15, 20, 25, 30, 50, 100, 200],
                    text: { none: '暂无相关数据' },
                    cols: [
                        [
                            { type: "checkbox", fixed: "left" },
                            { field: 'refundId', title: '退款单', sort: false, width: 125 },
                            { field: 'aftersalesId', title: '售后单', sort: false, width: 125 },
                            { field: 'money', title: '退款金额', sort: false, width: 65, templet: function (data) { return '￥' + data.money } },
                            {
                                field: 'userNickName', title: '用户', sort: false, width: 155, templet: function (d) {
                                    return d.userNickName + '（' + d.userId + '）';
                                }
                            },
                            { field: 'sourceId', title: '单号', sort: false, width: 125 },
                            {
                                field: 'type', title: '类型', sort: false, width: 105,
                                templet: function (data) {
                                    for (var i = 0; i < d.data.paymentsResourceTypes.length; i++) {
                                        if (data.type == d.data.paymentsResourceTypes[i].value) {
                                            return d.data.paymentsResourceTypes[i].description;
                                        }
                                    }
                                    return "";
                                }
                            },
                            {
                                field: 'status', title: '状态', sort: false, width: 85,
                                templet: function (data) {
                                    for (var i = 0; i < d.data.refundStatus.length; i++) {
                                        if (data.status == d.data.refundStatus[i].value) {
                                            return d.data.refundStatus[i].description;
                                        }
                                    }
                                    return "";
                                }
                            },
                            {
                                field: 'paymentCode', title: '退款方式', sort: false, width: 100,
                                templet: function (data) {
                                    for (var i = 0; i < d.data.paymentCode.length; i++) {
                                        if (data.paymentCode == d.data.paymentCode[i].title) {
                                            return d.data.paymentCode[i].description;
                                        }
                                    }
                                    return "";
                                }
                            },
                            { field: 'memo', title: '回执说明', sort: false },
                            { field: 'createTime', title: '创建时间', width: 130, sort: false },
                            { field: 'updateTime', title: '更新时间', width: 130, sort: false },
                            { width: 122, align: 'center', title: '操作', fixed: 'right', toolbar: '#LAY-app-CoreCmsBillRefund-tableBox-bar' }
                        ]
                    ]
                });
                //监听排序事件
                table.on('sort(LAY-app-CoreCmsBillRefund-tableBox)', function (obj) {
                    table.reload('LAY-app-CoreCmsBillRefund-tableBox', {
                        initSort: obj, //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                        where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                            orderField: obj.field, //排序字段
                            orderDirection: obj.type //排序方式
                        }
                    });
                });
                //监听行双击事件
                table.on('rowDouble(LAY-app-CoreCmsBillRefund-tableBox)', function (obj) {
                    //查看详情
                    doDetails(obj);
                });
                //头工具栏事件
                table.on('toolbar(LAY-app-CoreCmsBillRefund-tableBox)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'addData':
                            doCreate();
                            break;
                        case 'selectExportExcel':
                            doSelectExportExcel(checkStatus);
                            break;
                        case 'queryExportExcel':
                            doQueryExportexcel();
                            break;
                    };
                });
                //监听工具条
                table.on('tool(LAY-app-CoreCmsBillRefund-tableBox)',
                    function (obj) {
                        if (obj.event === 'detail') {
                            doDetails(obj);
                        } else if (obj.event === 'audit') {
                            doAudit(obj);
                        } else if (obj.event === 'reaudit') {
                            reAudit(obj)
                        }
                    });

                //执行预览操作
                function doDetails(obj) {
                    coreHelper.Post("Api/CoreCmsBillRefund/GetDetails", { id: obj.data.refundId }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '查看详情',
                                area: ['1000px', '90%'],
                                id: 'LAY-popup-CoreCmsBillRefund-details',
                                success: function (layero, index) {
                                    view(this.id).render('financial/billrefund/details', { data: e.data }).done(function () {
                                        form.render();
                                    });
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }

                //执行编辑操作
                function doAudit(obj) {
                    coreHelper.Post("Api/CoreCmsBillRefund/GetAudit", { id: obj.data.refundId }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '审核退款单',
                                area: ['630px', '350px'],
                                id: 'LAY-popup-CoreCmsBillRefund-edit',
                                success: function (layero, index) {
                                    view(this.id).render('financial/billrefund/audit', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsBillRefund-editForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsBillRefund/DoAudit", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        layui.table.reload('LAY-app-CoreCmsBillRefund-tableBox'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    })
                                    // 禁止弹窗出现滚动条
                                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }

                //执行再次退款
                function reAudit(obj) {
                    layer.confirm('确定执行再次退款吗？', function (index) {
                        coreHelper.Post("Api/CoreCmsBillRefund/DoReAudit", { id: obj.data.refundId }, function (e) {
                            if (debug) { console.log(e); } //开启调试返回数据
                            table.reload('LAY-app-CoreCmsBillRefund-tableBox');
                            layer.msg(e.msg);
                        });
                    });
                }

                //执行查询条件导出excel
                function doQueryExportexcel() {
                    layer.confirm('确定根据当前的查询条件导出数据吗？',
                        function (index) {
                            var field = searchwhere;
                            coreHelper.PostForm("Api/CoreCmsBillRefund/QueryExportExcel", field, function (e) {
                                if (debug) { console.log(e); } //开启调试返回数据
                                if (e.code === 0) {
                                    window.open(e.data);
                                } else {
                                    layer.msg(e.msg);
                                }
                            });
                        });
                }
                //执行选择目录导出数据
                function doSelectExportExcel(checkStatus) {
                    var checkData = checkStatus.data;
                    if (checkData.length === 0) {
                        return layer.msg('请选择您要导出的数据');
                    }
                    layer.confirm('确定导出选择的内容吗？',
                        function (index) {
                            var delidsStr = [];
                            layui.each(checkData,
                                function (index, item) {
                                    delidsStr.push(item.id);
                                });
                            layer.close(index);
                            coreHelper.Post("Api/CoreCmsBillRefund/SelectExportExcel", { id: delidsStr }, function (e) {
                                if (debug) { console.log(e); } //开启调试返回数据
                                if (e.code === 0) {
                                    window.open(e.data);
                                } else {
                                    layer.msg(e.msg);
                                }
                            });
                        });
                }
                //监听 表格复选框操作

            });
    };
</script>