import React, { useMemo, Fragment, useEffect } from 'react';
import { RouteComponentProps } from 'react-router-dom';
import {
	Spin,
	Descriptions,
	Form,
	Timeline,
	Divider,
	Image,
	Input,
	Switch,
	Row,
	Button,
	Col,
	message,
} from 'antd';
import {
	useTaskDetails,
	TaskProcess,
	useUpdateTask,
	TaskType,
	taskStatusEnums,
} from './hooks';
import Upload from '../../compontens/upload';

const TaskDetail: React.FC<RouteComponentProps<{ id: string }>> = ({
	match,
}) => {
	const [form] = Form.useForm();
	const { state: detailState, request: requestDetail } = useTaskDetails();
	const { state: upTaskState, request: requestUpdateTask } = useUpdateTask();
	const taskId = +match.params.id;
	useEffect(() => {
		requestDetail(+match.params.id);
	}, [taskId]);
	const handleSumitForm = (data) => {
		requestUpdateTask({ ...data, id: taskId })
			.then(() => {
				message.success('审批成功');
				requestDetail(taskId);
				form.resetFields();
			})
			.catch((error) => {
				message.error(error.message);
			});
	};
	const isCanHandle =
		detailState.dataSource?.status !== taskStatusEnums.reviewed;
	return (
		<Spin spinning={detailState.loading}>
			<Button
				type='primary'
				onClick={() => {
					history.back();
				}}>
				返回
			</Button>
			<Divider>任务详情</Divider>
			<Descriptions title='任务描述'>
				{[
					{
						key: 'task_name',
						label: '任务名称',
					},
					{
						key: 'task_code',
						label: '任务编码',
					},
					{
						key: 'task_type',
						label: '任务类型',
						value: (value) => value,
					},
				].map((infos) => {
					return (
						<Descriptions.Item label={infos.label} key={infos.key}>
							<b>
								{infos.value?.(TaskType[detailState.dataSource?.[infos.key]]) ||
									detailState.dataSource?.[infos.key]}
							</b>
						</Descriptions.Item>
					);
				})}
			</Descriptions>
			<Divider>任务进度</Divider>
			<Timeline>
				{detailState.dataSource?.list.map((process, index) => {
					return (
						<Timeline.Item key={index}>
							<Descriptions
								bordered
								layout='vertical'
								title={TaskProcess[process.pipe_type]}>
								{[
									{ label: '操作人', key: 'person' },
									{ label: '操作时间', key: 'pipe_time' },
									{
										label: '审批状态',
										key: 'approve_status',
										renderValue: process.approve_status ? '通过' : '拒绝',
									},
									{ label: '审批内容', key: 'desc' },
									{
										label: '附加内容',
										key: 'img',
										renderValue: (
											<div>
												{process.img.map((img) => {
													return (
														<Image width={200} src={img} key={img}></Image>
													);
												})}
											</div>
										),
									},
								].map((infos) => {
									return (
										<Descriptions.Item label={infos.label} key={infos.key}>
											<b>{infos.renderValue || process[infos.key] || '-'}</b>
										</Descriptions.Item>
									);
								})}
							</Descriptions>
						</Timeline.Item>
					);
				})}
			</Timeline>
			{isCanHandle && (
				<Fragment>
					<Divider>审批</Divider>
					<Row>
						<Col span={6}>
							<Form onFinish={handleSumitForm} form={form}>
								{[
									{
										label: '处理人',
										name: 'processing_person',
										required: true,
										children: <Input placeholder='请输入处理人'></Input>,
									},
									{
										name: 'processing_results',
										required: true,
										label: '备注',
										children: (
											<Input.TextArea placeholder='请输入备注'></Input.TextArea>
										),
									},
									{
										name: 'processing_results_img',
										label: '附件',
										required: true,
										valuePropName: 'fileList',
										children: <Upload></Upload>,
									},
								].map((formItem) => {
									return (
										<Form.Item
											rules={[
												{
													required: formItem.required,
													message: `请输入${formItem.label}`,
												},
											]}
											{...formItem}
											key={formItem.name}></Form.Item>
									);
								})}
								<Button
									type='primary'
									htmlType='submit'
									size='large'
									loading={upTaskState.loading}>
									审核
								</Button>
							</Form>
						</Col>
					</Row>
				</Fragment>
			)}
		</Spin>
	);
};
export default TaskDetail;
