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';

const DormigoryBedAuditFlowsAuditStatusEnum = {
  Approved: { text: '通过', value: 'Approved' },
  Rejected: { text: '驳回', value: 'Rejected' },
};
export default function Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Renovation.RenovationApplies.Audit>
      {...MyModalFormProps.props}
      title="审核"
      trigger={<MyButtons.Default title="审核" size="middle" type="primary" />}
      wrapperCol={{ span: 24 }}
      key={new Date().getTime()}
      width="600px"
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values) =>
        Apis.Renovation.RenovationApplies.Audit({
          ...values,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            message.success('审核成功');
            props.reload?.();
            return true;
          })
          .catch(() => false)
      }
      columns={[
        MyFormItems.EnumRadio({
          key: 'status',
          title: '审核意见',
          valueEnum: DormigoryBedAuditFlowsAuditStatusEnum,
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          valueType: 'dependency',
          name: ['status'],
          columns: ({ status }) => {
            return status === 'Rejected'
              ? [
                  {
                    title: '驳回理由',
                    valueType: 'textarea',
                    key: 'reason',
                    formItemProps: { ...rulesHelper.text },
                  },
                ]
              : [
                  {
                    title: '设置保证金',
                    colProps: { span: 6 },
                    key: 'is_deposit',
                    valueType: 'switch',
                  },

                  {
                    valueType: 'dependency',
                    name: ['is_deposit'],
                    columns: ({ is_deposit }) => {
                      return is_deposit
                        ? [
                            {
                              title: '保证金金额',
                              colProps: { span: 6 },
                              key: 'deposit_amount',
                              valueType: 'digit',
                              formItemProps: { ...rulesHelper.number },
                              fieldProps: {
                                style: {
                                  width: '100%',
                                },
                                min: 1,
                              },
                            },
                            Selects?.ProjectAccounts({
                              key: 'receipt_accounts_id',
                              title: '选择收款账户',
                              colProps: { span: 12 },
                              params: {
                                asset_projects_id:
                                  props.item?.asset_projects_id,
                              },
                              formItemProps: { ...rulesHelper.number },
                              fieldProps: {
                                showSearch: true,
                              },
                            }),
                          ]
                        : [];
                    },
                  },
                  Selects.RenovationInspectionRules({
                    key: 'renovation_inspection_rules_id',
                    title: '巡检规则',
                    required: true,
                    colProps: { span: 24 },
                  }),
                  {
                    title: '审核意见',
                    valueType: 'textarea',
                    key: 'reason',
                  },
                ];
          },
        },
      ]}
    />
  );
}
