import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  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';

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

  return (
    <BetaSchemaForm<ApiTypes.QualityCheck.QualityChecks.Store>
      {...MyModalFormProps.props}
      title={`添加核查记录`}
      trigger={<MyButtons.Create title="核查记录" />}
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      labelAlign="left"
      width="680px"
      form={form}
      key={new Date().getTime()}
      onOpenChange={(open: any) => {
        console.log(open, 'open');
        if (open) {
          form.resetFields(); // 清空表单数据
          form.setFieldsValue({
            check_date: new Date(),
          });
        }
      }}
      onFinish={async (values) => {
        return Apis.QualityCheck.QualityChecks.Store(values)
          .then(() => {
            props.reload?.();
            message.success('添加核查记录成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        Selects?.AssetProjects({
          title: '选择项目',
          key: 'asset_projects_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          key: 'check_date',
          title: '核查日期',
          valueType: 'date',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            disabledDate: (current: any) => {
              // 禁用未来的日期
              return current && current > new Date();
            },
            style: {
              width: '100%',
            },
          },
        },
        {
          key: 'check_object',
          title: '核查对象',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 24 },
          fieldProps: {
            placeholder: '请输入对象信息',
          },
        },
        {
          key: 'check_score',
          title: '核查得分',
          valueType: 'digit',
          fieldProps: {
            addonAfter: '分',
            max: 100,
            min: 0,
          },
          formItemProps: { required: true },
          colProps: { span: 24 },
        },
        {
          key: 'check_description',
          title: '核查说明',
          valueType: 'textarea',
          colProps: { span: 24 },
        },
        MyFormItems.UploadImages({
          key: 'check_images',
          title: '核查文件',
          tooltip: '最多上传5张图片',
          uploadType: 'file',
          max: 5,
          colProps: { span: 24 },
          formItemProps: { required: false },
        }),
        {
          key: 'is_rectification',
          title: '下发整改',
          colProps: { span: 24 },
          valueType: 'switch',
          formItemProps: { required: true },
          fieldProps: {
            checkedChildren: '是',
            unCheckedChildren: '否',
          },
        },

        {
          valueType: 'dependency',
          name: ['is_rectification'],
          columns: ({ is_rectification }) => {
            return is_rectification
              ? [
                  {
                    key: 'rectification_description',
                    title: '整改要求',
                    valueType: 'textarea',
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                  },
                  {
                    key: 'rectification_date',
                    title: '整改限期',
                    valueType: 'date',
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                    fieldProps: {
                      disabledDate: (current: any) => {
                        // 只能选未来的日期
                        return current && current < new Date();
                      },
                      addonAfter: '前',
                    },
                  },
                  MyFormItems.UploadImages({
                    key: 'rectification_images',
                    max: 9,
                    title: '关联图片',
                    colProps: { span: 24 },
                    formItemProps: { required: false },
                  }),
                ]
              : [];
          },
        },
      ]}
    />
  );
}
