import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { HouseWorkOrdersTypeEnum } 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 }}
      labelAlign="left"
      trigger={
        <MyButtons.Default
          title={props.title || '指派'}
          disabled={
            props?.item.assign_status !== 'Unassigned' &&
            props?.item.type === 'RenovationAcceptance'
          }
          type="primary"
        />
      }
      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,
          complaint_type: res.data.complaint_type,
          locationInfo: res.data.asset_house.full_name,
          type: res.data.type,
        };
      }}
      onFinish={async (values: any) => {
        Apis.WorkOrder.HouseWorkOrders.Assign({
          ...values,
          id: props.item.id,
          predict_complete_at: values.predict_complete_at,
        })
          .then(() => {
            props.reload?.();
            message.success('指派工单成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        MyFormItems.EnumRadio({
          key: 'type',
          title: '工单类型',
          colProps: { span: 24 },
          valueEnum: HouseWorkOrdersTypeEnum,
          readonly: true,
        }),
        {
          key: 'locationInfo',
          title: '房屋信息',
          colProps: { span: 24 },
          readonly: true,
        },
        {
          key: 'content',
          title: '工单描述',
          colProps: { span: 24 },
          readonly: true,
          fieldProps: {
            disabled: true,
          },
        },
        { valueType: 'divider' },

        // MyFormItems.EnumRadio({
        //   key: 'level',
        //   title: '优先级',
        //   colProps: { span: 24 },
        //   valueEnum: HouseWorkOrdersLevelEnum,
        //   required: true,
        // }),
        // {
        //   key: 'predict_complete_at',
        //   title: '完成时间要求',
        //   valueType: 'date',
        //   colProps: { span: 24 },
        //   formItemProps: { ...rulesHelper.text },
        //   fieldProps: {
        //     disabledDate: (current: any) => {
        //       // 只能选今天及以后的日期
        //       const today = new Date();
        //       today.setHours(0, 0, 0, 0); // 设置为今天的00:00:00
        //       return current && current < today;
        //     },
        //     addonAfter: '前',
        //   },
        // },
        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: '请输入指派备注（可选）',
          },
        },
      ]}
    />
  );
}
