import './proj-edit-step.css';
import {Breadcrumb, Col, message, Row, Checkbox, Divider} from "antd";
import {Link, useNavigate, useParams} from "react-router-dom";
import {useEffect, useState} from "react";
import {get} from "../../../util/AjaxUtils.ts";
import {Form, Input} from 'antd';
import {AxiosResponse} from "axios";
import {IEnvHard, IEnvLang, IEnvSoft, IEnvTechnical} from "../../../interfaces/proj/IEnv.ts";

const {TextArea} = Input;

type FieldType = {
    envHard: string;
    envHardCustom: string;
    envSoft: string;
    envSoftCustom: string;
    envLang: string;
    envLangCustom: string;
    envTechnical: string;
    envTechnicalCustom: string;
};

type FormFieldType = {
    envHard: string[];
    envHardCustom: string;
    envSoft: string[];
    envSoftCustom: string;
    envLang: string[];
    envLangCustom: string;
    envTechnical: string[];
    envTechnicalCustom: string;
};

export default function ProjEditStep3Show() {
    const nav = useNavigate();
    const pathParams = useParams();
    const [messageApi, contextHolder] = message.useMessage();
    const [form] = Form.useForm<FormFieldType>();
    const [envHardArray, setEnvHardArray] = useState<IEnvHard[]>([]);
    const [envSoftArray, setEnvSoftArray] = useState<IEnvSoft[]>([]);
    const [envLangArray, setEnvLangArray] = useState<IEnvLang[]>([]);
    const [envTechnicalArray, setEnvTechnicalArray] = useState<IEnvTechnical[]>([]);
    const height = window.innerHeight - 180;

    const listEnvHard = () => {
        return new Promise<IEnvHard[]>((resolve) => {
            get<IEnvHard[]>({
                messageApi,
                url: '/api/proj-env-hard/list',
                onSuccess({data}) {
                    resolve(data);
                }
            })
        })
    }
    const listEnvSoft = () => {
        return new Promise<IEnvSoft[]>((resolve) => {
            get<IEnvSoft[]>({
                messageApi,
                url: '/api/proj-env-soft/list',
                onSuccess({data}) {
                    resolve(data);
                }
            })
        })
    }
    const listEnvLang = () => {
        return new Promise<IEnvLang[]>((resolve) => {
            get<IEnvLang[]>({
                messageApi,
                url: '/api/proj/env/lang/list',
                onSuccess({data}) {
                    resolve(data);
                }
            })
        })
    }
    const listEnvTechnical = () => {
        return new Promise<IEnvTechnical[]>((resolve) => {
            get<IEnvTechnical[]>({
                messageApi,
                url: '/api/proj/env/technical/list',
                onSuccess({data}) {
                    resolve(data);
                }
            })
        })
    }

    const getData = () => {
        return new Promise<FieldType>((resolve) => {
            get({
                messageApi,
                url: `/api/proj/get/edit-step3/${pathParams.projId}`,
                onSuccess({data}: AxiosResponse) {
                    resolve(data);
                }
            })
        })
    }

    const render = async () => {
        const envHards = await listEnvHard();
        const envSofts = await listEnvSoft();
        const envLangs = await listEnvLang();
        const envTechnicals = await listEnvTechnical();
        const data = await getData();
        setEnvHardArray(envHards);
        setEnvSoftArray(envSofts);
        setEnvLangArray(envLangs);
        setEnvTechnicalArray(envTechnicals);

        form.setFieldsValue({
            envHard: data.envHard.split(','),
            envHardCustom: data.envHardCustom,
            envSoft: data.envSoft.split(','),
            envSoftCustom: data.envSoftCustom,
            envLang: data.envLang.split(','),
            envLangCustom: data.envLangCustom,
            envTechnical: data.envTechnical.split(','),
            envTechnicalCustom: data.envTechnicalCustom,
        })
    }

    useEffect(() => {
        render();
    }, [])

    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: '1000px'}}
                        disabled={true}
                        autoComplete="off"
                    >
                        <Form.Item<FormFieldType>
                            label="硬件环境"
                            name="envHard"
                            rules={[{required: true, message: '请选择硬件环境'}]}
                        >
                            <Checkbox.Group style={{width: '100%'}}>
                                <Row>{envHardArray.map((item, index) => {
                                    return (
                                        <Col span={4} key={`envHard-${index}`}>
                                            <Checkbox value={item.projEnvHardId} style={{ lineHeight: '32px' }}>{item.type} - {item.content}</Checkbox>
                                        </Col>
                                    )
                                })}</Row>
                            </Checkbox.Group>
                        </Form.Item>
                        <Form.Item<FormFieldType>
                            label="其他"
                            name="envHardCustom"
                            rules={[{required: false, message: '请输入其他硬件环境'}]}
                        >
                            <TextArea rows={3} placeholder="请输入其他硬件环境"/>
                        </Form.Item>
                        <Divider dashed />

                        <Form.Item<FormFieldType>
                            label="软件环境"
                            name="envSoft"
                            rules={[{required: true, message: '请选择软件环境'}]}
                        >
                            <Checkbox.Group style={{width: '100%'}}>
                                <Row>{envSoftArray.map((item, index) => {
                                    return (
                                        <Col span={4} key={`envHard-${index}`}>
                                            <Checkbox value={item.projEnvSoftId} style={{ lineHeight: '32px' }}>{item.type} - {item.content}</Checkbox>
                                        </Col>
                                    )
                                })}</Row>
                            </Checkbox.Group>
                        </Form.Item>
                        <Form.Item<FormFieldType>
                            label="其他"
                            name="envSoftCustom"
                            rules={[{required: false, message: '请输入其他软件环境'}]}
                        >
                            <TextArea rows={3} placeholder="请输入其他软件环境"/>
                        </Form.Item>
                        <Divider dashed />

                        <Form.Item<FormFieldType>
                            label="编程语言"
                            name="envLang"
                            rules={[{required: true, message: '请选择编程语言'}]}
                        >
                            <Checkbox.Group style={{width: '100%'}}>
                                <Row>{envLangArray.map((item, index) => {
                                    return (
                                        <Col span={4} key={`envHard-${index}`}>
                                            <Checkbox value={item.projEnvLangId} style={{ lineHeight: '32px' }}>{item.content}</Checkbox>
                                        </Col>
                                    )
                                })}</Row>
                            </Checkbox.Group>
                        </Form.Item>
                        <Form.Item<FormFieldType>
                            label="其他"
                            name="envLangCustom"
                            rules={[{required: false, message: '请输入其他编程语言'}]}
                        >
                            <TextArea rows={3} placeholder="请输入其他编程语言"/>
                        </Form.Item>
                        <Divider dashed />

                        <Form.Item<FormFieldType>
                            label="技术特点"
                            name="envTechnical"
                            rules={[{required: true, message: '请选择编程语言'}]}
                        >
                            <Checkbox.Group style={{width: '100%'}}>
                                <Row>{envTechnicalArray.map((item, index) => {
                                    return (
                                        <Col span={4} key={`envHard-${index}`}>
                                            <Checkbox value={item.projEnvTechnicalId} style={{ lineHeight: '32px' }}>{item.content}</Checkbox>
                                        </Col>
                                    )
                                })}</Row>
                            </Checkbox.Group>
                        </Form.Item>
                        <Form.Item<FormFieldType>
                            label="其他"
                            name="envTechnicalCustom"
                            rules={[{required: false, message: '请输入其他技术特点'}]}
                        >
                            <TextArea rows={3} placeholder="请输入其他技术特点"/>
                        </Form.Item>
                    </Form>
                </div>
            </div>
        </>
    )

}