<template>
	<div>
    <div style="position: relative;display: inline-block;margin: 10px 0px;">
      <span>类型：</span>
      <el-select v-model="type" style="width:150px;margin-left: 10px;" @change="animeDat(type)">
        <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select>&nbsp;&nbsp;&nbsp;
    </div>
    <div style="position: relative;display: inline-block;margin: 10px 0px;">
      <span>标题：</span>
      <el-input style="width: 200px;" @keydown.enter.native="animeDat" placeholder="请输入标题"
        v-model="title1">
      </el-input>&nbsp;&nbsp
    </div>
		<div style="position: relative;display: inline-block;margin-right:2%;">
      <el-button style="margin-left:15px;" size="mini" type="primary" icon="document" @click="animeDat">查询
      </el-button>
      <el-button style="margin-left:15px;" size="mini" type="primary" icon="document" @click="cleans">重置
      </el-button>
			<el-button style="margin-left:15px;" :disabled="!isAuth('materialLink:add')" size="mini" type="primary"
				icon="document" @click="advertNotice">添加</el-button>
		</div>
		<el-table v-loading="tableDataLoading" :data="guangaoData.list">
			<el-table-column fixed prop="trainingId" label="编号" width="80"></el-table-column>
      <el-table-column prop="type" label="类型" width="120">
      <template slot-scope="scope">
      	<div v-if="scope.row.type==1">用户端</div>
        <div v-if="scope.row.type==2">骑手端</div>
        <div v-if="scope.row.type==3">商户端</div>
      </template>
      </el-table-column>
			<el-table-column prop="title" label="标题" width="180">
			</el-table-column>
			<el-table-column prop="message" label="内容">
				<template slot-scope="scope">
					<div
						style="display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 3;overflow: hidden;">
						{{scope.row.message}}</div>
				</template>
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
			<el-table-column label="操作" width="180">
				<template slot-scope="scope">
					<el-button size="mini" type="primary" :disabled="!isAuth('materialLink:update')"
						@click="advertUpdates(scope.row)">修改</el-button>
					<el-button size="mini" type="danger" :disabled="!isAuth('materialLink:delete')"
						@click="advertdeletes(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div style="text-align: center;margin-top: 10px;">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:page-sizes="[10, 20, 30, 40]" :page-size="limit" :current-page="page"
				layout="total,sizes, prev, pager, next,jumper" :total="guangaoData.totalCount">
			</el-pagination>
		</div>
		<!-- 添加素材 -->
		<el-dialog title="添加" :visible.sync="dialogFormVisible" center>
      <div style="margin-bottom: 10px;">
        <span style="width: 200px;display: inline-block;text-align: right;">类型：</span>
        <el-select v-model="type1" style="width:50%;margin-left: 10px;">
          <el-option v-for="item in typeList1" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">标题：</span>
				<el-input style="width:50%;" v-model="title" placeholder="请输入标题" type="text">
				</el-input>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">内容：</span>
				<el-input v-model="message" style="width:45%;display: none;" placeholder="请输入备注"></el-input>
				<quill-editor ref="myTextEditor" v-model="message" :options="quillOption"
					style="padding-bottom: 50px;height: 300px;width:60%;display: inline-table;margin-bottom: 60px;">
				</quill-editor>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="advertNoticeTo()">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 修改素材 -->
		<el-dialog title="修改" :visible.sync="dialogFormVisible1" center>
			<el-form :model="form">
        <el-form-item label="标题：" :label-width="formLabelWidth">
        	<el-select v-model="form.type" style="width:50%;margin-left: 10px;">
        	  <el-option v-for="item in typeList1" :key="item.value" :label="item.label" :value="item.value">
        	  </el-option>
        	</el-select>
        </el-form-item>
				<el-form-item label="标题：" :label-width="formLabelWidth">
					<el-input style="width:90%;" v-model="form.title" placeholder="请输入标题" type="text"></el-input>
				</el-form-item>
				<el-form-item label="内容：" :label-width="formLabelWidth">
					<el-input v-model="form.message" style="width:45%;display: none;" placeholder="请输入内容"></el-input>
					<quill-editor ref="myTextEditor" v-model="form.message" :options="quillOption"
						style="padding-bottom: 50px;height: 300px;width:90%;display: inline-table;margin-bottom: 60px;">
					</quill-editor>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible1 = false">取 消</el-button>
				<el-button type="primary" @click="amendadvertTo()">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		quillEditor
	} from 'vue-quill-editor'
	import 'quill/dist/quill.core.css'
	import 'quill/dist/quill.snow.css'
	import 'quill/dist/quill.bubble.css'
	import quillConfig from './quill-config.js'
	export default {
		components: {
			quillEditor
		},
		data() {
			return {
				page: 1,
				limit: 10,
				message: '',
				title: '',
				type: '',
        title1:'',
				form: {
					id: '',
					message: '',
					title: '',
					createAt: '',
          type:'',
					deleteFlag: 0,
				},
				formLabelWidth: '200px',
				tableDataLoading: false,
				dialogFormVisible1: false,
				dialogFormVisible: false,
				guangaoData: {},
				quillOption: quillConfig,
        typeList:[
          {
            value:'',
            label:'全部'
          },
          {
            value:1,
            label:'用户端'
          },
          {
            value:2,
            label:'骑手端'
          },
          {
            value:3,
            label:'商户端'
          },
        ],
        type1:1,
        typeList1:[
          {
            value:1,
            label:'用户端'
          },
          {
            value:2,
            label:'骑手端'
          },
          {
            value:3,
            label:'商户端'
          },
        ]
			}
		},
		methods: {
			handleAvatarSuccess(file) {
				this.imgUrl = file.data
				this.linkUrl = this.imgUrl
			},
			handleAvatarSuccess1(file) {
				this.form.imgUrl = file.data
				this.form.linkUrl = this.form.imgUrl
			},
			handleSizeChange(val) {
				this.limit = val
				this.guangaoSelect()
			},
			handleCurrentChange(val) {
				this.page = val
				this.guangaoSelect()
			},
			// 刷新
			refresh() {
				this.choicenSelect()
				this.handpick()
			},
			// 添加素材弹框
			advertNotice() {
				this.dialogFormVisible = true
			},
			// 添加
			advertNoticeTo() {
				if (this.title == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入标题',
						type: 'warning'
					})
					return
				}
				if (this.message == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入内容',
						type: 'warning'
					})
					return
				}
				this.$http({
					url: this.$http.adornUrl('admin/errand/insertAdminTrainingCenter'),
					method: 'post',
					data: this.$http.adornData({
						'title': this.title,
						'message': this.message,
            'type':this.type1,
            'deleteFlag': '0',
					})
				}).then(({
					data
				}) => {
					this.dialogFormVisible = false
					this.$message({
						message: '操作成功',
						type: 'success',
						duration: 1500,
						onClose: () => {
							this.title = ''
							this.message = ''
							this.guangaoSelect()
						}
					})
				})
			},
			// 点击修改
			advertUpdates(rows) {
				this.form.id = rows.trainingId
				this.form.title = rows.title
				this.form.message = rows.message
				this.form.deleteFlag = rows.deleteFlag
        this.form.type = rows.type
				this.dialogFormVisible1 = true
			},
			// 确定修改
			amendadvertTo() {
				if (this.form.title == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入标题',
						type: 'warning'
					})
					return
				}
				if (this.form.message == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入内容',
						type: 'warning'
					})
					return
				}
				this.$http({
					url: this.$http.adornUrl('admin/errand/updateAdminTrainingCenter'),
					method: 'post',
					data: this.$http.adornData({
						'deleteFlag': this.form.deleteFlag,
						'trainingId': this.form.id,
						'title': this.form.title,
						'message': this.form.message,
            'type':this.form.type
					})
				}).then(({
					data
				}) => {
					if (data.code == 0) {
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.guangaoSelect()
								this.dialogFormVisible1 = false
							}
						})
					} else {
						this.$notify({
							title: '提示',
							duration: 1800,
							message: data.msg,
							type: 'warning'
						})
					}

				})
			},
			// 删除广告
			advertdeletes(row) {
				this.$confirm(`确定删除此条信息?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$http({
						url: this.$http.adornUrl('admin/errand/deleteAdminTrainingCenter'),
						method: 'post',
						params: this.$http.adornParams({
							'id': row.trainingId
						})
					}).then(({
						data
					}) => {
						if (data.code == 0) {
							this.$message({
								message: '删除成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.guangaoSelect()
								}
							})
						} else {
							this.$notify({
								title: '提示',
								duration: 1800,
								message: data.msg,
								type: 'warning'
							})
						}

					})
				})
			},
			// 获取素材数据
			guangaoSelect() {
				let page = this.page
				this.tableDataLoading = true
				this.$http({
					url: this.$http.adornUrl('admin/errand/trainingCenterList'),
					method: 'get',
					params: this.$http.adornParams({
						'page': this.page,
						'limit': this.limit,
            'title':this.title1,
            'type':this.type
					})
				}).then(({
					data
				}) => {
					this.tableDataLoading = false
					let returnData = data.data
					this.guangaoData = returnData
				})
			},
      animeDat(){
        this.page = 1
        this.guangaoSelect()
      },
      cleans(){
        this.page = 1
        this.title1 = ''
        this.type = ''
        this.guangaoSelect()
      }
		},
		mounted() {
			this.guangaoSelect()
		}
	}
</script>

<style>
	.customWidth {
		width: 80% !important;
	}
</style>
