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

const GoodsReleasesAuditStatusEnum = {
  Approved: { text: '通过', value: 'Approved' },
  Rejected: { text: '驳回', value: 'Rejected' },
};

export default function Review(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  const navigate = useNavigate();
  return (
    <BetaSchemaForm<ApiTypes.GoodsReleases.GoodsReleases.ApproveByEmployee>
      {...MyModalFormProps.props}
      title="审核"
      trigger={<MyButtons.Default title="审核" size="middle" 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.GoodsReleases.GoodsReleases.ApproveByEmployee({
          ...values,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            message.success('审核成功');
            navigate(-1);
            return true;
          })
          .catch(() => false)
      }
      columns={[
        MyFormItems.EnumRadio({
          key: 'status',
          title: '审核意见',
          valueEnum: GoodsReleasesAuditStatusEnum,
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          valueType: 'dependency',
          name: ['status'],
          columns: ({ status }) => {
            return status === 'Rejected'
              ? [
                  {
                    title: '驳回理由',
                    valueType: 'textarea',
                    key: 'reason',
                    formItemProps: { ...rulesHelper.text },
                  },
                ]
              : [
                  {
                    title: '审核意见',
                    valueType: 'textarea',
                    key: 'reason',
                  },
                ];
          },
        },
      ]}
    />
  );
}
