import React from 'react';
import {Table, Button} from 'semantic-ui-react';

const ListItem = ({i18n, data, status, delLeone, editLeone, runCmd}) => {
	let stats = i18n&&i18n.getResource&&i18n.language ?  i18n.getResource(i18n.language + '.translation.leone_stats') : [];
	let actlist = i18n&&i18n.getResource&&i18n.language ? i18n.getResource(i18n.language + '.translation.action_list') : [];
	let st = stats.filter(t => t.mode == status.mode);
	let tunit = i18n&&i18n.t ? [i18n.t('time_unit.sec'), i18n.t('time_unit.min'), i18n.t('time_unit.hour'), i18n.t('time_unit.day')] : [];
	let idx = 0;
	let mtime = Math.floor(Date.now() / 1000) - status.mtime;
	while(idx < 3){
		if(mtime >= 60){
			mtime = Math.floor(mtime / 60);
			idx++;
		}else{
			break;
		}
	}
	return (
		<Table.Row>
			<Table.Cell>
				<Button type="button" basic size="tiny" color="red" content={i18n&&i18n.t ? i18n.t('page.leone.table.button.del') : ''} onClick={() => {delLeone(data.leonelistuid)}} />
				<Button type="button" basic size="tiny" color="blue" content={i18n&&i18n.t ? i18n.t('page.leone.table.button.edit') : ''} onClick={() => {editLeone(1, data)}} />
			</Table.Cell>
			<Table.Cell>{data.leonename}</Table.Cell>
			<Table.Cell>{data.leoneip}</Table.Cell>
			<Table.Cell>{`${status.ts == '9999' || !isFinite(status.ts) ? '-' : `${status.ts} ${String.fromCharCode(8451)}`} / ${status.hs == '9999' || !isFinite(status.hs) ? '-' : `${status.hs} %`}`}</Table.Cell>
			<Table.Cell>{st[0]&&st[0].name ? st[0].name : status.mode}</Table.Cell>
			<Table.Cell>{data.leonepassword}</Table.Cell>
			<Table.Cell>{`${mtime}${tunit[idx]}${i18n&&i18n.t? i18n.t('time_unit.ago') : ''}`}</Table.Cell>
			<Table.Cell>
				<select onChange={(e)=>{
						runCmd(data.leonelistuid, data.leonename, e.target.value);
						e.target.selectedIndex = 0;
					}}>
					<option value="">{i18n&&i18n.t ? i18n.t('tip.select_action') : ''}</option>
					{
						actlist.map((t, idx) => <option key={idx} value={t.cmd}>{t.name}</option>)
					}
				</select>
			</Table.Cell>
		</Table.Row>
	)
}

export default ListItem;