周宾
5 天以前 8845ac3d32217b6cf765934dc29dffe0065232d8
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<template>
  <view>
    <up-popup v-model:show="dialogFormVisible" mode="bottom" round="12" @close="closeDia" :customStyle="{ height: '85vh' }">
      <view class="dia-container">
        <view class="dia-header">
          <text class="title">工序排产</text>
          <up-button size="mini" @click="addRow" type="primary">新增</up-button>
          <text class="pending">待排产数量:{{ pendingNum }}</text>
        </view>
 
        <scroll-view class="rows" scroll-y>
          <view v-for="(row, index) in tableData" :key="index" class="row-card">
            <view class="row-header">
              <text class="row-index">#{{ index + 1 }}</text>
              <up-button size="mini" type="error" plain @click="removeRow(index)">删除</up-button>
            </view>
 
            <up-form>
              <up-form-item label="工序" label-width="80">
                <up-input v-model="row.process" placeholder="请输入工序" />
              </up-form-item>
              <up-form-item label="单位" label-width="80">
                <up-input v-model="row.unit" placeholder="请输入单位" />
              </up-form-item>
              <up-form-item label="口味/品名/规格" label-width="110">
                <up-input v-model="row.type" placeholder="请输入" />
              </up-form-item>
              <up-form-item label="排产数量" label-width="80">
                <uni-number-box v-model="row.schedulingNum" :min="0" :step="0.1" style="width: 100%;" background="#2979FF" color="#fff" />
              </up-form-item>
              <up-form-item label="工时定额" label-width="80">
                <uni-number-box v-model="row.workHours" :min="0" :step="0.1" style="width: 100%;" background="#2979FF" color="#fff" />
              </up-form-item>
              <up-form-item label="排产日期" label-width="80">
                <view class="select-box" @click.stop="openDatePicker(index)">
                    <view class="label" :class="[row.schedulingDate?'':'un-value']">
                        {{row.schedulingDate||'选择日期'}}
                    </view>
                    <view class="value">
                        <up-icon name="calendar"></up-icon>
                    </view>
                </view>
             <!--   <up-input v-model="row.schedulingDate" placeholder="选择日期" :border="false" readonly @click="openDatePicker(index)" />
                <template #right>
                  <up-icon name="calendar" @click="openDatePicker(index)"></up-icon>
                </template> -->
              </up-form-item>
              <up-form-item label="排产人" label-width="80">
                <view class="select-box" @click.stop="openUserPicker(index)">
                    <view class="label" :class="[row.schedulingUserName?'':'un-value']">
                        {{row.schedulingUserName||'选择人员'}}
                    </view>
                    <view class="value">
                        <up-icon name="arrow-right"></up-icon>
                    </view>
                </view>
              <!--  <up-input v-model="row.schedulingUserName" placeholder="选择人员" readonly @click="openUserPicker(index)" />
                <template #right>
                  <up-icon name="arrow-right" @click="openUserPicker(index)"></up-icon>
                </template> -->
              </up-form-item>
              <up-form-item label="备注" label-width="80">
                <up-input v-model="row.remark" placeholder="请输入备注" />
              </up-form-item>
            </up-form>
          </view>
        </scroll-view>
 
        <view class="summary">
          <text>排产数量合计:{{ totalSchedulingNum }}</text>
        </view>
 
        <view class="dia-footer">
          <up-button type="primary" @click="submitForm">确认</up-button>
          <up-button @click="closeDia">取消</up-button>
        </view>
      </view>
    </up-popup>
 
    <!-- 日期选择器(up 系列) -->
    <up-popup :show="datePicker.show" mode="bottom" @close="datePicker.show = false">
      <up-datetime-picker :show="true" v-model="datePicker.valueData" mode="date" @confirm="onDateConfirm" @cancel="datePicker.show = false" />
    </up-popup>
 
    <!-- 人员选择器(up 系列 action-sheet) -->
    <up-action-sheet
      :show="userPicker.show"
      :actions="userActionList"
      title="选择人员"
      @select="onUserSelect"
      @close="userPicker.show = false"
    />
  </view>
</template>
 
<script setup>
import { ref, getCurrentInstance, computed } from 'vue'
import { userListNoPageByTenantId } from '@/api/system/user.js'
import { processScheduling } from '@/api/productionManagement/operationScheduling.js'
const { proxy } = getCurrentInstance()
const emit = defineEmits(['close'])
 
const dialogFormVisible = ref(false)
const operationType = ref('')
const tableData = ref([])
const unitFromRow = ref('')
const idFromRow = ref('')
const specificationModelFromRow = ref('')
const pendingNum = ref(0)
const userList = ref([])
const receive = ref('')
 
// pickers
const datePicker = ref({ show: false, valueData: Date.now(), rowIndex: -1 })
 
const userPicker = ref({ show: false, rowIndex: -1 })
 
// ActionSheet 数据
const userActionList = computed(() => {
  return userList.value.map(u => ({ name: u.nickName, value: u.userId }))
})
 
const totalSchedulingNum = computed(() => {
  return tableData.value.reduce((sum, r) => sum + (Number(r.schedulingNum || 0)), 0)
})
 
const userLabel = (uid) => {
  const u = userList.value.find(u => u.userId === uid)
  return u ? u.nickName : ''
}
 
// 打开弹框
const openDialog = (type, row) => {
  operationType.value = type
  dialogFormVisible.value = true
  userListNoPageByTenantId().then((res) => {
    userList.value = res.data
  })
 
  pendingNum.value = row?.pendingNum ?? 0
  unitFromRow.value = row?.unit ?? ''
  idFromRow.value = row?.id ?? ''
  specificationModelFromRow.value = row?.specificationModel ?? ''
  tableData.value = [createRow()]
}
 
const createRow = () => ({
  id: idFromRow.value,
  process: '',
  schedulingDate: '',
  schedulingNum: null,
  schedulingUserId: '',
  schedulingUserName: '',
  workHours: null,
  unit: unitFromRow.value,
  remark: '',
  type: specificationModelFromRow.value,
})
 
const openDatePicker = (idx) => {
  datePicker.value.rowIndex = idx
  datePicker.value.valueData = Date.now()
  datePicker.value.show = true
}
const onDateConfirm = (e) => {
  const val = e.value
  const d = new Date(val)
  const y = d.getFullYear()
  const m = String(d.getMonth() + 1).padStart(2, '0')
  const day = String(d.getDate()).padStart(2, '0')
  const str = `${y}-${m}-${day}`
  if (datePicker.value.rowIndex > -1) {
    tableData.value[datePicker.value.rowIndex].schedulingDate = str
  }
  datePicker.value.show = false
}
 
const openUserPicker = (idx) => {
  userPicker.value.rowIndex = idx
  userPicker.value.show = true
}
const onUserSelect = (item) => {
  if (item && userPicker.value.rowIndex > -1) {
    const row = tableData.value[userPicker.value.rowIndex]
    row.schedulingUserId = item.value
    row.schedulingUserName = item.name
  }
  userPicker.value.show = false
}
 
const submitForm = () => {
  // 1. 检查每一行是否填写完整
  for (let i = 0; i < tableData.value.length; i++) {
    const row = tableData.value[i]
    if (!row.process || !row.schedulingDate || row.schedulingNum === '' || row.schedulingNum === null || !row.schedulingUserId || row.workHours === '' || row.workHours === null || !row.unit) {
      uni.showToast({ title: `第${i + 1}行数据未填写完整`, icon: 'none' })
      return
    }
  }
  // 2. 合计排产数量
  const total = tableData.value.reduce((sum, row) => sum + Number(row.schedulingNum || 0), 0)
  if (total > Number(pendingNum.value)) {
    uni.showToast({ title: '排产数量合计不能超过待排产数量', icon: 'none' })
    return
  }
  // 3. 拼装数据
  const submitData = tableData.value.map(row => {
    const { loss, ...rest } = row
    return { ...rest, receive: receive.value }
  })
  processScheduling(submitData).then(() => {
    uni.showToast({ title: '提交成功', icon: 'success' })
    closeDia()
  })
}
 
// 关闭弹框
const closeDia = () => {
  dialogFormVisible.value = false
  receive.value = ''
  tableData.value = []
  unitFromRow.value = ''
  idFromRow.value = ''
  specificationModelFromRow.value = ''
  pendingNum.value = 0
  emit('close')
}
defineExpose({ openDialog })
 
const addRow = () => {
  tableData.value.push(createRow())
}
const removeRow = (index) => {
  tableData.value.splice(index, 1)
}
</script>
 
<style scoped lang="scss">
.dia-container { 
  padding: 12px; 
  height: 100%; 
  display: flex; 
  flex-direction: column; 
}
.dia-header { 
  display: flex; 
  align-items: center; 
  gap: 8px; 
  margin-bottom: 10px; 
}
.dia-header .title { 
  font-weight: 600; 
  font-size: 16px; 
  color: #333; 
}
.dia-header .pending { 
  margin-left: auto; 
  color: #666; 
  font-size: 12px; 
}
.rows { 
  display: flex; 
  flex-direction: column; 
  gap: 10px; 
  overflow-y: auto; 
  -webkit-overflow-scrolling: touch; 
  flex: 1;
  min-height: 0;
}
.row-card { 
  background: #f5f5f5; 
  border-radius: 10px; 
  padding: 20rpx; 
  box-shadow: 0 2px 8px rgba(0,0,0,0.05); 
  box-sizing: border-box;
  // border: 1px solid #f5f5f5;
  margin-bottom: 20rpx;
}
.row-header { 
  display: flex; 
  justify-content: space-between; 
  align-items: center; 
  margin-bottom: 6px; 
}
.row-index { 
  color: #999; 
  font-size: 12px; 
}
.summary { 
  padding: 10px 0; 
  color: #333; 
  font-size: 14px; 
  text-align: right; 
}
.dia-footer { 
  display: flex; 
  gap: 10px; 
  justify-content: flex-end; 
  padding-top: 8px; 
}
.select-box{
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    flex: 1;
    border-radius: 4px;
    box-sizing: border-box;
    padding: 6px 9px;
    border-width: 0.5px !important;
    border-color: #dadbde !important;
    border-style: solid;
    height: 100%;
    .label{
        min-height: 1.4em;
        color: rgb(48, 49, 51);
        font-size: 15px;
    }
    .un-value{
        color: #999;
    }
}
</style>