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 WorkOrderAssign(
  props: MyBetaModalFormProps & { item: any },
) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.WorkOrder.HouseWorkOrders.Assign>
      {...MyModalFormProps.props}
      title={`派发工单`}
      wrapperCol={{ span: 24 }}
      width="500px"
      trigger={<MyButtons.Edit title={`派发`} />}
      key={new Date().getTime()}
      form={form}
      request={async () => {
        const res = await Apis.WorkOrder.HouseWorkOrders.Show({
          id: props.item.id,
        });
        return {
          title: res.data.title,
          assign_employee_id: res.data.assign_employee_id,
        };
      }}
      onFinish={async (values) =>
        Apis.WorkOrder.HouseWorkOrders.Assign({
          ...values,
          id: props.item.id,
        })
          .then(() => {
            props.reload?.();
            message.success('指派工单成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'title',
          title: '工单标题',
          colProps: { span: 24 },
          readonly: true,
          fieldProps: {
            disabled: true,
          },
        },
        Selects?.Employees({
          title: '选择处理人',
          key: 'assign_employees_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          params: {
            companies_id: props.item.companies_id,
          },
          required: true,
        }),
        {
          key: 'assign_remark',
          title: '指派备注',
          valueType: 'textarea',
          colProps: { span: 24 },
          fieldProps: {
            rows: 3,
            placeholder: '请输入指派备注（可选）',
          },
        },
      ]}
    />
  );
}
