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

const ListItem = ({i18n, idx, data, dos, les, ios, changeActive, openModal, delItem, showGroup}) => {
	let actlist = i18n&&i18n.getResource&&i18n.language ? i18n.getResource(i18n.language + '.translation.action_list') : [];
	let weekArr = i18n&&i18n.t ? [
        i18n.t('week.mon'),
        i18n.t('week.tue'),
        i18n.t('week.wed'),
        i18n.t('week.thu'),
        i18n.t('week.fri'),
        i18n.t('week.sat'),
        i18n.t('week.sun')
    ] : [];

	let timeStr = '';
	let min = data.ioscheduleparam1 || '';
	let hour = data.ioscheduleparam2 || '';
	let day = data.ioscheduleparam3 || '';
	let month = data.ioscheduleparam4 || '';
	let week = data.ioscheduleparam5 || '';
	if(week != '-'){
		let w = week.split(',');
		let ws = [];
		w.sort();
		for(let i in w){
			ws.push(weekArr[w[i] - 1] || '');
		}
		timeStr = `${ws.join(',')} ${hour}:${min}`;
	}else{
		timeStr = `${month}/${day} ${hour}:${min}`;
	}

	let act = data.ioschedulecmd || '';
	act = act.split(',');
	let actcmd = '';
	let actname = '';
	if(act.length == 2){
		if(act[0] == 2){
			actcmd = '2'
		}else{
			actcmd = act.join(' ');
		}
	}
	if(actcmd.length > 0){
		for(let i in actlist){
			if(actlist[i].cmd == actcmd){
				actname = actlist[i].name;
			}
		}
	}

	let devs = data.ioscheduleio || '';
	let dev = devs.split(',');
	let items = [];
	if(dev.length > 0){
		for(let i in dev){
			if(/^do/i.test(dev[i])){
				for(let j in dos){
					if(`do${dos[j].douid}` == dev[i]) {
						items.push({
							type: 'DigitOutput',
							name: dos[j].doname,
							id: dev[i]
						});
						break;
					}
				}
			}else if(/^le/i.test(dev[i])){
				for(let j in les){
					if(`le${les[j].leonelistuid}` == dev[i]) {
						items.push({
							type: 'LeOne',
							name: les[j].leonename,
							id: dev[i]
						});
						break;
					}
				}
			}else if(/^iogroup/i.test(dev[i])){
				for(let j in ios){
					if(`iogroup${ios[j].iogroupuid}` == dev[i]){
						items.push({
							type: 'IOGroup',
							name: ios[j].iogroupname,
							id: dev[i]
						});
					}
				}
			}
		}
	}

	return (
		<Table.Row>
			<Table.Cell>
				<Button basic size="tiny" color={data.ioscheduleactive == 1 ? 'orange' : 'olive'} 
					content={i18n&&i18n.t ? (data.ioscheduleactive == 1 ? i18n.t('page.schedule.table.button.disable') : i18n.t('page.schedule.table.button.enable')) : ''} 
					onClick={()=>{ changeActive(data.ioscheduleuid || ''); }} />
				<Button basic size="tiny" color="red" content={i18n&&i18n.t ? i18n.t('page.schedule.table.button.del') : ''} onClick={() => {delItem(data.ioscheduleuid || '')}} />
				<Button basic size="tiny" color="blue" content={i18n&&i18n.t ? i18n.t('page.schedule.table.button.edit') : ''} onClick={() => {openModal(1, data, items);}} />
			</Table.Cell>
			<Table.Cell>{data.ioschedulename || ''}</Table.Cell>
			<Table.Cell>{timeStr}</Table.Cell>
			<Table.Cell>{actname}</Table.Cell>
			<Table.Cell>{i18n&&i18n.t ? (data.ioscheduleactive == 1 ? i18n.t('page.schedule.table.button.enable') : i18n.t('page.schedule.table.button.disable')) : ''}</Table.Cell>
			<Table.Cell>
				{
					items.length > 0 ? 
					(items.length == 1 ?
						<Item><Label content={items[0].type} />{items[0].name}</Item>: 
						<Button size="tiny" basic content={i18n&&i18n.t ? i18n.t('page.schedule.table.button.showgroup') : ''} onClick={()=>{showGroup(items)}}/> ) :
					 ''
				}
			</Table.Cell>
		</Table.Row>
	)
}

export default ListItem;