<template>
	<el-container>
		<el-header class="header-tabs">
			<el-tabs type="card" v-model="search.status" @tab-change="tabChange">
				<el-tab-pane label="所有" name="0"></el-tab-pane>
				<el-tab-pane label="在线公寓" name="1"></el-tab-pane>
				<el-tab-pane label="已下架" name="2"></el-tab-pane>
				<el-tab-pane label="短租公寓" name="3"></el-tab-pane>
			</el-tabs>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="关键词" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<xTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" :hideSetting="true" remoteSort remoteFilter stripe>
				<el-table-column type="selection" width="30"></el-table-column>
				<el-table-column label="ID" prop="id" width="50"></el-table-column>
				<el-table-column label="" prop="status" width="38">
					<template #default="scope">
						<x-status-indicator v-if="scope.row.status==1" pulse type="success"></x-status-indicator>
						<x-status-indicator v-if="scope.row.status==0" pulse type="info"></x-status-indicator>
					</template>
				</el-table-column>
				<el-table-column label="公寓名称" prop="title"  minWidth="300">
					<template #default="scope">
						<img :src="scope.row.image" height="25" style="border-radius: 8px;"> {{ scope.row.title }}
					</template>
				</el-table-column>
				<el-table-column label="图组" prop="attachment" width="60">
					<template #default="scope">
						<el-link :underline="false" size="small" @click="setImage(scope.row)">
							<el-badge :value="scope.row.attachment.length" type="danger"></el-badge>
						</el-link>
					</template>
				</el-table-column>
				<el-table-column label="视频" prop="video" width="60">
					<template #default="scope">
						<el-link :underline="false" size="small" @click="setVideo(scope.row)">
							<el-badge :value="Object.keys(scope.row.video||{}).length" type="warning"></el-badge>
						</el-link>
					</template>
				</el-table-column>
				<el-table-column label="直播" prop="live" width="60">
					<template #default="scope">
						<el-link :underline="false" size="small" @click="setLive(scope.row)">
							<el-badge :value="Object.keys(scope.row.live||{}).length" type="info"></el-badge>
						</el-link>
					</template>
				</el-table-column>
				<el-table-column label="客服ID" prop="customerservice" width="80"></el-table-column>
				<el-table-column label="品牌" prop="company" width="180"></el-table-column>
				<el-table-column label="租金" prop="rentprice" width="80"></el-table-column>
				<el-table-column label="排序" prop="sort" width="100"></el-table-column>
				<el-table-column label="添加时间" prop="timestamp" width="150"></el-table-column>
				<el-table-column label="浏览" prop="viewnum" width="90"></el-table-column>

				<el-table-column label="操作" fixed="right" align="left" width="195">
					<template #default="scope">
						<el-button-group>
							<el-button type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-button type="warning" size="small" @click="table_room(scope.row, scope.$index)">房型({{ scope.row.room }})</el-button>
							<el-button type="danger" size="small" @click="open(scope.row)">海报</el-button>
							<!-- <el-popconfirm title="确定下架吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button type="info" size="small">下架</el-button>
								</template>
							</el-popconfirm> -->
						</el-button-group>
					</template>
				</el-table-column> 
			</xTable>
		</el-main>
	</el-container>

	<room-dialog v-if="dialog.room" ref="roomDialog" @success="handleSuccess" @closed="dialog.room=false"></room-dialog>
	<update v-if="dialog.update" ref="update" @success="handleSuccess" @closed="dialog.update=false"></update>
	<images v-if="dialog.images" ref="images" @success="attachmenthandleSuccess" @closed="dialog.images=false"></images>
	<videos v-if="dialog.videos" ref="videos" @success="attachmenthandleSuccess" @closed="dialog.videos=false"></videos>
	<lives v-if="dialog.lives" ref="lives" @success="attachmenthandleSuccess" @closed="dialog.lives=false"></lives>
</template>

<script>
	import roomDialog from './room'
	import update from './update'
	import images from './images'
	import videos from './videos'
	import lives from './lives'
	import api from './api'

	export default {
		name: 'apartment',
		components: {
			roomDialog,
			update,
			videos,
			images,
			lives,
		},
		data() {
			return {
				dialog: {
					room: false,
					update: false,
					images: false,
					videos: false,
					lives: false,
				},
				apiObj: api.list,
				selection: [],
				search: {
					keyword: "",
					status:'0',
				}
			}
		},
		mounted() {

		},
		methods: {
			open(o){
				window.open(o.apartmentposter) 
			},
			//标签切换
			tabChange(status){
				this.search.status = status;
				this.$refs.table.reload(this.search)
			},
			//搜索
			upsearch(){
				this.$refs.table.reload(this.search)
			},
			//增加
			add(){
				this.dialog.update = true
				this.$nextTick(() => {
					this.$refs.update.open().setConfig()
				})
			},
			setImage(o){
				this.dialog.images = true
				this.$nextTick(() => {
					this.$refs.images.open().remoteData(o)
				})
			},
			setVideo(o){
				this.dialog.videos = true
				this.$nextTick(() => {
					this.$refs.videos.open().remoteData(o)
				})
			},
			setLive(o){
				this.dialog.lives = true
				this.$nextTick(() => {
					this.$refs.lives.open().remoteData(o)
				})
			},
			//编辑
			table_edit(row){
				this.dialog.update = true
				this.$nextTick(() => {
					this.$refs.update.open('edit').setConfig().setData(row)
				})
			},
			//房源类型
			table_room(row){
				this.dialog.room = true
				this.$nextTick(() => {
					this.$refs.roomDialog.open('edit').setData(row)
				})
			},
			//删除
			async table_del(row, index){
				var reqData = {id: row.id}
				var res = await api.post('delete', reqData);
				if(res.code == 200){
					//这里选择刷新整个表格 OR 插入/编辑现有表格数据
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定下架选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},

			async attachmenthandleSuccess(data, o){
				var res = await api.post('attachmentSubmit', data)		
				if (res.code==200) {		
					o.close();
					this.$refs.table.refresh()
					this.$message.success("操作成功")		
					return ;		
				}		
				this.$alert(res.message, "提示", {type: 'error'})		
			},
			//本地更新数据
			async handleSuccess(){

				this.$refs.table.refresh()
			}
		}
	}
</script>

<style>
</style>
