modify.vue 17.7 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
<template>
    <view class="page">
        <scroll-view class="scroll" scroll-y>
            <uni-list>
                <!-- 送样编号(只读) -->
                <uni-list-item class="select-item" :class="form.code ? 'is-filled' : 'is-empty'">
                    <template v-slot:body>
                        <view class="item-title"><text class="required">*</text><text>送样编号</text></view>
                    </template>
                    <template v-slot:footer>
                        <text class="readonly-text value-code">{{ form.code || '-' }}</text>
                    </template>
                </uni-list-item>

                <!-- 码单编号(弹窗选择) -->
                <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.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-item class="select-item" :class="form.department ? '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="departmentLabels" @change="onDepartmentChange">
                            <view class="picker-value" :class="{ placeholder: !form.department }">
                                {{ getDepartmentLabel() || '请选择科室' }}
                            </view>
                        </picker>
                    </template>
                </uni-list-item>
            </uni-list>

            <view class="footer">
                <button class="btn submit" type="primary" @click="onSubmit">保存</button>
            </view>
        </scroll-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 {
    assayApplyDetailApi,
    assayApplyUpdateApi,
    assayApplyTicketPageApi,
    assayApplyTicketDataApi,
    assayApplyBreedOptionsApi
} from '@/api/procure-manage/assayApply.js'
import { getDicByCodes } from '@/utils/dic.js'

export default {
    name: 'ModifySampleSubmission',
    components: { RelateSelectSheet },
    data() {
        return {
            id: '',
            form: {
                code: '',
                weighingTicketId: '',
                weighingTicketCode: '',
                procurementPlanId: '',
                productId: '',
                department: ''
            },
            dicOptions: { DEPARTMENT: [] },
            // 计划编号下拉选项(所选码单下的计划)
            planOptions: [],
            // 品种下拉选项(所选计划下的过磅品种)
            productOptions: [],
            ticketSheet: {
                visible: false,
                fetchFn: null,
                displayFields: [
                    { label: '码单编号', field: 'code' },
                    { label: '分厂', field: 'workshopName' },
                    { label: '创建人', field: 'createBy' },
                    { label: '创建时间', field: 'createTime' }
                ],
                selectedKeys: []
            }
        }
    },
    computed: {
        planLabels() {
            return (this.planOptions || []).map(it => it.label)
        },
        productLabels() {
            return (this.productOptions || []).map(it => it.label)
        },
        departmentLabels() {
            return (this.dicOptions.DEPARTMENT || []).map(it => it.name)
        }
    },
    created() {
        this.ticketSheet.fetchFn = this.fetchTickets.bind(this)
        this.loadDicData()
    },
    onLoad(query) {
        this.id = (query && query.id) || ''
        this.loadDetail()
    },
    methods: {
        async loadDicData() {
            try {
                const results = await getDicByCodes(['DEPARTMENT'])
                this.dicOptions.DEPARTMENT = (results.DEPARTMENT && results.DEPARTMENT.data) || []
            } catch (e) {
                this.dicOptions.DEPARTMENT = []
            }
        },
        loadDetail() {
            if (!this.id) {
                uni.showToast({ title: '缺少单据ID', icon: 'none' })
                return
            }
            uni.showLoading && uni.showLoading({ title: '加载中...', mask: true })
            assayApplyDetailApi({ id: this.id })
                .then(res => {
                    const data = res && res.data ? res.data : {}
                    this.form = {
                        code: data.code || '',
                        weighingTicketId: data.weighingTicketId || '',
                        weighingTicketCode: data.weighingTicketCode || '',
                        procurementPlanId: data.procurementPlanId || '',
                        productId: data.productId || '',
                        department: data.department == null ? '' : String(data.department)
                    }
                    // 计划编号/品种下拉:先用详情数据兜底,再异步刷新真实选项
                    this.planOptions = this.form.procurementPlanId
                        ? [{ label: data.planCode || '', value: this.form.procurementPlanId }]
                        : []
                    this.productOptions = this.form.productId
                        ? [{ label: data.productName || '', value: this.form.productId }]
                        : []
                    if (this.form.weighingTicketId) {
                        this.loadPlanOptions(this.form.weighingTicketId)
                    }
                    if (this.form.weighingTicketId && this.form.procurementPlanId) {
                        this.loadProductOptions(this.form.weighingTicketId, this.form.procurementPlanId)
                    }
                })
                .catch(() => {
                    uni.showToast({ title: '详情加载失败', icon: 'none' })
                })
                .finally(() => {
                    uni.hideLoading && uni.hideLoading()
                })
        },
        // 码单选择弹窗数据源(可选过磅码单分页)
        fetchTickets({ pageIndex, pageSize, extra }) {
            const keyword = (extra && extra.keyword) || ''
            const params = {
                pageIndex,
                pageSize,
                code: keyword || undefined
            }
            return assayApplyTicketPageApi(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.resetDownStream()
                return
            }
            this.form.weighingTicketId = row.id || ''
            this.form.weighingTicketCode = row.code || ''
            this.resetDownStream()
            this.loadPlanOptions(row.id)
        },
        resetDownStream() {
            this.form.procurementPlanId = ''
            this.form.productId = ''
            this.planOptions = []
            this.productOptions = []
        },
        // 根据所选码单加载计划编号下拉选项
        loadPlanOptions(ticketId) {
            assayApplyTicketDataApi(ticketId)
                .then(res => {
                    const data = (res && res.data) || {}
                    const options = ((data && data.planOptions) || []).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) {
            assayApplyBreedOptionsApi({
                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(() => {})
        },
        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.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
        },
        getDepartmentLabel() {
            const found = (this.dicOptions.DEPARTMENT || []).find(it => String(it.code) === String(this.form.department))
            return found ? found.name : ''
        },
        onDepartmentChange(e) {
            const opt = (this.dicOptions.DEPARTMENT || [])[Number(e.detail.value)]
            if (!opt) return
            this.form.department = opt.code != null ? String(opt.code) : ''
        },
        validate() {
            const f = this.form
            if (!f.weighingTicketId) return this.tip('请选择码单编号')
            if (!f.procurementPlanId) return this.tip('请选择计划编号')
            if (!f.productId) return this.tip('请选择品种')
            if (!f.department) 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
            uni.showLoading({ title: '提交中...', mask: true })
            assayApplyUpdateApi({
                id: this.id,
                code: this.form.code,
                weighingTicketId: this.form.weighingTicketId,
                procurementPlanId: this.form.procurementPlanId,
                productId: this.form.productId,
                department: this.form.department
            })
                .then(() => {
                    uni.hideLoading()
                    uni.showToast({ title: '保存成功', icon: 'none' })
                    try {
                        uni.setStorageSync('SAMPLE_SUBMISSION_LIST_NEED_REFRESH', '1')
                        uni.setStorageSync('SAMPLE_SUBMISSION_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;
                }
            }
        }
    }
}

.readonly-text {
    color: rgba(0, 0, 0, 0.9);
    font-size: 32rpx;
    line-height: 48rpx;
    text-align: right;
    white-space: pre-wrap;
    word-break: break-all;
}

.value-code {
    width: 260rpx;
    text-align: left;
}

.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>