assay-detail-table.vue 14.6 KB
<template>
    <!-- 化验明细:样品/数量/样号/送样编号 + 成份%(成分项按码单分厂区分,逐行填写) -->
    <view class="sub-table">
        <view class="sub-header">
            <image class="sub-bar" src="/static/images/title.png" />
            <text class="sub-title">化验明细</text>
            <view class="add-btn" @click="addRow">
                <text class="add-btn_text">+ 新增行</text>
            </view>
        </view>
        <view class="sub-body">
            <view v-if="!rows.length" class="empty-tip">暂无明细,请选择过磅品种后加载</view>
            <view v-for="(row, idx) in rows" :key="'line-' + idx" class="card">
                <view class="card-index">第 {{ idx + 1 }} 行</view>

                <!-- <view class="row">
                    <text class="label">送样编号</text>
                    <picker
                        class="picker-box"
                        mode="selector"
                        :range="getRowCodeLabels(row)"
                        @change="onSendCodeChange($event, idx)"
                    >
                        <view class="picker-value" :class="{ placeholder: !row.assayApplyCode }">
                            {{ row.assayApplyCode || '请选择送样编号' }}
                        </view>
                    </picker>
                </view> -->
                <!-- 取样编号:下拉选择(数据源与送样编号一致,按 sampleCode 字段展示/取值) -->
                <view class="row">
                    <text class="label">取样编号</text>
                    <picker
                        class="picker-box"
                        mode="selector"
                        :range="getRowSampleLabels(row)"
                        @change="onSampleCodeChange($event, idx)"
                    >
                        <view class="picker-value" :class="{ placeholder: !row.sampleCode }">
                            {{ row.sampleCode || '请选择取样编号' }}
                        </view>
                    </picker>
                </view>
                <view class="row">
                    <text class="label">样品</text>
                    <text class="value">{{ row.category || '-' }}</text>
                </view>
                <view class="row">
                    <text class="label">数量(kg)</text>
                    <text class="value">{{ row.quantity === null || row.quantity === undefined || row.quantity === '' ? '-' : row.quantity }}</text>
                </view>

                <!-- 成份%(按分厂区分的成分项,逐行填写) -->
                <view class="component-title">成份%</view>
                <view class="component-grid">
                    <view v-for="key in componentKeys" :key="key" class="component-item">
                        <text class="component-label">{{ componentLabels[key] || key }}</text>
                        <input
                            class="component-input"
                            type="digit"
                            :value="row[key] === null || row[key] === undefined ? '' : String(row[key])"
                            placeholder="未填"
                            placeholder-class="component-placeholder"
                            @input="onComponentInput($event, idx, key)"
                        />
                    </view>
                </view>

                <view class="card-delete-btn" @click="delRow(idx)"><text>删除该行</text></view>
            </view>
        </view>
    </view>
</template>

<script>
import { COMPONENT_LABELS, ALL_COMPONENT_KEYS, getComponentKeys } from './component-config.js'

export default {
    name: 'AssayDetailTable',
    props: {
        // 化验明细数据源(applyOptions 返回),供送样编号下拉与固定值使用
        options: {
            type: Array,
            default: () => []
        },
        // 码单分厂编码(ticketPage 返回 workshopCode),决定成份%项集合
        workshopCode: {
            type: String,
            default: ''
        },
        // 码单分厂名称(workshopCode 缺失时兜底匹配)
        workshopName: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            componentLabels: COMPONENT_LABELS,
            // 化验明细行数据
            rows: []
        }
    },
    computed: {
        // 当前分厂对应的成分项 key 列表(按 workshopCode 匹配,名称兜底)
        componentKeys() {
            return getComponentKeys(this.workshopCode, this.workshopName)
        }
    },
    methods: {
        // 构造行对象(初始化全部成分字段,保证响应式;样品取 category,对齐 PC 端 buildRow)
        buildRow(item, index) {
            const row = {
                category: (item && item.category) || '',
                productId: (item && item.productId) || '',
                quantity: item ? item.quantity : '',
                testCode: String(index + 1),
                assayApplyCode: (item && item.assayApplyCode) || '',
                assayApplyId: (item && item.assayApplyId) || '',
                assayApplyLineId: (item && item.assayApplyLineId) || '',
                sampleCode: (item && item.sampleCode) || ''
            }
            ALL_COMPONENT_KEYS.forEach(key => {
                row[key] = (item && item[key] !== undefined && item[key] !== null) ? item[key] : null
            })
            return row
        },
        // 按化验明细数据源填充表格(每条明细一行,样号从1开始编号)
        loadRows(options) {
            this.rows = (options || []).map((item, index) => this.buildRow(item, index))
        },
        // 回显表格数据(修改时使用),样号按行序重新编号
        setRows(list) {
            this.rows = (list || []).map((item, index) => this.buildRow(item, index))
        },
        // 获取提交用明细行(含行ID与各行成分值,未填/分厂未包含的成分为 null)
        getLines() {
            return (this.rows || [])
                .filter(row => row.assayApplyId)
                .map(row => {
                    const line = {
                        assayApplyId: row.assayApplyId,
                        assayApplyLineId: row.assayApplyLineId || ''
                    }
                    ALL_COMPONENT_KEYS.forEach(key => {
                        const val = row[key]
                        line[key] = (val === undefined || val === null || val === '') ? null : Number(val)
                    })
                    return line
                })
        },
        // 送样编号下拉标签(化验明细数据中所有的送样编号,排除已被其他行选中的,避免重复选择)
        getRowCodeLabels(row) {
            return this.getRowCodeOptions(row).map(it => it.label)
        },
        getRowCodeOptions(row) {
            const selectedIds = (this.rows || [])
                .filter(r => r !== row && r.assayApplyId)
                .map(r => r.assayApplyId)
            return (this.options || [])
                .filter(item => item.assayApplyCode && !selectedIds.includes(item.assayApplyId))
                .map(item => ({ label: item.assayApplyCode, value: item.assayApplyId }))
        },
        // 送样编号下拉变化:带出该送样编号对应的固定数据(成分值保留当前行已填写内容)
        onSendCodeChange(e, index) {
            const row = this.rows[index]
            if (!row) return
            const opt = this.getRowCodeOptions(row)[Number(e.detail.value)]
            if (!opt) return
            const item = (this.options || []).find(i => i.assayApplyId === opt.value) || {}
            row.productId = item.productId || ''
            row.assayApplyCode = item.assayApplyCode || ''
            row.assayApplyId = item.assayApplyId || ''
            row.assayApplyLineId = item.assayApplyLineId || ''
            row.category = item.category || ''
            row.quantity = item.quantity !== undefined && item.quantity !== null ? item.quantity : ''
            row.sampleCode = item.sampleCode || ''
        },
        // 取样编号下拉标签(数据源与送样编号一致,按 sampleCode 展示,排除已被其他行选中的)
        getRowSampleLabels(row) {
            return this.getRowSampleOptions(row).map(it => it.label)
        },
        getRowSampleOptions(row) {
            const selectedSamples = (this.rows || [])
                .filter(r => r !== row && r.sampleCode)
                .map(r => r.sampleCode)
            return (this.options || [])
                .filter(item => item.sampleCode && !selectedSamples.includes(item.sampleCode))
                .map(item => ({ label: item.sampleCode, value: item.sampleCode }))
        },
        // 取样编号下拉变化:与送样编号选择一致,按 sampleCode 找到对应数据带出
        onSampleCodeChange(e, index) {
            const row = this.rows[index]
            if (!row) return
            const opt = this.getRowSampleOptions(row)[Number(e.detail.value)]
            if (!opt) return
            const item = (this.options || []).find(i => i.sampleCode === opt.value) || {}
            row.productId = item.productId || ''
            row.assayApplyCode = item.assayApplyCode || ''
            row.assayApplyId = item.assayApplyId || ''
            row.assayApplyLineId = item.assayApplyLineId || ''
            row.category = item.category || ''
            row.quantity = item.quantity !== undefined && item.quantity !== null ? item.quantity : ''
            row.sampleCode = item.sampleCode || opt.value || ''
        },
        // 成份值输入:仅允许数字与小数点,空值重置为 null
        onComponentInput(e, index, key) {
            const row = this.rows[index]
            if (!row) return
            let value = String((e && e.detail && e.detail.value) == null ? '' : e.detail.value)
            value = value.replace(/[^\d.]/g, '')
            const parts = value.split('.')
            if (parts.length > 2) {
                value = parts[0] + '.' + parts.slice(1).join('')
            }
            row[key] = value === '' ? null : value
        },
        // 新增化验明细行:样品/数量为固定数据,样号按行序从1开始编号,成分值默认空
        addRow() {
            const fixed = (this.options || [])[0] || {}
            this.rows.push(this.buildRow(fixed, this.rows.length))
        },
        // 删除指定化验明细行,删除后样号重新从1开始编号
        delRow(index) {
            if (index < 0 || index >= this.rows.length) return
            this.rows.splice(index, 1)
            this.rows.forEach((row, i) => {
                row.testCode = String(i + 1)
            })
        }
    }
}
</script>

<style lang="scss" scoped>
.sub-table {
    background: #fff;
    margin-bottom: 20rpx;
}
.sub-header {
    display: flex;
    align-items: center;
    padding: 16rpx 24rpx 16rpx 32rpx;
    .sub-bar { width: 6rpx; height: 32rpx; margin-right: 14rpx; }
    .sub-title { font-size: 30rpx; font-weight: 600; color: rgba(0, 0, 0, 0.9); flex: 1; line-height: 42rpx; }
    .add-btn {
        height: 52rpx;
        line-height: 52rpx;
        padding: 0 24rpx;
        border-radius: 8rpx;
        background: $theme-primary;
        .add-btn_text { font-size: 26rpx; color: #fff; }
    }
}
.sub-body {
    padding: 0 32rpx 24rpx;
    .empty-tip {
        text-align: center;
        padding: 60rpx 0;
        color: rgba(0, 0, 0, 0.4);
        font-size: 28rpx;
    }
    .card {
        position: relative;
        background: #fafafa;
        border-radius: 12rpx;
        padding: 32rpx 28rpx 20rpx;
        margin-bottom: 24rpx;
        .card-index {
            position: absolute;
            top: 0;
            right: 0;
            background: rgba(24, 144, 255, 0.08);
            color: $theme-primary;
            font-size: 22rpx;
            line-height: 36rpx;
            padding: 0 16rpx;
            border-bottom-left-radius: 12rpx;
            border-top-right-radius: 12rpx;
        }
        .card-delete-btn {
            margin-top: 20rpx;
            height: 64rpx;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 8rpx;
            background: rgba(213, 73, 65, 0.06);
            border: 1rpx solid rgba(213, 73, 65, 0.45);
            text { font-size: 26rpx; color: #d54941; }
        }
        .row {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 14rpx 0;
            border-bottom: 1rpx dashed #f2f2f2;
            &:last-of-type { border-bottom: none; }
            .label {
                flex-shrink: 0;
                width: 220rpx;
                font-size: 28rpx;
                color: rgba(0, 0, 0, 0.6);
                line-height: 40rpx;
            }
            .picker-box { flex: 1; }
            .picker-value {
                font-size: 28rpx;
                color: rgba(0, 0, 0, 0.9);
                line-height: 40rpx;
                text-align: right;
                &.placeholder { color: rgba(0, 0, 0, 0.4); }
            }
            .value {
                flex: 1;
                text-align: right;
                font-size: 28rpx;
                color: rgba(0, 0, 0, 0.9);
                line-height: 40rpx;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }
        }
        .component-title {
            font-size: 28rpx;
            font-weight: 600;
            color: rgba(0, 0, 0, 0.9);
            margin: 16rpx 0 4rpx;
        }
        .component-grid {
            display: flex;
            flex-wrap: wrap;
            .component-item {
                width: 33.33%;
                box-sizing: border-box;
                padding: 12rpx 8rpx 0 0;
                .component-label {
                    display: block;
                    font-size: 24rpx;
                    color: rgba(0, 0, 0, 0.6);
                    line-height: 36rpx;
                }
                .component-input {
                    width: 100%;
                    height: 60rpx;
                    line-height: 60rpx;
                    margin-top: 6rpx;
                    padding: 0 16rpx;
                    border: 1rpx solid #e7e7e7;
                    border-radius: 8rpx;
                    font-size: 26rpx;
                    color: rgba(0, 0, 0, 0.9);
                    background: #fff;
                    box-sizing: border-box;
                }
                .component-placeholder { color: rgba(0, 0, 0, 0.3); }
            }
        }
    }
}
</style>