import './proj-edit-step.css';
import {Breadcrumb, Col, message, Row} from "antd";
import {Link, useParams} from "react-router-dom";
import {useEffect, useState} from "react";
import {get} from "../../../util/AjaxUtils.ts";
import {Form} from 'antd';
import {AxiosResponse} from "axios";
import CardImgSelect, {ImgSelect} from "../../../components/card/CardImgSelect.tsx";
import {ILoginpageList} from "../../../interfaces/loginpage/ILoginpage.ts";

type FieldType = {
    loginpageId: string
};

export default function ProjEditStep6Show() {
    const pathParams = useParams();
    const [messageApi, contextHolder] = message.useMessage();
    const [form] = Form.useForm<FieldType>();
    const [loginpageArray, setLoginpageArray] = useState<ImgSelect[]>([]);

    const height = window.innerHeight - 180;

    const listLoginpage = () => {
        return new Promise<ILoginpageList[]>(resolve => {
            get({
                messageApi,
                url: '/api/loginpage/list',
                onSuccess({data}) {
                    resolve(data);
                }
            })
        })
    }

    useEffect(() => {
        get({
            messageApi,
            url: `/api/proj/get/edit-step6/${pathParams.projId}`,
            onSuccess({data}: AxiosResponse) {
                listLoginpage().then(loginpages => {
                    loginpages.forEach(item => {
                        loginpageArray.push({
                            id: item.loginpageId,
                            title: item.pageTitle,
                            imgs: item.previewImgs,
                            selected: item.loginpageId == data.loginpageId
                        })
                    })
                    setLoginpageArray([
                        ...loginpageArray
                    ]);
                })
            }
        })

    }, [])

    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'}}
                        disabled={true}
                        autoComplete="off"
                    >
                        <Row gutter={15}>
                            {
                                loginpageArray.map(item => {
                                    return (
                                        <Col span={8} key={item.id} style={{textAlign: 'center'}}>
                                            <CardImgSelect
                                                id={item.id}
                                                title={item.title}
                                                imgs={item.imgs}
                                                selected={item.selected}
                                            />
                                        </Col>
                                    )
                                })
                            }

                        </Row>
                    </Form>
                </div>
            </div>"
        </>
    )

}