/** @format */
import { PlusOutlined } from "@ant-design/icons";
import type { ActionType, ProColumns } from "@ant-design/pro-components";
import { ProTable, TableDropdown } from "@ant-design/pro-components";
import { Button, Col, Drawer, Form, Input, Row, Space } from "antd";
import React, { useRef, useState } from "react";

type AliOssConfigItem = {
	id: number;
	userId: number;
	bucketName: number;
	endpoint: string;
	accessKey: string;
	secretKey: string;
	region: string;
	createdTime: string;
	updatedTime: string;
	status: string;
};
const data: AliOssConfigItem[] = [
	{
		id: 1,
		userId: 1,
		bucketName: 666,
		endpoint: "https://oss.aliyuncs.com",
		accessKey: "LTAI5tG3",
		secretKey: "G3",
		region: "G3",
		createdTime: "2022-01-01",
		updatedTime: "2022-01-01",
		status: "正常",
	},
];

const columns: ProColumns<AliOssConfigItem>[] = [
	{
		dataIndex: "index",
		valueType: "indexBorder",
		width: 48,
	},
	{
		title: "ID",
		dataIndex: "id",
		copyable: true,
		ellipsis: true,
		tooltip: "Id",
	},
	{
		disable: true,
		title: "服务地址",
		dataIndex: "endpoint",
		tooltip: "endpoint",
		ellipsis: true,
	},
	{
		disable: true,
		title: "密钥ID",
		dataIndex: "accessKey",
		tooltip: "access key",
		ellipsis: true,
	},
	{
		disable: true,
		title: "密钥值",
		dataIndex: "secretKey",
		tooltip: "secret key",
		ellipsis: true,
	},
	{
		disable: true,
		title: "地区",
		dataIndex: "region",
		tooltip: "region",
		ellipsis: true,
	},
	{
		disable: true,
		title: "状态",
		dataIndex: "status",
		search: true,
	},
	{
		title: "创建时间",
		dataIndex: "createdTime",
		valueType: "dateTime",
		sorter: true,
		hideInSearch: true,
	},
	{
		title: "更新时间",
		dataIndex: "updatedTime",
		valueType: "dateTime",
		sorter: true,
	},
	{
		title: "操作",
		valueType: "option",
		key: "option",
		// eslint-disable-next-line @typescript-eslint/ban-ts-comment
		// @ts-expect-error
		render: (text, record, _, action) => [
			<a
				key="editable"
				onClick={() => {
					action?.startEditable?.(record.id);
				}}>
				编辑
			</a>,
			<a target="_blank" rel="noopener noreferrer" key="view">
				查看
			</a>,
			<TableDropdown
				key="actionGroup"
				onSelect={() => action?.reload()}
				menus={[
					{ key: "copy", name: "复制" },
					{ key: "delete", name: "删除" },
				]}
			/>,
		],
	},
];

const JdSettings: React.FC = () => {
	const actionRef = useRef<ActionType>();
	const [open, setOpen] = useState(false);
	const showDrawer = () => {
		setOpen(true);
	};

	const onClose = () => {
		setOpen(false);
	};
	const AddJdOssConfigDrawer = () => {
		return (
			<>
				<Drawer
					title="创建连接配置"
					width={720}
					onClose={onClose}
					open={open}
					styles={{
						body: {
							paddingBottom: 80,
						},
					}}
					extra={
						<Space>
							<Button onClick={onClose}>取消</Button>
							<Button onClick={onClose} type="primary">
								提交
							</Button>
						</Space>
					}>
					<Form layout="vertical">
						<Row gutter={16}>
							<Col span={12}>
								<Form.Item
									name="endpoint"
									label="服务地址"
									rules={[{ required: true, message: "请输入服务地址！" }]}>
									<Input placeholder="请输入服务地址！" />
								</Form.Item>
							</Col>
							<Col span={12}>
								<Form.Item
									name="region"
									label="地区"
									rules={[{ required: true, message: "请输入地区！" }]}>
									<Input placeholder="请输入地区！" />
								</Form.Item>
							</Col>
						</Row>
						<Row gutter={16}>
							<Col span={12}>
								<Form.Item
									name="accessKey"
									label="密钥ID"
									rules={[{ required: true, message: "请输入密钥ID!" }]}>
									<Input placeholder="请输入密钥ID！" />
								</Form.Item>
							</Col>
							<Col span={12}>
								<Form.Item
									name="secretKey"
									label="密钥值"
									rules={[{ required: true, message: "请输入密钥值!" }]}>
									<Input placeholder="请输入密钥值！" />
								</Form.Item>
							</Col>
						</Row>
					</Form>
				</Drawer>
			</>
		);
	};
	return (
		<>
			<ProTable<AliOssConfigItem>
				columns={columns}
				dataSource={data}
				actionRef={actionRef}
				cardBordered={true}
				editable={{
					type: "multiple",
				}}
				columnsState={{
					persistenceKey: "pro-table-singe-demos",
					persistenceType: "localStorage",
					defaultValue: {
						option: { fixed: "right", disable: true },
					},
				}}
				rowKey="id"
				search={false}
				options={{
					setting: {
						listsHeight: 400,
					},
				}}
				pagination={{
					pageSize: 5,
				}}
				dateFormatter="string"
				headerTitle="京东云OSS配置"
				bordered={false}
				toolBarRender={() => [
					<Button
						key="button"
						icon={<PlusOutlined />}
						onClick={() => {
							showDrawer();
						}}
						type="primary">
						新建
					</Button>,
				]}
			/>
			<AddJdOssConfigDrawer />
		</>
	);
};

export default JdSettings;
