<template>
	<div>
		<div class="crumbs">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>
					<i class="el-icon-message"></i> Mail template list
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="container_state">
			<div class="handle-box">
				<el-button type="primary" icon="el-icon-plus" class="handle-del" @click="add_Selection">Add template
				</el-button>
			</div>
			<el-table :data="tableData" row-key="eid" border class="table" ref="multipleTable" default-expand-all
				header-cell-class-name="table-header" :tree-props="{children: 'children', hasChildren: true}"
				empty-text="New template (0)">
				<el-table-column label="Template name" width="220">
					<template slot-scope="scope">
						<span v-if="scope.row.epid==0">
							{{scope.row.etitle}}
						</span>
					</template>
				</el-table-column>
				<el-table-column prop="econtent" label="Content">
					<template slot-scope="scope">
						<p v-if="scope.row.epid!=0">
							{{scope.row.econtent}}
							<br>
							{{scope.row.etitle}}
						</p>
					</template>
				</el-table-column>
				<el-table-column label="Number of clicks" width="90" align="center">
					<template slot-scope="scope" v-if="scope.row.epid!=0">
						{{scope.row.num}}
					</template>
				</el-table-column>
				<el-table-column label="" width="110" align="center">
					<template slot-scope="scope">
						<el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)">
							Delete
						</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<!-- 添加弹出框 -->
		<el-dialog title="Add template" :visible.sync="addVisible" width="600px">
			<el-form ref="add_Form" :model="addForm" :rules="rules" label-width="135px">
				<el-form-item label="Template name :" prop="etitle">
					<el-input v-model="addForm.etitle"></el-input>
				</el-form-item>
				<el-form-item label="Parent :" prop="epid">
					<el-select v-model="addForm.epid" filterable placeholder="Please select the parent"
						value-key="groupID">
						<el-option key="0" label="None" value="0"></el-option>
						<el-option v-for="item in mouldList" :key="item.eid" :label="item.etitle" :value="item.eid">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="Content :" prop="econtent" v-if="addForm.epid!=0">
					<el-input v-model="addForm.econtent" type="textarea" rows="10"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="addVisible = false;">Cancel</el-button>
				<el-button type="primary" @click="saveAdd(addForm)">OK</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				addForm: {
					etitle: '',
					epid: '0',
					econtent: ''
				},
				tableData: [],
				addVisible: false,
				mouldList: [],
				rules: {
					etitle: [{
						required: true,
						message: 'Please enter a template title',
						trigger: 'blur'
					}],
					epid: [{
						required: true,
						message: 'Please select a parent',
						trigger: 'blur'
					}],
					econtent: [{
						required: true,
						message: 'Please enter template content',
						trigger: 'blur'
					}],
				}
			};
		},
		created: function() {
			this.getData();
		},
		computed: {

		},
		methods: {
			//获取数据
			getData() {
				this.$api
					.post('api/Email/getAllEmailTemplate')
					.then(res => {
						if (res.code == 0) {
							this.tableData = res.data.templates;
							this.mouldList = res.data.templates;
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
			},
			// 添加操作
			add_Selection(index, row) {
				this.addVisible = true;
			},
			// 保存添加
			saveAdd(addForm) {
				this.$refs.add_Form.validate((valid) => {
					if (valid) {
						this.$api
							.post('api/Email/addEmailTemplate', this.addForm)
							.then(res => {
								if (res.code == 0) {
									this.addVisible = false;
									this.$refs.add_Form.resetFields();
									this.$message.success('Template added successfully!');
									this.getData();
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});
					} else {
						return false;
					}
				});
			},
			// 删除操作
			handleDelete(row) {
				// 二次确认删除
				this.$confirm('Are you sure to delete this template "' + row.etitle + '"?', 'Tips', {
						distinguishCancelAndClose: true,
						confirmButtonText: 'OK',
						cancelButtonText: 'Cancel'
					})
					.then(() => {
						this.$api
							.post('api/Email/delEmailTemplate', row)
							.then(res => {
								if (res.code == 0) {
									this.$message.success('Delete succeeded!');
									this.getData();
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});
					})
					.catch(() => {});
			},
		}
	};
</script>

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

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

	.red {
		color: #ff0000;
	}
</style>
