Files
pig-farm-controller-fe/src/components/PigBatchList.vue

201 lines
5.0 KiB
Vue

<template>
<div class="pig-batch-list">
<div v-for="batch in pigBatches" :key="batch.id" class="pig-batch-item">
<div class="batch-header" @click="toggleExpand(batch)">
<div class="batch-info">
<span>批次编号: {{ batch.batch_number }}</span>
<span>状态: {{ batch.status }}</span>
<span>初始数量: {{ batch.initial_count }}</span>
</div>
<div class="batch-actions">
<el-button size="small" type="primary" @click.stop="showAddPenDialog(batch)">增加猪栏</el-button>
<el-button size="small" @click.stop="emitEditBatch(batch)">编辑</el-button>
<el-button size="small" type="danger" @click.stop="emitDeleteBatch(batch)">删除</el-button>
</div>
</div>
<div v-if="batch.isExpanded" class="batch-content">
<div v-if="batch.pens && batch.pens.length > 0" class="pig-pen-list">
<PigPenInfoCard
v-for="pen in batch.pens"
:key="pen.id"
:pen="pen"
@edit="emitEditPen"
@delete="emitDeletePen"
/>
</div>
<div v-else class="no-pens-message">
<p>该猪群下没有猪栏信息</p>
</div>
</div>
</div>
<!-- 增加猪栏对话框 -->
<el-dialog title="选择猪栏" v-model="addPenDialogVisible" width="30%">
<el-select v-model="selectedPenId" placeholder="请选择猪栏" style="width: 100%;">
<el-option
v-for="pen in availablePens"
:key="pen.id"
:label="pen.label"
:value="pen.id">
</el-option>
</el-select>
<template #footer>
<span class="dialog-footer">
<el-button @click="addPenDialogVisible = false"> </el-button>
<el-button type="primary" @click="assignPen"> </el-button>
</span>
</template>
</el-dialog>
</div>
</template>
<script>
import PigPenInfoCard from './PigPenInfoCard.vue';
import { getAllPens, getAllPigHouses } from '../api/pigBatch'; // Import new API functions
export default {
name: 'PigBatchList',
components: {
PigPenInfoCard
},
props: {
pigBatches: {
type: Array,
required: true
}
},
emits: ['edit-batch', 'delete-batch', 'add-pen', 'edit-pen', 'delete-pen', 'assign-pen-to-batch'],
data() {
return {
addPenDialogVisible: false,
availablePens: [],
selectedPenId: null,
currentBatch: null // To store the batch for which we are adding a pen
};
},
methods: {
toggleExpand(batch) {
batch.isExpanded = !batch.isExpanded;
},
async fetchAvailablePens() {
try {
const [pensResponse, housesResponse] = await Promise.all([
getAllPens(),
getAllPigHouses()
]);
const pens = pensResponse.data;
const houses = housesResponse.data;
// Create a map for quick lookup of house names by ID
const houseMap = new Map(houses.map(house => [house.id, house.name]));
this.availablePens = pens.map(pen => ({
id: pen.id,
label: `${pen.pen_number}(${houseMap.get(pen.house_id) || '未知猪舍'})` // Changed format here
}));
} catch (error) {
console.error("Error fetching pens or houses:", error);
this.$message.error("获取猪栏或猪舍信息失败");
}
},
showAddPenDialog(batch) {
this.currentBatch = batch;
this.selectedPenId = null; // Reset selection
this.fetchAvailablePens();
this.addPenDialogVisible = true;
},
assignPen() {
if (this.selectedPenId && this.currentBatch) {
this.$emit('assign-pen-to-batch', {
batchId: this.currentBatch.id,
penId: this.selectedPenId
});
this.addPenDialogVisible = false;
} else {
this.$message.warning("请选择一个猪栏");
}
},
// 猪群操作
emitEditBatch(batch) {
this.$emit('edit-batch', batch);
},
emitDeleteBatch(batch) {
this.$emit('delete-batch', batch);
},
// 猪栏操作
emitEditPen(pen) {
this.$emit('edit-pen', pen);
},
emitDeletePen(pen) {
this.$emit('delete-pen', pen);
}
}
}
</script>
<style scoped>
.pig-batch-list {
width: 100%;
}
.pig-batch-item {
border: 1px solid #eee;
border-radius: 4px;
margin-bottom: 16px;
overflow: hidden;
}
.batch-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px;
cursor: pointer;
background-color: #f9f9f9;
transition: background-color 0.3s;
}
.batch-header:hover {
background-color: #f0f0f0;
}
.batch-info {
display: flex;
flex-direction: column;
gap: 8px;
}
.batch-info span:first-child {
font-weight: bold;
}
.batch-info span {
margin-right: 20px;
font-size: 14px;
color: #606266;
}
.batch-actions {
display: flex;
gap: 10px;
}
.batch-content {
padding: 16px;
border-top: 1px solid #eee;
}
.pig-pen-list {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.no-pens-message {
color: #909399;
text-align: center;
padding: 20px 0;
}
</style>