<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="1200" :close-on-press-escape="false" :close-on-click-modal="false" destroy-on-close @closed="$emit('closed')">
		<el-container v-loading="loading">
			<el-main style="padding:0 20px 20px 20px">
				<el-form ref="dialogForm" :model="form" label-width="100px" label-position="top">
					<el-row :gutter="20">
						<el-col :span="16">
							<el-form-item label="公寓名称" prop="title">
								<el-input v-model="form.title" readonly placeholder="请输入中文描述标题"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="标识" prop="uniqid">
								<el-input v-model="form.uniqid" readonly placeholder="请输入唯一标识"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					
					<x-title title="房间类型"></x-title>

					<x-form-table v-model="form.room" :addTemplate="addTemplate" drag-sort placeholder="暂无数据">
						<el-table-column prop="name" label="名称">
							<template #default="scope">
								<el-input v-model="scope.row.name" placeholder="请输入名称"></el-input>
							</template>
						</el-table-column>
						<el-table-column prop="tags" label="标签">
							<template #default="scope">
								<el-input v-model="scope.row.tags" placeholder="多个标签,号隔开"></el-input>
							</template>
						</el-table-column>
						<el-table-column prop="allowance" label="可预定数量" width="120">
							<template #default="scope">
								<el-input v-model="scope.row.allowance" placeholder="可预定数量"></el-input>
							</template>
						</el-table-column>
						<el-table-column prop="allowance" label="房型" width="120">
							<template #default="scope">
								<el-select v-model="scope.row.type" placeholder="选择">
									<el-option key=1 label="单人间" value=1> 单人间</el-option>
									<el-option key=2 label="双人间" value=2> 双人间</el-option>
									<el-option key=3 label="三人间" value=3> 三人间</el-option>
									<el-option key=4 label="独卫套间" value=4> 独卫套间</el-option>
									<el-option key=5 label="双人套间" value=5> 双人套间</el-option>
									<el-option key=7 label="studio" value=7> studio</el-option>
									<el-option key=6 label="其他" value=6> 其他</el-option>
								</el-select>
							</template>
						</el-table-column>
						<el-table-column prop="price" label="价格" width="120">
							<template #default="scope">
								<el-input v-model="scope.row.price" placeholder="价格"></el-input>
							</template>
						</el-table-column>
						<el-table-column prop="discountprice" label="特价" width="120">
							<template #default="scope">
								<el-input v-model="scope.row.discountprice" placeholder="房间特价"></el-input>
							</template>
						</el-table-column>
						<el-table-column prop="status" label="可预定" width="70" align="center">
							<template #default="scope">
								<el-switch v-model="scope.row.status" active-value="1" inactive-value="0"></el-switch>
							</template>
						</el-table-column>

						<el-table-column prop="videos" label="视频" width="100" align="center">
							<template #default="scope">
								<el-button text size="small" :type="scope.row.videos.length==0?'':'primary'" @click="setVideo(scope.row)">
									视频
									<el-badge :hidden="scope.row.videos.length==0" :value="scope.row.videos.length" class="badge" type="danger"></el-badge>
								</el-button>
							</template>
						</el-table-column>
						<el-table-column prop="images" label="图片组" width="100" align="center">
							<template #default="scope">
								<el-button text size="small" :type="scope.row.images.length==0?'':'primary'" @click="setImage(scope.row)">
									图片
									<el-badge :hidden="scope.row.images.length==0" :value="scope.row.images.length" class="badge" type="danger"></el-badge>
								</el-button>
							</template>
						</el-table-column>
					</x-form-table>

				</el-form>
			</el-main>
			<el-footer>
				<el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
				<el-button @click="visible=false">取消</el-button>
			</el-footer>
		</el-container>
		<images ref="images" @success="function(a,o){o.close()}"></images>
		<videos ref="videos" @success="function(a,o){o.close()}"></videos>
	</el-drawer>
</template>


<script>
	import images from './images'
	import videos from './videos'
	import api from './api'

	export default {
		emits: ['success', 'closed'],
		components: {
			images,
			videos,
		},
		data() {
			return {
				token: null,
				loading: false,
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '公寓房间类型'
				},
				form: {
					room:[],
					title:'',
					uniqid:'',
				},
				addTemplate: {
					name:null,
					tags:null,
					type:null,
					allowance:0,
					price:0,
					discountprice:0,
					status:0,
					videos:[],
					images:[],
				},
				visible: false,
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			close(){
				this.visible = false;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await api.room.submit({room:this.form.room, token:this.token});
						this.isSaveing = false;
						if(res.code == 200){
							// this.$emit('success', this.form, this)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){

				this.loading = true
				const params = {
					id: data.id,
					uniqid: data.uniqid,
				}
				this.form.title = data.title;
				this.form.uniqid = data.uniqid;
				setTimeout(async ()=>{
					var res = await api.room.get(params)
					this.loading = false
					this.form.room = res.data.data;
					this.token = res.data.token;
				},400)

			},
			//设置过滤项
			setVideo(o){
				this.$nextTick(() => {
					this.$refs.videos.open().setData(o)
				})
			},
			//设置过滤项
			setImage(o){
				this.$nextTick(() => {
					this.$refs.images.open().setData(o)
				})
			}
		}
	}
</script>

<style>
</style>
