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

interface WorkLogsCreateProps {
  workOrderId: number;
  workOrderProjectId: number;
  item: any;
  reload?: () => void;
}

export default function WorkLogsCreate({
  workOrderId,
  workOrderProjectId,
  item,
  reload,
}: WorkLogsCreateProps) {
  const [form] = Form.useForm();

  return (
    <BetaSchemaForm<ApiTypes.WorkOrder.HouseWorkLogs.Store>
      {...MyModalFormProps.props}
      title={`添加处理记录`}
      width="600px"
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      labelAlign="right"
      trigger={<MyButtons.Create title={`处理`} />}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
          form.setFieldsValue({
            asset_projects_id: workOrderProjectId,
          });
        }
      }}
      key={new Date().getTime()}
      form={form}
      onFinish={async (values) => {
        try {
          await Apis.WorkOrder.HouseWorkLogs.Store({
            ...values,
            house_work_orders_id: workOrderId,
          });
          reload?.();
          message.success('添加处理记录成功');
          return true;
        } catch (error) {
          message.error('添加处理记录失败');
          return false;
        }
      }}
      columns={[
        MyFormItems.EnumRadio({
          key: 'status',
          title: '工单状态',
          colProps: { span: 24 },
          // valueEnum: HouseWorkOrdersStatusEnum,
          valueEnum: () => {
            let obj: any = JSON.parse(
              JSON.stringify(HouseWorkOrdersStatusEnum),
            );
            delete obj.Pending;
            delete obj.PendingPayment;
            return obj;
          },
          required: true,
          formItemProps: { ...rulesHelper.text },
        }),
        {
          key: 'description',
          title: '进度描述',
          valueType: 'textarea',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            rows: 4,
            placeholder: '请输入处理进度描述',
          },
        },
        MyFormItems.UploadImages({
          key: 'attachments',
          title: '相关图片',
          uploadType: 'file',
          max: 5,
          colProps: { span: 24 },
          formItemProps: { required: false },
        }),

        {
          name: ['status'],
          valueType: 'dependency',
          columns: ({ status }: any) => {
            return status === HouseWorkOrdersStatusEnum?.Completed?.value &&
              item?.location === 'MyHome'
              ? [
                  {
                    title: '是否收费',
                    key: 'is_fee',
                    valueType: 'switch',
                    colProps: { span: 24 },
                  },
                  {
                    name: ['is_fee'],
                    valueType: 'dependency',
                    columns: ({ is_fee }: any) => {
                      return is_fee
                        ? [
                            {
                              key: 'amount',
                              title: '收费金额',
                              colProps: { span: 24 },
                              formItemProps: {
                                ...rulesHelper.number,
                              },
                              fieldProps: {
                                mix: 0,
                                addonAfter: '元',
                                placeholder: '请输入客户需支付的金额',
                              },
                            },
                            Selects?.ProjectReceiptAccountsSelect({
                              key: 'company_project_receipt_accounts_id',
                              title: '选择账户',
                              params: {
                                projects_id: workOrderProjectId,
                              },
                              colProps: { span: 24 },
                              formItemProps: { ...rulesHelper.number },
                              fieldProps: {
                                showSearch: true,
                              },
                            }),
                          ]
                        : [];
                    },
                  },
                ]
              : [];
          },
        },
      ]}
    />
  );
}
