import React, { useState, useEffect, useMemo, Fragment } from 'react';
import { Table, Button, Row } from 'antd';
import { RouteComponentProps } from 'react-router-dom';
import { ColumnProps } from 'antd/lib/table';
import {
	useTaskList,
	TaskRecordProps,
	TaskType,
	taskStatus,
} from '../../hooks/task';
import Search from './components/warnSearch';
const columns: ColumnProps<TaskRecordProps>[] = [
	{
		title: '任务名',
		dataIndex: 'task_name',
		key: 'task_name',
	},

	{
		title: '任务编号',
		dataIndex: 'task_code',
		key: 'task_code',
	},

	{
		title: '派发日期',
		dataIndex: 'created_time',
		key: 'created_time',
	},

	{
		title: '超期',
		dataIndex: 'is_overdue',
		key: 'is_overdue',
		render: (_, record) => (record.is_overdue ? '是' : '否'),
	},
	{
		title: '任务状态',
		dataIndex: 'status',
		key: 'status',
		render: (_, record) => taskStatus[record.status],
	},
];
const TaskList: React.FC<RouteComponentProps> = ({ history }) => {
	const [searchState, changeSearchState] = useState({
		overdue: 0,
		page: 1,
		task_type: 1,
	});
	const { state: taskListState, request: requestTaskList } = useTaskList();
	useEffect(() => {
		requestTaskList(searchState as any);
	}, [JSON.stringify(searchState)]);
	const tableColums = useMemo(() => {
		return columns.concat({
			title: '操作',
			render: (record) => {
				return (
					<Button
						type='primary'
						onClick={() => {
							history.push(`/admin/task/${record.id}`);
						}}>
						详情
					</Button>
				);
			},
		});
	}, [columns]);
	return (
		<Fragment>
			<Search
				onSearch={(data) => {
					changeSearchState((preState) => ({
						...preState,
						...data,
						page: 1,
					}));
				}}></Search>
			<Table
				rowKey={(record) => record.id}
				loading={taskListState.loading}
				pagination={{
					hideOnSinglePage: true,
					current: taskListState.page,
					onChange: (page) => {
						changeSearchState((preState) => ({ ...preState, page }));
					},
				}}
				columns={tableColums}
				dataSource={taskListState.dataSource}></Table>
		</Fragment>
	);
};
export default TaskList;
