<template>
	<div>
		<div class="crumbs">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>
					<i class="el-icon-lx-global"></i> 通用话题管理
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="container">
			<div class="handle-box">
				<el-button type="primary" icon="el-icon-plus" class="handle-del" @click="add_Selection">添加</el-button>
			</div>
			<el-table :data="tableData" row-key="base_topic_id" border class="table" ref="multipleTable" default-expand-all
			 header-cell-class-name="table-header" :tree-props="{children: 'children', hasChildren: true}">
				<el-table-column prop="title" label="话题"></el-table-column>
				<el-table-column prop="base_topic_id" label="ID" width="100"></el-table-column>
				<el-table-column label="封面" align="center" width="200">
					<template slot-scope="scope">
						<el-image class="table-td-thumb" v-if="scope.row.icon!=''" :src="IMGdo_Url+'baseTopic/'+scope.row.icon" style="width: 120px;"></el-image>
					</template>
				</el-table-column>
				<el-table-column prop="sort" label="权重" width="100"></el-table-column>
				<el-table-column label="操作" width="180" align="center">
					<template slot-scope="scope">
						<el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
						<el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<!-- 添加弹出框 -->
		<el-dialog title="添加" :visible.sync="addVisible" width="30%">
			<el-form ref="add_Form" :model="addForm" :rules="rules" label-width="85px">
				<el-form-item label="话题" prop="title">
					<el-input v-model="addForm.title"></el-input>
				</el-form-item>
				<el-form-item label="所属父级" prop="parent_id">
					<el-select v-model="addForm.parent_id" filterable placeholder="请选择所属父级" value-key="groupID">
						<el-option key="0" label="无" value="0"></el-option>
						<el-option v-for="item in options" :key="item.base_topic_id" :label="item.title" :value="item.base_topic_id"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="封面" label-width="85px">
					<el-upload class="avatar-uploader" :action="IMGup_Url+'master/Journal/baseTopic_up_file'" :show-file-list="false"
					 name="baseTopic" :on-success="handleAvatarSuccess" :on-error="handleAvatarError" :before-upload="beforeAvatarUpload">
						<img v-if="imageUrl" :src="imageUrl" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
					<el-progress v-show="imgFlag == true" :percentage="percent"></el-progress>
				</el-form-item>
				<el-form-item label="权重">
					<el-input-number v-model="addForm.sort" :min="0"></el-input-number>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="addVisible = false;">取 消</el-button>
				<el-button type="primary" @click="saveAdd(addForm)">确 定</el-button>
			</span>
		</el-dialog>

		<!-- 编辑弹出框 -->
		<el-dialog title="编辑" :visible.sync="editVisible" width="30%">
			<el-form ref="edit_Form" :model="editForm" :rules="rules" label-width="85px">
				<el-form-item label="话题" prop="title">
					<el-input v-model="editForm.title"></el-input>
				</el-form-item>
				<el-form-item label="封面" label-width="85px">
					<el-upload class="avatar-uploader" :action="IMGup_Url+'master/Journal/baseTopic_up_file'" :show-file-list="false"
					 name="baseTopic" :on-success="handleEditSuccess" :on-error="handleAvatarError" :before-upload="beforeAvatarUpload">
						<img v-if="imageUrl" :src="imageUrl" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
				</el-form-item>
				<el-form-item label="权重">
					<el-input-number v-model="editForm.sort" :min="0"></el-input-number>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="editVisible = false">取 消</el-button>
				<el-button type="primary" @click="saveEdit(editForm)">确 定</el-button>
			</span>
		</el-dialog>

	</div>
</template>

<script>
	export default {
		data() {
			return {
				addForm: {
					sort: 0,
					icon: ''
				},
				tableData: [],
				IMGdo_Url: this.IMGdoURL,
				IMGup_Url: this.IMGupURL,
				imgFlag: false,
				percent: 0,
				imageUrl: '',
				addVisible: false,
				editVisible: false,
				editForm: {
					icon: ''
				},
				aid: -1,
				idx: -1,
				id: -1,
				options: [],
				rules: {
					title: [{
						required: true,
						message: '请输入标题',
						trigger: 'blur'
					}],
					parent_id: [{
						required: true,
						message: '请选择所属父级',
						trigger: 'blur'
					}],
				}
			};
		},
		created() {
			this.getData();
		},
		methods: {
			// 获取Notices列表数据
			getData() {
				this.$api
					.post('master/Journal/getBaseTopicList', {})
					.then(res => {
						if (res.code == 0) {
							this.tableData = res.data.topics;
							this.options = res.data.topics;
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
			},
			// 添加操作
			add_Selection(index, row) {
				this.aid = index;
				this.addVisible = true;
				this.imageUrl = "";
			},
			// 保存添加
			saveAdd(addForm) {
				this.$refs.add_Form.validate((valid) => {
					if (valid) {
						this.$api
							.post('master/Journal/addBaseTopic', this.addForm)
							.then(res => {
								if (res.code == 0) {
									this.addVisible = false;
									this.$refs.add_Form.resetFields();
									this.$message.success(`添加成功`);
									this.getData();
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});
					} else {
						return false;
					}
				});

			},
			// 编辑操作
			handleEdit(index, row) {
				this.idx = index;
				this.editForm = Object.assign({}, row);
				this.editVisible = true;
				if (row.icon != '') {
					this.imageUrl = this.IMGdo_Url + 'baseTopic/' + row.icon;
				} else {
					this.imageUrl = "";
				}
			},
			// 保存编辑
			saveEdit(editForm) {
				this.$refs.edit_Form.validate((valid) => {
					if (valid) {
						this.$api
							.post('master/Journal/editBaseTopic', this.editForm)
							.then(res => {
								if (res.code == 0) {
									this.editVisible = false;
									this.$message.success(`更改第 ${this.idx + 1} 行成功`);
									this.getData();
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});
					} else {
						this.$message.error('error submit!!');
						return false;
					}
				});
			},
			// 删除操作
			handleDelete(index, row) {
				// 二次确认删除
				this.$confirm('确定要删除吗？', '提示', {
						type: 'warning'
					})
					.then(() => {
						this.$api
							.post('master/Journal/delBaseTopic', row)
							.then(res => {
								if (res.code == 0) {
									this.$message.success('删除成功');
									this.getData();
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});
					})
					.catch(() => {});
			},
			//上传图片
			handleAvatarSuccess(res, file) {
				// this.imgFlag = false;
				if (res.code == 0) {
					this.addForm.icon = res.upurl;
				} else {
					this.$message.error(res.msg);
				}
				this.imageUrl = URL.createObjectURL(file.raw);
			},
			handleEditSuccess(res, file) {
				// this.imgFlag = false;
				if (res.code == 0) {
					this.editForm.icon = res.upurl;
				} else {
					this.$message.error(res.msg);
				}
				this.imageUrl = URL.createObjectURL(file.raw);
			},
			handleAvatarError(res, file) {
				// this.$message.error(res);
			},
			beforeAvatarUpload(file) {
				const isLt2M = file.size / 1024 / 1024 < 1;
				if (!isLt2M) {
					this.$message.error('图片大小不能超过 1M!');
				}
				return isLt2M;
			},

			// changeData(file, fileList) {
			// 	// 数据小于0.1M的时候按KB显示
			// 	const size = file.size / 1024 / 1024 > 0.1 ? `(${(file.size/1024/1024).toFixed(1)}M)` :
			// 		`(${(file.size/1024).toFixed(1)}KB)`
			// 	file.name.indexOf('M') > -1 || file.name.indexOf('KB') > -1 ? file.name : file.name += size
			// },

			// handleRequest(data) {
			// 	let formdata = new FormData()
			// 	formdata.append('file', data.file)
			// 	const config = {
			// 		onUploadProgress: progressEvent => {
			// 			// progressEvent.loaded:已上传文件大小
			// 			// progressEvent.total:被上传文件的总大小
			// 			this.progressPercent = Number((progressEvent.loaded / progressEvent.total * 100).toFixed(2))
			// 		}
			// 	}
			// 	console.log(data.file)
			// 	console.log(formdata)
			// 	console.log(config)

			// 	this.$api
			// 		.post('master/Journal/baseTopic_up_file', {
			// 			'baseTopic': formdata,
			// 		})
			// 		.then(res => {
			// 			if (res.code == 0) {
			// 				this.addForm.icon = res.upurl;
			// 			} else {
			// 				this.$message.error(res.msg);
			// 			}
			// 			this.imageUrl = URL.createObjectURL(file.raw);
			// 		})
			// 		.catch(err => {
			// 			this.$message.error(err);
			// 		});

			// 	// this.$api
			// 	// 	.post('master/Journal/delBaseTopic', row)
			// 	// 	.then(res => {
			// 	// 		if (res.code == 0) {
			// 	// 			this.$message.success('删除成功');
			// 	// 			this.getData();
			// 	// 		} else {
			// 	// 			this.$message.error(res.msg);
			// 	// 		}
			// 	// 	})
			// 	// 	.catch(err => {
			// 	// 		this.$message.error(err);
			// 	// 	});
			// },

		},
	};
</script>

<style>
	.handle-box {
		margin-bottom: 20px;
	}

	.table {
		width: 100%;
		font-size: 14px;
	}

	.red {
		color: #ff0000;
	}

	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
		width: 140px;
		height: 140px;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 40px;
		height: 40px;
		line-height: 140px;
		text-align: center;
	}

	.avatar {
		width: 140px;
		height: 140px;
		display: block;
	}
</style>
