import {
  MyBetaModalFormProps,
  MyButtons,
  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 Assign(props: MyBetaModalFormProps & { item: any }) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Asset.AssetItemMaintenances.Assign>
      {...MyModalFormProps.props}
      title={`指派维保工单`}
      wrapperCol={{ span: 24 }}
      width="600px"
      trigger={
        <MyButtons.Default
          title={props.title || '指派'}
          disabled={props.item.assigned_employee_id}
          type="primary"
        />
      }
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields();
        }
      }}
      key={new Date().getTime()}
      form={form}
      onFinish={async (values: any) => {
        Apis.Asset.AssetItemMaintenances.Assign({
          ...values,
          id: props.item.id,
        })
          .then(() => {
            props.reload?.();
            message.success('指派成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        Selects?.Employees({
          title: '选择员工',
          key: 'assigned_employee_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          required: true,
        }),
      ]}
    />
  );
}
