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

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();

  console.log(props.item, 'pppp');
  return (
    <BetaSchemaForm<ApiTypes.Grid.Grids.Store>
      {...MyModalFormProps.props}
      title={`范围添加`}
      width="800px"
      layout="horizontal"
      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) => {
        return Apis.Grid.Grids.Store({
          asset_projects_id: props?.item?.id || values?.asset_projects_id,
          ...values,
        }).then(() => {
          props.reload?.();
          message.success('网格添加成功');
          return true;
        });
      }}
      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 },
                  }),
                  {
                    key: 'grid_ranges',
                    title: '选择单元',
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.array },
                    renderFormItem: () => (
                      <MyTransferUnits
                        item={{
                          asset_projects_id:
                            props?.item?.id || asset_projects_id,
                          type: 1,
                        }}
                      />
                    ),
                  },
                ],
              },
            ];
          },
        },
      ]}
    />
  );
}
