<template>
	<view>
		<u-gap></u-gap>
		<u-empty mode="list" v-if="indexList.length == 0">
		</u-empty>
		<view class="u-page" v-else>
			<u-swipe-action>
				<u-list>
					<u-list-item v-for="(item,index) in indexList" :key="index">
						<u-swipe-action-item :options="options" :name="item" @click="deleteitem">
							<u-cell :title="`${index+1}`" :value="item"></u-cell>
						</u-swipe-action-item>
					</u-list-item>
				</u-list>
			</u-swipe-action>
		</view>
		<view class="total-bottom">
			<view class="counttext">{{indexList.length}}台</view>
			<view>
				<u-button text="扫码" @click="scan" type="primary"></u-button>
			</view>
			<view>
				<u-button text="导出" @click="exportexcel" type="success"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		exportcsv,getdevlist
	} from '../common/api.js';
	export default {
		data() {
			return {
				indexList: [],
				options: [{
					text: "删除",
					style: {
						backgroundColor: '#f56c6c'
					}
				}]
			}
		},
		onLoad(options) {
		},
		methods: {
			//扫码
			scan() {
				var that = this;
				uni.scanCode({
					success(res) {
						let result = res.result;
						let scantype = res.scanType;
						//针对带url的处理
						if (uni.$u.test.contains(result, 'https://ybapi.ybhdmob.com')) {
							var arr = result.split('?');
							result = arr.length == 2 ? arr[1].substring(2) : "";
						}
						//针对外箱码的处理
						else if (uni.$u.test.contains(result, '|')) {
							var arr = result.split('|');
							let errmsg = "";
							//e-当前值,index-索引,item-数组集合
							arr.forEach(function(e, index, item) {
								if (that.indexList.indexOf(e) == -1) {
									that.indexList.push(e);
								} else {
									errmsg += e + ",";
								}
							});
							if (errmsg !== "") {
								errmsg = errmsg.substring(0, errmsg.length - 1);
								uni.showModal({
									content: errmsg + "已存在"
								})
								return;
							}
							result = "";
						}else{
							that.getdevcodelist(result);
							result = "";
						}
						if (result != "") {
							if (that.indexList.indexOf(result) == -1) {
								that.indexList.push(result);
							} else {
								uni.showModal({
									content: result + "已存在"
								});
							}
						}
					},
					fail(res) {

					}
				})
			},
			//excel列表导出
			exportexcel() {
				var that = this;
				console.log(uni.$u.http.config.baseURL)
				if (that.indexList.length == 0) {
					uni.showModal({
						content: '设备列表不可为空'
					});
					return;
				}
				uni.showModal({
					editable: true,
					placeholderText: "请输入文件名称",
					success: function(res) {
						if (res.confirm) {
							var postdata = that.indexList.join(',');
							exportcsv({
								data:postdata,
								name:res.content
							}).then(ress => {
								uni.$u.http.download(ress).then(res=>{
									uni.saveFile({
										tempFilePath:res.tempFilePath,
										success:function(res){
											uni.openDocument({
												filePath:res.savedFilePath,
												showMenu:true
											})
										}
									})
								});
							});
						}
					}
				})
			},
			//删除项目
			deleteitem(res) {
				let item = res.name;
				let index = this.indexList.indexOf(item);
				this.indexList.splice(index, 1);
			},
			//根据id获取设备列表
			getdevcodelist(result){
				var that =this;
				console.log(result);
				getdevlist({
					id:result
				}).then(ress=>{
					var data = ress;
					//e-当前值,index-索引,item-数组集合
					data.forEach(function(e, index, item) {
						if (that.indexList.indexOf(e) == -1) {
							that.indexList.push(e);
						}
					});
				})
			}
		}
	}
</script>
<style scoped lang="scss">
	.total-bottom {
		position: fixed;
		bottom: 0;
		background-color: $u-primary-light;
		width: 100%;
		text-align: center;
		padding: 5px 0;
		display: flex;
		justify-content: space-around;

		.counttext {
			padding-top: 10px;
			color: $u-error;
		}
	}
</style>
