<template>
	<el-dialog width="70%" class="goods_popups" title="选择商品" :visible.sync="goodsDialog" :append-to-body="true">
		<div class="dflex_sb">
			<div></div>
			<div class="dflex_vertical_c margin_b_20">
				<div class="search_name">商品名称：</div>
				<el-input v-model="req.name" placeholder="请输入" class="search_input" @input="search" clearable></el-input>
			</div>
		</div>
		<el-table ref="goods" height="60vh" :data="tableDatas" highlight-current-row @selection-change="selectionChange" @row-click="choice">
			<el-table-column type="selection"></el-table-column>
			<el-table-column label="商品" width="400" align="center">
				<template slot-scope="scope">
					<div class="dflex_vertical_c">
						<el-image class="e_table_side_img100 margin_r_10" :src="scope.row.imgs" fit="cover"></el-image>
						<div class="left_just">
							<div class="ellipsis">{{ scope.row.name }}</div>
						</div>
					</div>
				</template>
			</el-table-column>
			<el-table-column label="价格" align="center">
				<template slot-scope="scope">
					<div class="price">{{ scope.row.price}}</div>
					<div class="m_price">{{ scope.row.marketPrice}}</div>
				</template>
			</el-table-column>
			<el-table-column label="状态" align="center">
				<template slot-scope="scope">
					<template v-if="scope.row.state==0">下架</template>
					<template v-if="scope.row.state==1">在售</template>
					<template v-if="scope.row.state==2">待审核</template>
				</template>
			</el-table-column>
			<el-table-column label="数据统计" align="left" width="100">
				<template slot-scope="scope">
					<div class="left_just">
						<div>已售数:{{ scope.row.saleCnt }}</div>
						<div>访问数:{{ scope.row.visitCnt }}</div>
						<div>收藏数:{{ scope.row.collectCnt }}</div>
						<!-- <div>分享数:{{ scope.row.shareCnt }}</div> -->
					</div>
				</template>
			</el-table-column>
			<el-table-column label="标签" align="center" width="220">
				<template slot-scope="scope">
					<el-tag effect="plain" type="info" class="margin_r_5 margin_t_5" v-for="(item, i) in scope.row.goodsServiceTags" :key="i">{{ item.tagName }}</el-tag>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			:current-page="req.page"
			:page-sizes="[10, 20, 30, 50, 100]"
			:page-size="req.rows"
			layout="total, sizes, prev, pager, next, jumper"
			:total="tableTotal"
			@size-change="sizeChange"
			@current-change="currentChange"
		></el-pagination>
		<div class="btn pos_a">
			<el-button class="confirm_btn" @click="confirm">确 定</el-button>
			<el-button @click="goodsDialog = false">取 消</el-button>
		</div>
	</el-dialog>
</template>

<script>
const __name = 'usemall-goods';
export default {
	// props: {
	//     limited: {
	//       type: String,
	//       required: true
	//     }
	//   },
	data() {
		return {
			goodsDialog: false,
			where_obj: {},
			req: {
				page: 1,
				rows: 10,
				orderby: 'sort asc',
				name: ''
			},
			tableDatas: [],
			tableTotal: 0,
			selectDatas: [],
			limited:'',
			hot:''
		};
	},
	methods: {
		loadData() {
			this.$axios.get("/goods/admin/list",
			{
				params:{
					'curPage':this.req.page,
					'pageSize':this.req.rows,
					'goodsName':this.req.name,//商品名称搜索
					'limited':this.limited,//是否限时精选;0否1是
					'hot':this.hot,//是否热门;0否1是
				},
				headers:{
					'Mall-Token': uni.getStorageSync("token")
				}
			}).then(response => {
				let res = response
				console.log(res)
				if (res.success) {
					this.tableDatas = res.data.list;
					this.tableTotal = res.data.totalCount;
				} else {
				}
			}).catch(res =>{
			});
			// this.$db[__name]
			// 	.whereif(this.req.name, { name: new RegExp(this.req.name) })
			// 	.where(this.where_obj)
			// 	.totable(this.req)
			// 	.then(res => {
			// 		if (res.code == 200) {
			// 			this.tableDatas = res.datas.rows;
			// 			this.tableTotal = res.datas.total;
			// 		}
			// 	});
		},
		search(e) {
			this.req.page = 1;
			this.req.name=e
			this.loadData();
		},
		show(data) {
			this.limited=data.limited
			this.hot=data.hot
			for (const key in data) {
				if (data.hasOwnProperty(key)) {
					this.where_obj[key] = data[key];
				}
			}
			this.goodsDialog = true;
			this.loadData();
		},
		choice(row) {
			this.$refs.goods.toggleRowSelection(row);
		},
		selectionChange(val) {
			this.selectDatas = val;
		},
		confirm() {
			if (this.selectDatas.length <= 0) {
				this.$message('请选择商品');
				return;
			}

			this.$emit('choiceGoods', this.selectDatas);
			let data = this.selectDatas.map(x => x.id);
			if(this.limited==0){
				var type='2'
			}else if(this.hot==0){
				var type='1'
			}
			this.$axios.put("/goods/admin/hot-limited-change",
			{
				"action": type,//1热门推荐、2限时精选
				"goodIds": data,
				"type": 1//1新增、0取消
			},
			{
				headers:{
					'Mall-Token': uni.getStorageSync("token")
				}
			}).then(response => {
				let res = response
				if (res.success) {
					this.loadData();
					this.goodsDialog = false;
				} else {
				}
			}).catch(res =>{
			});
			
		},
		sizeChange(size) {
			this.req.rows = size;
			this.loadData();
		},
		currentChange(current) {
			this.req.page = current;
			this.loadData();
		}
	}
};
</script>

<style>
.goods_popups .btn {
	bottom: 20px;
	right: 20px;
}
</style>
