<template>
	<view class="report-schedule">
		<!-- header始终显示 -->
		<view class="search-bar">
			<view class="search-container">
				<view class="task-type-button-container">
					<view class="task-type-button" @click="openDrawer">
						<uni-icons type="settings" color="#2979ff" size="22"></uni-icons>
					</view>
				</view>
				<view class="search-input custom-search-input">
					<input v-model="queryParams.name" class="input" type="text" placeholder="搜索客户名称" @confirm="onSearch"
						@keyup.enter="onSearch" />
					<view class="search-icon" @click="onSearch">
						<uni-icons type="search" color="#bbb" size="20"></uni-icons>
					</view>
					<view v-if="searchName" class="clear-icon" @click="onClearSearch">
						<uni-icons type="closeempty" color="#bbb" size="18"></uni-icons>
					</view>
				</view>
				<view class="add-button" @click="handleAdd">
					<uni-icons type="plusempty" color="#2979ff" size="22"></uni-icons>
				</view>
			</view>
		</view>

		<!-- 筛选抽屉 -->
		<uni-drawer ref="drawerRef" mode="left" :width="320">
			<view class="drawer-content">
				<view class="drawer-title">筛选</view>
				<view class="drawer-form">
					<view class="drawer-form-item">
						<text class="drawer-label">客户等级</text>
						<oa-dict-select v-model="queryParams.level" placeholder="客户等级" dictType='customer_level' />
					</view>
					<view class="drawer-form-item">
						<text class="drawer-label">所属行业</text>
						<oa-dict-select v-model="queryParams.industryId" placeholder="所属行业" dictType='industry_type' />
					</view>
					<view class="drawer-form-item">
						<text class="drawer-label">客户来源</text>
						<oa-dict-select v-model="queryParams.source" placeholder="客户来源" dictType='customer_source' />
					</view>
				</view>
				<view class="drawer-btns">
					<button class="drawer-btn-primary" @click="applyFilterAndClose">确定</button>
					<button class="drawer-btn" @click="resetFilterAndClose">重置</button>
					<button class="drawer-btn" @click="closeDrawer">关闭</button>
				</view>
			</view>
		</uni-drawer>

		<view>
			<!-- 自定义列表，右滑菜单（uni-ui实现） -->
			<scroll-view scroll-y style="height: 100vh;" @scrolltolower="loadMore">
				<view v-if="customerList.length">
					<uni-swipe-action>
						<block v-for="(item, index) in customerList" :key="item.customerId">
							<uni-swipe-action-item :right-options="getSwipeOptions(item)" @click="swipeActionClick($event, item)"
								style="margin-bottom: 16rpx;">
								<view class="card">
									<view class="card-title">
										<text class="project">{{ item.name }}</text>
									</view>
									<view class="card-content">
										<view>负责人：{{ item.nickName }}</view>
										<view>手机：{{ item.mobile }}</view>
										<view>详细地址：{{ item.detailAddress }}</view>
									</view>
								</view>
							</uni-swipe-action-item>
						</block>
					</uni-swipe-action>
				</view>
				<view v-else class="empty">暂无数据</view>
				<view class="load-more-tips">
					<u-loading-icon v-if="loadingMore" text="加载中..." size="20" textSize="14" />
					<text v-else-if="!hasMore && customerList.length">没有更多了</text>
				</view>
			</scroll-view>

			<!-- 新增/编辑弹窗 -->
			<uni-popup ref="popupRef" type="bottom">
				<view class="popup-content">
					<view class="uni-form">
						<view class="uni-form-item">
							<text class="uni-form-label">客户名称</text>
							<u-input v-model="form.name" placeholder="请输入客户名称" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">最后跟进时间</text>
							<uni-datetime-picker v-model="form.contactLastTime" type="datetime" placeholder="请选择最后跟进时间"
								style="width:100%" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">最后跟进内容</text>
							<u-input v-model="form.contactLastContent" placeholder="请输入最后跟进内容" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">下次联系时间</text>
							<uni-datetime-picker v-model="form.contactNextTime" type="datetime" placeholder="请选择下次联系时间"
								style="width:100%" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">负责人</text>
							<oa-user-select v-model="form.ownerUserId" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">微信</text>
							<u-input v-model="form.wechat" placeholder="微信" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">邮箱</text>
							<u-input v-model="form.email" placeholder="邮箱" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">详细地址</text>
							<u-input v-model="form.detailAddress" placeholder="详细地址" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">手机</text>
							<u-input v-model="form.mobile" placeholder="手机" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">电话</text>
							<u-input v-model="form.telephone" placeholder="电话" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">QQ</text>
							<u-input v-model="form.qq" placeholder="QQ" />
						</view>

						<view class="uni-form-item">
							<text class="uni-form-label">备注</text>
							<u-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">所属行业</text>
							<oa-dict-select v-model="form.industryId" dictType="industry_type" placeholder="请选择所属行业" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">客户等级</text>
							<oa-dict-select v-model="form.level" dictType="customer_level" placeholder="请选择客户等级" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">客户来源</text>
							<oa-dict-select v-model="form.source" dictType="customer_source" placeholder="请选择客户来源" />
						</view>
					</view>
					<view class="popup-btns">
						<u-button type="primary" @click="submitForm">确定</u-button>
						<u-button @click="closePopup">取消</u-button>
					</view>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import {
		addCustomer,
		delCustomer,
		getCustomer,
		listCustomer,
		updateCustomer
	} from '@/api/oa/customer.js'

	export default {
		data() {
			return {
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					name: undefined,
					followUpStatus: undefined,
					contactLastTime: undefined,
					contactLastContent: undefined,
					contactNextTime: undefined,
					ownerUserId: undefined,
					ownerTime: undefined,
					dealStatus: undefined,
					mobile: undefined,
					telephone: undefined,
					qq: undefined,
					wechat: undefined,
					email: undefined,
					areaId: undefined,
					detailAddress: undefined,
					industryId: undefined,
					level: undefined,
					source: undefined,
				},
				projectOptions: [],
				headerOptions: [],
				customerList: [],
				total: 0,
				single: true,
				multiple: true,
				form: {},
				showStartDate: false,
				showEndDate: false,
				swipeOptions: [{
						text: '编辑',
						style: {
							backgroundColor: '#2979ff',
							color: '#fff'
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#fa3534',
							color: '#fff'
						}
					}
				],
				// 新增：筛选相关
				searchName: '',
				filterProject: '',
				filterHeader: '',
				filterDateRange: [],
				pageNum: 1,
				pageSize: 10,
				loadingMore: false,
				hasMore: true,
				startForm: {
					reportTitle: '',
					reportDate: '',
					reporter: '',
					projectId: '',
					remark: '',
					type: 1
				},
				startLoading: false,
				startPopupOpen: false,
				_startScheduleId: null,
				showGanttView: false,
				ganttChartData: {
					categories: [],
					series: []
				}
			}
		},
		onLoad() {
			this.pageNum = 1
			this.hasMore = true
			this.handleQuery()
		},
		methods: {
			openDrawer() {
				this.$refs.drawerRef.open();
			},
			closeDrawer() {
				this.$refs.drawerRef.close();
			},
			applyFilterAndClose() {
				this.applyFilter();
				this.closeDrawer();
			},
			resetFilterAndClose() {
				this.resetFilter();
				this.closeDrawer();
			},
			// 搜索框检索
			onSearch() {
				this.searchName = this.searchName
				this.handleQuery()
			},
			onClearSearch() {
				this.searchName = ''
				this.queryParams.scheduleName = ''
				this.handleQuery()
			},
			// 筛选抽屉应用
			applyFilter() {
				this.queryParams.projectId = this.filterProject
				this.queryParams.header = this.filterHeader
				if (this.filterDateRange && this.filterDateRange.length === 2) {
					this.queryParams.dateRange = this.filterDateRange
				} else {
					this.queryParams.dateRange = []
				}
				this.handleQuery()
			},
			// 筛选抽屉重置
			resetFilter() {
				this.queryParams = {
					...this.queryParams,
					level: undefined,
					source: undefined,
					industryId: undefined,
				}
			},
			handleQuery() {
				this.pageNum = 1
				this.hasMore = true
				this.queryParams.pageNum = 1
				if (this.queryParams.dateRange && this.queryParams.dateRange.length === 2) {
					this.queryParams.startDate = this.queryParams.dateRange[0]
					this.queryParams.endDate = this.queryParams.dateRange[1]
				} else {
					this.queryParams.startDate = ''
					this.queryParams.endDate = ''
				}
				this.getList()
			},
			getList() {
				this.loadingMore = true
				this.queryParams.pageNum = this.pageNum
				this.queryParams.pageSize = this.pageSize
				listCustomer(this.queryParams).then(res => {
					const rows = res.rows || []
					if (this.pageNum === 1) {
						this.customerList = rows
					} else {
						this.customerList = this.customerList.concat(rows)
					}
					// 判断是否还有更多
					this.hasMore = rows.length === this.pageSize
					this.total = res.total || 0
				}).finally(() => {
					this.loadingMore = false
				})
			},
			loadMore() {
				if (!this.hasMore || this.loadingMore) return
				this.pageNum++
				this.getList()
			},
			handleAdd() {
				this.form = {}
				this.$refs.popupRef.open('bottom')
			},
			handleUpdate(row) {
				getCustomer(row.customerId).then(res => {
					this.form = res.data || {}
					this.$refs.popupRef.open('bottom')
				})
			},
			handleDelete(item) {
				uni.showModal({
					title: '确认删除',
					content: `确定要删除排产“${item.scheduleName}”吗？`,
					success: (res) => {
						if (res.confirm) {
							const ids = item ? [item.customerId] : this.selectedIds
							delCustomer(ids).then(() => {
								uni.showToast({
									title: '删除成功',
									icon: 'success'
								})
								this.getList()
							})
						}
					}
				})
			},
			handleExport() {
				// 导出逻辑可根据实际API实现
				uni.showToast({
					title: '导出功能开发中',
					icon: 'none'
				})
			},
			submitForm() {
				if (this.form.customerId) {
					updateCustomer(this.form).then(() => {
						uni.showToast({
							title: '修改成功',
							icon: 'success'
						})
						this.closePopup()
						this.getList()
					})
				} else {
					addCustomer(this.form).then(() => {
						uni.showToast({
							title: '新增成功',
							icon: 'success'
						})
						this.closePopup()
						this.getList()
					})
				}
			},
			closePopup() {
				this.$refs.popupRef.close()
			},
			getSwipeOptions(item) {
				const options = [{
						text: '编辑',
						style: {
							backgroundColor: '#2979ff',
							color: '#fff'
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#fa3534',
							color: '#fff'
						}
					}
				];
				return options;
			},
			swipeActionClick(e, item) {
				const text = e.content.text;
				if (text === '编辑') {
					this.handleUpdate(item);
				} else if (text === '删除') {
					this.handleDelete(item);
				} else if (text === '开始') {
					this.handleStart(item);
				}
			},
			handleStart(row) {
				const now = new Date();
				const pad = n => n < 10 ? '0' + n : n;
				const formatDate = d =>
					`${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
				this.startForm = {
					reportTitle: row.scheduleName,
					reportDate: formatDate(now),
					reporter: row.header,
					projectId: row.projectId,
					remark: row.remark,
					type: 1
				}
				this._startScheduleId = row.customerId
				this.openStartPopup()
			},
			openStartPopup() {
				this.$refs.startPopupRef.open('bottom')
				this.startPopupOpen = true
			},
			closeStartPopup() {
				this.$refs.startPopupRef.close()
				this.startPopupOpen = false
			},
			async submitStartForm() {
				if (!this.startForm.reportTitle || !this.startForm.reportDate || !this.startForm.reporter || !this.startForm
					.projectId) {
					uni.showToast({
						title: '请填写完整',
						icon: 'none'
					})
					return
				}
				this.startLoading = true
				try {
					await updateCustomer({
						customerId: this._startScheduleId,
						status: 1
					})
					uni.showToast({
						title: '开始成功',
						icon: 'success'
					})
					this.closeStartPopup()
					this.getList()
				} catch (e) {
					uni.showToast({
						title: '操作失败',
						icon: 'none'
					})
				} finally {
					this.startLoading = false
				}
			},
			getProjectName(id) {
				const p = this.projectOptions.find(i => i.value == id)
				return p ? p.text : id
			},
			getHeaderName(id) {
				const h = this.headerOptions.find(i => i.value == id)
				return h ? h.text : id
			},
			getStatusText(status) {
				if (status === 1) return '已开始'
				if (status === 0 || status === undefined) return '未开始'
				return '其他'
			},
			getGanttChartData(list) {
				if (!list || !list.length) {
					return {
						categories: [],
						series: []
					}
				}
				// 只保留唯一 projectId
				const uniqueProjectIds = Array.from(new Set(list.map(item => item.projectId)))
				const categories = uniqueProjectIds.map(id => this.getProjectName(id))

				const data = list.map(item => ({
					...item,
					projectName: this.getProjectName(item.projectId),
					headerName: this.getHeaderName(item.header),
				}))

				return {
					categories,
					series: [{
						name: '进度',
						data
					}]
				}
			},
			toggleGanttView() {
				this.showGanttView = !this.showGanttView;
				if (this.showGanttView) {
					this.ganttChartData = this.getGanttChartData(this.customerList);
				}
			}
		}
	}
</script>

<style scoped>
	.report-schedule {
		min-height: 100vh;
		background-color: #f5f5f5;
		padding-bottom: 120rpx;
	}

	.search-bar {
		padding: 20rpx;
		position: sticky;
		top: 0;
		z-index: 100;
		background: #fff;
	}

	.search-container {
		display: flex;
		align-items: center;
		gap: 20rpx;
	}

	.task-type-button-container {
		display: flex;
		gap: 12rpx;
	}

	.task-type-button {
		width: 60rpx;
		height: 60rpx;
		background-color: transparent;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.search-input {
		flex: 1;
		position: relative;
		display: flex;
		align-items: center;
		background: #f5f5f5;
		border-radius: 100rpx;
		padding: 0 24rpx;
		height: 60rpx;
	}

	.input {
		flex: 1;
		border: none;
		background: transparent;
		font-size: 30rpx;
		outline: none;
		height: 60rpx;
		line-height: 60rpx;
	}

	.search-icon {
		margin-left: 8rpx;
		display: flex;
		align-items: center;
	}

	.clear-icon {
		margin-left: 8rpx;
		display: flex;
		align-items: center;
	}

	.drawer-content {
		padding: 32rpx 24rpx 24rpx 24rpx;
	}

	.drawer-title {
		font-size: 32rpx;
		font-weight: bold;
		margin-bottom: 24rpx;
	}

	.drawer-form {
		margin-bottom: 32rpx;
	}

	.drawer-form-item {
		margin-bottom: 24rpx;
	}

	.drawer-label {
		display: block;
		font-size: 28rpx;
		color: #333;
		margin-bottom: 8rpx;
	}

	.drawer-btns {
		display: flex;
		gap: 16rpx;
		margin-top: 24rpx;
	}

	.drawer-btn-primary {
		flex: 1;
		background: #2979ff;
		color: #fff;
		border: none;
		border-radius: 8rpx;
		padding: 16rpx 0;
		font-size: 28rpx;
	}

	.drawer-btn {
		flex: 1;
		background: #f5f5f5;
		color: #333;
		border: none;
		border-radius: 8rpx;
		padding: 16rpx 0;
		font-size: 28rpx;
	}

	.card {
		background: #fff;
		border-radius: 16rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
		padding: 24rpx;
	}

	.card-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-weight: bold;
		margin-bottom: 12rpx;
	}

	.project {
		color: #2979ff;
	}

	.status {
		font-size: 24rpx;
		padding: 0 12rpx;
		border-radius: 8rpx;
	}

	.status-1 {
		background: #e0f7fa;
		color: #009688;
	}

	.status-0,
	.status-undefined {
		background: #fffbe6;
		color: #faad14;
	}

	.status-other {
		background: #fbeff2;
		color: #e91e63;
	}

	.card-content view {
		margin-bottom: 8rpx;
		color: #666;
		font-size: 26rpx;
	}

	.empty {
		text-align: center;
		color: #bbb;
		margin: 40rpx 0;
	}

	.popup-content {
		padding: 24rpx;
		background: #fff;
		border-radius: 16rpx 16rpx 0 0;
	}

	.uni-form {
		margin-bottom: 32rpx;
		max-height: 50vh;
		overflow-y: scroll;
	}

	.uni-form-item {
		margin-bottom: 32rpx;
	}

	.uni-form-label {
		display: block;
		font-size: 28rpx;
		color: #333;
		margin-bottom: 8rpx;
	}

	.load-more-tips {
		text-align: center;
		color: #bbb;
		padding: 24rpx 0 32rpx 0;
		font-size: 28rpx;
	}

	.card-ops {
		margin-top: 16rpx;
		display: flex;
		gap: 16rpx;
	}

	.gantt-toggle-bar {
		display: flex;
		gap: 16rpx;
		padding: 16rpx 0 8rpx 0;
		background: #fff;
		justify-content: flex-end;
	}
</style>