<template>
	<div>
		<!-- 列表 -->
		<div style="margin-left:10px;display: inline-block">
			<span>编码：</span>
			<el-input
				style="width: 200px"
				placeholder="请输入编码"
				v-model="qdsCode">
			</el-input>
		</div>
		<div style="margin-left:10px;display: inline-block">
			<span>渠道商：</span>
			<el-input
				style="width: 200px"
				placeholder="请输入渠道商"
				v-model="name">
			</el-input>
		</div>
		<el-button
			style="margin-left: 10px"
			size="mini"
			type="primary"
			icon="document"
			@click="handleSelect"
		>查询
		</el-button>
		<el-button
			style="margin-left: 10px"
			size="mini"
			type="primary"
			icon="document"
			@click="handleClear"
		>重置
		</el-button>
		<el-button
			style="margin-left: 10px"
			size="mini"
			type="primary"
			icon="document"
			@click="handleEdit(0)"
		>新增渠道码
		</el-button>
		<el-table v-loading="tableDataLoading" :data="tableData.list">
			<el-table-column
				prop="id"
				label="序号"
				width="80"
			>
			</el-table-column>
			<el-table-column
				prop="name"
				label="渠道码名称"
				width="220"
			>
			</el-table-column>
			<el-table-column
				prop="code"
				label="编码"
				width="150"
			>
			</el-table-column>
			<el-table-column prop="lm" label="所属类目">
				<template slot-scope="scope">
					{{ showDictValue(scope.row.lm, lmData) }}
				</template>
			</el-table-column>
			<el-table-column
				prop="qdsName"
				label="渠道商"
				width="200"
			>
			</el-table-column>
			<el-table-column
				prop="userName"
				label="绑定人员"
				width="150"
			>
			</el-table-column>
			<el-table-column
				prop="rate"
				label="渠道分成"
				width="150"
			>
				<template slot-scope="scope">
					<span>{{scope.row.rate}}%</span>
				</template>
			</el-table-column>
			<el-table-column prop="codeImage" label="渠道码">
				<template slot-scope="scope">
					<el-popover placement="top-start" title="" trigger="hover">
						<img style="width: 50px; height: 50px;margin: 3px;" :src="scope.row.codeImage" alt=""
							 slot="reference">
						<img style="width: 300px;height:auto" :src="scope.row.codeImage" alt="">
					</el-popover>
				</template>
			</el-table-column>
			<el-table-column
				prop="createTime"
				label="创建时间"
				width="150"
			>
			</el-table-column>
			<el-table-column label="操作" prop="id" width="360" fixed="right">
				<template slot-scope="scope">
					<el-button
							   size="mini"
							   type="primary"
							   style="margin: 5px"
							   @click="handleEdit(scope.row)"
					>编辑
					</el-button>
					<el-button
							   size="mini"
							   type="primary"
							   style="margin: 5px"
							   @click="handleDelete(scope.row)"
					>删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div style="text-align: center; margin-top: 10px">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:page-sizes="[10, 20, 30, 40]"
				:page-size="limit"
				:current-page="page"
				layout="total,sizes, prev, pager, next,jumper"
				:total="tableData.totalCount"
			>
			</el-pagination>
		</div>
		<!-- 添加、修改 -->
		<el-dialog :title="titles" :visible.sync="dialogFormVisible" center @close="closeDialog">
			<div style="margin-bottom: 10px">
				<span style="width: 200px; display: inline-block; text-align: right">渠道码名称：</span>
				<el-input
					style="width: 50%"
					v-model="name"
					placeholder="请输入渠道码名称"
				></el-input>
			</div>
			<div style="margin-bottom: 10px">
				<span style="width: 200px; display: inline-block; text-align: right">所属类目：</span>
				<el-select clearable v-model="lm" style="width: 50%">
					<el-option
						v-for="item in lmData"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					>
					</el-option>
				</el-select>
			</div>
			<div style="margin-bottom: 10px">
				<span style="width: 200px; display: inline-block; text-align: right">渠道分成%：</span>
				<el-input-number v-model="rate" controls-position="right" :precision="2" :min="0" :step="1" placeholder="请输入渠道分成(%)"/>
			</div>
			<div style="margin-bottom: 10px">
				<span style="width: 200px; display: inline-block; text-align: right">绑定人员：</span>
				<el-input
					style="width: 50%"
					v-model="userId"
					type="text"
					placeholder="请选择用户"
					:disabled="true"
					v-show="false"
				>
				</el-input>
				<el-input
					style="width: 50%"
					v-model="userName"
					type="text"
					placeholder="请选择用户"
					:disabled="true"
				>
				</el-input>
				<el-button
					style="margin: 10px"
					size="mini"
					type="primary"
					icon="document"
					@click="showUsersView()"
				>用户列表
				</el-button>
			</div>
			<div style="margin-bottom: 10px">
				<span style="width: 200px; display: inline-block; text-align: right">广告投放成本：</span>
				<el-input-number v-model="cost" controls-position="right" :precision="2" :min="0" :step="1" placeholder="请输入广告投放成本(元)"/>
			</div>
			<div style="margin-bottom: 10px">
			    <span style="width: 200px; display: inline-block; text-align: right">推广时间段：</span>
				<el-date-picker
					v-model="tgDate"
					type="daterange"
					format="yyyy-MM-dd"
					value-format="yyyy-MM-dd"
					range-separator="至"
					start-placeholder="开始日期"
					end-placeholder="结束日期">
				</el-date-picker>
			</div>
			<div style="margin-bottom: 10px">
		          <span style="width: 200px; display: inline-block; text-align: right">投放地点：</span>
				<el-input
					style="width: 50%"
					v-model="address"
					placeholder="请输入投放地点"
				></el-input>
			</div>
			<div style="margin-bottom: 10px">
		          <span style="width: 200px; display: inline-block; text-align: right"
				  >备注：</span
				  >
				<el-input
					style="width: 50%"
					v-model="remarks"
					type="textarea"
					:rows="4"
					placeholder="请输入备注"
				>
				</el-input>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="handleSubmit()">确 定</el-button>
			</div>
		</el-dialog>
		<!-- <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 用户列表弹框 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-->
		<el-dialog title="用户列表" :visible.sync="dialogUserListFormVisible" width="60%" center>
			<div style="margin-left: 10px; display: inline-block;">
				<span>昵称：</span>
				<el-input
					style="width: 200px"
					placeholder="请输入昵称"
					v-model="userName"
				></el-input>
			</div>
			<div style="margin-left: 10px; display: inline-block">
				<span>手机号：</span>
				<el-input
					style="width: 200px"
					placeholder="请输入手机号"
					v-model="phone"
				></el-input>
			</div>
			<el-button
				style="margin-left: 10px;"
				size="mini"
				type="primary"
				icon="document"
				@click="handleSelectUsers">查询
			</el-button>
			<el-button
				style="margin-left: 10px"
				size="mini"
				type="primary"
				icon="document"
				@click="handleClearUsers">重置
			</el-button>
			<el-table :data="userTableData.list" style="width: 100%">
				<el-table-column prop="userId" label="用户ID"></el-table-column>
				<el-table-column prop="avatar" label="头像">
					<template slot-scope="scope">
						<img
							v-if="scope.row.avatar && scope.row.avatar != ''"
							:src="scope.row.avatar"
							width="40"
							height="40"
						/>
						<span v-else>暂无图片</span>
					</template>
				</el-table-column>
				<el-table-column prop="userName" label="昵称"></el-table-column>
				<el-table-column prop="phone" label="手机号"></el-table-column>
				<el-table-column prop="qdsName" label="渠道商名称"></el-table-column>
				<el-table-column label="操作" prop="id">
					<template slot-scope="scope">
						<el-button
							style="margin: 10px 0"
							size="mini"
							type="primary"
							icon="document"
							@click="handleAddUser(scope.row)"
						>添加
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div style="text-align: center; margin-top: 10px">
				<el-pagination
					@size-change="handleUserSizeChange"
					@current-change="handleUserCurrentChange"
					:page-size="userLimit"
					:current-page="userPage"
					layout="total, prev, pager, next,jumper"
					:total="userTableData.totalCount"
				></el-pagination>
			</div>
		</el-dialog>
	</div>
</template>

<script>

export default {
	data() {
		return {
			apiUrl: "",
			titles: "",
			tableDataLoading: false,
			tableData: {},
			lmData: [],
			userId: "",
			avatar: "",
			userName: "",
			qdsName: "",
			name: "",
			qdsCode: "",
			phone: "",
			remarks: "",
			lm: "",
			rate: "",
			cost: 0,
			tgDate: "",
			startDate: "",
			endDate: "",
			address: "",
			page: 1,
			limit: 10,
			dialogFormVisible: false,
			dialogFormViewVisible: false,
			dialogUserListFormVisible: false,
			userTableDataLoading: false,
			userPage: 1,
			userLimit: 10,
			userTableData:{},
		};
	},
	methods: {
		showDictValue(data, datas){
			const dictItem = datas.find(item => data === item.id);
			return dictItem ? dictItem.name : '';
		},
		// 获取列表数据
		handleLm() {
			this.$http({
				url: this.$http.adornUrl("commission/qdsCode/lmList"),
				method: "get",
				params: this.$http.adornParams({}),
			}).then(({data}) => {
				if (data.code == 0) {
					this.lmData = data.data;
				} else {
					this.$notify({
						title: "提示",
						duration: 1800,
						message: data.msg,
						type: "warning",
					});
				}
			});
		},
		// 获取列表数据
		handleSelect() {
			this.tableDataLoading = true;
			this.$http({
				url: this.$http.adornUrl("commission/qdsCode/findPage"),
				method: "get",
				params: this.$http.adornParams({
					page: this.page,
					limit: this.limit,
					name: this.name,
					code: this.qdsCode,
				}),
			}).then(({data}) => {
				if (data.code == 0) {
					this.tableData = data.data;
				} else {
					this.$notify({
						title: "提示",
						duration: 1800,
						message: data.msg,
						type: "warning",
					});
				}
				this.tableDataLoading = false;
			});
		},
		// 重置
		handleClear() {
			this.name = "";
			this.qdsCode ="";
			this.page = 1;
			this.handleSelect();
		},

		//分页
		handleSizeChange(val) {
			this.limit = val;
			this.handleSelect();
		},

		//翻页
		handleCurrentChange(val) {
			this.page = val;
			this.handleSelect();
		},

		// 修改
		handleEdit(row) {
			if (row != 0) {
				this.titles = "修改";
				this.id = row.id;
				this.userId = row.userId;
				this.userName = row.userName;
				this.name = row.name;
				this.lm = row.lm;
				this.rate = row.rate;
				this.cost = row.cost;
				this.startDate = row.startDate;
				this.endDate = row.endDate;
				this.tgDate = [row.startDate,row.endDate];
				this.address = row.address;
				this.remarks = row.remarks;
			} else {
				this.titles = "添加";
				this.id = "";
				this.userId = "";
				this.userName = "";
				this.name = "";
				this.lm = "";
				this.rate = "";
				this.cost = 0;
				this.startDate = "";
				this.endDate = "";
				this.tgDate = [this.getCurrentDate(),this.getCurrentDate()];
				this.address = "";
				this.remarks = "";
			}
			this.dialogFormVisible = true;
		},

		closeDialog(){
			this.handleClear();
		},
		// 提交
		handleSubmit() {
			console.log(this.tgDate);
			if (this.name == "") {
				this.$notify({
					title: "提示",
					duration: 1800,
					message: "请输入渠道码名称",
					type: "warning",
				});
				return;
			}
			if (this.lm == "") {
				this.$notify({
					title: "提示",
					duration: 1800,
					message: "请选择类目",
					type: "warning",
				});
				return;
			}
			if (this.rate == "") {
				this.rate = 0;
			}
			if (this.userId == "") {
				this.$notify({
					title: "提示",
					duration: 1800,
					message: "请选择用户",
					type: "warning",
				});
				return;
			}
			if (this.cost == "") {
				this.cost = 0;
			}
			if (this.tgDate == "") {
				this.$notify({
					title: "提示",
					duration: 1800,
					message: "请选择推广时间段",
					type: "warning",
				});
				return;
			}
			if (this.address == "") {
				this.$notify({
					title: "提示",
					duration: 1800,
					message: "请输入投放地点",
					type: "warning",
				});
				return;
			}
			if (this.titles == "添加") {
				this.apiUrl = "commission/qdsCode/add";
			} else {
				this.apiUrl = "commission/qdsCode/update";
			}
			this.startDate = this.tgDate[0];
			this.endDate = this.tgDate[1];
			this.$http({
				url: this.$http.adornUrl(this.apiUrl),
				method: "post",
				params: this.$http.adornParams({
					id: this.id,
					userId: this.userId,
					name: this.name,
					rate: this.rate,
					cost: this.cost,
					address: this.address,
					startDate: this.startDate,
					endDate: this.endDate,
					lm: this.lm,
					remarks: this.remarks,
				}),
			}).then(({data}) => {
				if (data.code == 0) {
					this.dialogFormVisible = false;
					this.closeDialog();
					this.$message({
						message: "操作成功",
						type: "success",
						duration: 1500,
						onClose: () => {
							this.handleSelect();
						},
					});
				} else {
					this.$message({
						message: data.msg,
						type: "warning",
						duration: 1500,
						onClose: () => {
						},
					});
				}
			});
		},
		handleDelete(row){
			this.$confirm(`确定删除此条信息?`, "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning",
			}).then(() => {
				let id = row.id;
				this.$http({
					url: this.$http.adornUrl("commission/qdsCode/delete"),
					method: "post",
					params: this.$http.adornParams({
						id: id
					}),
				}).then(({data}) => {
					if (data.code == 0) {
						this.closeDialog();
						this.$message({
							message: "删除成功",
							type: "success",
							duration: 1500,
							onClose: () => {
								this.handleSelect();
							},
						});
					} else {
						this.$message({
							message: data.msg,
							type: "warning",
							duration: 1500,
							onClose: () => {
							},
						});
					}
				});
			}).catch(() => {});
		},

		showUsersView(){
			this.handleSelectUsers();
			this.dialogUserListFormVisible = true;
		},
		handleSelectUsers(){
			this.userTableDataLoading = true;
			this.$http({
				url: this.$http.adornUrl("commission/qdsCode/findUserPage"),
				method: "get",
				params: this.$http.adornParams({
					userPage: this.userPage,
					userLimit: this.userLimit,
					userName: this.userName,
					phone: this.phone,
				}),
			}).then(({data}) => {
				if (data.code == 0) {
					this.userTableData = data.data;
				} else {
					this.$notify({
						title: "提示",
						duration: 1800,
						message: data.msg,
						type: "warning",
					});
				}
				this.userTableDataLoading = false;
			});
		},

		//分页
		handleUserSizeChange(val) {
			this.userLimit = val;
			this.handleSelectUsers();
		},

		//翻页
		handleUserCurrentChange(val) {
			this.userPage = val;
			this.handleSelectUsers();
		},

		// 重置
		handleClearUsers() {
			this.userName = "";
			this.phone = "";
			this.userPage = 1;
			this.userLimit = 10;
			this.handleSelectUsers();
		},

		handleAddUser(row){
			this.userId = row.userId;
			this.userName = row.userName;
			this.dialogUserListFormVisible = false;
		},
		getCurrentDate(){
			const currentDate = new Date();
			const year = currentDate.getFullYear();
			const month = currentDate.getMonth() + 1; // 月份从0开始，需要加1
			const day = currentDate.getDate();
			return year + "-" + month + "-" + day;
		}
	},
	mounted() {
		this.handleLm();
		this.handleSelect();
	},
};
</script>

<style>
.customWidth {
	width: 80% !important;
}
.el-dialog--center {
	text-align: center;
 	margin-top: 1vh !important;
}

.el-tooltip__popper {
width: 200px;
padding: 10px;
color: #000 !important;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.4);
background-color: #fff !important;
}

.adver_main.box a {
display: flex;
justify-content: center;
height: 150px;
line-height: 150px;
text-decoration: none;
}

.bannerManin span {
display: inline-block;
margin-left: 5px;
}

.bannerManin img {
width: 48px;
height: 48px;
border-radius: 50%;
}

.bannerbtn a {
flex: 1;
text-align: center;
color: #3e8ef7 !important;
text-decoration: none;
}

.imgs {
position: relative;
border-radius: 6px;
width: 148px;
height: 148px;
margin-right: 10px;
display: inline-block;
}

.dels {
position: absolute;
top: 0;
left: 0;
display: none;
}

.dels .el-icon-delete {
line-height: 148px;
padding-left: 58px;
font-size: 25px;
color: #fff;
}

.imgs:hover .dels {
width: 100%;
height: 100%;
background: #000;
display: block;
opacity: 0.5;
}

.bqList {
padding: 4px 14px;
margin: 4px;
border: 1px solid #efefef;
font-size: 12px;
color: #999;
border-radius: 4px;
margin-right: 15px;
}

.delss {
display: none;
position: relative;
}

.delss .el-icon-delete {
position: absolute;
top: 0;
}

.bqList:hover .delss {
display: initial;
opacity: 0.5;
}

.tj {
padding: 6px !important;
margin: 4px;
font-size: 12px;
border: 1px solid #ccc;
border-radius: 4px;
}
</style>
