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';

const JudgmentEnum = {
  Yes: { text: '有效提报', color: '#00ff00', value: 'Yes' },
  No: { text: '无效提报', color: '#ff0000', value: 'No' },
};

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 === 'SecurityInspection' ||
            props?.item.status === 'Closed'
          }
          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,
          location: res.data.location,
          locationInfo:
            res.data.asset_house?.full_name || res.data.asset_project?.name,
          judgment: JudgmentEnum.Yes.value || JudgmentEnum.No.value,
        };
      }}
      onFinish={async (values: any) => {
        if (values.judgment === JudgmentEnum.Yes.value) {
          return Apis.WorkOrder.HouseWorkOrders.Assign({
            ...values,
            id: props.item.id,
          })
            .then(() => {
              props.reload?.();
              message.success('指派工单成功');
              return true;
            })
            .catch(() => false);
        } else {
          return Apis.WorkOrder.HouseWorkOrders.Closed({
            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: 'locationInfo',
          title: '位置信息',
          colProps: { span: 24 },
          readonly: true,
        },
        {
          key: 'content',
          title: '工单描述',
          colProps: { span: 24 },
          readonly: true,
          fieldProps: {
            disabled: true,
          },
        },
        MyFormItems.UploadImages({
          key: 'attachments',
          title: '相关图片',
          colProps: { span: 24 },
          formItemProps: { required: false },
          fieldProps: {
            disabled: true,
          },
        }),
        { valueType: 'divider' },
        MyFormItems.EnumRadio({
          key: 'judgment',
          title: '信息判定',
          colProps: { span: 24 },
          valueEnum: JudgmentEnum,
          required: true,
        }),
        {
          name: ['judgment'],
          valueType: 'dependency',
          columns: ({ judgment }: any) => {
            return judgment === JudgmentEnum.Yes.value
              ? [
                  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: '请输入指派备注（可选）',
                    },
                  },
                ]
              : [];
          },
        },
      ]}
    />
  );
}
