import './proj-edit-step.css';
import {Breadcrumb, Col, Flex, message, Modal, Row, Spin} from "antd";
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";

type FieldType = {
    applyName: string;
    applyPhone: string;
    applyAddress: string;
    applyZipCode: string;
    applyContactName: string;
    applyContactPhone: string;
    applyContactEmail: string;
    applyContactFax: string;
};

export default function ProjEditStep5() {
    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;

    useEffect(() => {
        get({
            messageApi,
            url: `/api/proj/get/edit-step5/${pathParams.projId}`,
            onSuccess({data}: AxiosResponse) {
                form.setFieldsValue({
                    applyName: data.applyName,
                    applyPhone: data.applyPhone,
                    applyAddress: data.applyAddress,
                    applyZipCode: data.applyZipCode,
                    applyContactName: data.applyContactName,
                    applyContactPhone: data.applyContactPhone,
                    applyContactEmail: data.applyContactEmail,
                    applyContactFax: data.applyContactFax,
                })
            }
        })
    }, [])

    return (
        <>
            {contextHolder}
            <Breadcrumb
                items={[
                    {title: <Link to={'/'}>首页</Link>},
                    {title: <Link to={'/proj-create'}>创建项目</Link>},
                    {title: <a onClick={() => {nav(-1)}}>编辑项目</a>},
                    {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={12}>
                                <Form.Item<FieldType>
                                    label="姓名"
                                    name="applyName"
                                    rules={[{required: true, message: '请输入姓名'}]}
                                >
                                    <Input placeholder="请输入姓名"/>
                                </Form.Item>
                            </Col>
                            <Col span={12}>
                                <Form.Item<FieldType>
                                    label="电话"
                                    name="applyPhone"
                                    rules={[{required: true, message: '请输入电话'}]}
                                >
                                    <Input placeholder="请输入电话"/>
                                </Form.Item>
                            </Col>
                        </Row>
                        <Row gutter={15}>
                            <Col span={12}>
                                <Form.Item<FieldType>
                                    label="地址"
                                    name="applyAddress"
                                    rules={[{required: true, message: '请输入地址'}]}
                                >
                                    <Input placeholder="请输入地址"/>
                                </Form.Item>
                            </Col>
                            <Col span={12}>
                                <Form.Item<FieldType>
                                    label="邮编"
                                    name="applyZipCode"
                                    rules={[{required: true, message: '请输入邮编'}]}
                                >
                                    <Input placeholder="请输入邮编"/>
                                </Form.Item>
                            </Col>
                        </Row>
                        <Row gutter={15}>
                            <Col span={12}>
                                <Form.Item<FieldType>
                                    label="联系人姓名"
                                    name="applyContactName"
                                    rules={[{required: true, message: '请输入联系人姓名'}]}
                                >
                                    <Input placeholder="请输入联系人"/>
                                </Form.Item>
                            </Col>
                            <Col span={12}>
                                <Form.Item<FieldType>
                                    label="联系人手机"
                                    name="applyContactPhone"
                                    rules={[{required: true, message: '请输入联系人手机'}]}
                                >
                                    <Input placeholder="请输入联系人手机"/>
                                </Form.Item>
                            </Col>
                        </Row>
                        <Row gutter={15}>
                            <Col span={12}>
                                <Form.Item<FieldType>
                                    label="联系人邮箱"
                                    name="applyContactEmail"
                                    rules={[{required: true, message: '请输入联系人'}]}
                                >
                                    <Input placeholder="请输入联系人"/>
                                </Form.Item>
                            </Col>
                            <Col span={12}>
                                <Form.Item<FieldType>
                                    label="联系人传真"
                                    name="applyContactFax"
                                    rules={[{required: false, 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-step5/${pathParams.projId}`,
                           body: {
                               applyName: form.getFieldValue('applyName'),
                               applyPhone: form.getFieldValue('applyPhone'),
                               applyAddress: form.getFieldValue('applyAddress'),
                               applyZipCode: form.getFieldValue('applyZipCode'),
                               applyContactName: form.getFieldValue('applyContactName'),
                               applyContactPhone: form.getFieldValue('applyContactPhone'),
                               applyContactEmail: form.getFieldValue('applyContactEmail'),
                               applyContactFax: form.getFieldValue('applyContactFax'),
                           },
                           onBefore() {
                               setLoading(true);
                           },
                           onSuccess() {
                               messageApi.open({
                                   type: 'success',
                                   content: '编辑成功'
                               })
                           },
                           onFinally() {
                               setLoading(false);
                           }
                       })
                   }}
                   onCancel={() => {
                       setIsEditModalOpen(false);
                   }}>
                <div>确定提交吗？</div>
            </Modal>
            <Spin tip="正在提交..." spinning={loading} fullscreen/>
        </>
    )

}