var device;
function process_desktop() {
	return {
		plan: $('INPUT[name="plan"]:checked').val(),
		name: $('INPUT[name="name"]').val(),
		phone: $('INPUT[name="phone"]').val(),
		email: $('INPUT[name="email"]').val(),
		reference: $('INPUT[name="reference"]').val(),
	}
}

function process_mobile() {
	return {
		plan: $('INPUT[name="plan-mobile"]:checked').val(),
		name: $('INPUT[name="name-mobile"]').val(),
		phone: $('INPUT[name="phone-mobile"]').val(),
		email: $('INPUT[name="email-mobile"]').val(),
		reference: $('INPUT[name="reference-mobile"]').val(),
	}
}

function order_success(data) {
	$('#mdlPaid').modal('show');
}

$(document).ready(function() {
	$('.btn-order').on('click', function () {
		console.log(document.body.offsetWidth);
		if (document.body.offsetWidth > 991) {
			$('#mdlOrderDesktop').modal('show');
		}
		else {
			$('#mdlOrderMobile').modal('show');
		}
	});

	$('INPUT[name="plan"]').on('change', function () {
		var _isChecked = $(this).is(':checked');
		var _id = $(this).attr('id');
		var _checkedId;
		var _price = 58;
		switch (_id) {
			case "6-months":
				if (_isChecked) {
					_checkedId = _id;
					_price = 38;
				}
				else {
					_checkedId = "12-months";
					_price = 58;
				}
				break;
			case "12-months":
				if (_isChecked) {
					_checkedId = _id;
					_price = 58;
				}
				else {
					_checkedId = "6-months";
					_price = 38;
				}
				break;
		}
		$('INPUT[name="plan"], INPUT[name="plan-mobile"]').prop('checked', false);
		$('#' + _checkedId + ', #' + _checkedId + '-mobile').prop('checked', true);
		$('.price').text(_price.toFixed(2));
		$('.qrcode-price').attr('src', base_url + 'assets/img/qrcode-' + _price + '.png');
	});

	$('INPUT[name="plan-mobile"]').on('change', function () {
		var _isChecked = $(this).is(':checked');
		var _id = $(this).attr('id').replace('-mobile', '');
		var _checkedId;
		var _price = 58;
		switch (_id) {
			case "6-months":
				if (_isChecked) {
					_checkedId = _id;
					_price = 38;
				}
				else {
					_checkedId = "12-months";
					_price = 58;
				}
				break;
			case "12-months":
				if (_isChecked) {
					_checkedId = _id;
					_price = 58;
				}
				else {
					_checkedId = "6-months";
					_price = 38;
				}
				break;
		}
		$('INPUT[name="plan"], INPUT[name="plan-mobile"]').prop('checked', false);
		$('#' + _checkedId + ', #' + _checkedId + '-mobile').prop('checked', true);
		$('.price').text(_price.toFixed(2));
		$('.qrcode-price').attr('src', base_url + 'assets/img/qrcode-' + _price + '.png');
	});

	$('.btn-form-submit').on('click', function (event) {
		event.preventDefault();
		var _data;
		device = $(this).attr('data-content');
		if (device == 'desktop') {
			if (! $('#agree').is(':checked')) {
				alert ("你必须同意个人资料收集声明");
				return false;
			}
			_data = process_desktop();
			$('#mdlOrderDesktop').modal('hide');
		}
		else {
			if (! $('#agree-mobile').is(':checked')) {
				alert ("你必须同意个人资料收集声明");
				return false;
			}
			_data = process_mobile();
			$('#mdlOrderMobile').modal('hide');
		}
		order_success({});
	});
});
