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

const DormigoryBedAuditFlowsAuditStatusEnum = {
  Approved: { text: '通过', value: 'Approved' },
  Rejected: { text: '驳回', value: 'Rejected' },
};
export default function Review(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Emergency.EmergencyEvents.Audit>
      {...MyModalFormProps.props}
      title="审核"
      trigger={
        <MyButtons.Default
          title="审核"
          disabled={props.item?.status !== 'PendingReview'}
          type="primary"
        />
      }
      wrapperCol={{ span: 24 }}
      key={new Date().getTime()}
      width="500px"
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values) =>
        Apis.Emergency.EmergencyEvents.Audit({
          ...values,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            message.success('审核成功');
            props.reload?.();
            return true;
          })
          .catch(() => false)
      }
      columns={[
        MyFormItems.EnumRadio({
          key: 'audit_status',
          title: '审核意见',
          valueEnum: DormigoryBedAuditFlowsAuditStatusEnum,
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          valueType: 'dependency',
          name: ['audit_status'],
          columns: ({ audit_status }) => {
            return audit_status === 'Rejected'
              ? [
                  {
                    title: '驳回理由',
                    valueType: 'textarea',
                    key: 'audit_remark',
                    formItemProps: { ...rulesHelper.text },
                  },
                ]
              : [
                  {
                    title: '审核意见',
                    valueType: 'textarea',
                    key: 'audit_remark',
                  },
                ];
          },
        },
      ]}
    />
  );
}
