today-plan-customer-popup.js 5.69 KB
/**
 * 洽谈明细 — 今日计划到店客户多选弹层
 * @file 底部弹层:日期切换、多选列表、确认导入
 */
const negotiationApi = require('../../api/negotiationDailyReport.js')
const {
	formatPlanStoreDateLabel,
	shiftPlanStoreDate,
	canShiftPlanStoreDate,
	formatCustomerCarSeriesLabel,
	maskCustomerPhone
} = require('../../services/today-plan-date.js')
var timeUtil = require('~/utils/timeFormat/time-utils.js')
const {
	buildWeappIconUrl,
	TODAY_PLAN_CHECK_ICON
} = require('../../utils/upgrade-icon-url.js')

Component({
	properties: {
		/** 父页控制显隐;true 时打开弹层并重置为 anchorDate */
		show: {
			type: Boolean,
			value: false,
			observer(n) {
				if (n) {
					this.openPopup()
				}
			}
		},
		/** 周历选中日(弹层日期锚点,默认今天) */
		anchorDate: {
			type: String,
			value: ''
		},
		/** 4S 店商户号 */
		merchantId: {
			type: String,
			value: ''
		},
		/** 驻店零售员工 id(staffUuid) */
		advanceUserId: {
			type: String,
			value: ''
		},
		staffName: {
			type: String,
			value: ''
		}
	},

	data: {
		visible: false,
		currentDate: '',
		dateLabel: '',
		canPrev: false,
		canNext: false,
		customerList: [],
		selectedCount: 0,
		isAllSelected: false,
		loading: false,
		loadComplete: false,
		isFullSucreen: false,
		checkIconUrl: ''
	},

	methods: {
		noScroll() {},

		/** 解析弹层日期锚点(周历选中日,缺省为今天) */
		resolveAnchorDate() {
			return this.properties.anchorDate || timeUtil.getTodayDateStr()
		},

		/** 打开弹层并重置为锚点日 */
		openPopup() {
			const anchorDate = this.resolveAnchorDate()
			this.setData({
				visible: true,
				currentDate: anchorDate,
				customerList: [],
				selectedCount: 0,
				isAllSelected: false,
				loadComplete: false
			})
			this.syncDateNavState(anchorDate)
			this.loadCustomerList(anchorDate)
		},

		/** 同步日期导航按钮与文案 */
		syncDateNavState(dateStr) {
			const anchorDate = this.resolveAnchorDate()
			this.setData({
				dateLabel: formatPlanStoreDateLabel(dateStr, anchorDate),
				canPrev: canShiftPlanStoreDate(dateStr, -1, anchorDate),
				canNext: canShiftPlanStoreDate(dateStr, 1, anchorDate)
			})
		},

		/** 拉取当前日期客户列表 */
		async loadCustomerList(inviteDate) {
			const { merchantId, advanceUserId } = this.properties
			if (!merchantId || !advanceUserId || !inviteDate) {
				this.setData({ customerList: [], loadComplete: true, loading: false })
				return
			}
			this.setData({ loading: true, loadComplete: false })
			try {
				const res = await negotiationApi.fetchTodayPlanStoreList({
					merchantId,
					advanceUserId,
					inviteDate
				})
				const list = (res.code === 0 ? res.data : []) || []
				const customerList = list.map((item, index) => ({
					...item,
					_rowKey: `${inviteDate}_${index}`,
					selected: false,
					carSeriesLabel: formatCustomerCarSeriesLabel(item),
					phoneMasked: maskCustomerPhone(item.customerPhone)
				}))
				this.setData({
					customerList,
					selectedCount: 0,
					isAllSelected: false,
					loadComplete: true
				})
			} catch (error) {
				console.error(error)
				wx.$util?.showToast?.('加载客户列表失败')
				this.setData({ customerList: [], loadComplete: true })
			} finally {
				this.setData({ loading: false })
			}
		},

		onMaskTap() {
			this.closePopup()
		},

		onCancel() {
			this.closePopup()
		},

		/** 关闭弹层并通知父页 */
		closePopup() {
			this.setData({ visible: false })
			this.triggerEvent('cancel')
		},

		onPrevDay() {
			const anchorDate = this.resolveAnchorDate()
			const next = shiftPlanStoreDate(this.data.currentDate, -1, anchorDate)
			if (!next) return
			this.changeDate(next)
		},

		onNextDay() {
			const anchorDate = this.resolveAnchorDate()
			const next = shiftPlanStoreDate(this.data.currentDate, 1, anchorDate)
			if (!next) return
			this.changeDate(next)
		},

		/** 切换日期:清空勾选并重新拉列表 */
		changeDate(dateStr) {
			this.setData({
				currentDate: dateStr,
				customerList: [],
				selectedCount: 0,
				isAllSelected: false
			})
			this.syncDateNavState(dateStr)
			this.loadCustomerList(dateStr)
		},

		/** 切换单条客户勾选 */
		onToggleItem(e) {
			const index = e.currentTarget.dataset.index
			const list = [...this.data.customerList]
			if (!list[index]) return
			list[index] = { ...list[index], selected: !list[index].selected }
			this.updateSelectionState(list)
		},

		/** 全选 / 取消全选 */
		onToggleAll() {
			const { customerList, isAllSelected } = this.data
			if (!customerList.length) return
			const nextAll = !isAllSelected
			const list = customerList.map(item => ({ ...item, selected: nextAll }))
			this.updateSelectionState(list)
		},

		/** 同步 selectedCount、isAllSelected 到 data */
		updateSelectionState(customerList) {
			const selectedCount = customerList.filter(item => item.selected).length
			const isAllSelected =
				customerList.length > 0 && selectedCount === customerList.length
			this.setData({ customerList, selectedCount, isAllSelected })
		},

		/**
		 * 确认导入:向父页派发 confirm,剥离 UI 衍生字段
		 * @fires confirm detail.customers 原始客户对象数组
		 */
		onConfirm() {
			const selected = this.data.customerList.filter(item => item.selected)
			if (!selected.length) return
			const payload = selected.map(({ selected: _s, _rowKey, carSeriesLabel, phoneMasked, ...rest }) => rest)
			this.setData({ visible: false })
			this.triggerEvent('confirm', { customers: payload })
		}
	},

	/** 生命周期:安全区与勾选 CDN 图标(根目录 check.png) */
	attached() {
		this.setData({
			isFullSucreen: wx.$util?.isFullSucreen?.(true) || false,
			checkIconUrl: buildWeappIconUrl(TODAY_PLAN_CHECK_ICON)
		})
	}
})