
import { ShareCodeGenerator } from "../sharecode/sharecodegenerator";
import { ShareCodeValidator } from "../sharecode/sharecodevalidator";
import { message, Tabs, Form, Spin, Alert } from "antd";
import { env } from '../../../env'
import { TusUploader } from "@web/src/components/common/uploader/TusUploader";
import { useState } from "react";
const { TabPane } = Tabs;
export default function DeptSettingPage() {
    const [form] = Form.useForm();
    const uploadFileId = Form.useWatch(["file"], form)?.[0]
    const [isGetingFileId, setIsGetingFileId] = useState(false);
    const handleShareSuccess = (code: string) => {
        message.success('分享码生成成功：' + code);
    }
    const handleValidSuccess = async (fileUrl: string, fileName: string) => {
        setIsGetingFileId(true);
        try {
            const downloadUrl = `http://${env.SERVER_IP}:${env.FILE_PORT}/uploads/${fileUrl}`;
            const link = document.createElement('a');
            link.href = downloadUrl;
            link.download = fileName;
            link.target = '_blank';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            message.success('文件下载开始');
        } catch (error) {
            console.error('下载失败:', error);
            message.error('文件下载失败');
        } finally {
            setIsGetingFileId(false);
        }
    };


    return (
        <>
            {
                isGetingFileId ?
                    (<Spin spinning={isGetingFileId} fullscreen />) :
                    (<div style={{ maxWidth: '1000px', margin: '0 auto', padding: '20px' }}>
                        <div className="flex items-center">
                            <img src="/logo.svg" className="h-16 w-16 rounded-xl" />
                            <span className="text-4xl py-4 mx-4">烽火快传</span>
                        </div>
                        <Tabs defaultActiveKey="upload">
                            <TabPane tab="上传分享" key="upload">
                                <div>
                                    <Form form={form}>
                                        <Form.Item name="file">
                                            <TusUploader
                                                multiple={false}
                                                style={"w-full py-4 mb-0 h-64"}
                                            ></TusUploader>
                                        </Form.Item>
                                    </Form>
                                </div>
                                <div style={{ marginBottom: '40px' }}>
                                    <ShareCodeGenerator
                                        fileId={uploadFileId}
                                        onSuccess={handleShareSuccess}
                                    />
                                </div>
                            </TabPane>
                            <TabPane tab="下载文件" key="download">
                                <div>
                                    <span className="text-lg block text-zinc-700 py-4">使用分享码下载文件</span>
                                    <ShareCodeValidator
                                        onValidSuccess={handleValidSuccess}
                                    />
                                </div>
                            </TabPane>
                        </Tabs>
                    </div>)
            }
        </>
    )

}