mattersHandle.js 22.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
import api from "~/api/api";
import { getMatterDetailFields, normalizeMatterItem } from "~/utils/matterItem";

Page({
	data: {
		navBarHeight: 0,
		task: null,
		reply: "",
		detailFields: [],
		submitting: false,
		isReadonly: false,
		loading: true,
		// 任务执行结果相关
		showTaskExecutionResult: false,
		executionResultCode: "",
		executionResultCodeOptions: [],
		// 人员更换评估特有
		showReplaceOptions: false,
		replaceSelectedOption: "",
		replaceOptions: [
			{ label: "发起招聘", value: "CREATE_RECRUITMENT_DEMAND" },
			{ label: "关联已有招聘", value: "LINK_EXISTING_RECRUITMENT_DEMAND" },
			{ label: "关联另一个驻店", value: "LINK_REPLACEMENT_STAFF" },
		],
		// 关联招聘需求列表
		recruitDemandOptions: [],
		selectedRecruitDemand: "",
		// 关联驻店人员列表
		staffOptions: [],
		selectedStaff: "",
		// 驻店人员分页
		staffCurrentPage: 1,
		staffPageSize: 20,
		staffTotal: 0,
		staffHasMore: true,
		staffLoading: false,
		staffSearchKeyword: "",
		// 当前选中的员工
		currentSelectedStaff: null,
		// 显示文本
		selectedRecruitDemandText: "",
		selectedStaffText: "",
		// picker索引
		selectedRecruitDemandIndex: 0,
		selectedStaffIndex: 0,
		// 发起招聘弹窗
		showRecruitPopup: false,
		// 关联已有招聘弹窗
		showLinkRecruitPopup: false,
		// 关联另一个驻店弹窗
		showLinkStaffPopup: false,
		// 当前选中的招聘详情(发起招聘或关联已有招聘)
		selectedRecruitDetail: null,
		// 当前选中的驻店人员详情
		selectedStaffDetail: null,
		// select-popup 绑定的数组值
		selectedRecruitDemandValues: [],
		selectedStaffValues: [],
		// 本次弹窗打开对应的目标选项(未确认时暂存,弹窗确认后覆盖replaceSelectedOption并清空)
		pendingReplaceOption: "",
	},

	onLoad(options) {
		const id = options?.id;
		if (!id) {
			wx.showToast({ title: "缺少任务信息", icon: "none" });
			setTimeout(() => wx.navigateBack(), 1500);
			return;
		}
		this.taskId = id;
		this.loadDetail();
	},

	async loadDetail() {
		this.setData({ loading: true });
		wx.showLoading({ title: "加载中", mask: true });
		try {
			const res = await api.getTaskCenterDetail({ taskId: this.taskId });
			if (res?.code !== "0" && res?.code !== 0) {
				wx.showToast({
					title: res?.message || "加载失败",
					icon: "none",
				});
				setTimeout(() => wx.navigateBack(), 1500);
				return;
			}
			const task = normalizeMatterItem(res.data || {}, 0);
			// 根据 taskType 设置任务执行结果选项
			const executionResultCodeOptions = this.getTaskExecutionResultOptions(task.taskType);
			const showTaskExecutionResult = this.shouldShowTaskExecutionResultByType(task.taskType);

			// 构建任务执行结果回显文本
			const executionResultCodeText = this.getTaskExecutionResultText(task.executionResultCode, task.taskType);

			// 编辑状态回显:如果有任务执行结果,恢复选项状态
			const executionResultCode = task.executionResultCode || "";
			const showReplaceOptions =
				showTaskExecutionResult && executionResultCode === "replace" && (task.taskType === "staff-replace-access" || task.taskType === "staff_replace_access" || task.taskType === "7");

			const selectedRecruitDemandText = this.buildRelatedRecruitDemandText(task);
			const selectedStaffText = task.relatedStaffName || task.staffName || "";

			// 初始回显replaceSelectedOption:优先从已有数据反推(有招聘文本=招聘类,有驻店=关联驻店),没有则再用task.replaceOption兜底
			let replaceSelectedOption = "";
			if (selectedStaffText) {
				replaceSelectedOption = "LINK_REPLACEMENT_STAFF";
			} else if (selectedRecruitDemandText) {
				// 发起招聘 vs 关联已有招聘:有recruitId且task里能区分时用replaceOption,否则默认关联已有
				const savedReplaceOption = task.replaceOption || task.executionResultCode || "";
				if (savedReplaceOption === "CREATE_RECRUITMENT_DEMAND" || savedReplaceOption === "LINK_EXISTING_RECRUITMENT_DEMAND") {
					replaceSelectedOption = savedReplaceOption;
				} else {
					replaceSelectedOption = "LINK_EXISTING_RECRUITMENT_DEMAND";
				}
			} else {
				replaceSelectedOption = task.replaceOption || "";
			}

			this.setData({
				task: {
					...task,
					executionResultCodeText,
					relatedRecruitDemandText: selectedRecruitDemandText,
					recruitManagerName: task.recruitManagerName || task.recruitChargeName || "",
					relatedStaffName: task.relatedStaffName || task.staffName || "",
				},
				detailFields: getMatterDetailFields(task.taskType),
				isReadonly: task.completed || task.overdue,
				reply: task.replyText || "",
				executionResultCode: executionResultCode,
				showReplaceOptions,
				replaceSelectedOption,
				selectedRecruitDemand: task.recruitId || "",
				selectedRecruitDemandValues: task.recruitId ? [task.recruitId] : [],
				selectedRecruitDemandText,
				selectedStaff: task.staffUuid || "",
				selectedStaffValues: task.staffUuid ? [task.staffUuid] : [],
				selectedStaffText,
				executionResultCodeOptions,
				showTaskExecutionResult,
				pendingReplaceOption: "",
				loading: false,
			});
		} catch (e) {
			console.error("加载详情失败", e);
			wx.showToast({
				title: "加载失败,请稍后重试",
				icon: "none",
			});
			setTimeout(() => wx.navigateBack(), 1500);
		} finally {
			wx.hideLoading();
			this.setData({ loading: false });
		}
	},

	/**
	 * 根据任务类型获取任务执行结果选项
	 */
	getTaskExecutionResultOptions(taskType) {
		switch (taskType) {
			case "multi-scene-expand":
				return [
					{ label: "不可抗拒原因无法落地多场景", value: "UNABLE_TO_IMPLEMENT" },
					{ label: "已合作", value: "COOPERATED" },
				];
			case "new-store-traffic":
				return [
					{ label: "有意向洽谈中", value: "NEGOTIATING" },
					{ label: "已开新店", value: "STORE_OPENED" },
					{ label: "无法开新店", value: "UNABLE_TO_OPEN" },
				];
			case "staff-replace-access":
				return [
					{ label: "更换", value: "replace" },
					{ label: "不更换", value: "NO_REPLACE" },
				];
			default:
				return [];
		}
	},

	/**
	 * 判断是否需要显示任务执行结果字段
	 */
	shouldShowTaskExecutionResultByType(taskType) {
		return ["multi-scene-expand", "new-store-traffic", "staff-replace-access"].includes(taskType);
	},

	/**
	 * 获取任务执行结果显示文本
	 */
	getTaskExecutionResultText(executionResultCode, taskType, replaceOption) {
		if (!executionResultCode) return "-";

		// 人员更换评估特殊处理
		if (taskType === "staff-replace-access") {
			if (executionResultCode === "NO_REPLACE") {
				return "不更换";
			} else {
				const optionMap = {
					CREATE_RECRUITMENT_DEMAND: "发起招聘",
					LINK_EXISTING_RECRUITMENT_DEMAND: "关联已有招聘",
					LINK_REPLACEMENT_STAFF: "关联另一个驻店",
				};
				return optionMap[executionResultCode];
			}
		}

		// 其他任务类型从选项中匹配文本
		const options = this.getTaskExecutionResultOptions(taskType);
		const matched = options.find((item) => item.value === executionResultCode);
		return matched ? matched.label : executionResultCode;
	},

	/**
	 * 构建已关联招聘需求显示文本
	 */
	buildRelatedRecruitDemandText(task) {
		if (task.executionResultCode === "LINK_REPLACEMENT_STAFF") return "";
		// 优先使用后端返回的招聘需求文本
		if (task?.staffReplaceResult?.recruitmentDemandName) {
			return task.staffReplaceResult.recruitmentDemandName;
		}

		return "";
	},

	/**
	 * 判断是否需要显示任务执行结果字段
	 */
	shouldShowTaskExecutionResult() {
		const { task } = this.data;
		if (!task) return false;

		const taskType = task.taskType;
		return ["multi-scene-expand", "new-store-traffic", "staff-replace-access"].includes(taskType);
	},

	navbarReady(e) {
		this.setData({ navBarHeight: e.detail.height });
	},

	onBack() {
		if (getCurrentPages()?.length > 1) {
			wx.navigateBack();
		} else {
			wx.switchTab({url: '/pages/tab/matters/matters'})
		}
	},

	onReplyInput(e) {
		this.setData({ reply: e.detail.value || "" });
	},

	/**
	 * 任务执行结果变更
	 */
	handleRadio(e) {
		const value = e.detail;
		this.setData({ executionResultCode: value });

		// 人员更换评估特殊处理
		const taskType = this.data.task?.taskType;
		if (taskType === "staff-replace-access" || taskType === "staff_replace_access" || taskType === "7") {
			if (value === "replace") {
				this.setData({ showReplaceOptions: true });
				this.loadReplaceOptions();
			} else {
				this.setData({
					showReplaceOptions: false,
					replaceSelectedOption: "",
					recruitDemandList: [],
					staffList: [],
					selectedRecruitDemand: "",
					selectedRecruitDemandValues: [],
					selectedStaff: "",
					selectedStaffValues: [],
					selectedRecruitDemandText: "",
					selectedStaffText: "",
					selectedRecruitDemandIndex: 0,
					selectedStaffIndex: 0,
					selectedRecruitDetail: null,
					selectedStaffDetail: null,
				});
			}
		}
	},

	/**
	 * 任务执行结果变更(兼容方法)
	 */
	onTaskExecutionResultChange(e) {
		this.handleRadio(e);
	},

	/**
	 * 加载人员更换选项相关数据
	 */
	async loadReplaceOptions() {
		// TODO: 加载招聘需求列表和驻店人员列表
		// 这里需要调用接口获取数据
	},

	/**
	 * 人员更换选项变更(vant radio)
	 */
	onReplaceOptionChange(e) {
		const value = e.detail;
		this.handleReplaceOptionChange(value);
	},

	/**
	 * 人员更换选项点击(标签按钮)
	 */
	onReplaceOptionTap(e) {
		const { value } = e.currentTarget.dataset;
		this.handleReplaceOptionChange(value);
	},

	/**
	 * 处理人员更换选项变更
	 */
	handleReplaceOptionChange(value) {
		const { replaceSelectedOption } = this.data;

		if (replaceSelectedOption === value) {
			return;
		}

		this.setData({ pendingReplaceOption: value }, () => {
			this.openReplaceEditPopupByValue(value);
		});
	},

	/**
	 * 按值打开对应编辑弹窗
	 */
	openReplaceEditPopupByValue(value) {
		if (value === "CREATE_RECRUITMENT_DEMAND") {
			this.openRecruitPopup();
		} else if (value === "LINK_EXISTING_RECRUITMENT_DEMAND") {
			this.openLinkRecruitPopup();
		} else if (value === "LINK_REPLACEMENT_STAFF") {
			this.openLinkStaffPopup();
		}
	},

	/**
	 * 打开关联已有招聘弹窗
	 */
	openLinkRecruitPopup() {
		// 打开弹窗时,把当前已选的招聘需求塞到select-popup选中项里,保证默认高亮回显
		const selectedRecruitDemandValues = this.data.selectedRecruitDemand ? [this.data.selectedRecruitDemand] : [];
		this.setData({ showLinkRecruitPopup: true, selectedRecruitDemandValues }, () => {
			this.loadRecruitDemandList();
		});
	},

	/**
	 * 关闭关联已有招聘弹窗(未点确认 → 保留上次已确认的选择)
	 */
	closeLinkRecruitPopup() {
		this.setData({ showLinkRecruitPopup: false, pendingReplaceOption: "" });
	},

	/**
	 * 打开关联另一个驻店弹窗
	 */
	openLinkStaffPopup() {
		const { selectedStaff, selectedStaffDetail } = this.data;
		// 编辑已有选择的情况:先用已选员工作为弹窗内的临时选中项
		const presetStaff = selectedStaff && selectedStaffDetail ? selectedStaffDetail : null;
		this.setData(
			{
				showLinkStaffPopup: true,
				staffCurrentPage: 1,
				staffHasMore: true,
				staffSearchKeyword: "",
				staffOptions: [],
				currentSelectedStaff: presetStaff,
			},
			() => {
				this.loadStaffList(false, presetStaff);
			},
		);
	},

	/**
	 * 关闭关联另一个驻店弹窗(未点确认 → 保留上次已确认的选择)
	 */
	closeLinkStaffPopup() {
		this.setData({ showLinkStaffPopup: false, currentSelectedStaff: null, pendingReplaceOption: "" });
	},

	/**
	 * 编辑已选的人员更换选项(右侧「编辑/新增」按钮)
	 */
	onEditReplaceOption() {
		const { replaceSelectedOption } = this.data;
		if (!replaceSelectedOption) return;
		this.openReplaceEditPopupByValue(replaceSelectedOption);
	},

	/**
	 * 打开发起招聘弹窗
	 */
	openRecruitPopup() {
		this.setData({ showRecruitPopup: true });
	},

	/**
	 * 关闭发起招聘弹窗(未点确认 → 保留上次已确认的选择)
	 */
	closeRecruitPopup() {
		this.setData({ showRecruitPopup: false, pendingReplaceOption: "" });
	},

	/**
	 * 招聘发起成功回调
	 */
	onRecruitSuccess(e) {
		const detail = e.detail?.data || {};
		const { task } = this.data;
		const recruitId = detail.recruitId || detail.id || "";

		const businessTypeName = detail.businessTypeName || task?.businessName || "";
		const recruitTypeName = detail.recruitTypeName || this.data.replaceOptions.find((item) => item.value === "CREATE_RECRUITMENT_DEMAND")?.label || "人员更换评估";
		const createTime = detail.createTime || this.formatDateTime(new Date());

		this.setData({
			showRecruitPopup: false,
			replaceSelectedOption: "CREATE_RECRUITMENT_DEMAND",
			pendingReplaceOption: "",
			selectedRecruitDemand: recruitId,
			selectedRecruitDemandValues: recruitId ? [recruitId] : [],
			selectedRecruitDetail: {
				...detail,
				businessTypeName,
				recruitTypeName,
				createTime,
				displayText: `${businessTypeName}/${recruitTypeName}/${createTime}`,
			},
			selectedRecruitDemandText: `${businessTypeName}/${recruitTypeName}/${createTime}`,
			// 切换到招聘类,清空驻店相关数据,避免下方内容不匹配
			selectedStaff: "",
			selectedStaffValues: [],
			selectedStaffText: "",
			selectedStaffDetail: null,
		});
	},

	/**
	 * 加载招聘需求列表
	 */
	async loadRecruitDemandList() {
		const { task } = this.data;
		wx.showLoading({ title: "加载中", mask: true });
		try {
			const params = {
				pageNum: 1,
				pageSize: 9999,
				// 可根据任务归属门店/场景过滤
				merchantId: task?.storeId || "",
				businessType: task?.businessType || "",
			};
			const res = await api.getRecruitmentPageAPI(params);
			if (res?.code === "0" || res?.code === 0) {
				const list = res.data?.records || res.data?.list || res.data || [];
				const recruitDemandOptions = list.map((item) => ({
					...item,
					recruitId: item.recruitId || item.id || "",
					displayText: `${item.businessTypeName || "-"}/${item.recruitmentTypeName || "-"}/${item.createdDate || "-"}`,
				}));
				this.setData({ recruitDemandOptions });
			} else {
				wx.showToast({ title: res?.message || "加载失败", icon: "none" });
			}
		} catch (e) {
			console.error("加载招聘需求列表失败", e);
			wx.showToast({ title: "加载失败", icon: "none" });
		} finally {
			wx.hideLoading();
		}
	},

	/**
	 * 加载驻店人员列表
	 */
	async loadStaffList(isLoadMore = false, presetStaff = null) {
		const { staffLoading, staffHasMore, staffCurrentPage, staffPageSize, staffSearchKeyword, staffOptions } = this.data;

		if (staffLoading || (!isLoadMore && !staffHasMore)) {
			return;
		}
		if (isLoadMore && !staffHasMore) {
			return;
		}

		this.setData({ staffLoading: true });
		try {
			const currentPage = isLoadMore ? staffCurrentPage + 1 : 1;
			const params = {
				currentPage,
				pageSize: staffPageSize,
				staffName: staffSearchKeyword || undefined,
			};

			const res = await api.getAllStaffListAPI(params);

			if (res?.code === "0" || res?.code === 0) {
				const newList = res.data?.records || res.data?.list || res.data || [];
				const total = res.data?.total || 0;

				const finalList = isLoadMore ? [...staffOptions, ...newList] : newList;
				const hasMore = finalList.length < total;

				// 编辑态:如果传了presetStaff但当前列表内还没找到对应uuid的高亮,则遍历找到第一个匹配的员工
				let nextSelectedStaff = this.data.currentSelectedStaff || null;
				if (presetStaff && presetStaff.staffUuid) {
					const found = finalList.find((s) => (s.staffUuid || s.id) === (presetStaff.staffUuid || presetStaff.id));
					if (found) nextSelectedStaff = found;
				}

				this.setData({
					staffOptions: finalList,
					staffCurrentPage: currentPage,
					staffTotal: total,
					staffHasMore: hasMore,
					staffLoading: false,
					currentSelectedStaff: nextSelectedStaff,
				});
			} else {
				wx.showToast({ title: res?.message || "加载失败", icon: "none" });
				this.setData({ staffLoading: false });
			}
		} catch (error) {
			console.error("加载驻店人员列表失败", error);
			wx.showToast({ title: "加载失败", icon: "none" });
			this.setData({ staffLoading: false });
		}
	},

	/**
	 * 驻店人员搜索输入
	 */
	onStaffSearchInput(e) {
		const keyword = e.detail?.value || "";
		this.setData({
			staffSearchKeyword: keyword,
		});
	},

	/**
	 * 驻店人员搜索确认
	 */
	onStaffSearchConfirm() {
		this.setData(
			{
				staffCurrentPage: 1,
				staffHasMore: true,
			},
			() => {
				this.loadStaffList(false);
			},
		);
	},

	/**
	 * 驻店人员搜索清空
	 */
	onStaffSearchClear() {
		this.setData(
			{
				staffSearchKeyword: "",
				staffCurrentPage: 1,
				staffHasMore: true,
			},
			() => {
				this.loadStaffList(false);
			},
		);
	},

	/**
	 * 驻店人员列表滚动到底部
	 */
	onStaffScrollToLower() {
		this.loadStaffList(true);
	},

	/**
	 * 选择驻店人员项
	 */
	onStaffItemTap(e) {
		const { item } = e.currentTarget.dataset;
		if (!item) return;
		this.setData({
			currentSelectedStaff: item,
		});
	},

	/**
	 * 确认选择驻店人员
	 */
	onStaffConfirm() {
		const { currentSelectedStaff } = this.data;
		if (!currentSelectedStaff) {
			wx.showToast({ title: "请选择员工", icon: "none" });
			return;
		}

		const staffUuid = currentSelectedStaff.staffUuid || currentSelectedStaff.id || "";
		this.setData({
			showLinkStaffPopup: false,
			replaceSelectedOption: "LINK_REPLACEMENT_STAFF",
			pendingReplaceOption: "",
			selectedStaff: staffUuid,
			selectedStaffValues: staffUuid ? [staffUuid] : [],
			selectedStaffText: currentSelectedStaff.staffName || "",
			selectedStaffDetail: currentSelectedStaff,
			currentSelectedStaff: null,
			// 切换到驻店,清空招聘类相关数据,避免下方内容不匹配
			selectedRecruitDemand: "",
			selectedRecruitDemandValues: [],
			selectedRecruitDemandText: "",
			selectedRecruitDetail: null,
		});
	},

	/**
	 * 关联已有招聘确认
	 */
	onRecruitDemandConfirm(e) {
		const selected = (e.detail?.items || [])[0];
		if (!selected) {
			wx.showToast({ title: "请选择招聘需求", icon: "none" });
			return;
		}

		const recruitId = selected.recruitId || selected.id || "";
		this.setData({
			showLinkRecruitPopup: false,
			replaceSelectedOption: "LINK_EXISTING_RECRUITMENT_DEMAND",
			pendingReplaceOption: "",
			selectedRecruitDemand: recruitId,
			selectedRecruitDemandValues: recruitId ? [recruitId] : [],
			selectedRecruitDemandText: selected.displayText || "",
			selectedRecruitDetail: {
				...selected,
				displayText: selected.displayText,
			},
			// 切换到招聘类,清空驻店相关数据,避免下方内容不匹配
			selectedStaff: "",
			selectedStaffValues: [],
			selectedStaffText: "",
			selectedStaffDetail: null,
		});
	},

	/**
	 * 格式化日期时间
	 */
	formatDateTime(date) {
		if (!date) return "";
		const d = new Date(date);
		const year = d.getFullYear();
		const month = String(d.getMonth() + 1).padStart(2, "0");
		const day = String(d.getDate()).padStart(2, "0");
		const hour = String(d.getHours()).padStart(2, "0");
		const minute = String(d.getMinutes()).padStart(2, "0");
		return `${year}-${month}-${day} ${hour}:${minute}`;
	},

	async submit() {
		const { reply, task, submitting, executionResultCode } = this.data;
		if (submitting || !task) return;

		const content = (reply || "").trim();
		if (!content) {
			wx.showToast({ title: "请输入专项任务回复", icon: "none" });
			return;
		}

		if (content.length > 800) {
			wx.showToast({ title: "回复内容不能超过800字", icon: "none" });
			return;
		}

		// 验证任务执行结果(如果需要显示)
		if (this.shouldShowTaskExecutionResult()) {
			if (!executionResultCode) {
				wx.showToast({ title: "请选择任务执行结果", icon: "none" });
				return;
			}

			// 人员更换评估特殊验证
			if (task.taskType === "staff-replace-access" || task.taskType === "staff_replace_access" || task.taskType === "7") {
				if (executionResultCode === "replace") {
					if (!this.data.replaceSelectedOption) {
						wx.showToast({ title: "请选择更换方式", icon: "none" });
						return;
					}

					// 根据选项校验是否已选择具体内容
					if (this.data.replaceSelectedOption === "CREATE_RECRUITMENT_DEMAND" || this.data.replaceSelectedOption === "LINK_EXISTING_RECRUITMENT_DEMAND") {
						if (!this.data.selectedRecruitDemand) {
							wx.showToast({ title: "请选择招聘需求", icon: "none" });
							return;
						}
					} else if (this.data.replaceSelectedOption === "LINK_REPLACEMENT_STAFF") {
						if (!this.data.selectedStaff) {
							wx.showToast({ title: "请选择驻店人员", icon: "none" });
							return;
						}
					}
				}
			}
		}

		this.setData({ submitting: true });
		wx.showLoading({ title: "提交中", mask: true });
		try {
			// 构建提交参数
			const params = {
				id: task.id,
				replyContent: content,
			};

			// 添加任务执行结果
			if (executionResultCode) {
				// 人员更换评估选择"更换"后,传具体子选项 code
				if (task.taskType === "staff-replace-access" || task.taskType === "staff_replace_access" || task.taskType === "7") {
					if (executionResultCode === "replace") {
						params.executionResultCode = this.data.replaceSelectedOption;
						params.recruitmentDemandId = this.data.selectedRecruitDemand || "";
						params.replacementStaffUuid = this.data.selectedStaff || "";
					} else {
						params.executionResultCode = executionResultCode;
					}
				} else {
					params.executionResultCode = executionResultCode;
				}
			}
			const res = await api.submitTaskCenter(params);
			if (res?.code === "0" || res?.code === 0) {
				this.onSubmitSuccess();
			} else {
				wx.showToast({
					title: res?.message || res?.msg || "提交失败",
					icon: "none",
				});
			}
		} catch (e) {
			wx.showToast({ title: "提交失败", icon: "none" });
		} finally {
			wx.hideLoading();
			this.setData({ submitting: false });
		}
	},

	onSubmitSuccess() {
		wx.hideLoading();
		this.setData({ submitting: false });
		wx.showToast({ title: "提交成功", icon: "success" });
		this.notifyRefresh();

		setTimeout(() => {
			if (getCurrentPages()?.length > 1) {
				wx.navigateBack();
			} else {
				wx.switchTab({url: '/pages/tab/matters/matters'})
			}
		}, 1000);
	},

	notifyRefresh() {
		const eventChannel = this.getOpenerEventChannel();
		eventChannel?.emit?.("refreshList");
	},
});