gaoluyang
2 天以前 b64a0deae5b5d33f9e20671a68936b27f0b9b00b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<script lang="ts" setup>
import type { MesWmSalesNoticeLineApi } from '#/api/mes/wm/salesnotice/line';
 
import { computed, ref, useAttrs, watch } from 'vue';
 
import { IconifyIcon } from '@vben/icons';
 
import { Input, Tooltip } from 'ant-design-vue';
 
import { getSalesNoticeLine } from '#/api/mes/wm/salesnotice/line';
 
import WmSalesNoticeLineSelectDialog from './line-select-dialog.vue';
 
defineOptions({ name: 'WmSalesNoticeLineSelect', inheritAttrs: false });
 
const props = withDefaults(
  defineProps<{
    allowClear?: boolean;
    disabled?: boolean;
    modelValue?: number;
    noticeId?: number; // 所属发货通知单编号
    placeholder?: string;
  }>(),
  {
    allowClear: true,
    disabled: false,
    modelValue: undefined,
    noticeId: undefined,
    placeholder: '请选择发货通知单行',
  },
);
 
const emit = defineEmits<{
  change: [item: MesWmSalesNoticeLineApi.SalesNoticeLine | undefined];
  'update:modelValue': [value: number | undefined];
}>();
 
const attrs = useAttrs();
const dialogRef = ref<InstanceType<typeof WmSalesNoticeLineSelectDialog>>();
const hovering = ref(false);
const selectedItem = ref<MesWmSalesNoticeLineApi.SalesNoticeLine>();
 
const displayLabel = computed(() => {
  const item = selectedItem.value;
  if (!item) {
    return '';
  }
  return `${item.itemCode ?? ''} - ${item.itemName ?? ''}`;
});
 
const showClear = computed(
  () =>
    props.allowClear &&
    !props.disabled &&
    hovering.value &&
    props.modelValue !== null,
);
 
/** 根据编号单条查询行信息(用于编辑回显) */
async function resolveItemById(id: number | undefined) {
  if (!id) {
    selectedItem.value = undefined;
    return;
  }
  if (selectedItem.value?.id === id) {
    return;
  }
  selectedItem.value = await getSalesNoticeLine(id);
}
 
watch(() => props.modelValue, resolveItemById, { immediate: true });
 
/** noticeId 变化时清空选中(关联的行已失效) */
watch(
  () => props.noticeId,
  () => {
    selectedItem.value = undefined;
    emit('update:modelValue', undefined);
    emit('change', undefined);
  },
);
 
/** 清空已选行 */
function clearSelected() {
  selectedItem.value = undefined;
  emit('update:modelValue', undefined);
  emit('change', undefined);
}
 
/** 打开行选择弹窗 */
function handleClick(event: MouseEvent) {
  if (props.disabled || !props.noticeId) {
    return;
  }
  const target = event.target as HTMLElement;
  if (showClear.value && target.closest('.ant-input-suffix')) {
    event.stopPropagation();
    clearSelected();
    return;
  }
  const selectedIds = (
    props.modelValue === null ? [] : [props.modelValue]
  ) as number[];
  dialogRef.value?.open(props.noticeId, selectedIds);
}
 
/** 弹窗选中回调 */
function handleSelected(rows: MesWmSalesNoticeLineApi.SalesNoticeLine[]) {
  const item = rows[0];
  if (!item) {
    return;
  }
  selectedItem.value = item;
  emit('update:modelValue', item.id);
  emit('change', item);
}
</script>
 
<template>
  <div
    v-bind="attrs"
    class="w-full"
    :class="disabled ? 'cursor-not-allowed' : 'cursor-pointer'"
    @click="handleClick"
    @mouseenter="hovering = true"
    @mouseleave="hovering = false"
  >
    <Tooltip :mouse-enter-delay="0.5" :open="selectedItem ? undefined : false">
      <template #title>
        <div v-if="selectedItem" class="leading-6">
          <div>物料编码:{{ selectedItem.itemCode || '-' }}</div>
          <div>物料名称:{{ selectedItem.itemName || '-' }}</div>
          <div>规格型号:{{ selectedItem.specification || '-' }}</div>
          <div>发货数量:{{ selectedItem.quantity ?? '-' }}</div>
        </div>
      </template>
      <Input
        :disabled="disabled"
        :placeholder="placeholder"
        :value="displayLabel"
        readonly
      >
        <template #suffix>
          <IconifyIcon
            class="size-4"
            :icon="showClear ? 'lucide:circle-x' : 'lucide:search'"
          />
        </template>
      </Input>
    </Tooltip>
  </div>
  <WmSalesNoticeLineSelectDialog ref="dialogRef" @selected="handleSelected" />
</template>