<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="名称" prop="name">
				<el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<!--<el-form-item label="图标" prop="icon">-->
			<!--<el-input-->
			<!--v-model="queryParams.icon"-->
			<!--placeholder="请输入图标"-->
			<!--clearable-->
			<!--@keyup.enter.native="handleQuery"-->
			<!--/>-->
			<!--</el-form-item>-->
			<!--<el-form-item label="链接" prop="ink">-->
			<!--<el-input-->
			<!--v-model="queryParams.ink"-->
			<!--placeholder="请输入链接"-->
			<!--clearable-->
			<!--@keyup.enter.native="handleQuery"-->
			<!--/>-->
			<!--</el-form-item>-->
			<!--<el-form-item label="合作数" prop="coNum">-->
			<!--<el-input-->
			<!--v-model="queryParams.coNum"-->
			<!--placeholder="请输入合作数"-->
			<!--clearable-->
			<!--@keyup.enter.native="handleQuery"-->
			<!--/>-->
			<!--</el-form-item>-->
			<!--<el-form-item label="拍讯" prop="seq">-->
			<!--<el-input-->
			<!--v-model="queryParams.seq"-->
			<!--placeholder="请输入拍讯"-->
			<!--clearable-->
			<!--@keyup.enter.native="handleQuery"-->
			<!--/>-->
			<!--</el-form-item>-->
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['system:coanchor:add']">新增
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['system:coanchor:edit']">修改
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['system:coanchor:remove']">删除
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['system:coanchor:export']">导出
				</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="coanchorList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<!--<el-table-column label="主键ID" align="center" prop="id" />-->
			<el-table-column label="名称" align="center" prop="name" />
			<el-table-column label="描述" align="center" prop="content" />
			<el-table-column label="图标" align="center">
				<template slot-scope="scope">
					<el-image :src="pictureServerPath+scope.row.icon" style="width: 50px;height: 50px;"></el-image>
				</template>
			</el-table-column>
			<el-table-column label="链接" align="center" prop="ink" />
			<el-table-column label="邀请码" align="center" prop="invitationCode">
				<template slot-scope="scope">
					<el-tooltip class="item" effect="dark" content="点我查看所有用户" placement="top-start">
						<span style="color: blue;cursor: pointer; "
							@click="goinviteUsers(scope.row.invitationCode)">{{scope.row.invitationCode}}</span>
					</el-tooltip>

				</template>
			</el-table-column>
			<el-table-column label="是否分红" align="center" prop="dividends">
				<template slot-scope="scope">
					<el-tag v-if="scope.row.dividends=='true'">是</el-tag>
					<el-tag v-if="scope.row.dividends=='false'" type="danger">否</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="分红比例" align="center" prop="proportions">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.propor_tions" :value="scope.row.proportions" />
				</template>
			</el-table-column>
			<!-- <el-table-column prop="zbMode" label="主播模式">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.anchor_mode" :value="scope.row.zbMode" />
				</template>
			</el-table-column> -->
			<el-table-column label="合作数" align="center" prop="coNum" />
			<el-table-column label="排序" align="center" prop="seq" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['system:coanchor:edit']">修改
					</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['system:coanchor:remove']">删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改合作主播对话框 -->
		<el-dialog :title="title" :visible.sync="open" v-if="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="名称" prop="name">
					<el-input v-model="form.name" placeholder="请输入名称" />
				</el-form-item>
				<el-form-item label="描述">
					<editor v-model="form.content" :min-height="192" />
				</el-form-item>
				<el-form-item label="图标" prop="icon">
					<image-upload-sign :showPic="form.icon" :pictureServerPath="pictureServerPath"
						v-model="form.icon"></image-upload-sign>
				</el-form-item>


				<el-form-item label="链接" prop="ink">
					<el-input v-model="form.ink" placeholder="请输入链接" />
				</el-form-item>
				<el-form-item label="邀请码" prop="invitationCode">
					<el-input v-if="form.invitationCode" style="width: 60%;" disabled v-model="form.invitationCode"
						placeholder="请输入邀请码" />
					<el-input v-if="form.invitationCode==''" style="width: 60%;" v-model="form.invitationCode"
						placeholder="请输入邀请码" />
					<el-button v-if="form.invitationCode==''" @click="getinvitationCode()">生成邀请码</el-button>
				</el-form-item>
				<el-form-item label="是否分红" prop="dividends">
					<el-switch v-model="form.dividends" active-text="是" inactive-text="否">
					</el-switch>
				</el-form-item>
				<el-form-item label="分红比例" prop="proportions">
					<el-select style="width: 100%;" placeholder="请选择分红比例" v-model="form.proportions" clearable>
						<el-option v-for="(item,index) in dict.type.propor_tions" :label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<!-- <el-form-item label="主播模式" prop="isSecurity">
					<el-select style="width: 100%;" placeholder="请选择主播模式" v-model="form.zbMode" clearable>
						<el-option v-for="(item,index) in dict.type.anchor_mode" :label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item> -->
				<el-form-item label="合作数" prop="coNum">
					<el-input v-model="form.coNum" placeholder="请输入合作数" />
				</el-form-item>
				<el-form-item label="排序" prop="seq">
					<el-input v-model="form.seq" placeholder="请输入拍讯" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listCoanchor,
		getCoanchor,
		delCoanchor,
		addCoanchor,
		updateCoanchor
	} from "@/api/data/coanchor";
	import {
		getConfigKey
	} from "@/api/system/config";

	export default {
		name: "Coanchor",
		dicts: ['anchor_mode', 'propor_tions'],
		data() {
			return {
				pictureServerPath: null,
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 合作主播表格数据
				coanchorList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					name: null,
					content: null,
					icon: null,
					ink: null,
					coNum: null,
					seq: null
				},
				// 表单参数
				form: {
					invitationCode: '',
				},
				// 表单校验
				rules: {}
			};
		},
		created() {
			this.getServierPicture();
			this.getList();
		},
		methods: {
			goinviteUsers(item) {
				this.$router.push({
					path: '/anchorManagement/inviteUsers',
					query: {
						invitationCode: item,
					}
				})
			},
			getinvitationCode() {
				const numbers = '0123456789';
				const uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

				// 随机决定是2数字3字母还是3数字2字母
				const numDigits = Math.random() < 0.5 ? 2 : 3;
				const numLetters = 5 - numDigits;

				// 生成指定数量的数字和字母
				let code = [];

				// 生成数字
				for (let i = 0; i < numDigits; i++) {
					code.push(numbers.charAt(Math.floor(Math.random() * numbers.length)));
				}

				// 生成字母
				for (let i = 0; i < numLetters; i++) {
					code.push(uppercase.charAt(Math.floor(Math.random() * uppercase.length)));
				}

				// 打乱数组顺序确保随机性
				code = code.sort(() => Math.random() - 0.5);
				this.form.invitationCode = code.join('');
				this.$forceUpdate();
			},
			getServierPicture() {
				let that = this
				getConfigKey('pictureServerPath').then(res => {
					that.pictureServerPath = res.msg
				})
			},
			/** 查询合作主播列表 */
			getList() {
				this.loading = true;
				listCoanchor(this.queryParams).then(response => {
					this.coanchorList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					name: null,
					content: null,
					icon: null,
					ink: null,
					coNum: null,
					seq: null,
					invitationCode: '',
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加合作主播";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids
				getCoanchor(id).then(response => {
					this.form = response.data;
					if (this.form.dividends == 'true') {
						this.form.dividends = true;
					} else if (this.form.dividends == 'false') {
						this.form.dividends = false;
					}
					this.open = true;
					this.title = "修改合作主播";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updateCoanchor(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addCoanchor(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除合作主播编号为"' + ids + '"的数据项？').then(function() {
					return delCoanchor(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('system/coanchor/export', {
					...this.queryParams
				}, `coanchor_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>