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 Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Emergency.EmergencyEvents.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Edit />}
      wrapperCol={{ span: 24 }}
      width="800px"
      form={form}
      key={new Date().getTime()}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          Apis.Emergency.EmergencyEvents.Show({
            id: props.item?.id ?? 0,
          }).then((res) => {
            form.setFieldsValue({
              ...res?.data,
              two_emergency_categories_id: [
                res?.data?.one_emergency_categories_id,
                res?.data?.two_emergency_categories_id,
              ],
            });
          });
        }
      }}
      onFinish={async (values: any) =>
        Apis.Emergency.EmergencyEvents.Update({
          ...values,
          two_emergency_categories_id: values?.two_emergency_categories_id?.[1],
          emergency_event_organizations_id:
            values?.emergency_event_organizations_id?.[0],
          id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success('编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'name',
          title: '名称',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        },
        Selects?.AssetProjects({
          title: '选择项目',
          key: 'asset_projects_id',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        }),
        // MyFormItems.EnumSelect({
        //   key: 'status',
        //   title: '状态',
        //   valueEnum: EmergencyEventsStatusEnum,
        //   colProps: { span: 12 },
        //   formItemProps: { ...rulesHelper.text },
        // }),
        Selects.EmergencyEventLevels({
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        }),
        Selects.EmergencyCategories({
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        }),
        Selects.OrganizationSearch({
          title: '突发事件提报组织',
          key: 'emergency_event_organizations_id',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            showSearch: true,
            fieldNames: {
              label: 'name',
              value: 'id',
              children: 'children',
            },
          },
        }),
        {
          key: 'emergency_time',
          title: '突发事件时间',
          colProps: { span: 12 },
          valueType: 'date',
          fieldProps: {
            style: { width: '100%' },
          },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'emergency_location',
          title: '突发事件发生地点',
          valueType: 'textarea',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'emergency_description',
          title: '突发事件描述',
          valueType: 'textarea',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'emergency_cause',
          title: '突发事件原因',
          formItemProps: { ...rulesHelper.text },
          valueType: 'textarea',
          colProps: { span: 12 },
        },
        {
          key: 'cause_result',
          title: '突发事件造成结果',
          formItemProps: { ...rulesHelper.text },
          valueType: 'textarea',
          colProps: { span: 12 },
        },
        {
          key: 'supplement',
          title: '突发事件补充信息',
          valueType: 'textarea',
          colProps: { span: 12 },
        },
        {
          key: 'related_person',
          title: '突发事件关联人物',
          valueType: 'textarea',
          colProps: { span: 12 },
        },
        // MyFormItems.EnumRadio({
        //   key: 'compensation_type',
        //   title: '突发事件赔偿类型',
        //   valueEnum: EmergencyEventsCompensationTypeEnum,
        //   colProps: { span: 6 },
        // }),
        // {
        //   key: 'compensation_rate',
        //   title: '突发事件赔偿比例',
        //   colProps: { span: 6 },
        //   valueType: 'digit',
        //   fieldProps: {
        //     style: { width: '100%' },
        //     suffix: '%',
        //   },
        // },
        // {
        //   key: 'compensation_amount',
        //   title: '突发事件赔偿金额',
        //   colProps: { span: 6 },
        //   valueType: 'digit',
        //   fieldProps: {
        //     style: { width: '100%' },
        //     prefix: '¥',
        //   },
        // },
        // {
        //   key: 'compensation_paid_amount',
        //   title: '突发事件已支付赔偿金额',
        //   colProps: { span: 6 },
        //   valueType: 'digit',
        //   fieldProps: {
        //     style: { width: '100%' },
        //     prefix: '¥',
        //   },
        // },
        MyFormItems.UploadImages({
          key: 'emergency_images',
          title: '突发事件图片',
          max: 100,
        }),
      ]}
    />
  );
}
