<template>
<el-drawer title="店铺商品" v-model="visible" :size="1200" 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="add">添加商品</el-button>
		</div>
	</el-header>
	<el-main>
		<scTable tableName="member" ref="table" :apiObj="list.apiObj" :column="list.column" row-key="id" :params="search" border stripe>
			<template #cover="scope">
				<img :src="scope.row.cover" width="40" height="40" v-if="scope.row.cover" />
			</template>
			<el-table-column label="操作" fixed="right" align="center" width="80">
				<template #default="scope">
					<el-button-group>
						<el-popconfirm title="确定移除吗？" @confirm="table_del(scope.row, scope.$index)">
							<template #reference>
								<el-button type="danger">移除</el-button>
							</template>
						</el-popconfirm>
					</el-button-group>
				</template>
			</el-table-column>
		</scTable>
	</el-main>
	<add-goods v-if="dialog.addgoods" ref="addgoodsBox" @success="upsearch" @closed="dialog.addgoods = false" />
</el-drawer>
</template>

<script>
import addGoods from './addGoods.vue';

export default {
	emits: ['success', 'closed'],
	components: {addGoods},
	data() {
		return {
			visible: false,
			isSaveing: false,
			loading: false,
			dialog: {
				addgoods: false
			},
			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: 'created_at', label: '添加时间', width: 140},
					{prop: 'updated_at', label: '更新时间', width: 140},
				],
			},
			search: {store_id: 0},
			store: []
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(id){
			this.visible = true;
			return this;
		},
		upsearch(){
			this.$refs.table.reload(this.search);
		},
		async table_del(row){
			let res = await this.$API.store.goods.delete.post({goods_id: row.id, store_id: this.search.store_id});
			if(res.code == 1){
				this.$message.success("删除成功");
				this.upsearch();
			}else{
				this.$message.error(res.message)
			}
		},
		async add(){
			this.dialog.addgoods = true;
			this.$nextTick(() => {
				this.$refs.addgoodsBox.open().setData({store_id: this.search.store_id});
			});
		},
		//表单注入数据
		setData(data){
			this.search.store_id = data.id;
		}
	}
}
</script>
