discounts.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <div>
  3. <el-table
  4. :data="tableData"
  5. class="discount-table"
  6. header-row-class-name="customer-row"
  7. row-class-name="customer-row"
  8. v-loading="loading">
  9. <el-table-column
  10. label="Charge Type:"
  11. min-width="120px">
  12. <template slot-scope="{row}">
  13. <el-select
  14. v-model="row.chargeType"
  15. class="add-text"
  16. placeholder="">
  17. <el-option
  18. v-for="item in typeOptions"
  19. :key="item"
  20. :label="item"
  21. :value="item">
  22. </el-option>
  23. </el-select>
  24. </template>
  25. </el-table-column>
  26. <el-table-column
  27. label="Eligible Sites:"
  28. min-width="120px"
  29. align="center">
  30. <template slot-scope="{row}">
  31. <span
  32. class="link-detail underline bold"
  33. @click="showAssignment(row)">{{row.discountSiteLable}}</span>
  34. </template>
  35. </el-table-column>
  36. <el-table-column
  37. label="Discount(%):"
  38. min-width="100px">
  39. <template slot-scope="{row}">
  40. <el-input
  41. v-model="row.discount"
  42. class="add-text"
  43. min="0"
  44. max="100"
  45. type="number"
  46. maxlength="5"/>
  47. </template>
  48. </el-table-column>
  49. <el-table-column
  50. label=""
  51. width="100px">
  52. <template slot-scope="{row, $index}">
  53. <div class="flexc">
  54. <img
  55. class="list-item-icon"
  56. @click="subtItem(row, $index)"
  57. src="../../assets/form-list-sub.png"/>
  58. <img
  59. v-if="tableData.length - 1 === $index"
  60. class="list-item-icon"
  61. @click="addItem"
  62. src="../../assets/form-list-add.png"/>
  63. </div>
  64. </template>
  65. </el-table-column>
  66. </el-table>
  67. <AssignmentSite
  68. :countryCode="countryCode"
  69. :groupPk="group"
  70. :visible="assignment.visible"
  71. :item="assignment.item"
  72. @hide="hideAssignment"/>
  73. </div>
  74. </template>
  75. <script>
  76. import api from '@/http/api/group';
  77. import AssignmentSite from './AssignmentSite.vue';
  78. export default {
  79. name: "DiscountAssignment",
  80. props: {
  81. data: {
  82. type: Array,
  83. default: () => []
  84. },
  85. group: {
  86. type: String | Number,
  87. default: ""
  88. },
  89. countryCode: String
  90. },
  91. data() {
  92. return {
  93. loading: false,
  94. typeOptions: [],
  95. tableData: [],
  96. assignment: {
  97. visible: false,
  98. item: {}
  99. }
  100. };
  101. },
  102. components: {
  103. AssignmentSite
  104. },
  105. mounted() {
  106. this.getTypeOptions();
  107. },
  108. watch: {
  109. group: {
  110. handler(pk, old) {
  111. if (pk) {
  112. this.init();
  113. }
  114. }
  115. },
  116. data: {
  117. deep: true,
  118. handler(n, o) {
  119. this.$nextTick(() => {
  120. if (this.group) {
  121. this.init();
  122. console.log("监听变化");
  123. }
  124. });
  125. }
  126. }
  127. },
  128. methods: {
  129. init() {
  130. this.tableData = this.data;
  131. if (this.tableData) {
  132. if (this.tableData.length == 0) {
  133. this.addItem();
  134. }
  135. } else {
  136. this.tableData = [];
  137. this.addItem();
  138. }
  139. },
  140. onChange() {
  141. this.$emit("change")
  142. },
  143. getTypeOptions() {
  144. api.getChargeTypes().then(res => {
  145. if (res.data) {
  146. this.typeOptions = res.data
  147. }
  148. }).catch(err => {
  149. this.$message({
  150. message: err,
  151. type: 'error'
  152. })
  153. })
  154. },
  155. validDiscount() {
  156. let pattern = /^\d*(\.)?\d{1,2}$/
  157. if (pattern.test(value)) {
  158. const lang = Number(value);
  159. if (lang < 0 || lang > 100) {
  160. return "Discount must to be between 0 and 100";
  161. } else {
  162. return "";
  163. }
  164. } else {
  165. return "Please enter the correct discount";
  166. }
  167. },
  168. showAssignment(row) {
  169. this.assignment.item = row;
  170. this.assignment.visible = true;
  171. },
  172. hideAssignment() {
  173. this.assignment.item = {};
  174. this.assignment.visible = false;
  175. this.onChange();
  176. },
  177. addItem() {
  178. this.loading = true;
  179. api.addDiscountItem({
  180. groupPk: this.group
  181. }).then(res => {
  182. this.loading = false;
  183. if (res.data) {
  184. this.tableData.push(res.data)
  185. }
  186. }).catch(err => {
  187. this.loading = false;
  188. this.$message({
  189. message: err,
  190. type: 'error'
  191. })
  192. })
  193. },
  194. subtItem(item, index) {
  195. this.$confirm('Confirm delete?', 'Delete', {
  196. confirmButtonText: 'Confirm',
  197. cancelButtonText: 'Cancel',
  198. type: 'warning',
  199. }).then(() => {
  200. this.deleteDiscount(item.userGroupDiscountId, index)
  201. })
  202. },
  203. deleteDiscount(id, index) {
  204. if (id) {
  205. this.loading = true;
  206. api.deleteDiscountItem(id).then(res => {
  207. this.loading = false;
  208. this.tableData.splice(index, 1);
  209. }).catch(err => {
  210. this.loading = false;
  211. this.$message({
  212. message: err,
  213. type: 'error'
  214. })
  215. })
  216. } else {
  217. this.tableData.splice(index, 1);
  218. }
  219. }
  220. }
  221. }
  222. </script>
  223. <style scoped lang="scss">
  224. .discount-table {
  225. max-width: 500px;
  226. padding-bottom: 10px;
  227. }
  228. .discount-table:before {
  229. height: 0;
  230. }
  231. .add-text {
  232. width: 100%;
  233. }
  234. ::v-deep .customer-row {
  235. th,td {
  236. background: #fff;
  237. border-bottom: none;
  238. }
  239. .time-picker {
  240. width: 100%;
  241. }
  242. }
  243. .list-item-icon {
  244. height: 30px;
  245. cursor: pointer;
  246. font-size: 26px;
  247. font-weight: 500;
  248. }
  249. .list-item-icon + .list-item-icon {
  250. margin-left: 15px;
  251. }
  252. </style>