<template>
	<div>
		<use-table ref="tbl"></use-table>
		<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="请输入" class="search_input" @input="loadData"></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" row-key="id" :tree-props="{ children: 'childCategory', hasChildren: 'hasChildren' }">
				<el-table-column prop="name" label="分类名称" width="120"></el-table-column>
				<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 prop="id" label="分类ID" align="center" width="250"></el-table-column>
				<el-table-column label="图片" align="center">
					<template slot-scope="scope">
						<el-image style="width: 100px; height: 80px;" :preview-src-list="scope.row.imgs" :src="scope.row.icon || require('static/img/noimage.png')" fit="contain"></el-image>
					</template>
				</el-table-column>
				<!-- <el-table-column prop="create_time" label="创建时间" align="center"></el-table-column> -->
				<el-table-column label="状态" align="center">
					<template slot-scope="scope">
						<el-tooltip :content="scope.row.state == '0' ? '点击启用' : '点击禁用'" placement="top" :hide-after="1000" :enterable="false" effect="light">
							<el-switch
								v-model="scope.row.state"
								active-color="#ff6a6c"
								inactive-color="#bbb"
								active-value="1"
								inactive-value="0"
								@change="stateChange(scope.row)"
							></el-switch>
						</el-tooltip>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center">
					<template slot-scope="scope">
						<el-tooltip content="编辑" placement="top" :hide-after="1000" :enterable="false" effect="light">
							<el-button type="text" icon="iconfont iconbianji" class="bbb" @click="toEdit(scope.row)"></el-button>
						</el-tooltip>
						<el-tooltip content="删除" placement="top" :hide-after="1000" :enterable="false" effect="light">
							<el-button type="text" icon="iconfont iconshanchu" class="bbb" @click="remove(scope.row.id)"></el-button>
						</el-tooltip>
					</template>
				</el-table-column>
			</el-table>
		</div>
	</div>
</template>

<script>
	import {
		delgoodslei
	} from '@/util/loginJie.js'
const __name = 'usemall-goods-category';
export default {
	data() {
		return {
			req: {
				page: 1,
				rows: 500,
				orderby: 'sort asc',
				name: ''
			},
			tblHeight: 0,
			tableDatas: [],
			allTableDatas: [],
			now_date:'',//当前时间
		};
	},
	methods: {
		loadData() {
			this.req.startWith = "pid == ''";
			if (this.req.name) {
				this.req.startWith = "/"+ this.req.name +"/.test(name)";
			}
			this.$axios.get("/goodsCategory/admin/list",
			{
				params:{
				},
				headers:{
					"Mall-Token": uni.getStorageSync('token')
				}
			}).then(response => {
				let res = response
				console.log(res)
				if (res.success) {
					let datas = [];
					res.data.forEach((row, idx) => {
						
						row.imgs = [row.icon];
					});
					res.data.forEach((row, idx) => {
						if (row.pid && res.data.find(x => x.id == row.pid)) {
							return;
						}
						datas.push(row);
					});
					this.tableDatas = res.data;
				} else {
				}
			}).catch(res =>{
			});
			// this.$db[__name]
			// 	.totree(this.req)
			// 	.then(res => {
			// 		if (res.code == 200) {
			// 			let datas = [];
			// 			res.datas.forEach((row, idx) => {
			// 				row.create_time = new Date(row.create_time).format();
			// 				if (row.children && row.children.length > 0) {
			// 					row.children.forEach(c => {
			// 						c.create_time = new Date(c.create_time).format();
			// 					});
			// 				}
			// 				row.imgs = [row.img];
			// 			});
			// 			res.datas.forEach((row, idx) => {
			// 				if (row.pid && res.datas.find(x => x._id == row.pid)) {
			// 					return;
			// 				}
			// 				datas.push(row);
			// 			});
			// 			this.tableDatas = datas;
			// 		}
			// 	});
		},
		toAdd() {
			uni.navigateTo({
				url: `/pages/goods/classify/goods_classify_add_edit?tab=添加分类`,
				events: {
					refreshData: () => {
						this.loadData();
					}
				}
			});
		},
		toEdit(row) {
			uni.navigateTo({
				url: `/pages/goods/classify/goods_classify_add_edit?id=${row.id}
				&tab=编辑分类&name=${row.name}&pid=${row.pid}
				&sort=${row.sort}&icon=${row.icon}&level=${row.level}
				&state=${row.state}`,
				events: {
					refreshData: () => {
						this.loadData();
					}
				}
			});
		},
		remove(id) {
			var data=[id]
			var headers={
				'Content-Type': 'application/json; charset=utf-8',
				"Mall-Token": uni.getStorageSync('token')
			}
			this.$confirm('此操作将永久删除该数据！', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				delgoodslei(data,headers).then((res) => {
					if (res.success) {
						this.loadData();
					}
				}).catch((err)=>{
					this.$confirm(err.message)
				})
				// this.$db[__name].remove(id).then(res => {
				// 	if (res.code == 200) {
				// 		this.loadData();
				// 	}
				// });
			});
		},
		stateChange(row) {
			this.$axios.put("/goodsCategory/admin/update",
			{
				'createBy':row.createBy,//创建人
				'createTime':row.createTime,//创建时间
				'updateBy':uni.getStorageSync("nickName"),//更新人
				'updateTime':this.now_date,//更新时间
				"name": row.name,
				"icon": row.icon,
				"level": row.level,
				"pid": row.pid,
				'sort':row.sort,//排序
				'state':row.state,
				'id':row.id
			},
			{
				headers:{
					'Mall-Token': uni.getStorageSync("token")
				}
			}).then(response => {
				let res = response
				if (res.success) {
					this.loadData()
				} else {
					alert(res.message)
				}
			})
		},
		sortChange(row) {
			if (row.sort == '') {
				return;
			}

			this.$axios.put("/goodsCategory/admin/update",
			{
				'createBy':row.createBy,//创建人
				'createTime':row.createTime,//创建时间
				'updateBy':uni.getStorageSync("nickName"),//更新人
				'updateTime':this.now_date,//更新时间
				"name": row.name,
				"icon": row.icon,
				"level": row.level,
				"pid": row.pid,
				'sort':row.sort,//排序
				'state':row.state,
				'id':row.id
			},
			{
				headers:{
					'Mall-Token': uni.getStorageSync("token")
				}
			}).then(response => {
				let res = response
				if (res.success) {
					this.loadData()
				} else {
					alert(res.message)
				}
			})
		},
		//获取当前时间
		getNowDate() {
		  var _this = this;
		  // this.timer = setInterval(function() {
			var aData = new Date();
			var month = aData.getMonth() < 9 ? "0" + (aData.getMonth() + 1) : aData.getMonth() + 1;
			var date = aData.getDate() <= 9 ? "0" + aData.getDate() : aData.getDate();
			var date2 = aData.getDate() <= 9 ? "0" + (aData.getDate()-1) : (aData.getDate()-1);
			var Hour = aData.getHours() <= 9 ? "0" + (aData.getHours()) : aData.getHours();
			var Miunte = aData.getMinutes() <= 9 ? "0" + (aData.getMinutes()) : aData.getMinutes();
			var Seconds = aData.getSeconds() <= 9 ? "0" + (aData.getSeconds()) : aData.getSeconds();
			// console.log(aData.getTime())
			_this.now_date = aData.getFullYear() + "-" + month + "-" + date + ' '+ Hour +":"+ Miunte +":"+ Seconds;
			// console.log(aData.getFullYear() + "-" + month + "-" + date2)昨天
		  // }, 86400000);
		},
	},
	created() {
		this.loadData();
	},
	updated() {
		if (!this.tblHeight) {
			this.tblHeight = this.$refs.tbl.tblHeight;
		}
	}
};
</script>

<style  ></style>
