import { FormPageFooter, MemoSpinLoding } from "@/components";
import {
  DageUpload,
  DageUploadConsumer,
  DageUploadProvider,
  DageUploadType,
} from "@dage/pc-components";
import { DatePicker, Form, FormInstance, Input } from "antd";
import { useCallback, useRef, useState } from "react";
import { useNavigate, useParams } from "react-router-dom";
import RichText from "@/components/Z_RichText";

export default function InformationCreateOrEditPage() {
  const formRef = useRef<FormInstance>(null);
  const navigate = useNavigate();
  const params = useParams();
  const [loading, setLoading] = useState(false);
  const richTxtRef = useRef<any>(null);
  const [check, setCheck] = useState(false);
  // 文件的code码
  const [dirCode, setDirCode] = useState("");

  const handleCancel = useCallback(() => {
    navigate(-1);
  }, [navigate]);

  const handleSubmit = useCallback(async () => {
    if (!(await formRef.current?.validateFields())) return;

    const { banner = [], ...rest } = formRef.current?.getFieldsValue();

    if (params.id) {
      rest.id = params.id;
    }

    handleCancel();
  }, [handleCancel, params]);

  return (
    <div style={{ position: "relative" }}>
      {loading && <MemoSpinLoding />}
      <DageUploadProvider>
        <DageUploadConsumer>
          {(data) => (
            <>
              <Form ref={formRef} labelCol={{ span: 2 }}>
                <Form.Item
                  label="标题"
                  name="title"
                  rules={[{ required: true, message: "请输入内容" }]}
                >
                  <Input
                    className="w220"
                    placeholder="请输入内容，最多20字"
                    maxLength={20}
                    showCount
                  />
                </Form.Item>
                <Form.Item
                  label="正文"
                  name="content"
                  rules={[{ required: true, message: "请输入正文" }]}
                >
                  <RichText
                    myUrl="cms/goods/upload"
                    ref={richTxtRef}
                    check={check}
                    dirCode={dirCode}
                    isLook={false}
                  />
                </Form.Item>
                <Form.Item
                  label="发布日期"
                  name="date"
                  rules={[{ required: true, message: "请选择发布日期" }]}
                >
                  <DatePicker className="w220" />
                </Form.Item>
                <Form.Item
                  label="封面图"
                  name="banner"
                  rules={[{ required: true, message: "请上传封面图" }]}
                >
                  <DageUpload
                    tips="支持png、jpg和jpeg格式；最多1张，最大20M"
                    action={
                      process.env.REACT_APP_API_URL + "/api/cms/history/upload"
                    }
                    maxSize={20}
                    maxCount={1}
                  />
                </Form.Item>
                <Form.Item label="视频" name="video">
                  <DageUpload
                    dType={DageUploadType.VIDEO}
                    tips="支持avi,mp4,mkv,wmv等格式；最大200M，最多1个"
                    action={
                      process.env.REACT_APP_API_URL + "/api/cms/history/upload"
                    }
                    maxSize={200}
                    maxCount={1}
                  />
                </Form.Item>
              </Form>

              {!loading && (
                <FormPageFooter
                  disabled={data?.uploading}
                  onSubmit={handleSubmit}
                  onCancel={handleCancel}
                />
              )}
            </>
          )}
        </DageUploadConsumer>
      </DageUploadProvider>
    </div>
  );
}
