import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import MyTransferUnits from '@/components/TransferUnits';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form } 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) => {
        console.log(values, 'values');
        // (values.asset_projects_id =
        //   values?.asset_projects_id || props?.item?.id),
        //   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 },
                  }),
                  {
                    valueType: 'formItem',
                    dataIndex: 'grid_units_id',
                    title: '选择单元',
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                    renderFormItem: () => (
                      <MyTransferUnits
                        item={{
                          asset_projects_id:
                            props?.item?.id || asset_projects_id,
                        }}
                      />
                    ),
                  },
                ],
              },
            ];
          },
        },
      ]}
    />
  );
}
