negotiation-summary.js 4.1 KB
const negotiationApi = require('../../api/negotiationDailyReport.js')
const { isApiSuccess } = require('../../utils/api-response.js')
import { buildNegotiationSummaryTables } from '../../form-engine/negotiation-summary-transform.js'

Component({
	properties: {
		reportDate: {
			type: String,
			value: ''
		},
		merchantId: {
			type: String,
			value: ''
		},
		sceneType: {
			type: null,
			value: 0
		}
	},

	data: {
		loading: false,
		detailLoading: false,
		loadComplete: false,
		loadError: '',
		staffList: [],
		currentIndex: 0,
		sections: [],
		sectionNavIndex: [],
		isFullEmptyView: false
	},

	observers: {
		'reportDate, merchantId'(reportDate, merchantId) {
			if (reportDate && merchantId) {
				this.loadStaffList()
			}
		},
		'loadComplete, loading, loadError, staffList'() {
			this.syncFullEmptyView()
		}
	},

	lifetimes: {
		attached() {
			const { reportDate, merchantId } = this.properties
			if (reportDate && merchantId) {
				this.loadStaffList()
			}
		}
	},

	methods: {
		/** 无员工空态:整页居中,需 --empty 绝对定位布局 */
		syncFullEmptyView() {
			const { loadComplete, loading, loadError, staffList } = this.data
			const isFullEmptyView =
				loadComplete && !loading && !loadError && !staffList.length
			if (this.data.isFullEmptyView !== isFullEmptyView) {
				this.setData({ isFullEmptyView })
			}
		},

		reload() {
			this.loadStaffList()
		},

		onBack() {
			this.triggerEvent('close')
		},

		onStaffTabTap(e) {
			const index = Number(e.currentTarget.dataset.index)
			if (Number.isNaN(index) || index === this.data.currentIndex) return
			this.setData({ currentIndex: index })
			this.loadStaffDetail(this.data.staffList[index])
		},

		onSectionNav(e) {
			const sectionIndex = Number(e.currentTarget.dataset.sectionIndex)
			const direction = Number(e.detail?.direction)
			if (Number.isNaN(sectionIndex) || !direction) return
			const sections = this.data.sections || []
			const section = sections[sectionIndex]
			const rowTotal = section?.recordRows?.length || 0
			if (!rowTotal) return
			const nav = [...(this.data.sectionNavIndex || [])]
			const current = nav[sectionIndex] ?? 0
			const next = current + direction
			if (next < 0 || next >= rowTotal) return
			nav[sectionIndex] = next
			this.setData({ sectionNavIndex: nav })
		},

		async loadStaffList() {
			const { reportDate, merchantId, sceneType } = this.properties
			if (!reportDate || !merchantId) return
			this.setData({
				loading: true,
				loadComplete: false,
				loadError: '',
				staffList: [],
				sections: [],
				sectionNavIndex: [],
				currentIndex: 0
			})
			try {
				const res = await negotiationApi.fetchDiscussionStaffList({
					merchantId,
					reportDate,
					sceneType
				})
				if (!isApiSuccess(res.code)) {
					this.setData({
						loadError: res.message || '加载失败',
						loadComplete: true
					})
					return
				}
				const staffList = res.data || []
				this.setData({
					staffList,
					loadComplete: true
				})
				if (staffList.length) {
					await this.loadStaffDetail(staffList[0])
				}
			} catch (error) {
				console.error(error)
				this.setData({
					loadError: '加载失败',
					loadComplete: true
				})
			} finally {
				this.setData({ loading: false })
			}
		},

		async loadStaffDetail(staff = {}) {
			const { reportDate, merchantId, sceneType } = this.properties
			if (!staff.staffUuid || !reportDate || !merchantId) {
				this.setData({ sections: [], sectionNavIndex: [] })
				return
			}
			this.setData({ detailLoading: true })
			try {
				const res = await negotiationApi.fetchReportDetail({
					merchantId,
					staffUuid: staff.staffUuid,
					reportDate,
					sceneType
				})
				if (!isApiSuccess(res.code) || !res.data) {
					this.setData({ sections: [], sectionNavIndex: [] })
					return
				}
				const jsonContent = res.data.jsonContent || ''
				const sections = buildNegotiationSummaryTables(jsonContent)
				this.setData({
					sections,
					sectionNavIndex: sections.map(() => 0)
				})
			} catch (error) {
				console.error(error)
				this.setData({ sections: [], sectionNavIndex: [] })
			} finally {
				this.setData({ detailLoading: false })
			}
		}
	}
})