import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { CreditRatingEnum, RatingOutlookEnum } from '@/gen/Enums';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';

export default function AddCredit(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.CompanySupplier.SupplierCreditReports.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) {
          const formValues = {
            ...props.item,
          };
          form.setFieldsValue(formValues);
        }
      }}
      onFinish={async (values: any) =>
        Apis.CompanySupplier.SupplierCreditReports.Store({
          ...values,
          supplier_id: props.item?.company_suppliers_id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success('添加第三方资信调查报告成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'issue_date',
          title: '出具日期',
          valueType: 'date',
          width: '100%',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'expiry_date',
          title: '有效期',
          valueType: 'date',
          width: '100%',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            placeholder: '请选择报告有效性的截止日期',
          },
        },
        {
          key: 'investigation_agency',
          title: '调查机构',
          valueType: 'text',
          width: '100%',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.EnumRadio({
          key: 'credit_rating',
          title: '信用评级',
          valueEnum: CreditRatingEnum,
          required: true,
          colProps: { span: 24 },
        }),
        MyFormItems.EnumRadio({
          key: 'rating_outlook',
          title: '评级展望',
          valueEnum: RatingOutlookEnum,
          required: true,
          colProps: { span: 24 },
        }),

        {
          key: 'has_execution_info',
          title: '失信执行',
          valueType: 'switch',
          width: '100%',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.boolean },
          fieldProps: {
            checkedChildren: '有失信被执行信息',
            unCheckedChildren: '无失信被执行信息',
            onChange: () => {
              // 切换计量单位时清空计费模式
              form.setFieldValue('execution_info', undefined);
            },
          },
        },
        {
          valueType: 'dependency',
          name: ['has_execution_info'],
          columns: ({ has_execution_info }) => {
            return has_execution_info === true
              ? [
                  {
                    key: 'execution_info',
                    title: '执行信息',
                    valueType: 'textarea',
                    width: '100%',
                    colProps: { span: 24 },
                  },
                ]
              : has_execution_info === false
              ? []
              : [];
          },
        },
        {
          key: 'has_lawsuit_record',
          title: '法律诉讼',
          valueType: 'switch',
          width: '100%',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.boolean },
          fieldProps: {
            checkedChildren: '有法律诉讼记录',
            unCheckedChildren: '无法律诉讼记录',
            onChange: () => {
              // 切换计量单位时清空计费模式
              form.setFieldValue('lawsuit_info', undefined);
            },
          },
        },
        {
          valueType: 'dependency',
          name: ['has_lawsuit_record'],
          columns: ({ has_lawsuit_record }) => {
            return has_lawsuit_record === true
              ? [
                  {
                    key: 'lawsuit_info',
                    title: '诉讼信息',
                    valueType: 'textarea',
                    width: '100%',
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                  },
                ]
              : has_lawsuit_record === false
              ? []
              : [];
          },
        },

        {
          key: 'has_admin_penalty',
          title: '行政处罚',
          valueType: 'switch',
          width: '100%',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.boolean },
          fieldProps: {
            checkedChildren: '有行政处罚记录',
            unCheckedChildren: '无行政处罚记录',
            onChange: () => {
              // 切换计量单位时清空计费模式
              form.setFieldValue('admin_penalty_info', undefined);
            },
          },
        },
        {
          valueType: 'dependency',
          name: ['has_admin_penalty'],
          columns: ({ has_admin_penalty }) => {
            return has_admin_penalty === true
              ? [
                  {
                    key: 'admin_penalty_info',
                    title: '处罚信息',
                    valueType: 'textarea',
                    width: '100%',
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                  },
                ]
              : has_admin_penalty === false
              ? []
              : [];
          },
        },
        {
          key: 'performance_evaluation',
          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 },
        }),
      ]}
    />
  );
}
