import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  CooperationSuggestionEnum,
  InfoVerificationEnum,
  QualificationEvaluationEnum,
  RiskLevelEnum,
} from '@/gen/Enums';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';

export default function AddSurvey(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.CompanySupplier.SupplierFieldReports.Store>
      {...MyModalFormProps.props}
      title={`添加实地调查报告`}
      trigger={<MyButtons.Create title="实地调查" />}
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      labelAlign="left"
      width="600px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values: any) =>
        Apis.CompanySupplier.SupplierFieldReports.Store({
          ...values,
          supplier_id: props.item?.company_suppliers_id ?? 0,
          investigation_start_date: values.investigation_date_range?.[0],
          investigation_end_date: values.investigation_date_range?.[1],
        })
          .then(() => {
            props.reload?.();
            message.success('添加实地调查报告成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'investigation_date_range',
          title: '调查日期',
          valueType: 'dateRange',
          width: '100%',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'location',
          title: '调查地点',
          valueType: 'text',
          width: '100%',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        Selects?.Employees({
          key: 'investigator',
          title: '调查人',
          colProps: { span: 24 },
          required: true,
          fieldProps: {
            showSearch: true,
            fieldNames: {
              label: 'label',
              value: 'value',
            },
            labelRender: (res: any) => {
              console.log(res, '222');
              if (res?.label) {
                return res?.label;
              } else {
                return props?.item?.company_employee?.name;
              }
            },
          },
        }),
        {
          key: 'issue_date',
          title: '出具日期',
          valueType: 'date',
          width: '100%',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.EnumRadio({
          key: 'info_verification',
          title: '信息核实',
          valueEnum: InfoVerificationEnum,
          required: true,
          colProps: { span: 24 },
          fieldProps: {
            buttonStyle: 'solid',
            onChange: () => {
              // 切换计量单位时清空计费模式
              form.setFieldValue('discrepancy_note', undefined);
            },
          },
        }),
        {
          valueType: 'dependency',
          name: ['info_verification'],
          columns: ({ info_verification }) => {
            return info_verification ===
              InfoVerificationEnum.Discrepancy.value ||
              info_verification === InfoVerificationEnum.Fake.value
              ? [
                  {
                    key: 'discrepancy_note',
                    title: '差异说明',
                    valueType: 'textarea',
                    width: '100%',
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                  },
                ]
              : info_verification === InfoVerificationEnum.Authentic.value
              ? []
              : [];
          },
        },
        MyFormItems.EnumRadio({
          key: 'qualification_evaluation',
          title: '资质评价',
          valueEnum: QualificationEvaluationEnum,
          required: true,
          colProps: { span: 24 },
        }),
        MyFormItems.EnumRadio({
          key: 'risk_level',
          title: '风险等级',
          valueEnum: RiskLevelEnum,
          required: true,
          colProps: { span: 24 },
        }),
        MyFormItems.EnumRadio({
          key: 'cooperation_suggestion',
          title: '合作建议',
          valueEnum: CooperationSuggestionEnum,
          required: true,
          colProps: { span: 24 },
        }),

        {
          key: 'comprehensive_explanation',
          title: '综合说明',
          valueType: 'textarea',
          width: '100%',
          colProps: { span: 24 },
        },
        MyFormItems.UploadImages({
          key: 'files',
          title: '上传附件',
          tooltip: '可以上传任意格式的文件',
          uploadType: 'file',
          // accept: '.docx,.doc,.pdf',
          colProps: { span: 24 },
          max: 9,
          formItemProps: { ...rulesHelper.array },
        }),
      ]}
    />
  );
}
