import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';
import { useEffect, useState } from 'react';

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  const [transferData, setTransferData] = useState<any[]>([]);
  const [targetKeys, setTargetKeys] = useState<string[]>([]);

  // 获取Transfer数据源
  useEffect(() => {
    const asset_projects_id = props?.item?.id || 0;
    if (asset_projects_id) {
      Apis.Asset.AssetUnits.GridSelect({ asset_projects_id })
        .then((res) => {
          const data =
            res.data?.map((item: any) => ({
              key: item.value?.toString(),
              title: item.label,
              description: item.label,
            })) || [];
          setTransferData(data);
        })
        .catch(() => {
          setTransferData([]);
        });
    }
  }, [props?.item?.id]);

  console.log(props.item, 'pppp');
  return (
    <BetaSchemaForm<ApiTypes.Grid.Grids.Store>
      {...MyModalFormProps.props}
      title={`范围添加`}
      width="700px"
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 24 }}
      labelAlign="right"
      trigger={
        <MyButtons.Create
          title={`${props.title}`}
          size={props?.item?.size || 'middle'}
        />
      }
      form={form}
      key={new Date().getTime()}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
          form.setFieldsValue({
            asset_projects_id: props?.item?.id,
          });
        }
      }}
      onFinish={async (values: any) => {
        values?.grid_ranges?.map((res: { asset_projects_id: string }) => {
          res.asset_projects_id = props?.item?.id;
        });
        return Apis.Grid.Grids.Store({
          ...values,
        })
          .then(() => {
            props.reload?.();
            message.success('网格添加成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        ...(props?.item?.id
          ? []
          : [
              Selects?.AssetProjects({
                key: 'asset_projects_id',
                title: '选择项目',
                colProps: { span: 24 },
                required: true,
              }),
            ]),

        {
          valueType: 'dependency',
          name: ['asset_projects_id'],
          columns: ({ asset_projects_id }) => {
            return [
              {
                valueType: 'group',
                columns: [
                  Selects?.GetGridMark({
                    key: 'grid_mark',
                    title: '范围标识',
                    params: {
                      asset_projects_id:
                        asset_projects_id || props?.item?.id || 0,
                    },
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                  }),
                  Selects?.GridUnits({
                    key: 'grid_units_id',
                    title: '选择单元',
                    params: {
                      asset_projects_id:
                        asset_projects_id || props?.item?.id || 0,
                    },
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                  }),
                  // {
                  //   valueType: 'formItem',
                  //   dataIndex: 'grid_units_id',
                  //   title: '选择单元',
                  //   colProps: { span: 24 },
                  //   formItemProps: { ...rulesHelper.text },
                  //   renderFormItem: () => (
                  //     <Transfer
                  //       dataSource={transferData}
                  //       targetKeys={targetKeys}
                  //       onChange={(nextTargetKeys) => {
                  //         setTargetKeys(nextTargetKeys as string[]);
                  //         form.setFieldValue('grid_units_id', nextTargetKeys);
                  //       }}
                  //       render={(item) => item.title}
                  //       titles={['可选单元', '已选单元']}
                  //       showSearch
                  //       listStyle={{
                  //         width: 250,
                  //         height: 300,
                  //       }}
                  //       operations={['选择', '移除']}
                  //       locale={{
                  //         itemUnit: '项',
                  //         itemsUnit: '项',
                  //         searchPlaceholder: '请输入搜索内容',
                  //         notFoundContent: '列表为空',
                  //       }}
                  //     />
                  //   ),
                  // },
                ],
              },
            ];
          },
        },
      ]}
    />
  );
}
