import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { HouseWorkOrdersLevelEnum } from '@/gen/Enums';
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}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({
            title: props?.item?.title,
            content: props?.item?.content,
            assign_employee_id: props?.item?.assign_employee_id,
          });
        }
      }}
      // 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: 'content',
          title: '工单描述',
          colProps: { span: 24 },
          readonly: true,
          fieldProps: {
            disabled: true,
          },
        },
        MyFormItems.EnumRadio({
          key: 'level',
          title: '优先级',
          colProps: { span: 24 },
          valueEnum: HouseWorkOrdersLevelEnum,
          required: 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: '请输入指派备注（可选）',
          },
        },
      ]}
    />
  );
}
