/**
 * Created by Administrator on 2017/12/11 0011.
 */

$(function () {

    //查询
    //jquery data table 初始化
    var orderTable = $("#orderTable").DataTable({
        "paging": true,
        "select": true,
        "processing": true,
        "serverSide": true,
        "language": {
            'lengthMenu': '每页显示 _MENU_ 记录',
            'zeroRecords': '没有数据',
            'info': ' 第_PAGE_ 页/ _PAGES_页，共 _TOTAL_ 条',
            'infoEmpty': '没有符合条件的记录',
            'search': '查找',
            'infoFiltered': '(从  _MAX_ 条记录中过滤)',
            'paginate': {"first": "首页 ", "last": "末页", "next": "下一页", "previous": "上一页"}
        },
        "ajax": {
            url: rootPath + "payment/orderData",
            type: "post",
            //增加参数查询参数
            data: function (data) {
                data.payment_stime = $('input[name="payment_stime"]').val();
                data.payment_etime = $('input[name="payment_etime"]').val();
                data.uid = $('input[name="userid"]').val();
                data.payment_type = $('select[name="payment_type"]').val();
                data.channel = $('#channel').val();
                data.order_id = $('input[name="orderid"]').val();
                data.sl_platform_serial_num = $('input[name="sl_platform_serial_num"]').val();
                data.sku = $('input[name="sku"]').val();
                console.log(data)
            }
        },
        "lengthChange": true,
        "searching": false,
        "ordering": false,
        "info": true,
        "autoWidth": false,
        "columns": [
            {"data": "index"},
            {"data": "sl_orderid"},
            {
                "data": "sl_uid", class: "uid", render: function (data) {
                    return '<a target = "_blank" href="#user/index/' + data + '">' + data + '</a>';
                }
            },
            {"data": "sl_levelId"},
            {"data": "sl_gid"},
            {"data": "gsku"},
            {"data": "gname"},
            {"data": "gdesc", class: "gdesc"},
            {"data": "sl_pusd"},
            {"data": "sl_order_time"},
            {"data": "sl_pay_time"},
            {"data": "gchannel"},
            {"data": "gplatform"},
            {"data": "sl_status"},
            {
                "data": "source", render: function (data) {
                    switch (data) {
                        case '0':
                            return "正常流程";
                        case '1':
                            return "脚步自动补发到邮件";
                        case '2':
                            return "客户端补发";
                        case '3':
                            return "外部回调";
                        case '4':
                            return "后台补发";
                        default:
                            return "-";
                    }
                }
            },
            {"data": "sl_platform_serial_num"},
            {
                "data": "sl_content", render: function (data) {
                    return data;
                }
            },
            {
                "data": "status", render: function (data) {
                    console.log(data)
                    if (data === 0) {
                        return '<input type="button" class="btn btn-sm btn-primary reissue" style="margin-bottom:5px;" value="补发" name="reissue-name" />'
                    }
                    return "-"
                }
            }
        ]
    });

    //当点击search按钮时按条件重新加载数据
    $("#searchBtn").click(function () {
        orderTable.ajax.reload();
    });


    //点击补发
    $("#orderTable").on('click', '.reissue', function () {
        let _this = $(this),
            parentElement = _this.parent().parent(),
            orderId = parentElement.children().eq(1).text(),
            uid = parentElement.children().eq(2).text(),
            sku = parentElement.children().eq(5).text(),

            orderIdElem = $('#orderId'),
            uidElem = $('#uid'),
            skuElem = $('#sku'),
            payIdElem = $('#payId');

        $('#modalSubmitBtn').attr('disabled', false);

        orderIdElem.val(orderId);
        uidElem.val(uid);
        skuElem.val(sku);
        payIdElem.val("");
        $('#reissueOrder').modal();
    });

    //如果点取消
    $('#modalCancel').click(function () {
        $('#modalSubmitBtn').attr('disabled', false);
    });

    //点击确认按钮
    $('#modalSubmitBtn').click(function () {
        let msgTip = $('#msg-tip'),
            orderId = $('#orderId').val(),
            uid = $('#uid').val(),
            sku = $('#sku').val(),
            payId = $('#payId').val(),
            _this = $(this);
        _this.attr('disabled', true);

        if (isEmpty(orderId) || isEmpty(uid) || isEmpty(sku) || isEmpty(payId)) {
            msgTip.messager().error('订单ID、用户ID、平台商品ID、支付订单ID都是必填项');
            _this.attr('disabled', false);
            return false;
        }

        const data = {orderId, uid, sku, payId}
        $.ajax({
            url: rootPath + 'payment/reissue',
            type: 'POST',
            data: data,
            dataType: 'json',
            success: function (data) {
                if (data.ret == 0) {
                    $('#reissueOrder .close').click();
                    alert(data.msg)
                    orderTable.ajax.reload();//重新加载页面
                } else {
                    _this.attr('disabled', false);
                    msgTip.messager().error(data.msg);
                }
            },
            error: function () {
                _this.attr('disabled', false);
                msgTip.messager().error('网络错误，请刷新后重试');
            }
        });
    });

});
