hejianhao
2025-04-16 dab2d210ca06c1faa514c6388fbd5de1ab355360
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
<template>
  <div class='duty_add'>
    <v-header :title="title"></v-header>
    <div class="cont">
      <section
        v-for="(i,j) in list"
        :key="j+'-s'"
      >
        <div class="time f">
          <p class="label">时间:</p>
          <article>
            <el-date-picker
              v-model="i.rotaDate"
              value-key="i.rotaDate"
              type="date"
              placeholder="选择日期"
              size="small"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </article>
        </div>
        <div class="people">
          <div class="f">
            <p class="label">值班领导:</p>
            <article>
              <el-select
                v-model="i.rotaLeader"
                size="small"
              >
                <el-option
                  v-for="item in choosePersonData"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </article>
          </div>
          <div
            class="f"
            v-for="(x,y) in i.rotaPerson"
            :key="y+'-rotaPerson'"
          >
            <p class="label">值班人员{{y+1}}:</p>
            <article>
              <el-select
                v-model="x.id"
                size="small"
              >
                <el-option
                  v-for="item in choosePersonData"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </article>
          </div>
          <div class="f">
            <button
              class="m_btn small bgc_tran"
              @click="onAddStaff(i,j)"
            ><b class="col_primary"><i class="el-icon-plus"></i>添加人员</b></button>
            <button
              class="m_btn small bgc_tran"
              @click="deletes(i,j)"
            ><b class="col_primary"><i class="el-icon-plus"></i>删除人员</b></button>
            <button class="m_btn small bgc_tran" @click="repeat(i,j)"><b class="col_primary"><i class="el-icon-plus"></i>重复今日</b></button>
          </div>
        </div>
      </section>
    </div>
 
 
 
    <div class="add_list">
      <button
        class="m_btn small bgc_tran"
        @click="onOnceList"
      ><b class="col_primary"><i class="el-icon-plus"></i>增加</b></button>
    </div>
    <div class="foot">
      <button
        class="m_btn demo_btn"
        @click="$router.back()"
      ><b>取消</b></button>
      <button class="m_btn bgc_primary" @click="isOk('recordform')">确定</button>
    </div>
  </div>
</template>
 
<script>
export default {
  props: {
    // edit: { type: Boolean, default: false },
    item: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  components: {},
  data() {
    return {
      list:[  ],
      a: "",
      b: "",
      choosePersonData:[],
      selectIdsArr:[]
    };
  },
  created () {
    //获取选择人员
    this.choosePerson()
    const { id } = this.$route.params;
    if (id) {
      this.getDutyDetail(); 
    } else {
      this.onOnceList();
    }
  },
  computed: {
    title() {
      return this.$route.query.id ? "编辑" : "新增";
    },
  },
  watch: {},
  methods: {
    /** 获取详情 */
    getDutyDetail () {
      this.$api.get('patrolRecord/detailRota', {rotaId: this.$route.params.id}, d => {
        const item = {
          id: d.id,
          rotaDate: d.rotaDate,
          rotaLeader: +d.rotaLeader,
          rotaPerson: d.rotaPerson.split(',').map(d => ({id: +d}))
        };
        this.list.push(item);
      })
    },
 
    choosePerson() {
      this.$api.post('patrolRecord/getTotlePerson', {param:""}, e => {
        let newPerson = e.map(d=> {
          return {
            label : d.name,
            value : d.id,
            phone:d.phone,
          }      
        });
        this.choosePersonData = newPerson;
      })
    },
 
    getListOnce() {
      return {
        rotaDate: "",
        rotaLeader: "",
        rotaPerson: [{ id: "" }],
      };
    },
    onOnceList() {
      this.list.push(this.getListOnce());
    },
    onAddStaff(i, j) {
      i.rotaPerson.push({ id: "" });
    },
 
    //删除值班人员
    deletes(i, j) {
      if (i !== 0) {
        i.rotaPerson.splice(i, 1)
        console.log('deleted:', JSON.stringify(i.rotaPerson))
      }
    },
 
    // 重复今日
    repeat(i,j) {
      const item = JSON.parse(JSON.stringify(i));
      this.list.splice(j, 0, item);
    },
 
    //确认新增
    isOk() {
      for (let i = 0; i < this.list.length; i++ ) {
        let rotaPerson = {}, item = this.list[i];
        // 验证     
        if(!item.rotaDate) {
          demo.toast("请选择日期")
          return
        }
        if(!item.rotaLeader) {
          demo.toast("请选择值班领导")
          return
        }
        for (let n = 0; n < item.rotaPerson.length; n++) {
          let rotaItem = item.rotaPerson[n];
          if(!rotaItem.id) {
            return  demo.toast("请选择值班人员")
          }
          if (rotaItem.id && rotaPerson[`${rotaItem.id}`]) {
            demo.toast(`值班表${i + 1}  值班人员重复,请重新选择`);
            return false;
          } else {
            rotaPerson[`${rotaItem.id}`] = rotaItem.id;
          }
        }
      }
 
      let params = this.list.map(d => {
        let rotaPerson = [], personName = [], personPhone = [], leaderName = '', leaderPhone = '';
        d.rotaPerson.forEach(m => {
          const item = this.choosePersonData.find(k => k.value === m.id);
          personName.push(item.label);
          personPhone.push(item.phone);
          rotaPerson.push(m.id);
 
          const leader = this.choosePersonData.find(k => k.value === d.rotaLeader);
          leaderName = leader.label;
          leaderPhone = leader.phone
        });
        return {
          ...d,
          rotaPerson: rotaPerson.join(','),
          personName: personName.join(','),
          personPhone: personPhone.join(','),
          leaderName,
          leaderPhone,
        }
      });
      // console.log(params)
      this.$api.post('patrolRecord/addRota', params, e => {
        demo.toast('保存成功');
        this.$router.back()
      })
    }
  }
};
</script>
<style lang='less' scoped>
.duty_add {
  overflow-y: auto;
   .label:before {
        content: '* ';
        color: red;
    }
  .cont {
    width: calc(100% - 30px);
    margin: 0 auto;
    > section {
      display: flex;
      margin-bottom: 20px;
      box-shadow: 0 0 20px rgb(240, 240, 240);
      border-radius: 5px;
      padding: 20px 5px;
      transition: all 0.5s;
      box-sizing: border-box;
      &:hover {
        box-shadow: 0 0 20px rgba(45, 238, 228, 0.3);
      }
      .time {
        width: 25%;
      }
      .people {
        width: 75%;
        display: flex;
        flex-wrap: wrap;
        .f {
          width: 30%;
          min-width: 200px;
          max-width: 350px;
          margin-bottom: 5px;
        }
      }
    }
  }
  .add_list {
    padding: 30px 10px;
    .m_btn {
      min-width: 80px;
    }
  }
  .foot {
    display: flex;
    padding-bottom: 50px;
    .m_btn {
      min-width: 100px;
      margin-right: 20px;
    }
  }
  .f {
    display: flex;
    .label {
      width: 90px;
      line-height: 32px;
      font-size: 14px;
      text-align: right;
    }
    article {
      width: calc(100% - 110px);
      > div {
        width: 100%;
        max-width: 100%;
      }
    }
    .m_btn {
      flex: 1;
    }
  }
}
</style>