import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
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();

  return (
    <BetaSchemaForm<ApiTypes.Company.CompanyQuickActions.Store>
      {...MyModalFormProps.props}
      title="添加金刚区"
      wrapperCol={{ span: 24 }}
      width="600px"
      trigger={<MyButtons.Create title="添加金刚区" />}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values: any) =>
        Apis.Company.CompanyQuickActions.Store({
          ...values,
          is_show: values?.is_show || 0,
          is_bind_house: values?.is_bind_house || 0,
          companies_id: props?.item?.id,
        })
          .then(() => {
            props.reload?.();
            message.success('添加成功！');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'name',
          title: '名称',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'skip_url',
          title: '跳转链接',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'sort',
          title: '排序',
          tooltip: '越小越靠前',
          valueType: 'digit',
          colProps: { span: 8 },
          fieldProps: {
            style: { width: '100%' },
          },
        },
        {
          title: '是否显示',
          key: 'is_show',
          valueType: 'switch',
          colProps: { span: 8 },
        },
        {
          title: '是否要绑房打开',
          key: 'is_bind_house',
          tooltip: '是：必须绑房后才能使用功能，否：不绑房也可以使用当前功能',
          valueType: 'switch',
          colProps: { span: 8 },
        },
        MyFormItems.UploadImages({
          key: 'icon',
          title: '图标',
          max: 1,
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.array },
        }),
      ]}
    />
  );
}
