topics-edit.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <simple-form-modal ref="modalRef" :items="formItems" :baseUrl="baseUrl" @success="handleSuccess" :title="title"
  3. width="600px" labelWidth="100px">
  4. <template #relatedItems="{ model }">
  5. <div class="flex items-center space-x-4">
  6. <el-checkbox v-model="isSpecified" label="指定商品" />
  7. <div v-if="isSpecified" class="flex items-center text-gray-600">
  8. <span>已选择 <span class="text-red-500 font-bold">{{ (model.relatedItems || []).length }}</span> 款商品,</span>
  9. <el-button link type="primary" @click="openGoodsDialog(model)">选择商品 ></el-button>
  10. </div>
  11. </div>
  12. </template>
  13. <template #style="{ model }">
  14. <div class="flex flex-col">
  15. <image-upload v-model="model.style" :limit="1" :fileSize="2" fileType="jpg" />
  16. <span class="text-gray-400 text-xs mt-1">只支持.jpg 格式</span>
  17. </div>
  18. </template>
  19. <template #status="{ model }">
  20. <el-switch v-model="model.status" active-value="online" inactive-value="offline" />
  21. </template>
  22. </simple-form-modal>
  23. <goods-select-dialog v-model="goodsDialogVisible" :default-selected="selectedGoods" @confirm="handleGoodsConfirm" />
  24. </template>
  25. <script setup>
  26. import { ref, computed } from 'vue';
  27. import SimpleFormModal from '@/components/CommonPage/SimpleFormModal.vue';
  28. import GoodsSelectDialog from '../../components/GoodsSelectDialog.vue';
  29. import ImageUpload from '@/components/ImageUpload/index.vue';
  30. const emit = defineEmits(['success']);
  31. const modalRef = ref(null);
  32. const title = ref('添加专题');
  33. const goodsDialogVisible = ref(false);
  34. const selectedGoods = ref([]);
  35. const isSpecified = ref(true); // Default checked as per image
  36. const currentFormModel = ref(null);
  37. const baseUrl = {
  38. add: '/salesOps/topics/add',
  39. update: '/salesOps/topics/update'
  40. };
  41. const formItems = computed(() => [
  42. {
  43. label: '专题名称',
  44. prop: 'name',
  45. type: 'input',
  46. required: true,
  47. props: { placeholder: '请输入专题名称' }
  48. },
  49. {
  50. label: '关联商品',
  51. prop: 'relatedItems',
  52. type: 'relatedItems',
  53. slot: 'relatedItems',
  54. required: true,
  55. },
  56. {
  57. label: '是否上线',
  58. prop: 'status',
  59. type: 'status',
  60. slot: 'status',
  61. required: true,
  62. },
  63. {
  64. label: '专题样式',
  65. prop: 'style',
  66. type: 'style',
  67. slot: 'style',
  68. required: true,
  69. }
  70. ]);
  71. const handleOpen = (data) => {
  72. title.value = data && data.id ? '编辑专题' : '添加专题';
  73. // Initialize selected goods from data if available
  74. selectedGoods.value = data && data.relatedGoods ? data.relatedGoods : [];
  75. // Mocking 10 selected items if it's a new or existing one for demo
  76. if (selectedGoods.value.length === 0 && data) {
  77. // selectedGoods.value = ...
  78. }
  79. modalRef.value.handleOpen(data);
  80. };
  81. const handleSuccess = (data) => {
  82. emit('success', data);
  83. };
  84. const openGoodsDialog = (model) => {
  85. currentFormModel.value = model;
  86. // ensure relatedItems is initialized
  87. if (!model.relatedItems) {
  88. model.relatedItems = [];
  89. }
  90. // Use the model's data for the dialog
  91. selectedGoods.value = model.relatedItems;
  92. goodsDialogVisible.value = true;
  93. };
  94. const handleGoodsConfirm = (rows) => {
  95. selectedGoods.value = rows;
  96. if (currentFormModel.value) {
  97. currentFormModel.value.relatedItems = rows;
  98. }
  99. goodsDialogVisible.value = false;
  100. };
  101. defineExpose({
  102. handleOpen
  103. });
  104. </script>