<template>
<el-drawer title="添加店铺商品" v-model="visible" :size="900" destroy-on-close :close-on-click-modal="false" @closed="$emit('closed')">
	<el-header>
		<div class="left-panel">
			<el-input v-model="search.keyword" placeholder="关键词" style="width:150px; margin-right: 10px;" clearable />
			<sc-select-tree v-model="search.category_id" :apiObj="$API.goods.category.list" :props="{label: 'title', value: 'id'}" />
			<el-button-group style="margin-left: 10px;">
				<el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
			</el-button-group>
		</div>
		<div class="right-panel">
			<el-button type="danger" @click="save">确认添加</el-button>
		</div>
	</el-header>
	<el-main>
		<scTable tableName="member" ref="table" :apiObj="list.apiObj" :column="list.column" row-key="id" @selection-change="selectionChange" border stripe>
			<el-table-column type="selection" width="50"></el-table-column>
			<template #cover="scope">
				<img :src="scope.row.cover" width="40" height="40" v-if="scope.row.cover" />
			</template>
			<template #status="scope">
				<el-tag :type="scope.row.status == 1 ? 'success' : 'danger'">{{scope.row.status == 1 ? '上架' : '下架'}}</el-tag>
			</template>
		</scTable>
	</el-main>
</el-drawer>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data(){
		return {
			visible: false,
			isSaveing: false,
			loading: false,
			store_id: 0,
			search: {},
			list: {
				apiObj: this.$API.goods.lists.list,
				column: [
					{prop: 'id', label: 'ID', width: 80},
					{prop: 'title', label: '商品名称'},
					{prop: 'cover', label: '封面图', width: 120},
					{prop: 'price', label: '售价', width: 120},
					{prop: 'original_price', label: '原价', width: 120},
					{prop: 'status', label: '状态', width: 80},
					{prop: 'created_at', label: '添加时间', width: 140},
					{prop: 'updated_at', label: '更新时间', width: 140},
				],
			},
			selection: []
		}
	},
	mounted(){
	},
	methods:{
		//显示
		open(id){
			this.visible = true;
			return this;
		},
		upsearch(){
			this.$refs.table.reload(this.search);
		},
		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		async save(){
			let goods = this.selection.map(item => item.id)
			if(!goods.length){
				this.$message.error("请选择商品")
				return false
			}
			let res = await this.$API.store.goods.add.post({goods: goods, store_id: this.store_id});
			if(res.code == 1){
				this.$message.success("添加成功");
				this.$emit('success');
				this.visible = false;
			}else{
				this.$message.error(res.message)
			}
		},
		//表单注入数据
		setData(data){
			this.store_id = data.store_id;
		}
	}
}
</script>

<style scoped>
</style>
