// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子： 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};
// 代金券消费
function queryCouponList(urls, queryParams) {
    // 默认查询第一页 query查询时保存当前条件
    var TempPageNumber = 1, pageSize = 10;
    try {
        TempPageNumber = $('#datatable').datagrid('getPager').pagination("options").pageNumber;
        pageSize = $('#datatable').datagrid('getPager').pagination("options").pageSize;
    } catch (e) {

    }
    $('#datatable')
        .datagrid(
            {
                title: "信息列表",
                iconCls: 'icon-search',
                pageNumber: TempPageNumber,
                pageSize: pageSize,
                pageList: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
                nowrap: true,
                striped: true,
                border: true,
                noheader: true,
                url: urls,
                loadMsg: '数据加载中......',
                queryParams: queryParams,
                remoteSort: false,
                fitColumns: true,
                singleSelect: false,
                columns: [[

                    {
                        field: 'vinCode',
                        title: 'VIN码',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }, {
                        field: 'userName',
                        title: '姓名',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }
                    /*, {
                     field : 'userPhone',
                     title : '电话',
                     sortable : 'true',
                     align : 'center',
                     width : 10
                     }*/
                    , {
                        field: 'license',
                        title: '车牌号',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'charge',
                        title: '余额',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'operate', title: '操作', align: 'center', width: 5,
                        formatter: function (value, row, index) {
                            var str = '<a href="#" name="opera" class="easyui-linkbutton opera" onclick=showConsumePanel("' + row.vinCode + '","' + row.charge + '");>消费</a>';
                            return str;
                        }
                    }
                ]],
                pagination: true,
                rownumbers: false,
                onLoadSuccess: function (data) {
                },
                toolbar: [],
                onCheck: function (rowIndex, rowData) {
                },
                onCheckAll: function (rows) {
                },
                onUncheck: function (rowIndex, rowData) {
                },
                onLoadSuccess: function (data) {
                },
                onUncheckAll: function (rows) {
                }

            });
    $('#datatable').datagrid('getPager').pagination({
        displayMsg: '当前显示从 {from} 到 {to} 共{total}条记录',
        onBeforeRefresh: function (pageNumber, pageSize) {
            $(this).pagination('loading');
            $(this).pagination('loaded');
        }
    });
}

function showConsumePanel(vin, balance) {
    $("#vinCode").val(vin);
    $("#balance").val(balance);

    $("#balancetext").html("余额： " + balance + "元");
    $("#consume_dialog").show();
}

//代金券历史充值记录和消费
function queryCouponHistoryList(urls, queryParams) {
    // 默认查询第一页 query查询时保存当前条件
    var TempPageNumber = 1, pageSize = 10;
    try {
        TempPageNumber = $('#datatable').datagrid('getPager').pagination("options").pageNumber;
        pageSize = $('#datatable').datagrid('getPager').pagination("options").pageSize;
    } catch (e) {

    }
    $('#datatable')
        .datagrid(
            {
                title: "信息列表",
                iconCls: 'icon-search',
                pageNumber: TempPageNumber,
                pageSize: pageSize,
                pageList: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
                nowrap: true,
                striped: true,
                border: true,
                noheader: true,
                url: urls,
                loadMsg: '数据加载中......',
                queryParams: queryParams,
                remoteSort: false,
                fitColumns: true,
                singleSelect: false,
                columns: [[

                    {
                        field: 'vinCode',
                        title: 'VIN码',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }, {
                        field: 'electronicAccounts',
                        title: '电子账户',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }, {
                        field: 'userPhone',
                        title: '手机号',
                        sortable: 'true',
                        align: 'center',
                        width: 10,
                        formatter: function (value, row, index) {
                            var str = '<a href="#" name="opera" class="easyui-linkbutton" onclick="sendMsg(\'' + value + '\',\'' + row.password + '\');">' + value + '</a>';
                            return str;
                        }
                    }, {
                        field: 'charge',
                        title: '余额',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'operate', title: '详情', align: 'center', width: 5,
                        formatter: function (value, row, index) {
                            var str = '<a href="#" name="opera" class="easyui-linkbutton" onclick="showConsumePanelHistory(\'' + row.vinCode + '\');">查看</a>';
                            return str;
                        }
                    }
                ]],
                pagination: true,
                rownumbers: false,
                onLoadSuccess: function (data) {
                },
                toolbar: [],
                onCheck: function (rowIndex, rowData) {
                },
                onCheckAll: function (rows) {
                },
                onUncheck: function (rowIndex, rowData) {
                },
                onLoadSuccess: function (data) {
                },
                onUncheckAll: function (rows) {
                }

            });
    $('#datatable').datagrid('getPager').pagination({
        displayMsg: '当前显示从 {from} 到 {to} 共{total}条记录',
        onBeforeRefresh: function (pageNumber, pageSize) {
            $(this).pagination('loading');
            $(this).pagination('loaded');
        }
    });
}

function showConsumePanelHistory(vin) {

    $("#consume_dialog").show();
    $("#vinSelectCode").val(vin);
    $("#c_type").val("");
    setTimeout(function () {
        queryHistory(2);
    }, 100);

}

function sendMsg(phoneNum, password) {
    var param = {};
    param["phoneNum"] = phoneNum;
    var msgContent = "尊敬的用户您好，您的代金卷账户密码为" + password;
    var msgContentEx = "确定发送消息\"" + msgContent + "\"给客户吗？";
    var res
    if(confirm(msgContentEx)) {
        param["msgContent"] = msgContent;
        res= ajaxgetresultpost($("#paths").val() + '/coupon/sendMsg.do', param);
    }else{
        return;
    }
    if (res.sign) {
        alert("发送成功");
    } else {
        alert("发送失败");
    }
}


//代金券历史消费记录
function queryCouponConsumeHistoryList(urls, queryParams) {
    // 默认查询第一页 query查询时保存当前条件
    var TempPageNumber = 1, pageSize = 5;
    try {
        TempPageNumber = $('#datatable2').datagrid('getPager').pagination("options").pageNumber;
        pageSize = $('#datatable2').datagrid('getPager').pagination("options").pageSize;
    } catch (e) {

    }
    $('#datatable2')
        .datagrid(
            {
                title: "信息列表",
                iconCls: 'icon-search',
                pageNumber: TempPageNumber,
                pageSize: pageSize,
                pageList: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
                nowrap: true,
                striped: true,
                border: true,
                noheader: true,
                url: urls,
                loadMsg: '数据加载中......',
                queryParams: queryParams,
                remoteSort: false,
                fitColumns: true,
                singleSelect: false,
                columns: [[

                    {
                        field: 'vinCode',
                        title: 'VIN码',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }, {
                        field: 'consumeCount',
                        title: '消费金额',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'settlementState',
                        title: '消费状态',
                        sortable: 'true',
                        align: 'center',
                        width: 10,
                        formatter: function (value, row, index) {
                            return getSettlementStateStr(value);
                        }
                    }, {
                        field: 'remark',
                        title: 'DMS单号',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }, {
                        field: 'consumeTime',
                        title: '消费时间',
                        sortable: 'true',
                        align: 'center',
                        width: 10,
                        formatter: function (value, row, index) {
                            var str = new Date(value.time).Format("yyyy-MM-dd hh:mm:ss");
                            return str;
                        }
                    }
                ]],
                pagination: true,
                rownumbers: false,
                onLoadSuccess: function (data) {
                },
                toolbar: [],
                onCheck: function (rowIndex, rowData) {
                },
                onCheckAll: function (rows) {
                },
                onUncheck: function (rowIndex, rowData) {
                },
                onLoadSuccess: function (data) {
                },
                onUncheckAll: function (rows) {
                }

            });
    $('#datatable2').datagrid('getPager').pagination({
        displayMsg: '当前显示从 {from} 到 {to} 共{total}条记录',
        onBeforeRefresh: function (pageNumber, pageSize) {
            $(this).pagination('loading');
            $(this).pagination('loaded');
        }
    });
}
function getSettlementStateStr(val) {

    //结算状态 ：    0或者空 未结算，5 提交结算 ,10 同意付款,100 已付款,101 撤销
    if (val == null || val == '' || val == 0) {
        return '未结算';
    } else if (val == 5) {
        return '提交结算';
    } else if (val == 10) {
        return '同意付款';
    } else if (val == 100) {
        return '已付款';
    } else if (val == 101) {
        return '撤销';
    }
}
//代金券历史充值记录
function queryCouponRechargehistoryList(urls, queryParams) {
    // 默认查询第一页 query查询时保存当前条件
    var TempPageNumber = 1, pageSize = 5;
    try {
        TempPageNumber = $('#datatable2').datagrid('getPager').pagination("options").pageNumber;
        pageSize = $('#datatable2').datagrid('getPager').pagination("options").pageSize;
    } catch (e) {

    }
    $('#datatable2')
        .datagrid(
            {
                title: "信息列表",
                iconCls: 'icon-search',
                pageNumber: TempPageNumber,
                pageSize: pageSize,
                pageList: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
                nowrap: true,
                striped: true,
                border: true,
                noheader: true,
                url: urls,
                loadMsg: '数据加载中......',
                queryParams: queryParams,
                remoteSort: false,
                fitColumns: true,
                singleSelect: false,
                columns: [[

                    {
                        field: 'orderCode',
                        title: '案件编号',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }, {
                        field: 'rechargeCount',
                        title: '充值金额',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }
                    /*, {
                     field : 'consumeCount',
                     title : '已消费金额',
                     sortable : 'true',
                     align : 'center',
                     width : 10
                     }*/
                    , {
                        field: 'serviceName',
                        title: '服务类型',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'rechargeTime',
                        title: '充值时间',
                        sortable: 'true',
                        align: 'center',
                        width: 10,
                        formatter: function (value, row, index) {
                            var str = new Date(value.time).Format("yyyy-MM-dd hh:mm:ss");
                            return str;
                        }
                    }, {
                        field: 'overdueTime',
                        title: '过期时间',
                        sortable: 'true',
                        align: 'center',
                        width: 10,
                        formatter: function (value, row, index) {
                            var str = new Date(value.time).Format("yyyy-MM-dd hh:mm:ss");
                            return str;
                        }
                    }

                ]],
                pagination: true,
                rownumbers: false,
                onLoadSuccess: function (data) {
                },
                toolbar: [],
                onCheck: function (rowIndex, rowData) {
                },
                onCheckAll: function (rows) {
                },
                onUncheck: function (rowIndex, rowData) {
                },
                onLoadSuccess: function (data) {
                },
                onUncheckAll: function (rows) {
                }

            });
    $('#datatable2').datagrid('getPager').pagination({
        displayMsg: '当前显示从 {from} 到 {to} 共{total}条记录',
        onBeforeRefresh: function (pageNumber, pageSize) {
            $(this).pagination('loading');
            $(this).pagination('loaded');
        }
    });
}

