/**
 * 报销列表页
 * 
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/ 
 * 
 * @author Strief http://blog.u-bo.com
 * @since 0.1
 */
jQuery(function($) {

	$('.show-div').mouseover(function(event) {
		show(event, 1, $(this).data('title'), $(this).data('remark'));
	});
	$('.show-div').mouseout(function(event) {
		show(event, 0);
	});

	$(document).on('focus', '#reimbursement-begin_time', function() {
		$('#reimbursement-end_time').val('');
	});

	/**
	 * 列表页快捷审核操作
	 */
	$(document).on('click', '.set-vreify-status', function() {
		if ($(this).data('status') == 'accept') {
			if (!confirm('确定通过该条审核？')) {
				return false;	
			} else {
				$.ajax({
					type: 'GET',
					dataType: 'json',
					url: $(this).data('setUrl'),
					success: function (json) {
					    art.dialog({
		                    title: '成功提示',
		                    time: 3,
		                    icon: 'succeed',
		                    content: json.info,
		                    left:'40%',
		                    top:'30%',
		                    close: function() {
		                        var opener = art.dialog.opener;
		                        var mainDialog = opener.art.dialog.list['main-dialog'];
		                        if (mainDialog != undefined) {
		                            mainDialog.close();
		                        }
		                        return true;
		                    }
		                });
						//artDialog.tips(resp.info);
						//var top = art.dialog.opener;
			            //top.location.reload();
			            setTimeout("window.location.reload()",1000);
						//window.location.href = index;
					},
					statusCode: {
					    403: function (xhr) {
					    xhr.responseText = $.parseJSON(xhr.responseText);
					    //console.log(xhr.responseText.info);
					    art.dialog({
		                    title: '失败提示',
		                    time: 3,
		                    icon: 'error',
		                    left:'40%',
		                    top:'30%',
		                    content: xhr.responseText.info
		                });
					    }
					}
				});
			}
		}
		if ($(this).data('status') == 'veto') {
			openStatusDialog($(this).data('setUrl'));
		}
	});

	/**
	 * 列表页更改结算状态操作
	 */
	$(document).on('click', '.set-isclearing', function() {
		if (!confirm('确定更改该条报销结算状态？')) {
			return false;	
		}
		$.ajax({
			type: 'GET',
			dataType: 'json',
			url: $(this).data('setUrl'),
			success: function (json) {
				art.dialog({
		            title: '成功提示',
		            time: 3,
		            icon: 'succeed',
		            content: json.info,
		            left:'40%',
		            top:'30%',
		            close: function() {
		                var opener = art.dialog.opener;
		                var mainDialog = opener.art.dialog.list['main-dialog'];
		                if (mainDialog != undefined) {
		                            mainDialog.close();
		                        }
		                        return true;
		                    }
		                });
						//var top = art.dialog.opener;
			            setTimeout("window.location.reload()",1000);
					},
					statusCode: {
					    403: function (xhr) {
					    xhr.responseText = $.parseJSON(xhr.responseText);
					    art.dialog({
		                    title: '失败提示',
		                    time: 3,
		                    icon: 'error',
		                    left:'40%',
		                    top:'30%',
		                    content: xhr.responseText.info
		                });
					    }
					}
				});
	});

	/**
	 * 详情页快捷审核操作
	 */
	$(".change-status-select").on('change', function() {
		var status = $(this).find('option:selected').val();
		if (status == enable) {
			if (!confirm('确定通过这条审核？')) {
				return false;
			} else {
				$.ajax({
					type: 'GET',
					dataType: 'json',
					url: $(this).data('setUrl')+'&status='+status,
					success: function (json) {
					    art.dialog({
		                    title: '成功提示',
		                    time: 3,
		                    icon: 'succeed',
		                    content: json.info,
		                    close: function() {
		                        var opener = art.dialog.opener;
		                        var mainDialog = opener.art.dialog.list['main-dialog'];
		                        if (mainDialog != undefined) {
		                            mainDialog.close();
		                        }
		                        return true;
		                    }
		                });
						setTimeout("window.location.href = detail",2000);
					},
					statusCode: {
					    403: function (xhr) {
					    xhr.responseText = $.parseJSON(xhr.responseText);
					    //console.log(xhr.responseText.info);
					    art.dialog({
		                    title: '失败提示',
		                    time: 3,
		                    icon: 'error',
		                    content: xhr.responseText.info
		                });
					    }
					}
				});
			}
		}
		if (status == disable) {
			openStatusDialog($(this).data('setUrl')+'&status='+status, status);
		}
	});

	/**
	 * 详情页更改结算状态操作
	 */
	$(".change-isclearing-select").on('change', function() {
		var isclearing = $(this).find('option:selected').val();
			if (!confirm('确定更改这条结算状态？')) {
				return false;
			} else {
				$.ajax({
					type: 'GET',
					dataType: 'json',
					url: $(this).data('setUrl')+'&isclearing='+isclearing,
					success: function (json) {
					    art.dialog({
		                    title: '成功提示',
		                    time: 3,
		                    icon: 'succeed',
		                    content: json.info,
		                    close: function() {
		                        var opener = art.dialog.opener;
		                        var mainDialog = opener.art.dialog.list['main-dialog'];
		                        if (mainDialog != undefined) {
		                            mainDialog.close();
		                        }
		                        return true;
		                    }
		                });
						setTimeout("window.location.href = detail",2000);
					},
					statusCode: {
					    403: function (xhr) {
					    xhr.responseText = $.parseJSON(xhr.responseText);
					    //console.log(xhr.responseText.info);
					    art.dialog({
		                    title: '失败提示',
		                    time: 3,
		                    icon: 'error',
		                    content: xhr.responseText.info
		                });
					    }
					}
				});
			}
	});

	/**
	 * 自定义报销弹窗操作
	 */
	$(document).on('click', '.open-verify-dialog', function() {
        var params = {
            url: $(this).data('url'),
            width: $(this).data('width'),
            height: $(this).data('height'),
            title: $(this).data('title'),
            isReload: true,
            isSubmit: true,
        }
        if ($(this).data('reload') == false) {
            params.isReload = false;
        }
        if ($(this).data('submit') == false) {
            params.isSubmit = false;
        }
        openVerifyDialog(params);
    });
});

/**
 * 鼠标滑过显示报销信息
 */
function show(event, type, title, remark) {
        if (type) {
            var eInfo = title + '<br /><span style="color:#CC2104">备注：' + remark + '</span>';
            $("#info").html(eInfo);
            $('#info').css({'top': (event.pageY), 'left': (event.pageX), 'display': 'block'});
        } else {
            $("#info").hide();
        }
    }

//当审核操作为否决时，显示填写否决理由弹窗
function openStatusDialog(url,status) {
	art.dialog.through({
		content: '<textarea id="textarea" style="width:240px; height:110px;" />',
        id: 'status-dialog',
		title: '请填写否决原因',
		lock: true,
		width: 250,
		height: 150,
		//left:'40%',
		ok: function() {
			var input = art.dialog.top.document.getElementById('textarea');
			if (input.value != '') {
				$.ajax({
					type: 'GET',
					dataType: 'json',
					url: url,
					data:{remark:input.value},
					success: function (json) {
					    art.dialog.through({
		                    title: '成功提示',
		                    time: 3,
		                    icon: 'succeed',
		                    content: json.info,
		                    close: function() {
		                        var opener = art.dialog.opener;
		                        var mainDialog = opener.art.dialog.list['main-dialog'];
		                        if (mainDialog != undefined) {
		                            mainDialog.close();
		                        }
		                        return true;
		                    }
		                });
			            setTimeout("window.location.reload()",1000);
						//top.location.reload();
					},
					statusCode: {
					    403: function (xhr) {
					    xhr.responseText = $.parseJSON(xhr.responseText);
					    art.dialog.through({
		                    title: '失败提示',
		                    time: 3,
		                    icon: 'error',
		                    content: xhr.responseText.info
		                });
					    }
					}
				});
			} else {
				alert('请填写否决原因')
				return false;
			}
			return false;
		},
        okVal: '提交',
		cancel: true,
		cancelVal: '取消'
	});
	return false;
}

//弹窗
function openVerifyDialog(params) {
    art.dialog.open(params.url, {
        id: 'main-dialog',
        title: params.title,
        lock: true,
        width: params.width || 650,
        height: params.height || 450,
        button: [
        	{
        		name: '通过',
        		callback: function() {
        			if (params.isSubmit) {
		                var iframe = this.iframe.contentWindow;
		                if (!iframe.document.body) {
		                    alert('正在加载中...')
		                    return false;
		                }
		                iframe.$("#main-form").trigger('submit');
		            }
		            return false;
        		},
        		focus: true
        	},
        	{
        		name: '否决',
        		callback: function() {
        			if (params.isSubmit) {
		                var iframe = this.iframe.contentWindow;
		                if (!iframe.document.body) {
		                    alert('正在加载中...')
		                    return false;
		                }
		                iframe.$("input[name='verify_status']").val(disable);
		                iframe.$("#main-form").trigger('submit');
		            }
		            return false;
        		},
        	}
        ],
        close: function() {
            var top = art.dialog.opener;
            if (params.isReload) {
                top.location.reload();
            }
        },
        cancel: true,
        cancelVal: '放弃'
    });
    return false;
}

//弹窗
function openDialog(params) {
    art.dialog.open(params.url, {
        id: 'main-dialog',
        title: params.title,
        lock: true,
        width: params.width || 650,
        height: params.height || 450,
        ok: function() {
            if (params.isSubmit) {
                var iframe = this.iframe.contentWindow;
                if (!iframe.document.body) {
                    alert('正在加载中...')
                    return false;
                }
                iframe.$("#main-form").trigger('submit');
            }
            return false;
        },
        okVal: '保存',
        close: function() {
            var top = art.dialog.opener;
            if (params.isReload) {
            	if ($('#iframe_default').attr('name') =='iframe') {
            		$('#iframe_default')[0].contentWindow.location.reload();
            	} else {
            		top.location.reload();
            	}
            }
        },
        cancel: true,
        cancelVal: '放弃'
    });
    return false;
}