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

export default function CollectionCreateOrEditPage() {
  const dageFileCheckboxRef = useRef<DageFileCheckboxMethods>(null);
  const fileList = useRef<DageFileResponseType[]>([]);
  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]);

  const handleFileChange = useCallback((list: DageFileResponseType[]) => {
    fileList.current = list;
  }, []);

  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="请输入内容"
                    maxLength={20}
                    showCount
                  />
                </Form.Item>
                <Form.Item label="时代" name="title">
                  <Input
                    className="w220"
                    placeholder="请输入内容"
                    maxLength={10}
                    showCount
                  />
                </Form.Item>
                <Form.Item label="质地" name="title">
                  <Input
                    className="w220"
                    placeholder="请输入内容"
                    maxLength={10}
                    showCount
                  />
                </Form.Item>
                <Form.Item label="级别" name="type">
                  <Select
                    defaultValue="lucy"
                    style={{ width: 220 }}
                    options={[
                      { value: "jack", label: "Jack" },
                      { value: "lucy", label: "Lucy" },
                      { value: "Yiminghe", label: "yiminghe" },
                      { value: "disabled", label: "Disabled", disabled: true },
                    ]}
                  />
                </Form.Item>
                <Form.Item
                  label="来源"
                  name="title"
                  rules={[{ required: true, message: "请输入内容" }]}
                >
                  <Input
                    className="w220"
                    placeholder="请输入内容"
                    maxLength={10}
                    showCount
                  />
                </Form.Item>
                <Form.Item label="简介" name="content">
                  <Input.TextArea
                    className="w450"
                    placeholder="请输入内容，最多200字"
                    maxLength={200}
                    showCount
                    rows={6}
                  />
                </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="fileTypes"
                  rules={[
                    {
                      validator: (...args) => {
                        dageFileCheckboxRef.current!.validate(...args);
                      },
                    },
                  ]}
                  validateTrigger="onSubmit"
                >
                  <DageFileCheckbox
                    ref={dageFileCheckboxRef}
                    hasMobileModel={true}
                    action="https://sit-shgybwg.4dage.com/api/cms/goods/upload"
                    onFileChange={handleFileChange}
                  />
                </Form.Item>
                <Form.Item
                  label="发布日期"
                  name="date"
                  rules={[{ required: true, message: "请选择发布日期" }]}
                >
                  <DatePicker className="w220" />
                </Form.Item>
              </Form>

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