ServiceInfo.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <template>
  2. <div class="service-info">
  3. <div class="main-content">
  4. <div class="service-title">
  5. <img class="label-icon" src="@/assets/img/home/label-icon.png" alt="">
  6. 服务信息
  7. </div>
  8. <div class="service-content">
  9. <div class="service-item">
  10. <div class="sub-title">服务作物</div>
  11. <div class="tag-group" v-if="!isEdit">
  12. <div class="tag-item" :class="{ selected: item.selected && isEdit }" @click="handleSelect('crops', idx, $event)" v-for="(item, idx) in crops" :key="'c-'+idx">{{ item.name }}</div>
  13. </div>
  14. <div class="tag-group add-tag-group" v-else>
  15. <div class="tag-item" :class="{ self: item.isSelf === 1, selected: item.selected && isEdit }" @click="handleSelect('crops', idx, $event)" v-for="(item, idx) in crops" :key="'ce-'+idx">
  16. <span class="text">{{ item.name }}<el-icon @click.stop="handleEdit(item.name, 'crops')" v-if="item.isSelf===1" class="edit-icon"><Edit /></el-icon></span>
  17. <el-icon v-if="item.isSelf===1" class="del-icon" @click.stop="handleDelete('crops', idx)"><Close /></el-icon>
  18. </div>
  19. <div class="tag-item last-add" @click="handleAdd('作物')"><el-icon class="add-icon"><Plus /></el-icon>作物</div>
  20. </div>
  21. </div>
  22. <div class="service-item">
  23. <div class="sub-title">服务类型</div>
  24. <div class="tag-group" v-if="!isEdit">
  25. <div class="tag-item" :class="{ selected: item.selected && isEdit }" @click="handleSelect('serviceTypes', idx, $event)" v-for="(item, idx) in serviceTypes" :key="'t-'+idx">{{ item.name }}</div>
  26. </div>
  27. <div class="tag-group add-tag-group" v-else>
  28. <div class="tag-item" :class="{ self: item.isSelf === 1, selected: item.selected && isEdit }" @click="handleSelect('serviceTypes', idx, $event)" v-for="(item, idx) in serviceTypes" :key="'te-'+idx">
  29. <span class="text">{{ item.name }}<el-icon @click.stop="handleEdit(item.name, 'serviceTypes')" v-if="item.isSelf===1" class="edit-icon"><Edit /></el-icon></span>
  30. <el-icon v-if="item.isSelf===1" class="del-icon" @click.stop="handleDelete('serviceTypes', idx)"><Close /></el-icon>
  31. </div>
  32. <div class="tag-item last-add" @click="handleAdd('类型')"><el-icon class="add-icon"><Plus /></el-icon>类型</div>
  33. </div>
  34. </div>
  35. <div class="service-item">
  36. <div class="sub-title">农机设备</div>
  37. <div class="tag-group" v-if="!isEdit">
  38. <div class="tag-item" :class="{ selected: item.selected && isEdit }" @click="handleSelect('machines', idx, $event)" v-for="(item, idx) in machines" :key="'m-'+idx">{{ item.name }}</div>
  39. </div>
  40. <div class="tag-group add-tag-group" v-else>
  41. <div class="tag-item" :class="{ self: item.isSelf === 1, selected: item.selected && isEdit }" @click="handleSelect('machines', idx, $event)" v-for="(item, idx) in machines" :key="'me-'+idx">
  42. <span class="text">{{ item.name }}<el-icon @click.stop="handleEdit(item.name, 'machines')" v-if="item.isSelf===1" class="edit-icon"><Edit /></el-icon></span>
  43. <el-icon v-if="item.isSelf===1" class="del-icon" @click.stop="handleDelete('machines', idx)"><Close /></el-icon>
  44. </div>
  45. <div class="tag-item last-add" @click="handleAdd('设备')"><el-icon class="add-icon"><Plus /></el-icon>设备</div>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. <popup class="add-tag-popup" round v-model:show="showAddPopup">
  51. <div class="popup-title" v-if="isEditPopup">编辑标签</div>
  52. <div class="popup-title" v-else>添加{{addTypeName}}<span class="ml-2">标签</span></div>
  53. <el-input class="popup-input" v-model="input" placeholder="标签" size="large" />
  54. <div class="popup-button">
  55. <div class="cancel" @click="showAddPopup = false">取消</div>
  56. <div @click="handleConfirm">{{isEditPopup ? '确定' : '添加'}}</div>
  57. </div>
  58. </popup>
  59. </div>
  60. </template>
  61. <script setup>
  62. import { ref } from "vue";
  63. import { Popup } from "vant";
  64. import { Edit, Close, Plus } from '@element-plus/icons-vue'
  65. const props = defineProps({
  66. crops: {
  67. type: Array,
  68. default: () => []
  69. },
  70. serviceTypes: {
  71. type: Array,
  72. default: () => []
  73. },
  74. machines: {
  75. type: Array,
  76. default: () => []
  77. },
  78. isEdit: {
  79. type: Boolean,
  80. default: false
  81. }
  82. });
  83. const emit = defineEmits(['update:crops', 'update:serviceTypes', 'update:machines', 'update:isEdit']);
  84. const showAddPopup = ref(false);
  85. const input = ref("");
  86. const addTypeName = ref("");
  87. const isEditPopup = ref(false);
  88. const currentCategory = ref('');
  89. const currentEditIndex = ref(-1);
  90. const currentEditName = ref('');
  91. function handleAdd(type) {
  92. isEditPopup.value = false;
  93. addTypeName.value = type;
  94. input.value = "";
  95. showAddPopup.value = true;
  96. // 根据类型设置当前分类
  97. if (type === '作物') {
  98. currentCategory.value = 'crops';
  99. } else if (type === '类型') {
  100. currentCategory.value = 'serviceTypes';
  101. } else if (type === '设备') {
  102. currentCategory.value = 'machines';
  103. }
  104. currentEditIndex.value = -1;
  105. }
  106. function handleDelete(category, index) {
  107. if (category === 'crops') {
  108. const newCrops = [...props.crops];
  109. newCrops.splice(index, 1);
  110. emit('update:crops', newCrops);
  111. }
  112. if (category === 'serviceTypes') {
  113. const newServiceTypes = [...props.serviceTypes];
  114. newServiceTypes.splice(index, 1);
  115. emit('update:serviceTypes', newServiceTypes);
  116. }
  117. if (category === 'machines') {
  118. const newMachines = [...props.machines];
  119. newMachines.splice(index, 1);
  120. emit('update:machines', newMachines);
  121. }
  122. }
  123. function handleEdit(val, category) {
  124. isEditPopup.value = true;
  125. input.value = val;
  126. showAddPopup.value = true;
  127. currentCategory.value = category;
  128. currentEditName.value = val;
  129. // 找到对应的索引
  130. let targetArray = [];
  131. if (category === 'crops') {
  132. targetArray = props.crops;
  133. } else if (category === 'serviceTypes') {
  134. targetArray = props.serviceTypes;
  135. } else if (category === 'machines') {
  136. targetArray = props.machines;
  137. }
  138. currentEditIndex.value = targetArray.findIndex(item => item.name === val && item.isSelf === 1);
  139. }
  140. function handleSelect(category, index, event) {
  141. // 如果点击的是编辑图标或删除图标,不触发选中
  142. if (event && event.target) {
  143. const target = event.target;
  144. if (target.closest && (target.closest('.edit-icon') || target.closest('.del-icon'))) {
  145. return;
  146. }
  147. }
  148. if (category === 'crops') {
  149. const newCrops = [...props.crops];
  150. if (newCrops[index]) {
  151. if (newCrops[index].selected === undefined) {
  152. newCrops[index].selected = false;
  153. }
  154. newCrops[index].selected = !newCrops[index].selected;
  155. emit('update:crops', newCrops);
  156. }
  157. } else if (category === 'serviceTypes') {
  158. const newServiceTypes = [...props.serviceTypes];
  159. if (newServiceTypes[index]) {
  160. if (newServiceTypes[index].selected === undefined) {
  161. newServiceTypes[index].selected = false;
  162. }
  163. newServiceTypes[index].selected = !newServiceTypes[index].selected;
  164. emit('update:serviceTypes', newServiceTypes);
  165. }
  166. } else if (category === 'machines') {
  167. const newMachines = [...props.machines];
  168. if (newMachines[index]) {
  169. if (newMachines[index].selected === undefined) {
  170. newMachines[index].selected = false;
  171. }
  172. newMachines[index].selected = !newMachines[index].selected;
  173. emit('update:machines', newMachines);
  174. }
  175. }
  176. }
  177. function handleConfirm() {
  178. if (!input.value.trim()) {
  179. return;
  180. }
  181. if (isEditPopup.value && currentEditIndex.value >= 0) {
  182. // 编辑模式
  183. if (currentCategory.value === 'crops') {
  184. const newCrops = [...props.crops];
  185. newCrops[currentEditIndex.value].name = input.value.trim();
  186. emit('update:crops', newCrops);
  187. } else if (currentCategory.value === 'serviceTypes') {
  188. const newServiceTypes = [...props.serviceTypes];
  189. newServiceTypes[currentEditIndex.value].name = input.value.trim();
  190. emit('update:serviceTypes', newServiceTypes);
  191. } else if (currentCategory.value === 'machines') {
  192. const newMachines = [...props.machines];
  193. newMachines[currentEditIndex.value].name = input.value.trim();
  194. emit('update:machines', newMachines);
  195. }
  196. } else {
  197. // 添加模式
  198. const newItem = { name: input.value.trim(), isSelf: 1, selected: false };
  199. if (currentCategory.value === 'crops') {
  200. const newCrops = [...props.crops, newItem];
  201. emit('update:crops', newCrops);
  202. } else if (currentCategory.value === 'serviceTypes') {
  203. const newServiceTypes = [...props.serviceTypes, newItem];
  204. emit('update:serviceTypes', newServiceTypes);
  205. } else if (currentCategory.value === 'machines') {
  206. const newMachines = [...props.machines, newItem];
  207. emit('update:machines', newMachines);
  208. }
  209. }
  210. showAddPopup.value = false;
  211. input.value = "";
  212. currentEditIndex.value = -1;
  213. currentEditName.value = '';
  214. }
  215. function getServiceInfo() {
  216. // 获取所有选中的项或自己新加的项的名称
  217. const selectedCrops = props.crops.filter(item => item.selected || item.isSelf === 1).map(item => item.name);
  218. const selectedServiceTypes = props.serviceTypes.filter(item => item.selected || item.isSelf === 1).map(item => item.name);
  219. const selectedMachines = props.machines.filter(item => item.selected || item.isSelf === 1).map(item => item.name);
  220. // 返回所有选中项和自己新加的项
  221. return {serviceCropsJson: selectedCrops, serviceTypeJson: selectedServiceTypes, agriculturalEquipmentJson: selectedMachines}
  222. }
  223. defineExpose({
  224. getServiceInfo
  225. });
  226. </script>
  227. <style lang="scss" scoped>
  228. .service-info {
  229. .main-content {
  230. max-height: calc(100% - 40px);
  231. overflow: auto;
  232. padding: 15px 12px;
  233. margin: 14px 12px;
  234. border-radius: 12px;
  235. background: #fff;
  236. .service-title {
  237. font-size: 18px;
  238. color: #222222;
  239. font-weight: 500;
  240. margin-bottom: 10px;
  241. .label-icon {
  242. width: 14px;
  243. padding-right: 5px;
  244. }
  245. }
  246. .service-content {
  247. margin-top: 12px;
  248. padding-top: 12px;
  249. border-top: 1px solid #F5F5F5;
  250. .service-item {
  251. .sub-title {
  252. font-size: 16px;
  253. font-weight: 500;
  254. color: rgba(0, 0, 0, 0.9);
  255. }
  256. .tag-group {
  257. display: flex;
  258. align-items: center;
  259. flex-wrap: wrap;
  260. gap: 0 12px;
  261. font-size: 16px;
  262. .tag-item {
  263. margin-top: 10px;
  264. position: relative;
  265. background: #E8F5FF;
  266. border-radius: 8px;
  267. color: #2199f8;
  268. padding: 0 12px;
  269. box-sizing: border-box;
  270. min-width: 26vw;
  271. height: 48px;
  272. text-align: center;
  273. line-height: 48px;
  274. cursor: pointer;
  275. transition: all 0.3s;
  276. .text { display: inline-flex; align-items: center; }
  277. .edit-icon { margin-left: 8px; }
  278. .del-icon {
  279. position: absolute;
  280. right: -8px;
  281. top: -8px;
  282. background: #2199F8;
  283. border-radius: 50%;
  284. width: 16px; height: 16px;
  285. font-size: 10px;
  286. display: flex; align-items: center; justify-content: center;
  287. color: #fff;
  288. }
  289. &.selected {
  290. border: 1px solid #2199F8;
  291. background: #E8F5FF;
  292. color: #2199F8;
  293. }
  294. }
  295. &.add-tag-group {
  296. .tag-item {
  297. color: #000000;
  298. background: none;
  299. border: 1px solid #999999;
  300. cursor: pointer;
  301. &.self {
  302. border: 1px solid #2199F8;
  303. background: #E8F5FF;
  304. color: #2199F8;
  305. }
  306. &.selected {
  307. border: 1px solid #2199F8;
  308. background: #E8F5FF;
  309. color: #2199F8;
  310. }
  311. &.last-add {
  312. background: #F7F7F7;
  313. color: #343434;
  314. border: none;
  315. display: flex;
  316. align-items: center;
  317. justify-content: center;
  318. cursor: pointer;
  319. .add-icon {
  320. font-size: 14px;
  321. font-weight: bold;
  322. margin-right: 3px;
  323. }
  324. }
  325. }
  326. }
  327. }
  328. }
  329. .service-item + .service-item {
  330. margin-top: 20px;
  331. }
  332. }
  333. }
  334. }
  335. .add-tag-popup{
  336. width: 90%;
  337. padding: 20px 16px;
  338. .popup-title{
  339. font-size: 18px;
  340. font-weight: 500;
  341. text-align: center;
  342. margin-bottom: 12px;
  343. }
  344. .ml-2 {
  345. margin-left: 3px;
  346. }
  347. .popup-input{
  348. margin-bottom: 30px;
  349. }
  350. .popup-button{
  351. display: flex;
  352. padding-top: 20px;
  353. border-top: 1px solid rgba(0, 0, 0, 0.1);
  354. div{
  355. flex: 1;
  356. font-size: 16px;
  357. padding: 9px;
  358. border-radius: 20px;
  359. background: #2199F8;
  360. color: #fff;
  361. text-align: center;
  362. cursor: pointer;
  363. }
  364. .cancel{
  365. margin-right: 20px;
  366. color: #000;
  367. background: #fff;
  368. border: 1px solid #999999;
  369. }
  370. }
  371. }
  372. </style>