add.vue 13.5 KB
<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.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 {
    assayApplyTicketPageApi,
    assayApplyTicketDataApi,
    assayApplyBreedOptionsApi,
    assayApplyCreateApi
} from '@/api/procure-manage/assayApply.js'
import { getDicByCodes } from '@/utils/dic.js'

export default {
    name: 'AddSampleSubmission',
    components: { RelateSelectSheet },
    data() {
        return {
            form: {
                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()
    },
    methods: {
        async loadDicData() {
            try {
                const results = await getDicByCodes(['DEPARTMENT'])
                this.dicOptions.DEPARTMENT = (results.DEPARTMENT && results.DEPARTMENT.data) || []
            } catch (e) {
                this.dicOptions.DEPARTMENT = []
            }
        },
        // 码单选择弹窗数据源(可选过磅码单分页)
        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) || {}
                    this.planOptions = ((data && data.planOptions) || []).map(item => ({
                        label: item.planCode,
                        value: item.planId
                    }))
                })
                .catch(() => {
                    this.planOptions = []
                })
        },
        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 = []
            assayApplyBreedOptionsApi({
                ticketId: this.form.weighingTicketId || undefined,
                planId: opt.value
            })
                .then(res => {
                    const list = (res && res.data) || []
                    this.productOptions = (list || []).map(item => ({
                        label: item.productName,
                        value: item.productId
                    }))
                })
                .catch(() => {
                    this.productOptions = []
                })
        },
        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 })
            assayApplyCreateApi({
                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') } 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>