headerCustom.vue 14.2 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
<template>
  <div class="top-header">
    <!-- Tabs -->
    <div ref="tabsNav"
         class="tabs-nav"
         :class="{ 'tabs-nav--no-border': !showBottomBorder }">
      <div v-for="(tab, index) in tabs"
           :key="getTabKey(tab, index)"
           :ref="`tabItem${index}`"
           class="tab-item"
           :class="[{ active: isTabActive(tab) }]"
           @click="switchTab(tab)">
        {{ getTabLabel(tab) }}
      </div>
      <!-- 选中指示条 - 独立元素,通过动态计算位置实现滑动效果 -->
      <div class="active-line"
           :style="activeLineStyle">
      </div>
    </div>

    <!-- 筛选区 + 按钮 -->
    <div v-if="showRightArea" class="filter-area">
      <div class="filter-group">
        <span class="label">门店名称:</span>
        <el-input v-model="localFilters.storeName"
                  class="filter-select"
                  maxlength="50"
                  placeholder="请输入"
                  size="small"
                  clearable>
        </el-input>
      </div>
      <div class="filter-group">
        <span class="label">项目组:</span>
        <el-select v-model="localFilters.groupId"
                   class="filter-select"
                   placeholder="全部"
                   size="small"
                   clearable
                   filterable>
          <el-option v-for="item in groupOptions"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </div>
      <div class="filter-group">
        <span class="label">是否下发策略:</span>
        <el-select v-model="localFilters.isStrategySent"
                   class="filter-select"
                   placeholder="全部"
                   size="small">
          <el-option v-for="item in strategyIssuedOptions"
                     :key="String(item.value)"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </div>
      <div v-if="false" class="filter-group">
        <span class="label">区域:</span>
        <el-select v-model="localFilters.provinceId"
                   class="filter-select"
                   placeholder="全部"
                   size="small"
                   clearable
                   filterable>
          <el-option v-for="item in provinceOptions"
                     :key="item.code"
                     :label="item.name"
                     :value="item.code">
          </el-option>
        </el-select>
      </div>
      <div v-if="false" class="filter-group">
        <span class="label">品牌:</span>
        <el-cascader v-model="localFilters.brand"
                     class="filter-select-brand"
                     placeholder="全部"
                     size="small"
                     clearable
                     collapse-tags
                     filterable
                     :options="brandOptions"
                     :props="cascaderProps"
                     :show-all-levels="false">
        </el-cascader>
      </div>
      <div v-if="false" class="filter-group">
        <span class="label">区域运营:</span>
        <el-select v-model="localFilters.regionOps"
                   class="filter-select"
                   placeholder="全部"
                   size="small"
                   clearable
                   filterable>
          <el-option v-for="item in regionOpsOptions"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </div>
      <div v-if="false" class="filter-group">
        <span class="label">线上运营:</span>
        <el-select v-model="localFilters.onlineOps"
                   class="filter-select"
                   placeholder="全部"
                   size="small"
                   clearable
                   filterable>
          <el-option v-for="item in onlineOpsOptions"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </div>
      <div class="filter-actions-right">
        <div class="filter-group filter-more" @click="openFiltrateDialog">
          <span class="filter-more-text">更多筛选</span>
          <i class="el-icon-arrow-right filter-more-icon"></i>
        </div>
        <div class="btn-group">
          <el-button class="btn-search"
                     size="small"
                     type="primary"
                     @click="handleSearch">
            查询
          </el-button>
          <el-button class="btn-reset"
                     size="small"
                     @click="handleReset">
            重置
          </el-button>
        </div>
      </div>

      <FiltrateDialog :brand-options="brandOptions"
                      :cascader-props="cascaderProps"
                      :filters="localFilters"
                      :online-ops-options="onlineOpsOptions"
                      :province-options="provinceOptions"
                      :region-ops-options="regionOpsOptions"
                      :supervisor-ops-options="supervisorOpsOptions"
                      :visible.sync="filtrateDialogVisible"
                      @confirm="handleFiltrateConfirm" />
    </div>
  </div>
</template>

<script>
import FiltrateDialog from "./filtrateDialog.vue";

export default {
	name: "HeaderCustom",
	components: {
		FiltrateDialog,
	},
	props: {
		// Tabs 配置
		tabs: {
			type: Array,
			default: () => ["延保", "权益", "轻改"],
		},
		// 当前选中的 Tab
		value: {
			type: [String, Object],
			default: () => ({ label: "延保", value: 0 }),
		},
		// 筛选条件
		filters: {
			type: Object,
			default: () => ({
				storeName: "",
				isStrategySent: "",
				groupId: "",
				region: "",
				provinceId: "",
				brand: [],
				regionOps: "",
				onlineOps: "",
			}),
		},
		// 品牌级联选择器数据
		brandOptions: {
			type: Array,
			default: () => [],
		},
		// 省份选项数据
		provinceOptions: {
			type: Array,
			default: () => [],
		},
		groupOptions: {
			type: Array,
			default: () => [],
		},
		// 城市选项数据
		cityOptions: {
			type: Array,
			default: () => [],
		},
		// 区域运营选项数据
		regionOpsOptions: {
			type: Array,
			default: () => [],
		},
		// 线上运营选项数据
		onlineOpsOptions: {
			type: Array,
			default: () => [],
		},
		// 区域督导选项数据
		supervisorOpsOptions: {
			type: Array,
			default: () => [],
		},
		// 控制筛选区域是否展示
		showRightArea: {
			type: Boolean,
			default: false,
		},
		// 是否显示 tabs 区域下边框,默认显示
		showBottomBorder: {
			type: Boolean,
			default: true,
		},
	},
	data() {
		return {
			filtrateDialogVisible: false,
			currentTab: this.value,
			activeLineStyle: { transform: "translateX(0px)", width: "52px" },
			strategyIssuedOptions: [
				{ label: "全部", value: "" },
				{ label: "是", value: 1 },
				{ label: "否", value: 0 },
			],
			localFilters: {
				...this.filters,
				storeName: this.filters.storeName || "",
				isStrategySent: this.filters.isStrategySent ?? "",
				provinceId: this.filters.provinceId || "",
				groupId: this.filters.groupId || "",
			},
			// 级联选择器配置
			cascaderProps: {
				value: "id",
				label: "label",
				children: "children",
				expandTrigger: "hover",
				emitPath: true,
				checkStrictly: true, // 允许选择任意一级
				multiple: true, // 启用多选功能(Element UI 2.x 需要在 props 中配置)
			},
		};
	},
	watch: {
		// 监听外部传入的 value 变化
		value(newVal) {
			this.currentTab = newVal;
			this.$nextTick(this.updateActiveLinePosition);
		},
		// 监听外部传入的 filters 变化
		filters: {
			handler(newVal) {
				this.localFilters = {
					...newVal,
					storeName: newVal.storeName || "",
					isStrategySent: newVal.isStrategySent ?? "",
					provinceId: newVal.provinceId || "",
					groupId: newVal.groupId || "",
				};
			},
			deep: true,
		},
	},
	mounted() {
		this.$nextTick(this.updateActiveLinePosition);
	},
	methods: {
		switchTab(tab) {
			this.currentTab = tab;
			this.$emit("input", tab);
			this.$emit("tab-change", tab);
			this.$nextTick(this.updateActiveLinePosition);
		},
		updateActiveLinePosition() {
			const currentIndex = this.getCurrentTabIndex();
			const tabItemRef = this.$refs[`tabItem${currentIndex}`];
			if (!tabItemRef?.[0])
				return;

			const { offsetLeft, offsetWidth } = tabItemRef[0];
			this.activeLineStyle = {
				transform: `translateX(${offsetLeft}px)`,
				width: `${offsetWidth}px`,
			};
		},
		// 获取 tab 的 key(用于 v-for)
		getTabKey(tab, index) {
			if (typeof tab === "string") {
				return tab;
			}
			return tab.value !== undefined ? tab.value : index;
		},
		// 获取 tab 的 label(用于显示)
		getTabLabel(tab) {
			if (typeof tab === "string") {
				return tab;
			}
			return tab.label || tab;
		},
		// 判断 tab 是否激活
		isTabActive(tab) {
			if (typeof this.currentTab === "string" && typeof tab === "string") {
				return this.currentTab === tab;
			}
			if (typeof tab === "object" && typeof this.currentTab === "object") {
				// 比较 value 或整个对象
				if (tab.value !== undefined && this.currentTab.value !== undefined) {
					return tab.value === this.currentTab.value;
				}
				// 如果没有 value,比较 label
				if (tab.label !== undefined && this.currentTab.label !== undefined) {
					return tab.label === this.currentTab.label;
				}
				// 最后比较整个对象
				return JSON.stringify(tab) === JSON.stringify(this.currentTab);
			}
			// 混合类型比较:字符串和对象
			if (typeof tab === "object" && typeof this.currentTab === "string") {
				return tab.label === this.currentTab;
			}
			if (typeof tab === "string" && typeof this.currentTab === "object") {
				return tab === this.currentTab.label;
			}
			return false;
		},
		// 获取当前 tab 的索引
		getCurrentTabIndex() {
			return this.tabs.findIndex(tab => this.isTabActive(tab));
		},
		openFiltrateDialog() {
			this.filtrateDialogVisible = true;
		},
		handleFiltrateConfirm(dialogFilters) {
			this.localFilters = {
				...this.localFilters,
				groupId: dialogFilters.groupId ?? this.localFilters.groupId,
				region: dialogFilters.region ?? this.localFilters.region,
				provinceId: dialogFilters.provinceId ?? null,
				brand: Array.isArray(dialogFilters.brand)
					? dialogFilters.brand.map(item => (Array.isArray(item) ? [...item] : item))
					: [],
				regionOps: dialogFilters.regionOps ?? null,
				onlineOps: dialogFilters.onlineOps ?? null,
				supervisorOps: dialogFilters.supervisorOps ?? null,
			};
		},
		handleSearch() {
			this.$emit("search", { ...this.localFilters });
		},
		handleReset() {
			const resetFilters = {
				storeName: "",
				isStrategySent: "",
				region: "",
				provinceId: "",
				brand: [],
				regionOps: "",
				onlineOps: "",
				groupId: "",
			};
			this.localFilters = { ...resetFilters };
			this.$emit("reset", resetFilters);
			this.$emit("search", resetFilters);
		},
	},
};
</script>

<style lang="scss" scoped>
// 颜色变量
$primary: #2a58ad;
$text-main: #1d2129;
$text-sub: #333333;
$text-sub-2: #666666;
$border: #e5e6eb;
$bg-red: #fef0f0;
$text-red: #f53f3f;
$bg-orange: #fdf6ec;
$text-orange: #ff7d00;

// ================= Top Header =================
.top-header {
	background: #fff;
	display: flex;
	flex-direction: column;
	align-items: stretch;
	border-radius: 4px;
	margin-bottom: 1px;
	position: sticky;
	top: 0;
	z-index: 100;
	// 确保 sticky 定位正常工作
	flex-shrink: 0;
}

.tabs-nav {
	position: relative;
	display: flex;
	flex-shrink: 0;
	border-bottom: 1px solid #e5e5e5;
	padding: 17px 24px;
	.tab-item {
		height: 34px;
		width: 54px;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-right: 32px;
		font-size: 16px;
		color: $text-sub;
		cursor: pointer;
		transition: all 0.2s;
		&:hover {
			color: $primary;
		}
		&.active {
			color: $primary;
			font-weight: 500;
			font-size: 16px;
		}
	}

	&--no-border {
		border-bottom: none;
	}
}

.active-line {
	position: absolute;
	bottom: 0;
	left: 0;
	height: 3px;
	background: $primary;
	border-radius: 2px 2px 0 0;
	transition:
		transform 0.3s cubic-bezier(0.645, 0.045, 0.355, 1),
		width 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.filter-area {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	justify-content: flex-start;
	width: 100%;
	min-width: 0;
	box-sizing: border-box;
	padding: 16px 24px;
	gap: 8px 0;
	.filter-group {
		display: flex;
		align-items: center;
		margin-right: 24px;
		max-width: 100%;
		flex-shrink: 1;
		min-width: 0;

		.label {
			font-size: 14px;
			color: $text-sub;
			margin-right: 8px;
			white-space: nowrap;
			flex-shrink: 0;
		}
		.filter-select {
			width: 300px;
			max-width: 100%;
			flex-shrink: 1;
			min-width: 0;
		}
		.filter-select-brand {
			min-width: 120px;
			width: auto;
		}
	}

	.filter-actions-right {
		display: flex;
		align-items: center;
		gap: 12px;
		margin-left: auto;
		flex-shrink: 0;

		.filter-more {
			width: 88px;
			height: 34px;
			margin-right: 0;
			border: 1px solid #e5e5e5;
			color: $text-sub-2;
			font-size: 14px;
			justify-content: center;
			cursor: pointer;
			flex-shrink: 0;
			box-sizing: border-box;
			border-radius: 4px;

			.filter-more-text {
				line-height: 1;
			}

			.filter-more-icon {
				margin-left: 2px;
				font-size: 12px;
				color: $text-sub-2;
			}
		}
	}

	.btn-group {
		display: flex;
		gap: 12px;
	}
	.btn-search {
		background: $primary;
		border-color: $primary;
		border-radius: 4px;
		&:hover {
			opacity: 0.9;
		}
	}
	.btn-reset {
		background: #f2f3f5;
		border-color: #f2f3f5;
		color: $text-sub;
		&:hover {
			background: #e5e6eb;
		}
		border-radius: 4px;
	}
}

// 覆盖 Element UI 样式
::v-deep {
	.el-input__inner {
		border-radius: 2px;
		border-color: $border;
		color: $text-main;
		&:hover {
			border-color: #c9cdd4;
		}
		&:focus {
			border-color: $primary;
		}
	}

	.el-button--small {
		border-radius: 2px;
	}

	// 确保级联选择器的 input 宽度自适应
	.filter-select-brand {
		.el-input {
			width: 100%;
		}
		.el-input__inner {
			width: 100%;
			min-width: 120px;
		}
		.el-cascader__tags {
			flex-wrap: nowrap;
		}
	}
}
</style>