modal.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <script setup lang="ts">
  2. import { ref, shallowRef } from "vue"
  3. import { useRenderIcon } from "/@/components/ReIcon/src/hooks"
  4. import { httpAdd } from "/@/api/serviceParam/supplierCat";
  5. import { SupplierQuery } from "/@/components/BasicForm"
  6. import { rules } from "./config/_options"
  7. import { useVModel } from "@vueuse/core"
  8. import { useTask } from "/@/hooks/core"
  9. import { ElForm, ElMessage } from "element-plus"
  10. import CategoryModal from "./category-modal.vue"
  11. const initialData = ():Record<string, any> => ({
  12. supplierNo: '',
  13. cat_id: []
  14. })
  15. const emit = defineEmits(['refresh'])
  16. const props = defineProps<{ visible: boolean; id: string, type: string }>()
  17. const formData = ref(initialData())
  18. const formRef = ref<InstanceType<typeof ElForm> | null>(null)
  19. const visible = useVModel(props, 'visible')
  20. const categoryVisible = shallowRef(false)
  21. const supportCreateTask = useTask({ success })
  22. function success(){
  23. visible.value = false
  24. emit('refresh')
  25. }
  26. function handleCategoryChange(values: Record<string, any>[]){
  27. const ids = formData.value.cat_id.map(({id}) => id)
  28. for(const value of values){
  29. if(ids.includes(value.id)){ continue }
  30. formData.value.cat_id.push({ ...value })
  31. }
  32. }
  33. async function onSubmit(){
  34. try {
  35. console.log(1)
  36. await formRef.value.validate()
  37. const { supplierNo, cat_id } = formData.value
  38. if(cat_id.length === 0){
  39. ElMessage.warning('至少添加一个分类')
  40. return
  41. }
  42. supportCreateTask
  43. .run(httpAdd({
  44. supplierNo,
  45. cat_id: cat_id.map(({id}) => id)
  46. }))
  47. }catch(err){
  48. console.log(err)
  49. }
  50. }
  51. </script>
  52. <template>
  53. <ElDialog v-model="visible" title="新建供应商支持分类" center>
  54. <ElForm
  55. label-width="100px"
  56. :rules="rules"
  57. :model="formData"
  58. :disabled="type === 'preview'"
  59. ref="formRef"
  60. >
  61. <ElFormItem label="供应商" prop="supplierNo">
  62. <SupplierQuery v-model="formData.supplierNo" placeholder="供应商" />
  63. </ElFormItem>
  64. <ElFormItem label="商品分类">
  65. <ElTable border size="small" :data="formData.cat_id">
  66. <ElTableColumn label="序号" width="80px" type="index" show-overflow-tooltip />
  67. <ElTableColumn label="分类名称" prop="search" show-overflow-tooltip />
  68. <ElTableColumn label="操作" width="80px" show-overflow-tooltip>
  69. <template #header>
  70. <div class="flex justify-between">
  71. <span>操作</span>
  72. <ElButton
  73. link
  74. type="primary"
  75. size="small"
  76. :icon="useRenderIcon('add')"
  77. @click="categoryVisible = true"
  78. />
  79. </div>
  80. </template>
  81. <template #="{ $index }">
  82. <ElButton
  83. link
  84. type="danger"
  85. size="small"
  86. :icon="useRenderIcon('delete')"
  87. @click="formData.cat_id.splice($index, 1)"></ElButton>
  88. </template>
  89. </ElTableColumn>
  90. </ElTable>
  91. </ElFormItem>
  92. <ElFormItem>
  93. <div class="w-full flex justify-end">
  94. <ElButton type="primary" :loading="supportCreateTask.loading" @click="onSubmit">提交</ElButton>
  95. </div>
  96. </ElFormItem>
  97. </ElForm>
  98. <CategoryModal
  99. v-model:visible="categoryVisible"
  100. @change="handleCategoryChange"
  101. />
  102. </ElDialog>
  103. </template>