<template>
	<div>
		<div class="crumbs">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>
					<i class="el-icon-discount"></i> Suggestion list
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="container">
			<div class="handle-box">
				<el-select v-model="query.problem_id" filterable style="margin-right: 20px;" @change="getSelect()">
					<el-option :key="-1" label="All Classification" :value="-1"></el-option>
					<el-option v-for="item in dysfuncte" :key="item.problem_id" :label="item.problem_title" :value="item.problem_id"></el-option>
				</el-select>
				<el-select v-model="query.suggest_state" filterable @change="getSelect()">
					<el-option :key="-1" label="All State" :value="-1"></el-option>
					<el-option v-for="item in dystate" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</div>
			<el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header" empty-text="New messages (0)">
				<el-table-column label="Status" width="115px" align="center">
					<template slot-scope="scope">
						<el-tag v-if="scope.row.suggest_state==1" type="success">Reviewed</el-tag>
						<el-tag v-if="scope.row.suggest_state==0" type="danger">Not approved</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="Time" width="90px" align="center">
					<template slot-scope="scope">
						{{formatDate(scope.row.suggest_ctime)}}
					</template>
				</el-table-column>
				<el-table-column prop="realname" label="Realname"></el-table-column>
				<el-table-column prop="problem_title" label="Problem classification"></el-table-column>
				<el-table-column prop="suggest_desc" label="Problem description"></el-table-column>
				<el-table-column label="Description picture" align="center" width="150">
					<template slot-scope="scope">
						<el-image @click="vbs(mediaUrl+scope.row.suggest_url)" :preview-src-list="[mediaUrl+scope.row.suggest_url]" v-if="scope.row.suggest_url!=''"
						 :src="mediaUrl+scope.row.suggest_url" style="width: 128px;height: 80px;"></el-image>
					</template>
				</el-table-column>
				<el-table-column label=" " width="210">
					<template slot-scope="scope">
						<el-button plain type="primary" icon="el-icon-tickets" @click="handleDetail(scope.row)">Detail</el-button>
						<el-button v-if="scope.row.suggest_state==0" plain type="warning" icon="el-icon-edit-outline" @click="handleEdit(scope.row)">Examine</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="pagination">
				<el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex" :page-size="query.pageSize"
				 :total="link_Total" @current-change="handlePageChange"></el-pagination>
			</div>
		</div>

		<!-- 详情弹出框 -->
		<el-dialog title="Details of recommendations" :visible.sync="detailVisible" width="700px"  :close-on-click-modal="false">
			<el-form ref="edit_Form" :model="detailForm" label-width="160px">
				<el-form-item label="Realname :">
					{{detailForm.realname}}
				</el-form-item>
				<el-form-item label="Status :">
					<el-tag v-if="detailForm.suggest_state==1" type="success">Reviewed</el-tag>
					<el-tag v-if="detailForm.suggest_state==0" type="danger">Not approved</el-tag>
				</el-form-item>
				<el-form-item label="Time :">
					{{formatDate(detailForm.suggest_ctime)}}
				</el-form-item>
				<el-form-item label="Problem classification :">
					{{detailForm.problem_title}}
				</el-form-item>
				<el-form-item label="Problem description :">
					{{detailForm.suggest_desc}}
				</el-form-item>
				<el-form-item label="Description picture :" v-if="detailForm.suggest_url!=''">
					<el-image :src="mediaUrl+detailForm.suggest_url"></el-image>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button type="primary" @click="detailVisible = false">OK</el-button>
			</span>
		</el-dialog>

	</div>
</template>

<script>
	export default {
		data() {
			return {
				mediaUrl: this.Common.mediaUrl,
				tableData: [],
				detailForm: {},
				query: {
					problem_id: -1,
					suggest_state: -1,
					pageIndex: 1,
					pageSize: 10
				},
				link_Total: 0,
				dysfuncte: [],
				dystate: [{
					value: 1,
					label: 'Reviewed'
				}, {
					value: 0,
					label: 'Not approved'
				}],
				detailVisible: false,
				srcList: [],
			};
		},
		mounted() {

		},
		created() {
			this.getDate()
		},
		methods: {
			// 获取数据
			getDate() {
				this.$api
					.post('api/Suggest/getSuggestList', this.query)
					.then(res => {
						if (res.code == 0) {
							this.tableData = res.data.data;
							this.link_Total = res.data.total || 0;
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
				this.$api
					.post('api/Suggest/getProblem', this.proForm)
					.then(res => {
						if (res.code == 0) {
							this.dysfuncte = res.data
						} else {
							this.$message.error(res.msg);
						}
					})
					.catch(err => {
						this.$message.error(err);
					});
			},
			// 下拉筛选
			getSelect(){
				this.getDate() 
			},
			// 详情操作
			handleDetail(row) {
				this.detailForm = Object.assign({}, row);
				this.detailVisible = true;
			},
			// 编辑操作
			handleEdit(row) {
				// 二次确认更改状态
				this.$confirm('Are you sure you want to review this suggestion？', 'Tip', {
						type: 'warning'
					})
					.then(() => {
						this.$api
							.post('api/Suggest/updateSuggest', row)
							.then(res => {
								if (res.code == 0) {
									this.$message.success('Audit successful!');
									this.getDate();
								} else {
									this.$message.error(res.msg);
								}
							})
							.catch(err => {
								this.$message.error(err);
							});
					})
					.catch(() => {});
			},


			// 分页导航
			handlePageChange(val) {
				this.$set(this.query, 'pageIndex', val);
				this.getDate();
			},

			// 时间转换
			formatDate(timestamp) {
				var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
				var Y = date.getFullYear() + '-';
				var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
				var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
				var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
				var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
				var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
				return Y + M + D;
			},

			// 大图预览
			vbs(val) {
				this.srcList = []
				this.srcList.push(val)
			}

		},
		computed: {

		},
		watch: {

		}
	};
</script>

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