<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="gameName">
				<el-input v-model="queryParams.gameName" 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="['game:game: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="['game:game: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="['game:game: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="['game:game:export']">导出
				</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="gameList" @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="gameName">
				<template slot-scope="scope">
					<label style="color: #ff0000;" @click="toGameArae(scope.row)">{{scope.row.gameName}}</label>
				</template>
			</el-table-column>
			<!-- <el-table-column label="游戏图片" align="center" prop="gameFile" />-->
			<el-table-column label="游戏图片(淘号玩)" align="center" prop="gameFile" width="100">
				<template slot-scope="scope">
					<image-preview :src="pictureServerPath+scope.row.gameFile" :width="50" :height="50" />
				</template>
			</el-table-column>
			<el-table-column label="游戏图片(代售玩)" align="center" prop="dswGameFile" width="100">
				<template slot-scope="scope">
					<image-preview :src="pictureServerPath+scope.row.dswGameFile" :width="50" :height="50" />
				</template>
			</el-table-column>

			<el-table-column label="游戏类型" align="center" prop="gameType" width="100">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.game_type" :value="scope.row.gameType" />
				</template>
			</el-table-column>



			<el-table-column>
				<template slot-scope="scope">
					<label @click="setSerUserCode(scope.row)">客服码设置</label>
				</template>
			</el-table-column>
			<el-table-column>
				<template slot-scope="scope">
					<label @click="setSerUserCode1(scope.row)">回收群码设置</label>
				</template>
			</el-table-column>
			<el-table-column label="回收群号" align="center" prop="recoveryMode">
			</el-table-column>
			<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="['game:game:edit']">修改
					</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['game:game:remove']">删除
					</el-button>
					<!-- <el-button size="mini" type="text" icon="el-icon-plus" @click="addAttributes(scope.row)"
						v-hasPermi="['game:game: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="gameName">
					<el-input v-model="form.gameName" placeholder="请输入游戏名称" />
				</el-form-item>

				<el-form-item label="首字母" prop="gameInitial">
					<el-input v-model="form.gameInitial" placeholder="请输入游戏首字母" />
				</el-form-item>

				<el-form-item label="游戏类型" prop="gameType">
					<el-select v-model="form.gameType">
						<el-option v-for="dict in dict.type.game_type" :key="dict.value" :label="dict.label"
							:value="dict.value"></el-option>
					</el-select>
				</el-form-item>

				<el-form-item label="排序" prop="gameSort">
					<el-input v-model="form.gameSort" placeholder="请输入排序" />
				</el-form-item>

				<el-form-item label="推荐号" prop="isRecommend">
					<el-input v-model="form.isRecommend" placeholder="请输入推荐号" />
				</el-form-item>
				<el-form-item label="回收群号" prop="recoveryMode">
					<el-input v-model="form.recoveryMode" placeholder="请输入回收群号" />
				</el-form-item>


				<el-form-item label="游戏图片(淘号玩)" prop="gameFile">
					<image-upload-sign :showPic="form.gameFile" :pictureServerPath="pictureServerPath"
						v-model="form.gameFile"></image-upload-sign>
				</el-form-item>
				<el-form-item label="游戏图片(代售玩)" prop="dswGameFile">
					<image-upload-sign :showPic="form.dswGameFile" :pictureServerPath="pictureServerPath"
						v-model="form.dswGameFile"></image-upload-sign>
				</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>

		<el-dialog title="客服码设置" :visible="isShowUserUserCode" @close="isShowUserUserCode = false">
			<div>
				<el-image @click="deleteSerUserCode(item.id)" style="width: 50px;height: 50px;"
					v-for="(item) in userCodeList" :key="item.id" :src="pictureServerPath+item.url">
				</el-image>
			</div>
			<image-upload-sign ref="serCodePic" :showPic="serUserCodeForm.url" :pictureServerPath="pictureServerPath"
				v-model="serUserCodeForm.url"></image-upload-sign>
			<el-button @click="uploadSerCodePic">确定</el-button>
		</el-dialog>
		<el-dialog title="回收群码设置" :visible="isShowUserUserCode1" @close="isShowUserUserCode1 = false">
			<div>
				<el-image @click="deleteSerUserCode1()" style="width: 50px;height: 50px;"
					:src="pictureServerPath+gameRecycling">
				</el-image>
			</div>
			<image-upload-sign ref="serCodePic" :showPic="serUserCodeForm.gameRecycling" :pictureServerPath="pictureServerPath"
				v-model="serUserCodeForm.gameRecycling"></image-upload-sign>
			<el-button @click="uploadSerCodePic1">确定</el-button>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listGame,
		getGame,
		delGame,
		addGame,
		updateGame
	} from "@/api/data/game";
	import {
		getConfigKey
	} from "@/api/system/config";
	import {
		queryAttach,
		addAttach,
		delAttach,
		recycling
	} from "@/api/data/attach";

	export default {
		name: "Game",
		dicts: ['game_type'],
		props: {
			ImageUpload: Function,
			value: [String, Object, Array],
			// 图片数量限制
			limit: {
				type: Number,
				default: 5,
			},
			// 大小限制(MB)
			fileSize: {
				type: Number,
				default: 10,
			},
			// 文件类型, 例如['png', 'jpg', 'jpeg']
			fileType: {
				type: Array,
				default: () => ["png", "jpg", "jpeg"],
			},
			// 是否显示提示
			isShowTip: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
				isShowComp: true,
				isShowPerson: false,
				serUserCodeForm: {},
				userCodeList: [],
				// userCodeList1: [],
				gameRecycling:null,
				isShowUserUserCode: false,
				isShowUserUserCode1: false,

				pictureServerPath: null,
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 平台支持游戏表格数据
				gameList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					gameName: null,
					gameFile: null,
					dswGameFile: null,
					gameInitial: null,
					gameSort: null,
					file: null
				},
				// 表单参数
				form: {
					dialogImageUrl: '',
					serUserCodePic: null
				},
				// 表单校验
				rules: {}
			};
		},
		created() {
			this.getServierPicture();
			this.getList();

		},


		methods: {

			deleteSerUserCode(id) {
				let that = this
				this.$modal.confirm("确定删除此图片吗？").then(function() {
					delAttach(id).then(r => {
						that.querySerUserCode(that.serUserCodeForm.busId)
					})
				})
			},
			deleteSerUserCode1() {
				let that = this
				let id=this.serUserCodeForm.busId
				this.$modal.confirm("确定删除此图片吗？").then(function() {
					delAttach(id).then(r => {
						that.querySerUserCode(that.serUserCodeForm.busId)
					})
				})
			},

			uploadSerCodePic() {
				let that = this
				addAttach(this.serUserCodeForm).then(res => {
					that.querySerUserCode(that.serUserCodeForm.busId)
				})
			},
			uploadSerCodePic1() {
				let that = this
				let query={
					id:this.serUserCodeForm.busId,
					gameRecycling:this.serUserCodeForm.gameRecycling,
				}
				recycling(query).then(res => {
					this.isShowUserUserCode1=false;
					that.querySerUserCode(that.serUserCodeForm.busId);
					this.getList();
				})
			},
			setSerUserCode(row) {
				let that = this
				this.serUserCodeForm.busId = row.id
				this.$nextTick(() => {
					this.isShowUserUserCode = true
				})
				that.querySerUserCode(row.id)
			},
			setSerUserCode1(row) {
				this.gameRecycling=row.gameRecycling
				let that = this
				this.serUserCodeForm.busId = row.id
				this.$nextTick(() => {
					this.isShowUserUserCode1 = true
				})
				that.querySerUserCode(row.id)
			},

			querySerUserCode(busId) {
				let that = this
				let obj = new Object()
				obj.busId = busId
				queryAttach(obj).then(res => {
					that.userCodeList = res.data
				})
			},

			toGameArae(row) {
				/*console.log(row);*/
				this.$router.push({
					path: '/guanli/gamearea',
					query: {
						gameId: row.id,
						parentId: 0
					}
				});
			},

			getServierPicture() {
				let that = this
				getConfigKey('pictureServerPath').then(res => {
					that.pictureServerPath = res.msg
				})
			},
			/** 查询平台支持游戏列表 */
			getList() {
				this.loading = true;
				listGame(this.queryParams).then(response => {
					this.gameList = response.rows;
					// this.userCodeList1=response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					gameName: null,
					gameFile: null,
					dswGameFile: null
				};
				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
				getGame(id).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改平台支持游戏";
				});
			},
			/** 提交按钮 */
			submitForm() {

				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updateGame(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addGame(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除平台支持游戏编号为"' + row.gameName + '"的数据项？').then(function() {
					return delGame(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('game/game/export', {
					...this.queryParams
				}, `game_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
