<template>
	<view>
		<view class="body">
			订单编号：<input class="input" v-model="orderNo" />
			<button type="primary" @click="select(orderNo)" class="button1">
				<p class="p">搜索</p>
			</button>
			<button type="primary" @click="expert(orderNo)" class="button1">
				<p class="p">导出</p>
			</button>
		</view>
		<view class="">
			<uni-collapse :accordion="true">
				<uni-list>
					<view  v-for="machine in machineList" :key="machine.id">
						<uni-list-item :title="machine.coding" @click="trun(machine.coding)"/>
					</view>
				</uni-list>
			</uni-collapse>	
		</view>
			
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from 'vuex'
	import uniCollapse from '@/components/uni-collapse/uni-collapse.vue'
	import uniCollapseItem from '@/components/uni-collapse-item/uni-collapse-item.vue'
	import uniList from '@/components/uni-list/uni-list.vue'
	import uniListItem from '@/components/uni-list-item/uni-list-item.vue'
	export default {
		components: {
			uniCollapse,
			uniCollapseItem,
			uniList,
			uniListItem,
		},
		data() {
			return {
				orderNo:'',
				machineList:''
			}
		},
		methods: {
			select(orderNo) {
				var that = this;
				var serverUrl = that.serverurl;
				var token = uni.getStorageSync("token");
				uni.request({
					url: serverUrl + "/TMachine/selectMachineByOrderNo?orderNo=" + orderNo,
					method: "GET",
					header: {
						'token': token
					},
					success: (Result) => {
						var res = Result;
						if (res.data.code == true) {
							that.machineList = res.data.data;
						} else {
							uni.showModal({
								title: '提示',
								content: res.data.message,
							});
						}
					}
				});
			},
			trun(coding) {
				var item = {
					coding:'',
					type:''
				};
				item.coding = coding;
				uni.navigateTo({
				    url:'selectMachine?item='+encodeURIComponent(JSON.stringify(item))
				});
			},
			expert(orderNo) {
				var that = this;
				var serverUrl = that.serverurl;
				var token = uni.getStorageSync("token");
				uni.request({
					url: serverUrl + "/TMachine/expert?orderNo=" + orderNo,
					method: "GET",
					header: {
						'token': token
					},
					success: (Result) => {
						var res = Result;
						if (res.data.code == true) {
							var url = res.data.data;
							var a= [];
							a=url.split("machine/");
							// a=url.split("excel/");
							var fileName = a[1];
							wx.downloadFile({
							  url: url,
							  /* 这里指定filePath属性，不然会是一个临时文件，不能用对应的程序打开 */
							  filePath:wx.env.USER_DATA_PATH + '/' + fileName,
							  success(result) {
							    const filePath = result.filePath;
							    wx.openDocument({
							      filePath: filePath,
							      showMenu: true,
							      fileType: "xls",
							      success: rlt => {
							        console.log("打开文档成功",rlt);
							      },
							      fail: err => {
							        wx.showModal({
							          content: JSON.stringify(err),
							          showCancel: false
							        })
							      }
							    });
							  },
							  fail(error){
							    wx.showModal({
							      content: JSON.stringify(error),
							      showCancel: false
							    })
							  }
							})
						} else {
							uni.showModal({
								title: '提示',
								content: res.data.message,
							});
						}
					}
				});
			}
		}
	}
</script>

<style>
	.body {
		background-color: #FFFFFF;
		padding: 20upx 20upx 20upx 20upx;
		font-size: 30upx;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
	}
</style>
