import './proj-edit-step.css';
import {Breadcrumb, Col, DatePicker, Flex, message, Modal, Row, Spin} from "antd";
import locale from 'antd/es/date-picker/locale/zh_CN';
import {Link, useNavigate, useParams} from "react-router-dom";
import {useEffect, useState} from "react";
import {get, put} from "../../../util/AjaxUtils.ts";
import {Button, Form, Input} from 'antd';
import {AxiosResponse} from "axios";
import dayjs, {Dayjs} from 'dayjs';

type FieldType = {
    projSubName: string;
    projVersion: string;
    projDevCompleteDate: Dayjs;
    companyName: string;
    companyNameEn: string;
};

export default function ProjEditStep2() {
    const nav = useNavigate();
    const pathParams = useParams();
    const [messageApi, contextHolder] = message.useMessage();
    const [form] = Form.useForm<FieldType>();
    const [loading, setLoading] = useState<boolean>(false);
    const [isEditModalOpen, setIsEditModalOpen] = useState(false);
    const height = window.innerHeight - 180;
    const dateFormat = 'YYYY年MM月DD日';

    useEffect(() => {
        get({
            messageApi,
            url: `/api/proj/get/edit-step2/${pathParams.projId}`,
            onSuccess({data}: AxiosResponse) {
                form.setFieldsValue({
                    projSubName: data.projSubName,
                    projVersion: data.projVersion,
                    projDevCompleteDate: data.projDevCompleteDate ? dayjs(data.projDevCompleteDate, 'YYYY-MM-DD') : '',
                    companyName: data.companyName,
                    companyNameEn: data.companyNameEn,
                })
            }
        })
    }, [])

    return (
        <>
            {contextHolder}
            <Breadcrumb
                items={[
                    {title: <Link to={'/'}>首页</Link>},
                    {title: <Link to={'/proj-create'}>创建项目</Link>},
                    {title: <Link to={`/proj-edit/${pathParams.projId}`}>编辑项目</Link>},
                    {title: '基本信息'},
                ]}
            />
            <div className="form-container" style={{height: `${height}px`}}>
                <div className="form-body">
                    <Form
                        name="basic"
                        form={form}
                        layout="vertical"
                        labelCol={{span: 8}}
                        wrapperCol={{span: 24}}
                        style={{width: '800px'}}
                        onFinish={() => {
                            setIsEditModalOpen(true);
                        }}
                        autoComplete="off"
                    >
                        <Row gutter={15}>
                            <Col span={24}>
                                <Form.Item<FieldType>
                                    label="项目简称"
                                    name="projSubName"
                                    rules={[{required: false, message: '请输入项目简称'}]}
                                >
                                    <Input placeholder="请输入项目简称"/>
                                </Form.Item>
                            </Col>


                        </Row>
                        <Row gutter={15}>
                            <Col span={12}>
                                <Form.Item<FieldType>
                                    label="项目版本"
                                    name="projVersion"
                                    rules={[{required: true, message: '请输入项目版本'}]}
                                >
                                    <Input placeholder="请输入项目版本"/>
                                </Form.Item>
                            </Col>
                            <Col span={12}>
                                <Form.Item<FieldType>
                                    label="开发完成时间"
                                    name="projDevCompleteDate"
                                    rules={[{required: true, message: '请输入开发完成时间'}]}
                                >
                                    <DatePicker placeholder="请选择开发完成日期"
                                                format={dateFormat}
                                                locale={locale}
                                                style={{width: '100%'}}
                                    />
                                </Form.Item>
                            </Col>
                        </Row>

                        <Row gutter={15}>
                            <Col span={12}>
                                <Form.Item<FieldType>
                                    label="公司名称"
                                    name="companyName"
                                    extra="公司名称在操作手册中引用"
                                    rules={[{required: true, message: '请输入公司名称'}]}
                                >
                                    <Input placeholder="请输入公司名称"/>
                                </Form.Item>
                            </Col>
                            <Col span={12}>
                                <Form.Item<FieldType>
                                    label="公司英文名称"
                                    name="companyNameEn"
                                    extra="公司英文名称在操作手册中引用"
                                    rules={[{required: true, message: '请输入公司英文名称'}]}
                                >
                                    <Input placeholder="请输入公司英文名称"/>
                                </Form.Item>
                            </Col>
                        </Row>

                        <Form.Item wrapperCol={{span: 24}}>
                            <Flex align="center" justify="center" gap="large">
                                <Button type="primary"
                                        htmlType="submit"
                                        style={{backgroundColor: 'var(--color-primary)'}}>
                                    保存
                                </Button>
                                <Button type="default" htmlType="button" onClick={() => {
                                    nav(-1);
                                }}>
                                    返回
                                </Button>
                            </Flex>
                        </Form.Item>
                    </Form>
                </div>
            </div>
            <Modal title="提示"
                   okText="确定"
                   cancelText="取消"
                   open={isEditModalOpen}
                   onOk={() => {
                       setIsEditModalOpen(false);
                       put({
                           messageApi,
                           url: `/api/proj/update/edit-step2/${pathParams.projId}`,
                           body: {
                               projSubName: form.getFieldValue('projSubName'),
                               projVersion: form.getFieldValue('projVersion'),
                               projDevCompleteDate: dayjs(form.getFieldValue('projDevCompleteDate')).format(dateFormat),
                               companyName: form.getFieldValue('companyName'),
                               companyNameEn: form.getFieldValue('companyNameEn'),
                           },
                           onBefore() {
                               setLoading(true);
                           },
                           onSuccess() {
                               messageApi.open({
                                   type: 'success',
                                   content: '编辑成功'
                               })
                           },
                           onFinally() {
                               setLoading(false);
                           }
                       })
                   }}
                   onCancel={() => {
                       setIsEditModalOpen(false);
                   }}>
                <div>确定提交吗？</div>
            </Modal>
            <Spin tip="正在提交..." spinning={loading} fullscreen/>
        </>
    )

}