wangjuncheng
2025-04-15 65dc7a8dab046264766f7ae32070ded7bf34e6fe
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
<template>
  <div class="real-time-simulation">
    <div class="left-top">
      <span>实时模拟</span>
      <span class="clickable-text" @click="toggleDetails">{{ isCollapsed ? '展开' : '收起' }}</span>
    </div>
    <div class="details" :class="{ hidden: isCollapsed }">
      <div class="input-group">
        <div class="input-item">
          <label>雨量数据:</label>
          <el-select v-model="selectedRainfall" placeholder="请选择" popper-class="mySelectStyle">
            <el-option
              v-for="item in rainfallData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>
      </div>
      <div class="table-container">
        <div class="table-row" v-for="(item, index) in filteredTableData" :key="index">
          <input type="checkbox" v-model="item.selected" />
          <span>{{ item.name }}</span>
        </div>
      </div>
      <div style="margin-top: 10px;">
        <label>仿真参数:</label>
        <div style="width: 100%; height: 60px; background-color: #fff;"></div>
      </div>
    </div>
    <div class="buttons">
      <el-button type="primary" @click="openSaveDialog">保存方案</el-button>
      <el-button type="success" @click="startPlay">开始模拟</el-button>
      <el-button type="success" @click="futurePredictions">未来预测</el-button>
    </div>
 
    <!-- 保存方案对话框 -->
    <el-dialog
      v-model="saveDialogVisible"
      title="保存方案"
      width="50%"
      :before-close="handleClose"
       custom-class="custom-dialog"
    >
      <div class="dialog-content">
        <p><strong>所选重点沟:</strong>{{ props.selectedArea }}</p>
        <p><strong>模拟类型:</strong>实时模拟</p>
        <p><strong>雨量数据类型:</strong>{{ selectedRainfallName }}</p>
        <p><strong>设备信息:</strong></p>
        <ul>
          <li v-for="item in selectedDevices" :key="item.id">{{ item.name }}</li>
        </ul>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="saveDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmSave">确定保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import { ref, watch, defineProps, computed, inject } from 'vue';
import { ElMessage } from 'element-plus';
import { initeWaterPrimitiveView } from "@/utils/water";
import { useSimStore } from "@/store/simulation.js"; // 引入 Store
 
// 获取 Store 实例
const simStore = useSimStore();
 
// 注入模拟操作方法
const { startSimulate, endSimulate } = inject("simulateActions");
 
function startPlay() {
  const selectedItems = filteredTableData.value.filter(item => item.selected);
  if (selectedItems.length > 0) {
    console.log('选中的项:', selectedItems.map(item => item.name));
  } else {
    console.log('未选中任何项');
  }
  console.log('当前选中的区域:', props.selectedArea);
  console.log('当前选中的雨量数据:', selectedRainfall.value);
  initeWaterPrimitiveView();
  startSimulate();
}
 
// 工具函数:深拷贝并设置默认选中状态
function deepCloneAndSetSelected(data) {
  const newData = {};
  for (const key in data) {
    newData[key] = data[key].map(item => ({ ...item, selected: true }));
  }
  return newData;
}
 
// 接收父组件传递的 props
const props = defineProps({
  selectedArea: {
    type: String,
    required: true
  }
});
 
// 子组件内部状态
const rainfallData = ref([]); // 雨量数据
const tableData = ref({}); // 表格数据(按雨量数据分组)
const selectedRainfall = ref(''); // 当前选中的雨量数据
const isCollapsed = ref(false); // 控制展开/收起状态
const saveDialogVisible = ref(false); // 控制保存方案对话框显示状态
 
// 模拟不同区域的数据
const areaDataMap = {
  孙胡沟: {
    rainfallData: [
      { id: '1', name: '气象实时数据 - 孙胡沟' },
      { id: '2', name: '雨量计实时数据 - 孙胡沟' }
    ],
    tableData: {
      '1': [
        { id: '001', name: '孙胡沟气象站001' },
        { id: '002', name: '孙胡沟气象站002' } 
      ],
      '2': [
        { id: '003', name: '孙胡沟雨量计003' },
        { id: '004', name: '孙胡沟雨量计004' } 
      ]
    }
  },
  鱼水洞后沟: {
    rainfallData: [
      { id: '1', name: '气象实时数据 - 鱼水洞后沟' },
      { id: '2', name: '雨量计实时数据 - 鱼水洞后沟' }
    ],
    tableData: {
      '1': [
        { id: '005', name: '鱼水洞后沟气象站005' },
        { id: '006', name: '鱼水洞后沟气象站006' } 
      ],
      '2': [
        { id: '007', name: '鱼水洞后沟雨量计007' },
        { id: '008', name: '鱼水洞后沟雨量计008' } 
      ]
    }
  },
  于家西沟: {
    rainfallData: [
      { id: '1', name: '气象实时数据 - 于家西沟' },
      { id: '2', name: '雨量计实时数据 - 于家西沟' }
    ],
    tableData: {
      '1': [
        { id: '009', name: '于家西沟气象站009' },
        { id: '010', name: '于家西沟气象站010' } 
      ],
      '2': [
        { id: '011', name: '于家西沟雨量计011' },
        { id: '012', name: '于家西沟雨量计012' } 
      ]
    }
  }
};
 
// 根据 selectedArea 更新数据
watch(
  () => props.selectedArea,
  (newArea) => {
    if (areaDataMap[newArea]) {
      rainfallData.value = areaDataMap[newArea].rainfallData;
      // 使用深拷贝设置默认选中状态
      tableData.value = deepCloneAndSetSelected(areaDataMap[newArea].tableData);
      selectedRainfall.value = ''; // 清空选中的雨量数据
    } else {
      rainfallData.value = [];
      tableData.value = {};
      selectedRainfall.value = '';
    }
  },
  { immediate: true } // 立即执行一次,确保初始数据正确
);
 
// 动态计算表格数据
const filteredTableData = computed(() => {
  return selectedRainfall.value && tableData.value[selectedRainfall.value]
    ? tableData.value[selectedRainfall.value]
    : [];
});
 
// 获取当前选中的雨量数据名称
const selectedRainfallName = computed(() => {
  const selected = rainfallData.value.find(item => item.id === selectedRainfall.value);
  return selected ? selected.name : '';
});
 
// 获取当前选中的设备信息
const selectedDevices = computed(() => {
  return filteredTableData.value.filter(item => item.selected);
});
 
// 打开保存方案对话框
const openSaveDialog = () => {
  if (!props.selectedArea || !selectedRainfall.value || selectedDevices.value.length === 0) {
    ElMessage.warning('请先选择区域、雨量数据并勾选设备');
    return;
  }
  saveDialogVisible.value = true;
};
 
// 关闭保存方案对话框
const handleClose = () => {
  saveDialogVisible.value = false;
};
 
// 确认保存
const confirmSave = () => {
  // 构造新的方案对象
  const newScheme = {
    id: Date.now().toString(), // 唯一 ID
    area: props.selectedArea, // 区域
    name: selectedRainfallName.value, // 方案名称(雨量数据类型)
    createTime: new Date().toISOString(), // 创建时间
    taskStatus: 0, // 初始状态为未开始
    rainfallType: selectedRainfallName.value, // 雨量数据类型
    devices: selectedDevices.value.map((item) => item.name), // 设备信息
  };
 
  // 调用 Store 的方法添加方案
  simStore.addSchemCard(newScheme);
 
  console.log("保存方案成功", newScheme);
  ElMessage.success("方案已保存");
 
  // 关闭对话框
  saveDialogVisible.value = false;
};
 
const toggleDetails = () => {
  isCollapsed.value = !isCollapsed.value;
};
 
const futurePredictions = () => {
  console.log('未来预测按钮被点击');
};
</script>
 
<style scoped>
.custom-dialog {
  z-index: 3000 !important;
}
.real-time-simulation {
  margin-bottom: 20px;
}
 
.clickable-text {
  margin-left: 160px;
  cursor: pointer;
  font-size: 14px;
  color: #61f7d4;
}
 
.details {
  margin-top: 10px;
  transition: height 0.3s ease, opacity 0.3s ease;
  overflow: hidden;
}
 
.hidden {
  height: 0;
  opacity: 0;
}
 
.input-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
 
.input-item {
  display: flex;
  align-items: center;
}
 
label {
  text-align: left;
  white-space: nowrap;
  margin-right: 10px;
}
 
.el-select {
  flex: 4;
  text-align: left;
  margin-bottom: 10px;
}
 
.table-container {
  font-size: 12px;
  height: 120px;
  overflow-y: auto;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 5px;
}
 
.table-row {
  display: flex;
  justify-content: space-between;
  padding: 5px 0;
  border-bottom: 1px solid #ddd;
}
 
.table-row:last-child {
  border-bottom: none;
}
 
.table-row span {
  flex: 1;
  text-align: left;
}
 
.table-row input[type="checkbox"] {
  margin-right: 10px;
}
 
.buttons {
  margin-top: 20px;
  display: flex;
  gap: 10px;
}
 
.el-button {
  flex: 1;
}
</style>