<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="status">
				<el-select v-model="queryParams.status" style="width: 200px" clearable placeholder="请选择状态">
					<el-option v-for="(item,index) in options1" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="所属游戏" prop="remark">
				<el-select v-model="queryParams.remark" filterable multiple collapse-tags placeholder="请选择游戏">
					<el-option v-for="(item,index) in gameList" :key="item.id" :label="item.gameName"
						:value="item.gameName"></el-option>
				</el-select>
				<el-button @click="selectAll">全选</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery()">搜索</el-button>
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd()">新增</el-button>
			</el-form-item>
		</el-form>

		<el-table :data="tableData" style="width: 100%">
			<el-table-column prop="type" label="类型">
				<template slot-scope="scope">
					<span v-if="scope.row.type=='HS'">回收</span>
					<span v-if="scope.row.type=='QT'">其他</span>
				</template>
			</el-table-column>
			<!-- <el-table-column prop="gameId" label="游戏名称">
			</el-table-column> -->
			<el-table-column prop="avatar" label="客服头像">
				<template slot-scope="scope">
					<image-preview :src="'https://static.taohaowan.com/'+scope.row.avatar" :width="100" :height="100" />
				</template>
			</el-table-column>
			<el-table-column prop="customerName" label="客服名称">
			</el-table-column>
			<el-table-column prop="contactPhone" label="联系电话">
			</el-table-column>
			<el-table-column prop="weChat" label="微信">
			</el-table-column>
			<el-table-column prop="imId" label="imId">
			</el-table-column>
			<el-table-column prop="status" label="状态">
				<template slot-scope="scope">
					<span v-if="scope.row.status=='S'">正常</span>
					<span v-if="scope.row.status=='N'">停用</span>
				</template>
			</el-table-column>
			<el-table-column prop="seq" label="排序">
			</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="small" type="primary" v-hasPermi="['system:goodsPaymentTX:info']"
						@click="handleUpdate1(scope.row)">修改
					</el-button>
					<el-button size="small" type="danger" v-hasPermi="['system:goodsPaymentTX:info']"
						@click="handleDelete(scope.row)">删除
					</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 v-if="open" :title="title" :visible.sync="open" append-to-body width="1000px">
			<el-form ref="form1" :model="form1" :rules="rules" label-width="80px">
				<el-row>
					<el-col :span="12">
						<el-form-item label="类型" prop="type">
							<el-select filterable v-model="form1.type" style="width: 100%;" clearable
								placeholder="请选择类型">
								<el-option v-for="(item,index) in typeOptions" :label="item.label" :value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="客服名称" prop="customerName">
							<el-input v-model="form1.customerName" placeholder="客服名称" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="联系电话" prop="contactPhone">
							<el-input v-model="form1.contactPhone" placeholder="请输入联系电话" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="微信" prop="weChat">
							<el-input v-model="form1.weChat" placeholder="请输入微信" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="imId" prop="imId">
							<el-input v-model="form1.imId" placeholder="请输入imId" />
						</el-form-item>
					</el-col>

				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="显示排序" prop="seq">
							<el-input-number v-model="form1.seq" controls-position="right" :min="0" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="状态" prop="status">
							<el-select filterable v-model="form1.status" style="width: 100%;" clearable
								placeholder="请选择状态">
								<el-option v-for="(item,index) in dkOptions" :label="item.label" :value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>

				</el-row>
				<el-row>

					<el-col :span="12">
						<el-form-item label="客服头像" prop="avatar">
							<div class="img_list" v-if="title=='修改客服信息'">
								<div style="position: relative;width: 100px;height: 100px;">
									<image-preview :src="'https://static.taohaowan.com/'+form1.avatar" :width="100"
										:height="100" />
								</div>
							</div>
							<el-upload ref="upload" :action="uploadImgUrl" list-type="picture-card"
								:before-upload="beforeAvatarUpload" :on-success="handleAvatarSuccess"
								:on-remove="handleRemove" :on-change="changeUpload" :show-file-list="true"
								accept="image/png, image/jpeg, application/pdf" :multiple="true" :headers="headers">
							</el-upload>
						</el-form-item>
					</el-col>

				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="所属游戏" prop="remark">
							<el-select v-model="form1.remark" filterable multiple collapse-tags placeholder="请选择游戏">
								<el-option v-for="(item,index) in gameList" :key="item.id" :label="item.gameName"
									:value="item.gameName"></el-option>
							</el-select>
							<el-button @click="selectAll">全选</el-button>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>

			<div slot="footer" class="dialog-footer">
				<el-button v-hasPermi="['system:TBusGameRecall:add']" type="primary" @click="submitForm1">确 定
				</el-button>
				<el-button @click="open=false;form1=[]">取 消</el-button>
			</div>
		</el-dialog>





	</div>
</template>

<script>
	import {
		getInitGoodsManager,
	} from "@/api/data/goods";
	import {
		goodsPaymentQX,
		queryTBusShop,
		querentx,
		getInfoTx,
		update,
		deleteInfo
	} from "@/api/data/customerService";
	import {
		getToken
	} from "@/utils/auth";
	import Cookies from "js-cookie";
	export default {
		data() {
			return {
				gameList: [],
				fkje: 0,
				uploadImgUrl: process.env.VUE_APP_BASE_API + "/file/upload", // 上传的图片服务器地址
				headers: {
					Authorization: "Bearer " + getToken(),
				},
				open: false,
				// 提现状态
				options1: [{
					value: 'S',
					label: '正常'
				}, {
					value: 'N',
					label: '停用'
				}],
				dkOptions: [{
					value: "S",
					label: '正常'
				}, {
					value: "N",
					label: '停用'
				}],
				typeOptions: [{
					value: "HS",
					label: '回收'
				}, {
					value: "QT",
					label: '其他'
				}],
				rules: {

					type: [{
						required: true,
						message: '请选择类型',
						trigger: 'blur'
					}],
					customerName: [{
						required: true,
						message: '请输入客服名称',
						trigger: 'blur'
					}, ],
					contactPhone: [{
						required: true,
						message: '请输入联系电话',
						trigger: 'blur'
					}],
					weChat: [{
						required: true,
						message: '请输入微信',
						trigger: 'blur'
					}],
					imId: [{
						required: true,
						message: '请输入imId',
						trigger: 'blur'
					}],
					seq: [{
						required: true,
						message: '请输入排序',
						trigger: 'blur'
					}],
					status: [{
						required: true,
						message: '请选择状态',
						trigger: 'blur'
					}],
				},
				tableData: [],
				//店铺名称
				restaurants: [],
				options8: [],
				// 遮罩层
				loading: true,
				// 总条数
				total: 0,
				// 显示搜索条件
				showSearch: true,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					publishStatus: null,
					deleteStatus: null,
					updateStatus: null,
					goodsName: null,
					gameName: null,
					partnerName: null,
					// beginTime: this.$moment().subtract(15, 'days').format("YYYY-MM-DD"), // 当前时间往前推 15 天
					// endTime: this.$moment().format("YYYY-MM-DD"), // 获取当前日期,
					paymentStatus: null,
					originalPrice: null,
					shopId: null,
					shopName: null,
					quxianStatus: null,
					deptId: null,
					remark:'',
				},
				queryForm: {
					publishStatus: null,
					deleteStatus: null,
					updateStatus: null,
					goodsName: null,
					gameName: null,
					partnerName: null,
				},
				form1: {},
				// 表单参数
				form2: {
					electronicDataList: [],
				},
			};
		},
		created() {
			this.getList();
			this.initGoodsManager();
		},
		mounted() {
			this.restaurants = this.loadAll();
		},
		methods: {
			selectAll() {
				this.form1.remark = this.gameList.map(item => item.gameName);
			},
			initGoodsManager() {
				getInitGoodsManager().then(res => {
					this.gameList = res.gameList
				})
			},
			// 上传成功
			handleAvatarSuccess(res) {
				this.form1.avatar = res.data.url
			},
			// 删除
			handleRemove(file, fileList) {
				this.form2.electronicDataList = this.form2.electronicDataList.filter(item => item !== file.response.data
					.webUrl);
			},
			// 文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用
			changeUpload(file, fileList) {
				if (file.status === 'success') {
					this.$refs.upload.uploadFiles = this.$refs.upload.uploadFiles.filter((item) => {
						return item.raw.size / 1024 / 1024 < 50
					})
					this.tobase64(file, fileList)
				}
			},
			tobase64(file, fileList) {
				var reader = new FileReader()
				reader.readAsDataURL(file.raw)
				reader.onload = (e) => {
					const tempobj = {
						data: file.response.data.webUrl,
						suffix: '.' + file.raw.type.split('/')[1],
						fileName: file.name
					}
					this.form2.electronicDataList.push(tempobj.data)

				}

			},
			submitForm1() {
				this.$refs["form1"].validate(valid => {
					if (valid) {
						if (this.title == '新增客服信息') {
							this.form1.remark = this.form1.remark.join('/');
							querentx(this.form1).then(res => {
								this.getList();
								this.form1 = []
								this.open = false;
								this.$modal.msgSuccess("新增成功");
							})
						} else if (this.title == '修改客服信息') {
							this.form1.remark = this.form1.remark.join('/');
							update(this.form1).then(res => {
								this.getList();
								this.form1 = []
								this.open = false;
								this.$modal.msgSuccess("修改成功");
							})
						}
					}
				})

			},
			/** 下架按钮操作 */
			handleDelete(row) {
				const ids = row.id;
				this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
					return deleteInfo(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("已删除!");
				}).catch(() => {});
			},
			//店铺名称
			querySearch(queryString, cb) {
				var restaurants = this.restaurants;
				var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
				// 调用 callback 返回建议列表的数据
				cb(results);
			},
			createFilter(queryString) {
				return (restaurant) => {
					return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) !== -1);
				};
			},
			loadAll() {
				queryTBusShop().then(response => {
					response.data.forEach((item) => {
						this.options8.push({
							label: item.id,
							value: item.name,
						});
					});
				});
				return this.options8;
			},
			handleSelect(item) {

			},
			//修改
			handleUpdate1(row) {
				this.title = '修改客服信息'
				Cookies.set('id', row.id)
				let query = {
					id: row.id
				}
				getInfoTx(query.id).then(res => {
					if (res.code == 200) {
						this.form1 = res.data;
						this.form1.remark = this.form1.remark.split("/");
						this.open = true;
					}
				})
			},

			/** 查询客服列表 */
			getList() {
				this.loading = true;
				console.log(this.queryParams)
				goodsPaymentQX(this.queryParams).then(response => {
					this.tableData = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				console.log(this.queryParams.remark)
				this.queryParams.remark = this.queryParams.remark.join('/');
				console.log(this.queryParams.remark)
				this.getList();
			},
			/** 新增按钮操作 */
			handleAdd() {
				// this.form1 = [];
				this.title = '新增客服信息'
				this.open = true;
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
		}
	};
</script>
<style>
	.father {
		height: 80px;
		line-height: 80px;
		font-size: 25px;
		display: flex;
		justify-content: space-around;
	}

	.el-icon-loading {
		color: green;
		margin-right: 5px;
	}

	.el-icon-message-solid {
		color: green;
		margin-right: 5px;
	}

	.el-icon-check {
		color: green;
		margin-right: 5px;
	}

	.el-icon-coin {
		color: green;
		margin-right: 5px;
	}
</style>