modify.vue 22.3 KB
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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
<template>
    <view class="page">
        <scroll-view class="scroll" scroll-y>
            <!-- 基础信息 -->
            <uni-list>
                <!-- 码单编号(弹窗选择) -->
                <uni-list-item class="select-item" :class="form.weighingTicketCode ? 'is-filled' : 'is-empty'" clickable
                    @click="openTicketSheet" :rightText="form.weighingTicketCode || '请选择码单'" showArrow>
                    <template v-slot:body>
                        <view class="item-title"><text class="required">*</text><text>码单编号</text></view>
                    </template>
                </uni-list-item>

                <!-- 日期(选择码单后带出) -->
                <uni-list-item class="select-item" :class="form.reportDate ? 'is-filled' : 'is-empty'">
                    <template v-slot:body>
                        <view class="item-title"><text class="required">*</text><text>日期</text></view>
                    </template>
                    <template v-slot:footer>
                        <view class="picker-value" :class="{ placeholder: !form.reportDate }">
                            {{ form.reportDate || '选择码单后带出' }}
                        </view>
                    </template>
                </uni-list-item>

                <!-- 客户名称(选择码单后带出) -->
                <uni-list-item class="select-item" :class="form.supplierName ? 'is-filled' : 'is-empty'">
                    <template v-slot:body>
                        <view class="item-title"><text class="required">*</text><text>客户名称</text></view>
                    </template>
                    <template v-slot:footer>
                        <view class="picker-value" :class="{ placeholder: !form.supplierName }">
                            {{ form.supplierName || '选择码单后带出' }}
                        </view>
                    </template>
                </uni-list-item>

                <!-- 计划编号 -->
                <uni-list-item class="select-item" :class="form.procurementPlanId ? 'is-filled' : 'is-empty'">
                    <template v-slot:body>
                        <view class="item-title"><text class="required">*</text><text>计划编号</text></view>
                    </template>
                    <template v-slot:footer>
                        <picker mode="selector" :range="planLabels" @change="onPlanChange" :disabled="!planOptions.length">
                            <view class="picker-value" :class="{ placeholder: !form.procurementPlanId }">
                                {{ getPlanLabel() || '请选择计划编号' }}
                            </view>
                        </picker>
                    </template>
                </uni-list-item>

                <!-- 过磅品种 -->
                <uni-list-item class="select-item" :class="form.productId ? 'is-filled' : 'is-empty'">
                    <template v-slot:body>
                        <view class="item-title"><text class="required">*</text><text>过磅品种</text></view>
                    </template>
                    <template v-slot:footer>
                        <picker mode="selector" :range="productLabels" @change="onProductChange" :disabled="!productOptions.length">
                            <view class="picker-value" :class="{ placeholder: !form.productId }">
                                {{ getProductLabel() || '请选择过磅品种' }}
                            </view>
                        </picker>
                    </template>
                </uni-list-item>
            </uni-list>

            <!-- 化验明细:样品/数量/样号/送样编号 + 成份%(成分项按码单分厂区分) -->
            <AssayDetailTable ref="detailTable" :options="applyDetailOptions" :workshop-code="ticketWorkshopCode" :workshop-name="ticketWorkshopName" />
        </scroll-view>

        <view class="footer">
            <button class="btn submit" type="primary" @click="onSubmit">保存</button>
        </view>

        <RelateSelectSheet :visible.sync="ticketSheet.visible" title="选择码单" :multiple="false"
            :fetchFn="ticketSheet.fetchFn" :display-fields="ticketSheet.displayFields"
            row-key="id" :selectedKeys.sync="ticketSheet.selectedKeys" @confirm="onTicketConfirm" />
    </view>
</template>

<script>
import RelateSelectSheet from '@/components/relate-select/index.vue'
import AssayDetailTable from './assay-detail-table.vue'
import {
    assayReportDetailApi,
    assayReportTicketPageApi,
    assayReportPlanOptionsApi,
    assayReportProductOptionsApi,
    assayReportApplyOptionsApi,
    assayReportUpdateApi
} from '@/api/procure-manage/assayReport.js'

export default {
    name: 'ModifyMaterialTest',
    components: { RelateSelectSheet, AssayDetailTable },
    data() {
        return {
            id: '',
            form: {
                weighingTicketId: '',
                weighingTicketCode: '',
                reportDate: '',
                supplierName: '',
                procurementPlanId: '',
                productId: ''
            },
            // 选中的码单行对象,供回显与分厂成分联动使用
            selectedTicketRow: null,
            // 计划编号下拉选项(所选码单下的计划)
            planOptions: [],
            // 过磅品种下拉选项(所选计划下的品种)
            productOptions: [],
            // 化验明细数据源(applyOptions 返回),供明细子组件送样编号下拉与固定值使用
            applyDetailOptions: [],
            ticketSheet: {
                visible: false,
                fetchFn: null,
                displayFields: [
                    { label: '码单编号', field: 'code' },
                    { label: '供应商', field: 'supplierName' },
                    { label: '分厂', field: 'workshopName' },
                    { label: '创建人', field: 'createBy' },
                    { label: '完成时间', field: 'completeTime' }
                ],
                selectedKeys: []
            }
        }
    },
    computed: {
        // 码单分厂编码/名称,决定化验明细成份项集合
        ticketWorkshopCode() {
            return (this.selectedTicketRow && this.selectedTicketRow.workshopCode) || ''
        },
        ticketWorkshopName() {
            return (this.selectedTicketRow && this.selectedTicketRow.workshopName) || ''
        },
        planLabels() {
            return (this.planOptions || []).map(it => it.label)
        },
        productLabels() {
            return (this.productOptions || []).map(it => it.label)
        }
    },
    created() {
        this.ticketSheet.fetchFn = this.fetchTickets.bind(this)
    },
    onLoad(query) {
        this.id = (query && query.id) || ''
        this.loadDetail()
    },
    methods: {
        // 打开页面:按 id 加载详情并回显
        loadDetail() {
            if (!this.id) {
                uni.showToast({ title: '缺少单据ID', icon: 'none' })
                return
            }
            uni.showLoading && uni.showLoading({ title: '加载中...', mask: true })
            assayReportDetailApi({ id: this.id })
                .then(res => {
                    const data = (res && res.data) || {}
                    this.form = {
                        weighingTicketId: data.weighingTicketId || '',
                        weighingTicketCode: data.weighingTicketCode || '',
                        reportDate: data.reportDate || '',
                        supplierName: data.supplierName || '',
                        procurementPlanId: data.procurementPlanId || '',
                        productId: data.productId || ''
                    }
                    // 码单编号回显:用详情返回的码单ID/编号构造已选行,供分厂成分联动使用
                    this.selectedTicketRow = data.weighingTicketId
                        ? {
                            id: data.weighingTicketId,
                            code: data.weighingTicketCode || '',
                            workshopCode: data.workshopCode || '',
                            workshopName: data.workshopName || ''
                          }
                        : null
                    // 计划编号/品种下拉:先用详情数据兜底,再异步刷新真实选项
                    this.planOptions = data.procurementPlanId
                        ? [{ label: data.planCode || '', value: data.procurementPlanId }]
                        : []
                    this.productOptions = data.productId
                        ? [{ label: data.productName || '', value: data.productId }]
                        : []
                    // 化验明细回显(成分值随行回显,由明细子组件 buildRow 统一初始化字段)
                    const detailLines = Array.isArray(data.lines) ? data.lines : []
                    const detailTable = this.$refs.detailTable
                    detailTable && detailTable.setRows && detailTable.setRows(detailLines)
                    // 异步刷新明细数据源与下拉选项
                    if (data.weighingTicketId && data.procurementPlanId && data.productId) {
                        this.loadApplyDetail(data.weighingTicketId, data.procurementPlanId, data.productId, detailLines)
                    }
                    if (data.weighingTicketId) {
                        this.loadPlanOptions(data.weighingTicketId)
                    }
                    if (data.weighingTicketId && data.procurementPlanId) {
                        this.loadProductOptions(data.weighingTicketId, data.procurementPlanId)
                    }
                })
                .catch(() => {
                    uni.showToast({ title: '详情加载失败', icon: 'none' })
                })
                .finally(() => {
                    uni.hideLoading && uni.hideLoading()
                })
        },
        // 根据所选码单加载计划编号下拉选项
        loadPlanOptions(ticketId) {
            assayReportPlanOptionsApi(ticketId)
                .then(res => {
                    const list = (res && res.data) || []
                    const options = (list || []).map(item => ({
                        label: item.planCode,
                        value: item.planId
                    }))
                    // 兜底:确保当前已选计划仍在选项中(回显时不丢失显示)
                    if (this.form.procurementPlanId && !options.some(item => item.value === this.form.procurementPlanId)) {
                        const fallback = this.planOptions.find(item => item.value === this.form.procurementPlanId)
                        if (fallback) options.push(fallback)
                    }
                    this.planOptions = options
                })
                .catch(() => {})
        },
        // 根据所选码单与计划加载过磅品种下拉选项
        loadProductOptions(ticketId, planId) {
            assayReportProductOptionsApi({
                ticketId: ticketId || undefined,
                planId
            })
                .then(res => {
                    const list = (res && res.data) || []
                    const options = (list || []).map(item => ({
                        label: item.productName,
                        value: item.productId
                    }))
                    // 兜底:确保当前已选品种仍在选项中(回显时不丢失显示)
                    if (this.form.productId && !options.some(item => item.value === this.form.productId)) {
                        const fallback = this.productOptions.find(item => item.value === this.form.productId)
                        if (fallback) options.push(fallback)
                    }
                    this.productOptions = options
                })
                .catch(() => {})
        },
        // 加载化验明细数据源(送样编号下拉选项),并合并详情明细兜底
        loadApplyDetail(ticketId, planId, productId, detailLines) {
            assayReportApplyOptionsApi({
                ticketId: ticketId || undefined,
                planId: planId || undefined,
                productId: productId || undefined
            })
                .then(res => {
                    const list = ((res && res.data) || []).map(item => ({
                        assayApplyId: item.assayApplyId || item.id || '',
                        assayApplyCode: item.assayApplyCode || '',
                        sampleCode: item.sampleCode || '',
                        productName: item.productName || '',
                        category: item.category || '',
                        productId: item.productId || '',
                        quantity: item.quantity
                    }))
                    // 兜底:详情明细中不在数据源内的送样编号并入选项,避免已选行回显异常
                    const extra = (detailLines || []).filter(line =>
                        line.assayApplyId && !list.some(item => item.assayApplyId === line.assayApplyId)
                    )
                    this.applyDetailOptions = list.concat(extra)
                })
                .catch(() => {
                    this.applyDetailOptions = (detailLines || []).map(item => ({ ...item }))
                })
        },
        // 码单选择弹窗数据源(可选过磅码单分页,合并筛选条件)
        fetchTickets({ pageIndex, pageSize, extra }) {
            const keyword = (extra && extra.keyword) || ''
            const params = {
                pageIndex,
                pageSize,
                code: keyword || undefined
            }
            return assayReportTicketPageApi(params).then(res => {
                const _data = (res && res.data) || {}
                const records = _data.datas || _data.list || _data.records || []
                const totalCount = _data.totalCount || _data.count || 0
                const hasNext = _data.hasNext || false
                return { records, totalCount, hasNext }
            }).catch(() => ({ records: [], totalCount: 0, hasNext: false }))
        },
        openTicketSheet() {
            this.ticketSheet.selectedKeys = this.form.weighingTicketId ? [this.form.weighingTicketId] : []
            this.ticketSheet.visible = true
        },
        // 码单选择回调:带出日期/客户名称,联动刷新计划编号下拉并重置下游数据
        onTicketConfirm({ items }) {
            const row = (items || [])[0]
            if (!row) {
                this.selectedTicketRow = null
                this.form.weighingTicketId = ''
                this.form.weighingTicketCode = ''
                this.form.reportDate = ''
                this.form.supplierName = ''
                this.resetDownStream()
                return
            }
            this.selectedTicketRow = row
            this.form.weighingTicketId = row.id || ''
            this.form.weighingTicketCode = row.code || ''
            // 切换码单后带出日期与客户名称并重置计划编号、品种及化验明细
            this.form.reportDate = String(row.completeTime || '').slice(0, 10)
            this.form.supplierName = row.supplierName || ''
            this.resetDownStream()
            this.loadPlanOptions(row.id)
        },
        resetDownStream() {
            this.form.procurementPlanId = ''
            this.form.productId = ''
            this.planOptions = []
            this.productOptions = []
            this.applyDetailOptions = []
            const detailTable = this.$refs.detailTable
            detailTable && detailTable.setRows && detailTable.setRows([])
        },
        getPlanLabel() {
            const found = (this.planOptions || []).find(it => it.value === this.form.procurementPlanId)
            return found ? found.label : ''
        },
        // 计划编号改选后:重置品种并按所选计划刷新过磅品种下拉
        onPlanChange(e) {
            const opt = (this.planOptions || [])[Number(e.detail.value)]
            if (!opt) return
            this.form.procurementPlanId = opt.value
            this.form.productId = ''
            this.productOptions = []
            this.applyDetailOptions = []
            const detailTable = this.$refs.detailTable
            detailTable && detailTable.setRows && detailTable.setRows([])
            this.loadProductOptions(this.form.weighingTicketId, opt.value)
        },
        getProductLabel() {
            const found = (this.productOptions || []).find(it => it.value === this.form.productId)
            return found ? found.label : ''
        },
        // 过磅品种改选后:按码单/计划/品种加载化验明细并填充表格
        onProductChange(e) {
            const opt = (this.productOptions || [])[Number(e.detail.value)]
            if (!opt) return
            this.form.productId = opt.value
            this.applyDetailOptions = []
            const detailTable = this.$refs.detailTable
            detailTable && detailTable.setRows && detailTable.setRows([])
            assayReportApplyOptionsApi({
                ticketId: this.form.weighingTicketId || undefined,
                planId: this.form.procurementPlanId || undefined,
                productId: opt.value || undefined
            })
                .then(res => {
                    const list = ((res && res.data) || []).map(item => ({
                        assayApplyId: item.assayApplyId || item.id || '',
                        assayApplyCode: item.assayApplyCode || '',
                        sampleCode: item.sampleCode || '',
                        productName: item.productName || '',
                        category: item.category || '',
                        productId: item.productId || '',
                        quantity: item.quantity
                    }))
                    this.applyDetailOptions = list
                    // 默认按化验明细填充表格,样号从1开始编号
                    detailTable && detailTable.loadRows && detailTable.loadRows(list)
                })
                .catch(() => {
                    this.applyDetailOptions = []
                })
        },
        validate() {
            const f = this.form
            if (!f.weighingTicketId) return this.tip('请选择码单编号')
            if (!f.reportDate) return this.tip('日期不能为空')
            if (!f.procurementPlanId) return this.tip('请选择计划编号')
            if (!f.productId) return this.tip('请选择过磅品种')
            return true
        },
        tip(msg) {
            uni.showToast({ title: msg, icon: 'none' })
            return false
        },
        async onSubmit() {
            if (!this.validate()) return
            const confirmRes = await new Promise(resolve => {
                uni.showModal({ title: '提示', content: '确定保存原料化验报告单吗?', confirmText: '确定', cancelText: '取消', success: resolve })
            })
            if (!(confirmRes && confirmRes.confirm)) return
            const detailTable = this.$refs.detailTable
            const lines = (detailTable && detailTable.getLines && detailTable.getLines()) || []
            uni.showLoading({ title: '提交中...', mask: true })
            assayReportUpdateApi({
                id: this.id,
                weighingTicketId: this.form.weighingTicketId,
                reportDate: this.form.reportDate,
                procurementPlanId: this.form.procurementPlanId,
                productId: this.form.productId,
                lines
            })
                .then(() => {
                    uni.hideLoading()
                    uni.showToast({ title: '保存成功', icon: 'none' })
                    try {
                        uni.setStorageSync('MATERIAL_TEST_LIST_NEED_REFRESH', '1')
                        uni.setStorageSync('MATERIAL_TEST_DETAIL_NEED_REFRESH', '1')
                    } catch (e) {}
                    setTimeout(() => { uni.navigateBack() }, 400)
                })
                .catch(e => {
                    uni.hideLoading()
                    uni.showToast({ title: (e && (e.msg || e.message)) || '保存失败', icon: 'none' })
                })
        }
    }
}
</script>

<style lang="scss" scoped>
.page {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.scroll {
    flex: 1;
    padding: 12rpx 0 200rpx !important;
}

.footer {
    z-index: 2;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 32rpx;
    padding-bottom: calc(32rpx + env(safe-area-inset-bottom));
    background: #fff;
    box-shadow: 0 -8rpx 24rpx rgba(0, 0, 0, 0.06);

    .btn {
        height: 80rpx;
        line-height: 80rpx;
        border-radius: 12rpx;
        font-size: 32rpx;
    }

    .submit {
        background: $theme-primary;
        color: #fff;
    }
}

::v-deep .uni-list {
    .uni-easyinput {
        display: flex;

        .uni-input-input {
            color: rgba(0, 0, 0, 0.9);
        }
    }

    .uni-input-placeholder {
        z-index: 1;
    }

    .uni-input-input {
        background-color: #ffffff;
    }

    background: transparent;

    &-item {
        &__extra-text {
            font-size: 32rpx;
        }

        &__content-title {
            font-size: 32rpx;
            color: rgba(0, 0, 0, 0.9);
        }

        &__container {
            padding: 32rpx;

            .item-title,
            .uni-list-item__content {
                flex: none;
                min-height: 48rpx;
                line-height: 48rpx;
                font-size: 32rpx;
                position: relative;
                width: 240rpx;
                margin-right: 32rpx;
                color: rgba(0, 0, 0, 0.9);

                .required {
                    color: red;
                    position: absolute;
                    top: 50%;
                    transform: translateY(-50%);
                    left: -16rpx;
                }
            }
        }

        &.select-item {
            &.is-empty {
                .uni-list-item__extra-text {
                    color: rgba(0, 0, 0, 0.4) !important;
                }
            }

            &.is-filled {
                .uni-list-item__extra-text {
                    color: rgba(0, 0, 0, 0.9) !important;
                }
            }
        }
    }
}

.picker-value {
    font-size: 32rpx;
    color: rgba(0, 0, 0, 0.9);
    line-height: 48rpx;
    text-align: right;
    min-height: 48rpx;

    &.placeholder {
        color: rgba(0, 0, 0, 0.4);
    }
}
</style>