create.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <div>
  3. <el-form ref="form" :model="form" inline>
  4. <el-row>
  5. <el-col :span="6">
  6. <el-form-item
  7. label="分部"
  8. prop="organId"
  9. :rules="[{required: true, message: '请选择分部'}]"
  10. >
  11. <el-select
  12. clearable
  13. filterable
  14. v-model="form.organId"
  15. placeholder="请选择分部"
  16. >
  17. <el-option v-for="(item,index) in selects.branchs"
  18. :key="index"
  19. :label="item.name"
  20. :value="item.id"></el-option>
  21. </el-select>
  22. </el-form-item>
  23. </el-col>
  24. <el-col :span="6">
  25. <el-form-item
  26. label="工作周期"
  27. prop="month"
  28. :rules="[{required: true, message: '请选择工作周期'}]"
  29. >
  30. <el-date-picker
  31. v-model="form.month"
  32. type="month"
  33. placeholder="请选择工作周期">
  34. </el-date-picker>
  35. </el-form-item>
  36. </el-col>
  37. </el-row>
  38. <el-row class="group" v-for="(groupItem, groupIndex) in form.group" :key="groupIndex">
  39. <el-col :span="6">
  40. <el-form-item
  41. label="乐团主管"
  42. :prop="`group.${groupIndex}.userId`"
  43. :rules="[{required: true, message: '请选择乐团主管'}]"
  44. >
  45. <el-select
  46. clearable
  47. filterable
  48. v-model="groupItem.userId"
  49. placeholder="请选择乐团主管"
  50. >
  51. <el-option v-for="(item,index) in technicians"
  52. :key="index"
  53. :label="item.realName"
  54. :disabled="form.group.map(m => m.userId).includes(item.userId)"
  55. :value="item.userId"></el-option>
  56. </el-select>
  57. </el-form-item>
  58. </el-col>
  59. <template v-for="(matterItem, matterIndex) in groupItem.matter">
  60. <el-col :offset="matterIndex === 0 ? 0 : 6" :span="6" :key="groupIndex + '-' + matterIndex + '1'">
  61. <el-form-item
  62. :label="'任务事项' + (matterIndex + 1)"
  63. :prop="`group.${groupIndex}.matter.${matterIndex}.item`"
  64. >
  65. <el-select
  66. clearable
  67. filterable
  68. v-model="matterItem.item"
  69. placeholder="请选择任务事项"
  70. >
  71. <el-option v-for="(item,index) in matterTypesOptions"
  72. :key="index"
  73. :label="item.label"
  74. :disabled="groupItem.matter.map(m => m.item).includes(item.value)"
  75. :value="item.value"></el-option>
  76. </el-select>
  77. </el-form-item>
  78. </el-col>
  79. <el-col :span="6" :key="groupIndex + '-' + matterIndex + '2'">
  80. <el-form-item
  81. :label="'任务次数' + (matterIndex + 1)"
  82. :prop="`group.${groupIndex}.matter.${matterIndex}.times`"
  83. >
  84. <el-input clearable v-model="matterItem.times" placeholder="请输入次数" />
  85. </el-form-item>
  86. </el-col>
  87. <el-col :span="6" :key="groupIndex + '-' + matterIndex + '3'">
  88. <div class="ctrl">
  89. <span>
  90. <el-tooltip content="添加任务" placement="top" :open-delay=".5">
  91. <el-button
  92. icon="el-icon-plus"
  93. @click="createMatter(groupItem.matter)"
  94. circle
  95. size="small"
  96. ></el-button>
  97. </el-tooltip>
  98. <el-tooltip content="删除任务" placement="top" :open-delay=".5">
  99. <el-button
  100. icon="el-icon-minus"
  101. circle
  102. size="small"
  103. @click="removeMatter(groupItem.matter, matterIndex)"
  104. :disabled="groupItem.matter.length <= 1"
  105. ></el-button>
  106. </el-tooltip>
  107. </span>
  108. <el-tooltip v-if="isCreate" content="删除乐团主管" placement="top" :open-delay=".5">
  109. <el-button
  110. icon="el-icon-delete"
  111. circle
  112. type="danger"
  113. size="small"
  114. v-if="matterIndex === 0"
  115. @click="removeGroup(form.group, groupIndex)"
  116. :disabled="form.group.length <= 1"
  117. ></el-button>
  118. </el-tooltip>
  119. </div>
  120. </el-col>
  121. </template>
  122. </el-row>
  123. <el-button v-if="isCreate" @click="createGroup" plain block style="width: 100%">添加乐团主管</el-button>
  124. </el-form>
  125. <div slot="footer" style="text-align: right;margin-top: 20px;">
  126. <el-button @click="$emit('close')">取消</el-button>
  127. <el-button type="primary" @click="submit">确认</el-button>
  128. </div>
  129. </div>
  130. </template>
  131. <script>
  132. import { findTechnician } from '@/api/repairManager'
  133. import { matterTypes } from '@/views/main/constant'
  134. import { objectToOptions } from '@/utils'
  135. import { createRandom } from '@/helpers/uuidv4'
  136. import { inspectionAdd, inspectionGetInfo, inspectionUpdate } from '@/views/main/api'
  137. const emptyMatter = {
  138. item: '',
  139. times: ''
  140. }
  141. export default {
  142. props: ['id'],
  143. data() {
  144. return {
  145. form: {
  146. organId: '',
  147. group: [{
  148. _uuid: createRandom(),
  149. userId: '',
  150. matter: [{...emptyMatter}]
  151. }]
  152. },
  153. technicians: []
  154. }
  155. },
  156. computed: {
  157. matterTypesOptions() {
  158. return objectToOptions(matterTypes)
  159. },
  160. isCreate() {
  161. return !this.id
  162. },
  163. },
  164. watch: {
  165. async 'form.organId'() {
  166. if (this.form.organId) {
  167. try {
  168. const res = await findTechnician({
  169. organId: this.form.organId
  170. })
  171. this.technicians = res.data
  172. this.$set(this.form, 'group', group.map(item => ({...item, userId: ''})))
  173. } catch (error) {}
  174. }
  175. }
  176. },
  177. async mounted() {
  178. try {
  179. const res = await inspectionGetInfo({
  180. id: this.id
  181. })
  182. this.form = {
  183. ...this.form,
  184. organId: res.data.organId,
  185. month: res.data.month,
  186. group: [{
  187. _uuid: createRandom(),
  188. userId: res.data.userId,
  189. matter: res.data.inspectionItems.map(item => ({item: item.item, times: item.times}))
  190. }]
  191. }
  192. } catch (error) {}
  193. },
  194. methods: {
  195. createGroup() {
  196. this.form.group.push({
  197. userId: '',
  198. matter: [{...emptyMatter}]
  199. })
  200. },
  201. createMatter(matter) {
  202. matter.push({...emptyMatter})
  203. },
  204. removeMatter(matter, index) {
  205. matter.splice(index, 1)
  206. },
  207. async removeGroup(group, index) {
  208. try {
  209. await this.$confirm('是否确认删除此乐团主管?', '提示', {
  210. type: 'warning'
  211. })
  212. group.splice(index, 1)
  213. } catch (error) {}
  214. },
  215. async submit() {
  216. try {
  217. this.$refs.form.validate(async valid => {
  218. if (valid) {
  219. const data = this.form.group.map(item => ({
  220. organId: this.form.organId,
  221. month: this.form.month,
  222. userId: item.userId,
  223. inspectionItems: item.matter.map(m => ({
  224. ...m,
  225. organId: this.form.organId,
  226. month: this.form.month,
  227. }))
  228. }))
  229. if (this.isCreate) {
  230. await inspectionAdd(data)
  231. this.$message.success('创建成功')
  232. } else {
  233. await inspectionUpdate({
  234. ...data[0],
  235. id: this.id,
  236. })
  237. this.$message.success('修改成功')
  238. }
  239. this.$emit('close')
  240. this.$emit('submited')
  241. }
  242. })
  243. } catch (error) {}
  244. }
  245. }
  246. }
  247. </script>
  248. <style lang="less" scoped>
  249. .group{
  250. background-color: rgba(0, 0, 0, 0.05);
  251. padding: 10px;
  252. border-radius: 3px;
  253. margin-bottom: 10px;
  254. }
  255. .ctrl{
  256. margin-top: 45px;
  257. display: flex;
  258. justify-content: space-between;
  259. i{
  260. font-size: 24px;
  261. }
  262. }
  263. </style>