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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!--
  业务审批列表筛选弹窗
-->
<template>
  <up-popup :show="modelShow"
            mode="bottom"
            round
            @close="closePopup">
    <view class="oa-filter-popup">
      <view class="oa-filter-head">筛选条件</view>
      <scroll-view scroll-y
                   class="oa-filter-scroll">
        <view v-for="field in fields"
              :key="field.key"
              class="oa-filter-field">
          <text class="oa-field-label">{{ field.label }}</text>
 
          <up-input v-if="field.type === 'input'"
                    v-model="localForm[field.key]"
                    :placeholder="field.placeholder || `请输入${field.label}`"
                    clearable
                    border="surround" />
 
          <view v-else-if="field.type === 'daterange'"
                class="oa-picker-row"
                @click="openDateRange(field.key)">
            <text :class="{ placeholder: !dateLabel(field.key) }">
              {{ dateLabel(field.key) || `请选择${field.label}` }}
            </text>
            <up-icon name="calendar"
                     size="18"
                     color="#999" />
          </view>
 
          <view v-else-if="field.type === 'user'"
                class="oa-picker-row"
                @click="openUserPicker">
            <text :class="{ placeholder: !userLabel }">
              {{ userLabel || field.placeholder || '请选择申请人' }}
            </text>
            <up-icon name="arrow-down"
                     size="14"
                     color="#999" />
          </view>
 
          <view v-else-if="field.type === 'select'"
                class="oa-picker-row"
                @click="openSelect(field)">
            <text :class="{ placeholder: !selectLabel(field) }">
              {{ selectLabel(field) || `请选择${field.label}` }}
            </text>
            <up-icon name="arrow-down"
                     size="14"
                     color="#999" />
          </view>
        </view>
      </scroll-view>
 
      <view class="oa-filter-actions">
        <up-button text="重置"
                   shape="circle"
                   @click="handleReset" />
        <up-button type="primary"
                   text="确定"
                   shape="circle"
                   @click="handleConfirm" />
      </view>
    </view>
 
    <up-calendar :show="calendarShow"
                 mode="range"
                 :maxDate="maxDate"
                 minDate="2020-01-01"
                 :monthNum="24"
                 @confirm="onDateConfirm"
                 @close="calendarShow = false" />
 
    <up-picker :show="pickerShow"
               :columns="pickerColumns"
               keyName="label"
               @confirm="onPickerConfirm"
               @cancel="pickerShow = false"
               @close="pickerShow = false" />
 
    <up-popup :show="userPickerShow"
              mode="bottom"
              round
              @close="userPickerShow = false">
      <view class="oa-user-popup">
        <view class="oa-user-popup-title">选择申请人</view>
        <up-input v-model="userKeyword"
                  placeholder="搜索姓名或编号"
                  clearable
                  border="surround" />
        <scroll-view scroll-y
                     class="oa-user-list">
          <view v-for="u in filteredUsers"
                :key="u.userId ?? u.id"
                class="oa-user-item"
                @click="pickUser(u)">
            <text>{{ userSelectLabel(u) }}</text>
          </view>
          <view v-if="!filteredUsers.length"
                class="oa-user-empty">暂无匹配用户</view>
        </scroll-view>
      </view>
    </up-popup>
  </up-popup>
</template>
 
<script setup>
  import { computed, reactive, ref, watch } from "vue";
  import dayjs from "dayjs";
  import { userListNoPageByTenantId } from "@/api/system/user";
  import {
    formatDateRangeLabel,
    getModuleSearchMeta,
    resetModuleSearchForm,
    userSelectLabel,
  } from "../_utils/approvalModuleListSearch.js";
 
  const props = defineProps({
    show: { type: Boolean, default: false },
    moduleKey: { type: String, required: true },
    modelValue: { type: Object, default: () => ({}) },
  });
 
  const emit = defineEmits(["update:show", "update:modelValue", "search", "reset"]);
 
  const modelShow = computed({
    get: () => props.show,
    set: v => emit("update:show", v),
  });
 
  function closePopup() {
    emit("update:show", false);
  }
 
  const localForm = reactive({});
  const calendarShow = ref(false);
  const activeDateKey = ref("applyDateRange");
  const maxDate = dayjs().format("YYYY-MM-DD");
  const pickerShow = ref(false);
  const pickerColumns = ref([[]]);
  const activeSelectField = ref(null);
  const userPickerShow = ref(false);
  const userKeyword = ref("");
  const allUsers = ref([]);
 
  const fields = computed(() => getModuleSearchMeta(props.moduleKey).fields || []);
 
  const userLabel = computed(() => {
    const id = localForm.applicantId;
    if (!id) return "";
    const u = allUsers.value.find(x => String(x.userId ?? x.id) === String(id));
    return u ? userSelectLabel(u) : String(id);
  });
 
  const filteredUsers = computed(() => {
    const q = userKeyword.value.trim().toLowerCase();
    const list = allUsers.value.filter(u => {
      if (u.delFlag === "2" || u.delFlag === 2) return false;
      if (u.status != null && String(u.status) !== "0") return false;
      return true;
    });
    if (!q) return list.slice(0, 50);
    return list
      .filter(u => {
        const nick = (u.nickName || "").toLowerCase();
        const name = (u.userName || "").toLowerCase();
        const id = String(u.userId ?? u.id ?? "");
        return nick.includes(q) || name.includes(q) || id.includes(q);
      })
      .slice(0, 50);
  });
 
  function unwrapUsers(res) {
    if (Array.isArray(res)) return res;
    if (Array.isArray(res?.data)) return res.data;
    if (Array.isArray(res?.rows)) return res.rows;
    return [];
  }
 
  async function loadUsers() {
    if (allUsers.value.length) return;
    try {
      const res = await userListNoPageByTenantId();
      allUsers.value = unwrapUsers(res);
    } catch {
      allUsers.value = [];
    }
  }
 
  watch(
    () => props.show,
    v => {
      if (!v) return;
      Object.assign(localForm, props.modelValue || {});
    }
  );
 
  function dateLabel(key) {
    return formatDateRangeLabel(localForm[key]);
  }
 
  function openDateRange(key) {
    activeDateKey.value = key;
    calendarShow.value = true;
  }
 
  function onDateConfirm(e) {
    if (!e?.length) {
      calendarShow.value = false;
      return;
    }
    localForm[activeDateKey.value] = [e[0], e[e.length - 1]];
    calendarShow.value = false;
  }
 
  function selectLabel(field) {
    const val = localForm[field.key];
    if (!val) return "";
    const opt = (field.options || []).find(o => o.value === val);
    return opt?.label || String(val);
  }
 
  function openSelect(field) {
    activeSelectField.value = field;
    pickerColumns.value = [field.options || []];
    pickerShow.value = true;
  }
 
  function onPickerConfirm(e) {
    const item = e?.value?.[0];
    if (activeSelectField.value?.key && item) {
      localForm[activeSelectField.value.key] = item.value;
    }
    pickerShow.value = false;
  }
 
  async function openUserPicker() {
    await loadUsers();
    userKeyword.value = "";
    userPickerShow.value = true;
  }
 
  function pickUser(u) {
    localForm.applicantId = u.userId ?? u.id ?? "";
    userPickerShow.value = false;
  }
 
  function handleReset() {
    resetModuleSearchForm(props.moduleKey, localForm);
    emit("update:modelValue", { ...localForm });
    emit("reset", { ...localForm });
    emit("update:show", false);
  }
 
  function handleConfirm() {
    emit("update:modelValue", { ...localForm });
    emit("search", { ...localForm });
    emit("update:show", false);
  }
</script>
 
<style scoped lang="scss">
  @import "../_styles/oa-approval-list.scss";
</style>