import { Grid, Typography, FormControlLabel, Checkbox, Box, TextField } from '@mui/material';

/**
 * One Hold at Terminal option for a carrier that returns a single terminal price: turn it on, name
 * it and mark it up. An optional note under the option tells when the carrier quotes it.
 */
const HoldAtTerminal = ({ state = {}, handleChange = (name = '', val = '') => {}, note = '' }) => {
	const enabled = [true, 1, '1', 'true', 'yes', 'on'].includes(state?.hold_at_terminal);

	return (
		<Grid container spacing={1} flex mt={3} mb={3} className='woo-rtsq-hold-at-terminal'>
			<Grid item flex={1} xs={4}>
				<Typography variant='subtitle1' fontWeight='bold'>
					Hold at terminal
				</Typography>
			</Grid>

			<Grid item flex={2} xs={8}>
				<FormControlLabel
					control={
						<Checkbox
							checked={enabled}
							onChange={e => handleChange('hold_at_terminal', e.target.checked)}
						/>
					}
					label='Offer hold at terminal as an option'
					title='Offer hold at terminal as an option'
				/>

				{note && (
					<Typography variant='body2' color='text.secondary' className='woo-rtsq-hold-at-terminal-note'>
						{note}
					</Typography>
				)}

				<Box mt={2} sx={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
					<TextField
						label='Label for hold at terminal option'
						title='Label for hold at terminal option'
						variant='standard'
						helperText='What the user sees during checkout, e.g. "LTL Freight". If left blank, the quote label followed by "(T)" will display as the shipping method.'
						InputLabelProps={{ shrink: true }}
						inputProps={{ maxLength: 50 }}
						fullWidth
						disabled={!enabled}
						value={state?.hold_at_terminal_label ?? ''}
						onChange={e => handleChange('hold_at_terminal_label', e.target.value)}
					/>

					<TextField
						label='Markup for hold at terminal option'
						title='Markup for hold at terminal option'
						variant='standard'
						helperText='Adjust the price of the Hold At Terminal option. Enter an amount, e.g. 3.75, or a percentage, e.g. 5%. Leave blank to use the price returned by the carrier.'
						InputLabelProps={{ shrink: true }}
						inputProps={{ maxLength: 7 }}
						fullWidth
						value={state?.hold_at_terminal_price ?? ''}
						onChange={e => {
							e.target.value.match(/^-?[0-9]*\.?[0-9]*%?$/) &&
								handleChange('hold_at_terminal_price', e.target.value);
						}}
					/>
				</Box>
			</Grid>
		</Grid>
	);
};

export default HoldAtTerminal;
