<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
			<el-form-item label="类型" prop="type">
				<el-select v-model="queryParams.type" clearable>
					<el-option v-for="item in dict.type.gamenews_type" :key="item.value" :label="item.label"
						:value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
				<el-button type="primary" plain icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button type="warning" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="tableData" style="width: 100%">
			<el-table-column prop="title" label="标题" width="300">
			</el-table-column>
			<el-table-column prop="content" show-overflow-tooltip align="center" height="20px" label="内容" width="600">
				<template slot-scope="scope">
					<span v-html="scope.row.content"></span>
				</template>
			</el-table-column>
			<el-table-column prop="type" label="类型" width="150">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.gamenews_type" :value="scope.row.type" />
				</template>
			</el-table-column>
			<el-table-column label="创建时间" align="center" prop="createTime">

			</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="['system:gameNews:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['system:gameNews: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 v-if="xinzeng" :title="title" :visible.sync="xinzeng" append-to-body width="1000px">
			<div>
				<el-form ref="form" :model="form" :rules="rules" label-width="120px">
					<el-row>
						<el-col :span="12">
							<el-form-item label="类型" prop="type">
								<el-select filterable v-model="form.type" style="width: 100%" clearable
									placeholder="请选择类型">
									<el-option v-for="(item,index) in dict.type.gamenews_type" :label="item.label"
										:value="item.value">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="标题" prop="title">
								<el-input v-model="form.title" placeholder="请输入标题" />
							</el-form-item>
						</el-col>
						<el-col :span="24">
							<el-form-item label="首图" prop="pic">
								<div class="img_list" v-if="form.id!=null">
									<div style="position: relative;width: 100px;height: 100px;">
										<image-preview :src="'https://static.taohaowan.com/'+form.pic" :width="100" :height="100" />
									</div>
								</div>
								<el-upload ref="upload" :action="uploadImgUrl" list-type="picture-card"
									: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-col :span="24">
							<el-form-item label="内容" prop="content">
								<editor v-model="form.content" :min-height="200" />
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>
			<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 {
		getSysDeptInfo,
		getDeptInfo,
		accountMonitoring,
		add,
		upData,
		getPhone,
		view,
		remove,
		getInitGoodsManager,
		getPaymentInfo,
		updateOriginalPrice,
		getTBusGoodsPaymentSum,
		updatePaymentStatus
	} from "@/api/data/gameNews";
	import Cookies from 'js-cookie';
	import {
		getToken
	} from "@/utils/auth";
	import {
		getInfo
	} from "../../../api/data/gameNews";
	export default {

		dicts: ['gamenews_type'],
		data() {
			return {
				uploadImgUrl: process.env.VUE_APP_BASE_API + "/file/upload", // 上传的图片服务器地址
				headers: {
					Authorization: "Bearer " + getToken(),
				},
				tableData: [],
				// 总条数
				total: 0,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					phone: null,
					type: null,
				},
				xinzeng: false,
				form: {
					// electronicDataList:[],
				},
				rules: {
					type: [{
						required: true,
						message: '请选择类型',
						trigger: 'blur'
					}],
					title: [{
						required: true,
						message: '请输入标题',
						trigger: 'blur'
					}],
					content: [{
						required: true,
						message: '请填入内容',
						trigger: 'blur'
					}],
				}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			// 上传成功
			handleAvatarSuccess(res) {
				this.form.pic = res.data.url
			},
			// 删除
			handleRemove(file, fileList) {
				this.form.electronicDataList = this.form.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.form.electronicDataList.push(tempobj.data)
				}

			},
			handleAdd() {
				this.form = {};
				this.xinzeng = true;
				this.title = '新增资讯信息';
			},
			handleUpdate(row) {
				getInfo(row.id).then(res => {
					this.form = res.data
					this.xinzeng = true;
					this.title = '修改资讯信息'
				})
			},
			handleDelete(row) {
				this.$modal.confirm('是否确认删除编号为"' + row.id + '"的数据项？').then(function() {
					return remove(row.id);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("已删除");
				}).catch(() => {});
			},
			submitForm() {
				this.$refs["form"].validate(valid => {
					if(valid){
						if (this.title == '新增资讯信息') {
							if(this.form.pic){
								add(this.form).then(res => {
									if (res.code == 200) {
										this.$modal.msgSuccess("新增成功");
										this.xinzeng = false;
										this.getList();
									}
								})
							}else{
								this.$modal.msgError("请上传首图!");
							}
							
						} else if (this.title == '修改资讯信息') {
							upData(this.form).then(res => {
								if (res.code == 200) {
									this.$modal.msgSuccess("修改成功");
									this.xinzeng = false;
									this.getList();
								}
							})
						}
					}
				})
			},
			cancel() {
				this.form = [];
				this.xinzeng = false;
			},
			/** 查询账号信息列表 */
			getList() {
				this.loading = true;
				accountMonitoring(this.queryParams).then(response => {
					this.tableData = response.rows;
					this.total = response.total
					this.loading = false;
				});
			},



			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},

			/** 重置按钮操作 */
			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>