<template>
  <div>
    <QueryListPage ref="queryListPage" :pager-config="pagerConfig" :query-form="queryForm" :query-list="queryList" />
  </div>
</template>

<script>
import { publishLessonList, delPublishLesson, putLessonOnLine, putLessonOffLine } from '@/api/jmap/lesson';
import { getSkinCodeList } from '@/api/management/mapskin';
import localStore from 'storejs';

export default {
	name: 'PublishMap',
	data() {
		return {
			cityList: [],
			skinCodeList: [],
			pagerConfig: {
				pageSize: 'pageSize',
				pageIndex: 'pageNum'
			},
			queryForm: {
				labelWidth: '80px',
				reset: true,
				queryObject: {
					name: {
						type: 'text',
						label: this.$t('global.name')
					},
					cityCode: {
						type: 'select',
						label: this.$t('publish.city'),
						config: {
							data: []
						}
					}
				}

			},
			queryList: {
				query: publishLessonList,
				selectCheckShow: false,
				indexShow: true,
				columns: [
					{
						title: this.$t('global.name'),
						prop: 'name'
					},
					{
						title: this.$t('publish.city'),
						prop: 'cityCode',
						type: 'tag',
						columnValue: (row) => { return this.$convertField(row.cityCode, this.cityList, ['code', 'name']); },
						tagType: (row) => { return 'success'; }
					},
					{
						title: this.$t('publish.lessonIntroduction'),
						prop: 'remarks'
					},
					{
						type: 'button',
						title: this.$t('global.operate'),
						width: '250',
						buttons: [
							{
								name: this.$t('global.putaway'),
								handleClick: this.handlePutaway,
								type: '',
								showControl: (row) => { return row.status != 1; }
							},
							{
								name: this.$t('global.soldOut'),
								handleClick: this.handleSoldOut,
								type: 'warning',
								showControl: (row) => { return row.status == 1; }
							},
							{
								name: this.$t('global.delete'),
								handleClick: this.handleDelete,
								type: 'danger',
								showControl: () => { return this.isShow != -1; }
							}
						]
					}
				]
			},

			currentModel: {}
		};
	},
	computed: {
		isShow() {
			return this.$store.getters['roles'].indexOf('05');
		}
	},
	created() {
		this.loadInitData();
	},
	methods: {
		loadInitData() {
			this.cityList = [];
			this.$Dictionary.cityType().then(list => {
				this.cityList = list;
				this.cityList.forEach(elem => {
					this.queryForm.queryObject.cityCode.config.data.push({ value: elem.code, label: elem.name });
				});
			}).catch(() => {
				this.$messageBox(this.$t('error.loadingCityListFailed'));
			});

			this.skinCodeList = [];
			getSkinCodeList().then(response => {
				this.skinCodeList = response.data;
			});
		},
		// 编辑
		handleEdit(index, row) {
		},

		// 删除
		handleDelete(index, row) {
			this.$confirm(this.$t('publish.wellDelType'), this.$t('global.tips'), {
				confirmButtonText: this.$t('global.confirm'),
				cancelButtonText: this.$t('global.cancel'),
				type: 'warning'
			}).then(() => {
				delPublishLesson(row.id).then(response => {
					this.$message.success(this.$t('publish.deleteSuccess'));
					this.reloadTable();
					localStore.remove('mapId');
				}).catch(() => {
					this.reloadTable();
					this.$messageBox(this.$t('error.deleteFailed'));
				});
			}).catch(() => { });
		},
		handlePutaway(index, row) {
			this.$confirm(this.$t('publish.wellPutawayTraining'), this.$t('global.tips'), {
				confirmButtonText: this.$t('global.confirm'),
				cancelButtonText: this.$t('global.cancel'),
				type: 'warning'
			}).then(() => {
				putLessonOnLine(row.id).then(response => {
					this.$message.success(this.$t('publish.operationSuccess'));
					this.reloadTable();
				}).catch(() => {
					this.reloadTable();
					this.$messageBox(this.$t('error.operationFailure'));
				});
			}).catch(() => { });
		},

		handleSoldOut(index, row) {
			this.$confirm(this.$t('publish.wellSoldOutTraining'), this.$t('global.tips'), {
				confirmButtonText: this.$t('global.confirm'),
				cancelButtonText: this.$t('global.cancel'),
				type: 'warning'
			}).then(() => {
				putLessonOffLine(row.id).then(response => {
					this.$message.success(this.$t('publish.operationSuccess'));
					this.reloadTable();
				}).catch(() => {
					this.reloadTable();
					this.$messageBox(this.$t('error.operationFailure'));
				});
			}).catch(() => { });
		},

		reloadTable() {
			this.queryList.reload();
		}
	}
};
</script>
