/**
 * 报销申请
 */
var showModal = function(msg, time, url) {
    var msg = msg || '出错了';
    var time = time || 3000;
    $(".modal-body").html(msg);
    $(".modal").modal();
    clearTimeout(modalTimer);
    var modalTimer = setTimeout(function() {
        $(".modal").modal('hide');
    }, time);
    if (url !== undefined) {
        $('.modal').on('hidden.bs.modal', function (e) {
            window.location.href = url;
        })
    }
};

var getFormValue = function (name) {
    var formFields = $("#main-form").serializeArray();
    var value = undefined;
    if (name == undefined) {
        return formFields;
    }
    $.each(formFields, function (index, field) {
        if (name == field.name) {
            value = field.value;
            return false;
        }
    });
    return value;
}


var validateForm = function () {
    var reimTime = getFormValue('Reimbursement[reim_time]');
    if (reimTime == undefined || reimTime == '')
        return {error:'请点击选择日期'};
    var name = getFormValue('Reimbursement[name]');
    if (name == undefined || name == '')
        return {error:'请输入用途'};
    var reimMoney = getFormValue('Reimbursement[reim_money]');
    if (reimMoney == undefined || reimMoney == '')
        return {error:'请输入金额'};
    else if (!/^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(reimMoney))
        return {error:'金额必须是数字'};
    var billAmount = getFormValue('Reimbursement[bill_amount]');
    if (billAmount == undefined || billAmount == '')
        return {error:'请输入票据'};
    else if (!/^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(billAmount))
        return {error:'票据必须是数字'};
    
    return true;
};

jQuery(function($) {

    //保存报销
    $("#save-btn").on('click', function() {

        if ((validate = validateForm()) !== true) {
            showModal(validate.error);
            return false;
        }

        if ($(this).html() == '保  存') 
            $(this).html('保存中...');
        else 
            return false;
        var actionUrl = $("#main-form").attr('action');
        $.post(actionUrl, $("#main-form").serialize(), function(json) {
            if (json.status == 0) {
                showModal(json.info);
                $("#save-btn").html('保  存');
            }
            else {
                showModal(json.info, 3000, json.url);
            }
        });
    });
    
    //选择申请单
    $("#appli-input").on('click', function(){
        var chooseUrl = $(this).data('chooseUrl');
        $.post(chooseUrl, $("#main-form").serialize(), function(json) {
            if (json.status == 1) {
                window.location.href = json.url;
            }
        });
    });

    var currYear = (new Date()).getFullYear();
	var opt = {
		theme: 'android-ics light', //皮肤样式
		mode: 'scroller', //日期选择模式
		lang: 'zh',
		startYear: currYear-1, //开始年份
		endYear: currYear, //结束年份
        maxDate: new Date(),
		setText: '确定',
		cancelText: '取消',
		dateFormat: 'yy-mm-dd',
		dateOrder: 'yymmdd',
		dayNames: ['周日', '周一;', '周二;', '周三', '周四', '周五', '周六'],
		dayNamesShort: ['日', '一', '二', '三', '四', '五', '六'],
		dayText: '日',
		hourText: '时',
		minuteText: '分',
		monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
		monthNamesShort: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
		monthText: '月',
		secText: '秒',
		timeFormat: 'HH:ii',
		timeWheels: 'HHii',
		yearText: '年'
	};
	$("input[name='Reimbursement[reim_time]']").mobiscroll().date(opt);


});
