index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <el-dialog
  3. :title="title"
  4. :visible.sync="visible"
  5. :before-close="handleClose"
  6. :close-on-click-modal="false"
  7. :close-on-press-escape="false"
  8. append-to-body
  9. width="35%"
  10. >
  11. <el-form
  12. :model="form"
  13. ref="tableForm"
  14. class="tableForm"
  15. :rules="tableFormRules"
  16. >
  17. <el-button
  18. type="primary"
  19. size="small"
  20. style="margin-bottom: 10px"
  21. @click="handleAdd()"
  22. v-if="!view"
  23. >新增
  24. </el-button>
  25. <span style="margin-left: 10px" v-if="!isAll"
  26. >待分配数量:<span style="color: #1890ff">{{ count }}</span></span
  27. >
  28. <el-table
  29. ref="multipleTable"
  30. :data="form.arrivalBatch"
  31. tooltip-effect="dark"
  32. style="width: 100%"
  33. stripe
  34. :header-cell-style="{ background: '#EEEEEE', border: 'none' }"
  35. >
  36. <el-table-column label="批次号" prop="batch" v-if="isBatch">
  37. <template slot-scope="{ row, $index }">
  38. <el-form-item
  39. :prop="'arrivalBatch.' + $index + '.batch'"
  40. :rules="tableFormRules.batch"
  41. >
  42. <el-input
  43. placeholder="请输入"
  44. clearable
  45. v-model="row.batch"
  46. :disabled="view"
  47. ></el-input>
  48. </el-form-item>
  49. </template>
  50. </el-table-column>
  51. <el-table-column label="数量" prop="arriveCount">
  52. <template slot-scope="{ row, $index }">
  53. <el-form-item
  54. :prop="'arrivalBatch.' + $index + '.arriveCount'"
  55. :rules="tableFormRules.arriveCount"
  56. >
  57. <el-input-number
  58. :min="0"
  59. :max="getCountMax(row)"
  60. placeholder="请输入"
  61. clearable
  62. @change="handleChange"
  63. v-model="row.arriveCount"
  64. :disabled="view"
  65. ></el-input-number>
  66. </el-form-item>
  67. </template>
  68. </el-table-column>
  69. <el-table-column label="到货时间">
  70. <template slot-scope="{ row, $index }">
  71. <el-form-item
  72. :prop="'arrivalBatch.' + $index + '.arriveDate'"
  73. :rules="tableFormRules.arriveDate"
  74. >
  75. <el-date-picker
  76. clearable
  77. v-model="row.arriveDate"
  78. type="date"
  79. value-format="yyyy-MM-dd"
  80. placeholder="请选择日期"
  81. :disabled="view"
  82. >
  83. </el-date-picker>
  84. </el-form-item>
  85. </template>
  86. </el-table-column>
  87. <el-table-column label="操作" prop="action" width="80" v-if="!view">
  88. <template slot-scope="{ $index }">
  89. <el-link
  90. type="primary"
  91. :underline="false"
  92. @click="handleDel($index)"
  93. >删除
  94. </el-link>
  95. </template>
  96. </el-table-column>
  97. </el-table>
  98. </el-form>
  99. <div class="btns">
  100. <el-button type="primary" size="small" @click="handleOk">确认</el-button>
  101. <el-button size="small" @click="handleClose">取消</el-button>
  102. </div>
  103. </el-dialog>
  104. </template>
  105. <script>
  106. import { copyObj } from '@/utils/util';
  107. export default {
  108. components: {},
  109. props: {
  110. view: {
  111. default: false
  112. },
  113. isBatch: {
  114. default: false
  115. },
  116. isAll: {
  117. default: false
  118. }
  119. },
  120. computed: {
  121. getCountMax() {
  122. return (row) => {
  123. if (this.isAll) {
  124. return 100000000;
  125. }
  126. let maxCount = this.current.totalCount * 1 || 0;
  127. let usedCount = this.form.arrivalBatch.reduce((n, item) => {
  128. n += item.arriveCount * 1;
  129. return n;
  130. }, 0);
  131. return maxCount - usedCount + row.arriveCount * 1;
  132. };
  133. }
  134. },
  135. data() {
  136. return {
  137. visible: false,
  138. title: '设置分批时间',
  139. count: 0,
  140. current: null,
  141. currentIndex: null,
  142. form: {
  143. arrivalBatch: [
  144. {
  145. arriveDate: null,
  146. arriveCount: null
  147. }
  148. ]
  149. },
  150. tableFormRules: {
  151. arriveCount: {
  152. required: true,
  153. message: '请输入数量',
  154. trigger: 'blur'
  155. },
  156. arriveDate: {
  157. required: true,
  158. message: '请选择日期',
  159. trigger: 'change'
  160. },
  161. batch: {
  162. required: true,
  163. message: '请输入批次号',
  164. trigger: 'change'
  165. }
  166. }
  167. };
  168. },
  169. methods: {
  170. open(row, index) {
  171. console.log(row);
  172. this.form.arrivalBatch =
  173. (row.arrivalBatch && copyObj(row.arrivalBatch)) || [];
  174. this.current = { ...row };
  175. this.currentIndex = index;
  176. this.visible = true;
  177. this.setCount();
  178. },
  179. setCount() {
  180. if (this.form.arrivalBatch.length) {
  181. let num = this.form.arrivalBatch.reduce((n, item) => {
  182. n += item.arriveCount * 1;
  183. return n;
  184. }, 0);
  185. this.count = this.current.totalCount * 1 - num || 0;
  186. } else {
  187. this.count = this.current.totalCount * 1 || 0;
  188. }
  189. },
  190. handleChange() {
  191. this.setCount();
  192. },
  193. handleAdd() {
  194. this.form.arrivalBatch.push({
  195. arriveDate: null,
  196. arriveCount: null,
  197. batch: null
  198. });
  199. },
  200. handleDel(index) {
  201. this.count += this.form.arrivalBatch[index].arriveCount;
  202. this.form.arrivalBatch.splice(index, 1);
  203. },
  204. handleOk() {
  205. if (this.count !== 0&&!this.isAll)
  206. return this.$message.warning('分配数量有误,请检查!');
  207. let batchS = this.form.arrivalBatch.map((item) => item.batch);
  208. if (
  209. new Set(batchS).size != this.form.arrivalBatch.length &&
  210. this.isBatch
  211. ) {
  212. return this.$message.warning('批次号不能重复!');
  213. }
  214. this.$refs.tableForm.validate((valid) => {
  215. if (valid) {
  216. this.$emit('chooseTime', {
  217. arrivalBatch: copyObj(this.form.arrivalBatch),
  218. index: this.currentIndex
  219. });
  220. this.handleClose();
  221. }
  222. });
  223. },
  224. handleClose() {
  225. this.$refs.tableForm && this.$refs.tableForm.resetFields();
  226. this.visible = false;
  227. }
  228. }
  229. };
  230. </script>
  231. <style lang="scss" scoped>
  232. .btns {
  233. margin-top: 20px;
  234. text-align: center;
  235. }
  236. .el-form-item {
  237. margin-bottom: 20px !important;
  238. }
  239. </style>