/*
	Created by Michael Schuijff <michael@reglobe.nl>
	Copyright Lost Images, The Netherlands
	
	For more information, visit www.michaelschuijff.nl
*/

router.register('/report/$type/$id', (values) => {
	if (!(values.type == 'post' || values.type == 'comment' || values.type == 'message')) {
		router.redirect('/');
		return;
	}

	if (!config.user) {
		if (!config.bot) {
			router.redirect('/login', { url: router.getURL() });
		} else {
			router.noIndex();
		}

		return;
	}
	
	let view = createView({
		hideMainTitleBar: true, hideBottomMenu: true, titleBar: __('Report user'), buttons: ['close'], className: 'report-view'
	});
	
	let notice = createElement('p', null, __('What do you want to report this ' + values.type + ' for?'));
	
	let form = createElement('form');
	
	let options = [
		{
			value: 'spam',
			label: __('Spam, annoying or irrelevant')
		}, {
			value: 'harassment',
			label: __('Harassment or bullying')
		}, {
			value: 'language',
			label: __('Use of bad language')
		}, {
			value: 'personal',
			label: __('Sharing of personal information')
		}, {
			value: 'threatening',
			label: __('Threatening, violent or suicidal content')
		}, {
			value: 'explicit',
			label: __('Sexually explicit content')
		}, {
			value: 'plagiarism',
			label: __('Plagiarism or copyright infringement')
		}
	];

	let reason;

	for (let option of options) {
		let input = createElement('input', 'report');
		Object.assign(input, { type: 'radio', name: 'reason', value: option.value });
		
		input.onclick = () => reason = option.value;
		
		let label = createElement('label', null, input, createElement('span', null, option.label));
		label.for = 'report-' + option.value;
		
		form.append(label);
	}
	
	let button = createElement('button', 'button button-submit', __('Submit'));
	
	button.onclick = () => {
		if (!reason) {
			alert(__('You must select one of these reasons.'));
			return;
		}
		
		api.post({
			url: '/report/' + values.type + '/' + values.id,
			data: { reason: reason },
			success: () => toast(__('Thank you for your feedback.'))
		});
		
		router.back();
	}
	
	form.append(button);
	
	view.content.append(notice, form);		
	
	return view;
});
