GroupChatDialog.vue 4.04 KB
<template>
	<d2-dialog
		title="选择企微群聊"
		:is-visible.sync="visible"
		width="1080px"
		@closeDialog="handleDialogClose"
		@confirmDialog="confirmGroupSelection"
	>
		<div>
			<d2-search
				ref="showSearch"
				class="show-search"
				:search-form="searchForm"
				:label-width="'80px'"
				:form-data="showFormData"
			/>
			<div>
				<el-button size="small" type="primary" @click="handleSearch">
					查询
				</el-button>
			</div>
		</div>
		<d2-table
			ref="groupTable"
			v-loading="loading"
			:table-data="groupChatList"
			:table-params="tableParams"
			:has-page="false"
			:has-num="false"
			:selectable-fn="checkSelectable"
			max-height="400px"
			@selectionTable="handleSelectionChange"
		/>
	</d2-dialog>
</template>

<script>
import { getGroupChatList } from "@/api/usedCarManagement";

export default {
	name: "GroupChatDialog",
	props: {
		selectedData: {
			type: Array,
			default: () => [],
		},
		editData: {
			type: Object,
			default: () => {},
		},
	},
	data() {
		return {
			visible: true,
			loading: false,
			groupChatList: [],
			selectedGroups: [],
			searchForm: {
				groupChatName: "",
			},
			searchFormSearch: {},
			showFormData: [
				{
					label: "群名称",
					name: "groupChatName",
					type: "input",
					maxlength: 100,
					placeholder: "请输入群名称",
				},
			],
			tableParams: {
				mutiSelectUniqueKey: "groupChatId",
				mutiSelectName: "groupChat",
				tableOption: false,
				tableColumns: [
					{ label: "群名称", prop: "groupChatName", align: "center" },
					{
						label: "群状态",
						prop: "free",
						align: "center",
						formatter: (cellValue) => {
							return cellValue.free ? "空闲" : "占用";
						},
						className: (cellValue) => {
							return cellValue.free ? "greenSpot" : "redSpot";
						},
					},
				],
			},
		};
	},
	created() {
		this.searchFormSearch = { ...this.searchForm };
		this.selectedGroups = [...this.selectedData];
		this.fetchGroupList();
	},
	methods: {
		handleSearch() {
			this.searchFormSearch = { ...this.searchForm };
			this.selectedGroups = this.getMutiDate();
			this.fetchGroupList();
		},
		fetchGroupList() {
			this.loading = true;
			const params = { ...this.searchFormSearch };
			if (this.editData?.id) {
				params.id = this.editData.id;
			}
			getGroupChatList(params)
				.then((res) => {
					this.groupChatList = res;
					this.recoverySelection(res);
				})
				.finally(() => {
					this.loading = false;
				});
		},
		recoverySelection(list) {
			this.$nextTick(() => {
				if (this.selectedGroups.length) {
					this.selectedGroups.forEach((row) => {
						const obj = this.groupChatList.find(
							(item) => item.groupChatId == row.groupChatId
						);
						obj &&
							this.$refs.groupTable.$refs[
								this.tableParams.mutiSelectName + "MutiTable"
							].toggleRowSelection(obj, true);
					});
				}
			});
		},

		getMutiDate() {
			let selectedData =
				this.$refs.groupTable.mutiSelectArr[this.tableParams.mutiSelectName] ||
				[];
			let deleteIds = [];
			this.selectedGroups.forEach((item) => {
				if (
					!this.groupChatList
						.map((k) => k.groupChatId)
						.includes(item.groupChatId) &&
					!selectedData.map((k) => k.groupChatId).includes(item.groupChatId)
				) {
					selectedData.push(item);
				}
				if (
					selectedData?.map((k) => k.groupChatId).includes(item.groupChatId) &&
					!selectedData?.map((k) => k.groupChatId).includes(item.groupChatId)
				) {
					deleteIds.push(item.groupChatId);
				}
			});
			deleteIds.forEach((item) => {
				const idx = selectedData.findIndex((k) => k.groupChatId == item.groupChatId);
				if (idx !== -1) {
					selectedData.splice(idx, 1);
				}
			});
			return selectedData;
		},
		handleSelectionChange(val) {},
		confirmGroupSelection() {
			this.selectedGroups = this.getMutiDate();
			this.$emit("confirm", this.selectedGroups);
			this.$emit("close");
		},
		handleDialogClose() {
			this.$emit("close");
		},
		checkSelectable(row) {
			return !row.disabled;
		},
	},
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body {
	padding-top: 0 !important;
}
</style>