<template>
	<div>
		<use-table ref="tbl"></use-table>
		<div class="container padding_b_0">
		</div>
		<div class="container use-table">
			<div class="dflex_sb margin_b_15">
				<div>
					<el-button class="batch_btn padding0" @click="removes">删除</el-button>
					<a href="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-7e00db99-ad65-4b9f-a74b-61bccb92b124/113267f1-26f4-42b8-8c56-3afd8755730c.xlsx">
						参照快递100公司编码
					</a>
				</div>
				<el-button class="add_btn pos_r padding0" icon="iconfont iconxinzeng" @click="toAdd">新增</el-button>
			</div>
			<el-table :height="tblHeight" :data="tableDatas" @selection-change="selectionChange" highlight-current-row>
				<el-table-column type="selection"></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 property="expressName" label="名称" align="center"></el-table-column>
				<el-table-column property="code" label="编码" align="center"></el-table-column>
				<el-table-column property="state" label="状态" align="center">
					<template slot-scope="scope">
						<el-tooltip :content="scope.row.state == '1' ? '点击禁用' : '点击启用'" 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" fixed="right">
					<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>
			<!-- 分页 -->
			<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>
	</div>
</template>

<script>
	import {
		delkuaidi
	} from '@/util/loginJie.js'
	export default {
		data() {
			return {
				advancedSearch: false,
				req: {
					page: 1,
					rows: 10,
					orderby: 'sort asc',
				},
				tblHeight: 0,
				tableDatas: [],
				tableTotal: 0,
				selectDatas: [],
				now_date:'',//更新时间
			};
		},
		methods: {
			loadData() {
				this.$axios.get("/expressCompany/open/list",
				{
					params:{
						'curPage':this.req.page,
						'pageSize':this.req.rows,
						'order':'asc',
						'orderField':'sort'
					},
					headers:{
					}
				}).then(response => {
					let res = response
					console.log(res)
					if (res.success) {
						this.tableDatas = res.data.list;
						this.tableTotal = res.data.totalCount;
					} else {
					}
				}).catch(res =>{
				});	
			},
			toAdd() {
				uni.navigateTo({
					url: `/pages/order/kuaidi_add_edit?tab=添加分类`,
					events: {
						refreshData: () => {
							this.loadData();
						}
					}
				});
			},
			toEdit(row) {
				uni.navigateTo({
					url: `/pages/order/kuaidi_add_edit?id=${row.id}&tab=编辑分类`,
					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(() => {
					delkuaidi(data,headers).then((res) => {
						if (res.success) {
							this.loadData();
						}
					})
				});
			},
			selectionChange(datas) {
				this.selectDatas = datas;
			},
			removes() {
				if (this.selectDatas.length <= 0) {
					this.$message('请勾选需要删除的数据');
					return;
				}

				let data = this.selectDatas.map(x => x.id);
				var headers={
					'Content-Type': 'application/json; charset=utf-8',
					"Mall-Token": uni.getStorageSync('token')
				}

				this.$confirm('此操作将永久删除勾选数据！', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					delkuaidi(data,headers).then((res) => {
						if (res.success) {
							this.loadData();
						}
					})
				});
			},
			stateChange(row) {
				this.$axios.put("/expressCompany/admin/update",
				{
					'createBy':row.createBy,//创建人
					'createTime':row.createTime,//创建时间
					"expressName": "瑾瑜.覃",
					'updateBy':uni.getStorageSync("nickName"),//更新人
					'updateTime':this.now_date,//更新时间
					'sort':row.sort,//排序
					'state':row.state,//状态0禁用1启用
					'id':row.id,
					"code": row.code,
					"contactsName": row.contactsName,
					"contactsPhone": row.contactsPhone,
					"delFlag": row.delFlag
				},
				{
					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("/expressCompany/admin/update",
				{
					'createBy':row.createBy,//创建人
					'createTime':row.createTime,//创建时间
					"expressName": "瑾瑜.覃",
					'updateBy':uni.getStorageSync("nickName"),//更新人
					'updateTime':this.now_date,//更新时间
					'sort':row.sort,//排序
					'state':row.state,//状态0禁用1启用
					'id':row.id,
					"code": row.code,
					"contactsName": row.contactsName,
					"contactsPhone": row.contactsPhone,
					"delFlag": row.delFlag
				},
				{
					headers:{
						'Mall-Token': uni.getStorageSync("token")
					}
				}).then(response => {
					let res = response
					if (res.success) {
						this.loadData()
					} else {
						alert(res.message)
					}
				})
			},
			sizeChange(size) {
				this.req.rows = size;
				this.loadData();
			},
			currentChange(current) {
				this.req.page = current;
				this.loadData();
			},
			//获取当前时间
			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>
