<template>
	<view class="enterprise-list">
		<view class="list-wrap">
			<u-sticky bgColor="#fff" class="main-sticky">
				<view class="search-wrap">
					<view class="input-wrap">
						<u-search bgColor='#fff' :showAction='false' :placeholder="$t('base.common.inputPlaceholder')"
							@search="searchList" @clear='searchList' v-model="keyword">
						</u-search>
						<view class="sx-btn" @click="searchList">{{ $t("customer_meter.search") }}</view>
					</view>
				</view>
				<view class="order-card" v-if="orderInfo">
					<view class="order-card-title">
						<span class="type" v-if="orderInfo.orderType == '1'">{{ $t('order.temp') }}</span>
						<span class="type fixed" v-if="orderInfo.orderType == '2'">{{ $t('order.fixed') }}</span>
						<span class="type fixed" v-if="orderInfo.orderType == '5'">{{ $t('order.plan') }}</span>
						<text class="info">{{ orderInfo.tenantName || '' }}</text>
						<view class="out-type" v-if="orderInfo.backFlag == '1'">
							{{ orderInfo.vehicleType == '101' ? $t('order.isBackOut') : $t('order.return') }}
						</view>
						<template v-if="orderInfo.orderType != '4'">
							<span class="status bad-status" v-if="orderInfo.enabledMarkType == '1'">已作废</span>
							<span class="status" v-else-if="orderInfo.orderStatus == '0'">未进厂</span>
							<span class="status" v-else-if="orderInfo.orderStatus == '1' && orderInfo.weighingStatus == '0'">已进厂</span>
							<span class="status" v-else-if="orderInfo.orderStatus == '2'">已完成</span>
							<span class="status" v-else-if="orderInfo.orderStatus == '1' && orderInfo.weighingStatus != '0'">已过磅</span>
						</template>
						<template v-if="orderInfo.orderType == '4'">
							<span class="status" v-if="orderInfo.orderStatus == '0'">未接单</span>
							<span class="status" v-if="orderInfo.orderStatus == '1'">接单中</span>
							<span class="status" v-if="orderInfo.orderStatus == '2'">已完成</span>
							<span class="status bad-status" v-if="orderInfo.enabledMarkType == '1' || orderInfo.orderStatus == '4'">已作废</span>
						</template>
					</view>
					<view class="order-card-info">
						<view class="order-type">
							<u-tag :text="$t('order.suppOrder')" type="error" v-if="orderInfo.isMain == 1"></u-tag>
							<u-tag :text="$t('order.mixed')" type="success" v-if="orderInfo.isMain == 2"></u-tag>
						</view>
						<view class="row">
							<p class="cell">
								<text class="info">{{ orderInfo.companyName || '' }}</text>
							</p>
						</view>
						<view class="row">
							<p class="cell">
								<text class="info">{{ orderInfo.setDate || '' }}</text>
							</p>
							<p class="cell">
								<text class="info" v-if="orderInfo.vehicleType == '101'">{{ $t('order.saleOrder') }}</text>
								<text class="info" v-if="orderInfo.vehicleType == '102'">{{ $t('order.purchaseOrder') }}</text>
							</p>
						</view>
						<view class="row">
							<p class="cell">
								<text class="info">{{ orderInfo.materialName || '' }}</text>
							</p>
							<p class="cell">
								<text class="info">{{ orderInfo.orderNumber || '' }}{{ orderInfo.materialKind == '101' &&
									orderInfo.companyClientType == '109' ? $t('customer_meter.unitB') : (orderInfo.orderType ==
										'4' ? $t('customer_meter.unitN') : $t('customer_meter.unitT')) }}</text>
							</p>
						</view>
						<view class="row">
							<p class="cell">
								<text class="info">{{ orderInfo.vehicle || '' }}</text>
							</p>
						</view>
						<view class="qrcode-logo" @click="drawQrcode(orderInfo)">
							<image class="img" src="/subpages/static/czzn-img/state/qrcode.png" mode="scaleToFill" />
						</view>
					</view>
					<view class="code">
						<view class="label">{{ $t('order.orderCode') }}</view>
						<view class="info">{{ orderInfo.code || '' }}</view>
						<view class="label">{{ $t('order.source') }}</view>
						<view class="info"> {{ $t('order.mini') }}</view>
					</view>
				</view>
			</u-sticky>
			<template v-if="driverList.length">
				<view class="business-card" v-for="(item, index) in driverList" :key="index">
					<view class="business-card-info" @click="onClickDriver(item)">
						<view class="info-line" v-if="item.vehicleType != 1">
							<text class="lebel">{{ item.driver }} {{ item.vehicle || '' }}</text>
						</view>
						<view class="info-line" v-if="item.vehicleType == 1">
							<text class="lebel">{{ item.vehicle }} {{ item.driver || '' }}</text>
							<text class="info">{{ $t("driver.fleet") }} {{ item.fleetName || '' }}</text>
						</view>
						<view class="businessStatus-info">
							<u--image width="100rpx" height="88rpx"
								:src="item.vehicleType == 1 ? '/subpages/static/czzn-img/state/fleet_vehicle.png' : '/subpages/static/czzn-img/state/private-vehicle.png'">
							</u--image>
						</view>
					</view>

				</view>
			</template>
			<template v-else>
				<u-empty mode="list" icon="https://cdn.uviewui.com/uview/empty/list.png">
				</u-empty>
			</template>
		</view>
		<uni-fab ref="fab" buttonColor="#3c9cff" @trigger="trigger" @fabClick="onClickRight" />
	</view>
</template>

<script>
import {
	getDealerVehicleList,
} from '../../api/dealer'
import { setVehicleToOrder } from '../../api/order'
export default {
	data() {
		return {
			driverList: [],
			keyword: '',
			status: undefined,
			orderInfo: null
		}
	},
	onShow() {
		this.bindDriver()
	},
	onLoad(option) {
		this.orderInfo = JSON.parse(option.orderInfo)
		this.status = option.status;
	},
	methods: {
		//点击司机返回
		onClickDriver(item) {
			let that = this
			uni.showModal({
				title: "分配车辆",
				content: `确认分配车号 '${item.vehicle}' 到当前订单`,
				success: function (res) {
					//用户点击确定
					if (res.confirm) {
						let data = {
							orderId:'',
							vehicle:item.vehicle,
						}
						setVehicleToOrder().then(res=>{
							if (res.statusCode == 200) {
								uni.showToast({
									title:"分配成功",
									icon: 'none',
									duration: 2000
								})
								setTimeout(()=>{
									uni.navigateBack()
								})
							}
						})
					}
				},
			});
			// let pages = getCurrentPages();
			// console.log("获取到司机信息、", item, this.status);
			// let prevPage = pages[pages.length - 2]; //上一个页面
			// debugger
			// if (this.status == 1) {
			// 	prevPage.$vm.orderForm.batchOrderList[prevPage.$vm.index].vehicle = item.vehicle; //重点$vm
			// 	prevPage.$vm.orderForm.batchOrderList[prevPage.$vm.index].driver = item.driver; //重点$vm
			// 	prevPage.$vm.orderForm.batchOrderList[prevPage.$vm.index].phone = item.phone; //重点$vm
			// 	prevPage.$vm.orderForm.batchOrderList[prevPage.$vm.index].driverId = item.driverVehicleId; //重点$vm
			// }
			// else if (this.status == 2) {
			// 	prevPage.$vm.vehicle = item.vehicle; //重点$vm
			// }
			// else {
			// 	prevPage.$vm.orderForm.vehicle = item.vehicle; //重点$vm
			// 	prevPage.$vm.orderForm.driver = item.driver; //重点$vm
			// 	prevPage.$vm.orderForm.phone = item.phone; //重点$vm
			// 	prevPage.$vm.orderForm.driverId = item.driverVehicleId; //重点$vm
			// 	prevPage.$vm.orderForm.idcard = item.idCard; //重点$vm
			// }
			// uni.navigateBack();
		},
		//获取车辆选项列表
		bindDriver() {
			var that = this;
			let json = {
				vehicleType: "0",
				keyword: this.keyword,
				appUserType: 3
			};
			getDealerVehicleList(json).then(res => {
				if (res.statusCode == 200) {
					that.driverList = res.data.data
					console.log("获取到司机列表", that.driverList);
				}
			})
		},
		//搜索列表
		searchList() {
			this.bindDriver()
		},
		//跳转添加司机表单
		onClickRight() {
			uni.navigateTo({
				url: '/subpages/driver/dealerAddVehicle?vehicleType=0'
			})
		}
	},

}
</script>
<style>
/* #ifndef H5 */
page {
	/* height: 100%; */
	background-color: #f2f2f2;
}

/* #endif */
</style>
<style lang="scss" scoped>
@import '../../common/css/public.scss';
</style>
