$(function () {
    $("#jqGrid").jqGrid({
        url: '../orderrefund/list',
        datatype: "json",
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '订单编号', name: 'orderSn', index: 'order_sn', width: 80},
			{label: '用户名称', name: 'userName', index: 'username', width: 80},
			{label: '商户退款单号', name: 'outRefundNo', index: 'out_refund_no', width: 80},
            {
                label: '申请时间', name: 'createTime', index: 'create_time', width: 100,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            },
			{label: '微信退款单号', name: 'refundId', index: 'refund_id', width: 80, hidden: true},
			{
			    label: '退款类型', name: 'refundType', index: 'refund_type', width: 80,
                formatter: function (value) {
                    if (value == '1') {
                        return '用户全额退款';
                    } else if (value == '2') {
                        return '系统部分退款';
                    }
                    return value;
                }
            },
			{
			    label: '退款时间', name: 'refundTime', index: 'refund_time', width: 100,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            },
			{label: '退款金额', name: 'refundMoney', index: 'refund_money', width: 80, align: 'right'},
			{
			    label: '退款状态', name: 'refundStatus', index: 'refund_status', width: 80,
                formatter: function (value) {
                    if (value == '1') {
                        return '申请中';
                    } else if (value == '2') {
                        return '退款成功';
                    } else if (value == '3') {
                        return '已拒绝';
                    }
                    return value;
                }
            },
			{label: '退款原因', name: 'refundReason', index: 'refund_reason', width: 200},
			{label: '退款入账账户', name: 'refundRecvAccout', index: 'refund_recv_accout', width: 80, hidden: true},
			{label: '审核人', name: 'approver', index: 'approver', width: 80, hidden: true},
			{
			    label: '审核时间', name: 'approvalTime', index: 'approval_time', width: 100,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            },
			{label: '审核备注', name: 'approvalRemark', index: 'approval_remark', width: 80},
            {
                label: '操作', width: 180, sortable: false,
                formatter: function (value, col, row) {
                    let htmlStr = '<button class="btn btn-outline btn-info" onclick="vm.lookDetail(' + row.id + ')"><i class="fa fa-info-circle"></i>详情</button>&nbsp;';
                    if (hasPermission('order:refund')) {
                        if (row.refundStatus == 1) {
                            htmlStr += '<button class="btn btn-outline btn-primary" onclick="vm.refundUpdate(' + row.orderId + ')"><i class="fa fa-check-circle"></i>&nbsp;退款</button>&nbsp;' +
                                '<button class="btn btn-outline btn-danger" onclick="vm.approval(' + row.id + ')"><i class="fa fa-times-circle-o"></i>审核</button>&nbsp;';
                        }
                    }
                    return htmlStr;
                }
            }],
		viewrecords: true,
        height: 645,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
	el: '#rrapp',
	data: {
        showList: 0,
        title: null,
		orderRefund: {},
		ruleValidate: {
            approvalRemark: [
				{required: true, message: '审核备注不能为空', trigger: 'blur'}
			]
		},
		q: {
            userName: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = 1;
			vm.title = "新增";
			vm.orderRefund = {};
		},
		update: function (event) {
            let id = getSelectedRow();
			if (id == null) {
				return;
			}
			vm.showList = 1;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.orderRefund.id == null ? "../orderrefund/save" : "../orderrefund/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.orderRefund),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows();
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
				$.ajax({
					type: "POST",
				    url: "../orderrefund/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function (r) {
						if (r.code == 0) {
							alert('操作成功', function (index) {
								$("#jqGrid").trigger("reloadGrid");
							});
						} else {
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../orderrefund/info/"+id, function (r) {
                vm.orderRefund = r.orderRefund;
            });
		},
        reloadSearch: function() {
            vm.q = {
                userName: ''
            }
            vm.reload();
		},
		reload: function (event) {
			vm.showList = 0;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'userName': vm.q.userName},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        refundUpdate: function (rowId) {
            let params = {};
            params.orderId = rowId;

            $.ajax({
                type: "POST",
                url: '../order/cancel',
                data: params,
                success: function (r) {
                    if (r.code === 0) {
                        alert('取消成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        approval: function(rowId) {
            vm.showList = 2;
            vm.title = "维权订单审核";
            $.get("../orderrefund/info/" + rowId, function (r) {
                vm.orderRefund = r.orderRefund;
            });
        },
        lookDetail: function (rowId) {
            vm.showList = 1;
            vm.title = "维权订单详情";
            $.get("../orderrefund/info/" + rowId, function (r) {
                vm.orderRefund = r.orderRefund;
            });
        }
	}
});