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

export default function Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.CompanySupplier.CompanySuppliers.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Edit size={props?.item?.size || 'small'} />}
      wrapperCol={{ span: 24 }}
      key={new Date().getTime()}
      width="820px"
      style={{
        padding: '20px',
        maxHeight: '680px',
        overflowY: 'auto',
        overflowX: 'hidden',
      }}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({
            ...props.item,
            casacader: [
              props.item?.province || '',
              props.item?.city || '',
              props.item?.area || '',
              props.item?.street || '',
            ],
          });
        }
      }}
      onFinish={async (values) =>
        Apis.CompanySupplier.CompanySuppliers.Update({
          ...values,
          is_general_taxpayer: values.is_general_taxpayer ? 1 : 0,
          id: props.item?.id ?? 0,
          counterparty: CompanySuppliersCounterpartyEnum.Supplier.value,
          certificate_type:
            values.supplier_type ===
            CompanySuppliersSupplierTypeEnum.Individual.value
              ? CompanySuppliersCertificateTypeEnum.IdCard.value
              : CompanySuppliersCertificateTypeEnum.BusinessLicense.value,
          company_name: values.company_name || values.name,
          contact_name: values.contact_name || values.name,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        MyFormItems.EnumRadio({
          key: 'supplier_type',
          title: '供应商类型',
          valueEnum: CompanySuppliersSupplierTypeEnum,
          required: true,
          colProps: { span: 6 },
        }),
        {
          key: 'name',
          title: '供应商名称',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 6 },
        },

        {
          name: ['supplier_type'],
          valueType: 'dependency',
          columns: ({ supplier_type }: any) => {
            return supplier_type ===
              CompanySuppliersSupplierTypeEnum.Enterprise.value
              ? [
                  {
                    key: 'legal_person',
                    title: '法定代表人',
                    colProps: { span: 6 },
                  },
                  {
                    key: 'registered_capital',
                    title: '注册资本',
                    valueType: 'number',
                    colProps: { span: 6 },
                    fieldProps: {
                      addonAfter: '万',
                    },
                  },
                  {
                    valueType: 'group',
                    columns: [
                      Address.Cascader({
                        key: 'casacader',
                        title: '选择地址',
                        // required: true,
                        colProps: { span: 12 },
                        keys: ['province', 'city', 'area', 'street'],
                      }),
                      {
                        key: 'address',
                        title: '详细地址',
                        required: true,
                        colProps: { span: 12 },
                      },
                    ],
                  },
                  {
                    key: 'business_scope',
                    title: '经营范围',
                    valueType: 'textarea',
                    colProps: { span: 24 },
                  },
                  {
                    valueType: 'divider',
                    fieldProps: {
                      orientation: 'left',
                      children: '开票信息',
                    },
                  },
                  {
                    key: 'tax_number',
                    title: '纳税人识别号',
                    colProps: { span: 8 },
                    fieldProps: {
                      placeholder: '请输入纳税人识别号',
                      maxLength: 18,
                    },
                  },
                  {
                    key: 'tax_address',
                    title: '开票地址',
                    colProps: { span: 8 },
                  },
                  {
                    key: 'tax_phone',
                    title: '开票电话',
                    colProps: { span: 8 },
                  },

                  {
                    key: 'tax_bank_name',
                    title: '开户行',
                    colProps: { span: 8 },
                  },
                  {
                    key: 'tax_bank_account',
                    title: '银行账号',
                    colProps: { span: 8 },
                  },
                  {
                    key: 'is_general_taxpayer',
                    title: '是否一般纳税人',
                    valueType: 'switch',
                    colProps: { span: 6 },
                  },
                  {
                    valueType: 'divider',
                    fieldProps: {
                      orientation: 'left',
                      children: '联系人信息',
                    },
                  },
                  {
                    key: 'contact_name',
                    title: '联系人姓名',
                    colProps: { span: 6 },
                  },
                  {
                    key: 'contact_phone',
                    title: '联系人电话',
                    colProps: { span: 6 },
                  },
                  {
                    key: 'contact_email',
                    title: '联系人邮箱',
                    colProps: { span: 6 },
                  },
                  {
                    key: 'contact_remark',
                    title: '备注',
                    colProps: { span: 6 },
                  },
                ]
              : supplier_type ===
                CompanySuppliersSupplierTypeEnum.Individual.value
              ? [
                  {
                    key: 'contact_phone',
                    title: '联系电话',
                    colProps: { span: 6 },
                    formItemProps: { ...rulesHelper.phone },
                  },
                  {
                    key: 'tax_number',
                    title: '个人证件号码',
                    colProps: { span: 6 },
                    formItemProps: { ...rulesHelper.text },
                    fieldProps: {
                      placeholder: '请输入个人身份证号码',
                      maxLength: 18,
                    },
                  },
                  {
                    valueType: 'group',
                    columns: [
                      Address.Cascader({
                        key: 'casacader',
                        title: '选择地址',
                        required: true,
                        colProps: { span: 12 },
                        keys: ['province', 'city', 'area', 'street'],
                      }),
                      {
                        key: 'address',
                        title: '详细地址',
                        required: true,
                        colProps: { span: 12 },
                      },
                    ],
                  },
                ]
              : [];
          },
        },

        {
          valueType: 'divider',
          fieldProps: {
            orientation: 'left',
            children: '收款账号',
          },
        },
        {
          valueType: 'formList',
          dataIndex: 'bank_accounts',
          fieldProps: {
            copyIconProps: false,
            // deleteIconProps: false,
          },
          columns: [
            {
              valueType: 'group',
              colProps: { span: 24 },
              columns: [
                {
                  key: 'company_name',
                  title: '开户名称',
                  colProps: { span: 8 },
                },
                {
                  key: 'tax_bank_name',
                  title: '开户银行',
                  colProps: { span: 8 },
                },
                {
                  key: 'tax_bank_account',
                  title: '银行账号',
                  colProps: { span: 8 },
                },
              ],
            },
          ],
        },
        {
          valueType: 'divider',
          fieldProps: {
            orientation: 'left',
            children: '证件资料',
          },
        },
        MyFormItems.UploadImages({
          key: 'certificates',
          colProps: { span: 24 },
          max: 10,
        }),
      ]}
    />
  );
}
