<template>
	<div>
		<use-table ref="tbl"></use-table>
		<div class="container_status dflex_vertical_c">
			<div class="item_interval" v-for="(item, i) in stateDatas" :key="i">
				<el-button class="badge_txt" :class="{ active: item.cnt == req.state }" size="small" @click="cutSta(item)">{{ item.name }}</el-button>
				<!-- <v-countup class="badge" start-value="0" :end-value="item.cnt"></v-countup> -->
			</div>
		</div>
		<div class="container padding_b_0">
			<div class="dflex_wrap">
				<div class="dflex_vertical_c margin_r_40 margin_b_20">
					<div class="search_name">商品名称：</div>
					<el-input v-model="req.name" placeholder="请输入" @input="loadData" class="search_input"></el-input>
				</div>
				<el-button size="mini" class="search_btn margin_b_20 margin_r_40" @click="loadData">搜索</el-button>
			</div>
		</div>
		<div class="container use-table">
			<div class="dflex_sb margin_b_15">
				<div></div>
				<el-button class="add_btn pos_r padding0" icon="iconfont iconxinzeng" @click="toAdd">新增</el-button>
			</div>
			<el-table :height="tblHeight" :data="tableDatas" highlight-current-row>
				<!-- <el-table-column label="排序" width="200" align="center">
					<template slot-scope="scope">
						<el-input-number size="small" :min="1" v-model="scope.row.sort" @change="sortChange(scope.row)" @blur="sortChange(scope.row)"></el-input-number>
					</template>
				</el-table-column> -->
				<el-table-column label="商品" width="330" align="center">
					<template slot-scope="scope">
						<div class="dflex_vertical_c">
							<el-image v-if="scope.row.imgs.search(',')==(-1)" style="width: 100px;height: 80px;" :src="scope.row.imgs || require('static/img/noimage.png')" :preview-src-list="scope.row.imgs" fit="contain"></el-image>
							<el-image v-else style="width: 100px;height: 80px;" :src="((scope.row.imgs).substring(0, ((scope.row.imgs).indexOf(',')))) || require('static/img/noimage.png')" :preview-src-list="scope.row.imgs" fit="contain"></el-image>
							<div class="left_just margin_l_10">
								<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="150">
					<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">
					<template slot-scope="scope">
						<el-tooltip :content="scope.row.hot == '1' ? '点击取消热门推荐' : '点击启用热门推荐'" placement="top" :hide-after="1000" :enterable="false" effect="light">
							<el-switch
								v-model="scope.row.hot"
								active-color="#ff6a6c"
								inactive-color="#bbb"
								active-value="1"
								inactive-value="0"
								@change="hotChange(scope.row)"
							></el-switch>
						</el-tooltip>
					</template>
				</el-table-column>
				<el-table-column label="标签" width="220" align="center">
					<template slot-scope="scope">
						<div class="left_just">
							<el-tag effect="plain" class="margin_t_5 margin_r_5" type="info" v-for="(item, i) in scope.row.goodsServiceTags" :key="i">{{ item.tagName }}</el-tag>
						</div>
					</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>
			<popupsGoods ref="child" @choiceGoods="choiceGoods"></popupsGoods>
		</div>
	</div>
</template>

<script>
import vCountup from 'vue-countupjs';
import popupsGoods from '@/components/popups_goods';
const __name = 'usemall-goods';
export default {
	components: { vCountup, popupsGoods },
	data() {
		return {
			state1: 1,//0下架 1在售 2 待审核
			stateObj: {
				待审核: '2',
				销售中: '1',
				已下架: '0',
				全部: ''
			},
			stateDatas: [],
			req: {
				hot: 1,
				page: 1,
				rows: 10,
				orderby: 'sort asc',
				name: '',
				state: ''
			},
			tblHeight: 0,
			tableDatas: [],
			tableTotal: 0
		};
	},
	methods: {
		loadData(e) {
			var _self=this
			_self.stateDatas = [];
			for (let key in _self.stateObj) {
				_self.stateDatas.push({
					name: key,
					cnt: _self.stateObj[key]
				});
			}
			_self.$axios.get("/goods/admin/list",
			{
				params:{
					'curPage':_self.req.page,
					'pageSize':_self.req.rows,
					'goodsName':e,//商品名称搜索
					'state':_self.req.state,//商品状态0下架 1在售 2 待审核
					'hot':1,//是否热门;0否1是
				},
				headers:{
					'Mall-Token': uni.getStorageSync("token")
				}
			}).then(response => {
				let res = response
				console.log(res)
				if (res.success) {
					// if (res.data.list && res.data.list.state) {
					// 	_self.stateDatas = res.data.list.state
					// }
					
					res.data.list.forEach(row => {
						// row.imgs = [row.imgs];
						// row.limited = row.limited + '';
					});
					
					_self.tableDatas = res.data.list;
					_self.tableTotal = res.data.totalCount;
				} else {
				}
			}).catch(res =>{
			});
			
			// await this.$db[__name]
			// 	.where({ hot: 1 })
			// 	.whereif(this.req.state && this.req.state != '全部', { state: this.req.state })
			// 	.whereif(this.req.name, { name: new RegExp(this.req.name) })
			// 	.withgroup({ field: 'state', obj: this.stateObj })
			// 	.totable(this.req)
			// 	.then(res => {
			// 		if (res.code == 200) {
			// 			if (res.datas.group && res.datas.group.datas) {
			// 				this.stateDatas = res.datas.group.datas;
			// 			}

			// 			res.datas.rows.forEach(row => {
			// 				row.imgs = [row.img];
			// 				row.hot = row.hot + '';
			// 			});

			// 			this.tableDatas = res.datas.rows;
			// 			this.tableTotal = res.datas.total;
			// 		}
			// 	});
		},
		toAdd() {
			this.$refs.child.show({
				hot: 0
			});
		},
		choiceGoods(datas) {
			let ids = datas.map(x => x._id);
			if (ids && ids.length > 0) {
				// this.$db[__name]
				// 	.where(`in(_id, ${JSON.stringify(ids)})`)
				// 	.update('', { hot: 1 })
				// 	.then(res => {
				// 		if (res.code == 200) this.loadData();
				// 	});
			}
		},
		hotChange(row) {
			if(row.hot=='1'){
				var type=1
			}else if(row.hot=='0'){
				var type=0
			}
			this.$axios.put("/goods/admin/hot-limited-change",
			{
				"action": "1",//1热门推荐、2限时精选
				"goodIds": [row.id],
				"type": type//1新增、0取消
			},
			{
				headers:{
					'Mall-Token': uni.getStorageSync("token")
				}
			}).then(response => {
				let res = response
				if (res.success) {
					this.loadData();
				} else {
				}
			}).catch(res =>{
			});
			// this.$db[__name].update(row._id, { hot: parseInt(row.hot) }).then(res => {
			// 	if (res.code == 200) this.loadData();
			// });
		},
		sortChange(row) {
			if (row.sort == '') {
				return;
			}
		},
		cutSta(item) {
			this.state1 = item.cnt;
			this.req.state = item.cnt;
			
			this.loadData();
		},
		sizeChange(size) {
			this.req.rows = size;
			this.loadData();
		},
		currentChange(current) {
			this.req.page = current;
			this.loadData();
		}
	},
	created() {
		this.loadData();
	},
	updated() {
		if (!this.tblHeight) {
			this.tblHeight = this.$refs.tbl.tblHeight;
		}
	}
};
</script>

<style></style>
