import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  HouseWorkOrdersLevelEnum,
  HouseWorkOrdersLocationEnum,
} 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: 19 }}
      width="600px"
      layout="horizontal"
      labelCol={{ span: 5 }}
      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,
          content: res.data.content,
          attachments: res.data.attachments,
          location: res.data.location,
        };
      }}
      onFinish={async (values: any) => {
        Apis.WorkOrder.HouseWorkOrders.Assign({
          ...values,
          id: props.item.id,
        })
          .then(() => {
            props.reload?.();
            message.success('更新工单成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        MyFormItems.EnumRadio({
          key: 'location',
          title: '报修位置',
          colProps: { span: 24 },
          valueEnum: HouseWorkOrdersLocationEnum,
          readonly: true,
        }),
        {
          key: 'content',
          title: '工单描述',
          colProps: { span: 24 },
          readonly: true,
          fieldProps: {
            disabled: true,
          },
        },
        MyFormItems.UploadImages({
          key: 'attachments',
          title: '相关图片',
          tooltip: '最多上传5张图片',
          max: 5,
          colProps: { span: 24 },
          formItemProps: { required: false },
          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: '请输入指派备注（可选）',
          },
        },
      ]}
    />
  );
}
