import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { SelectContract } from '@/components/SelectContract';
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.Attendance.AttendanceConfigs.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      wrapperCol={{ span: 24 }}
      width="500px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      trigger={<MyButtons.Create title={`配置`} />}
      onFinish={async (values) =>
        Apis.Attendance.AttendanceConfigs.Store({
          ...values,
          require_photo: values?.require_photo ? true : false,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        SelectContract.AssetProjects({
          key: 'asset_projects_id',
          title: '关联项目',
          colProps: { span: 24 },
          required: true,
          fieldProps: {
            showSearch: true,
            // mode: 'multiple',
          },
        }),
        {
          key: 'check_in_range',
          title: '可打卡范围',
          valueType: 'digit',
          fieldProps: {
            suffix: '米内',
            style: { width: '100%' },
          },
          formItemProps: { ...rulesHelper.number },
          colProps: { span: 24 },
        },
        {
          title: '是否要求拍照打卡',
          key: 'require_photo',
          valueType: 'switch',
          colProps: { span: 12 },
        },
        // {
        //   title: '是否允许范围外打卡',
        //   key: 'allow_out_range_checkin',
        //   valueType: 'switch',
        //   colProps: { span: 12 },
        // },
      ]}
    />
  );
}
