<template>
	<view>
		<view @tap="editPeople('add')"
			style="width: 90%;height: 80rpx;line-height: 80rpx;text-align: center;background: #088FEB;color: #fff;margin: 20rpx auto;border-radius: 10px;">
			新增到达站</view>
		<uni-card title="到达站列表">
			<uni-list>
				<uni-list-item direction="row" clickable v-for="(item , index) in staffList" :key="index">
					<template #header>
						<view class="avatar-wrap">
							<text class="staff-name">{{ item.stationName }}</text>
						</view>
					</template>
					<template #footer>
						<view style="display: flex;"> 
							<view @tap="around(item)"
								style="width: 80px;height: 60rpx;line-height: 60rpx;text-align: center;background: #088FEB;color: #fff;border-radius: 10px;">
								中转站</view>
							<view @tap="editPeople('edit',item)"><u-icon name="edit-pen-fill" size="28"></u-icon></view>
							<view @tap="delItem(item)"><u-icon name="trash-fill" size="28"></u-icon></view>
						</view>

					</template>
				</uni-list-item>
			</uni-list>
		</uni-card>
		<uni-popup ref="linePopup" background-color="#fff" :is-mask-click="true">
			<view class="type-popup" style="width: 600rpx;">
				<view style="height: 120rpx;font-size: 36rpx;font-weight: bold;line-height: 120rpx;text-align: center;">
					提示
				</view>
				<view style="text-align: center;">
					您有多条线路，请选择其中一条进行操作
				</view>
				<view v-for="(item,index) in lineList" :key="index" @tap="checkLine(item.circuitId)" style="height: 80rpx;line-height: 80rpx;text-align: center;border-radius:10px;border: 1px solid #eee;margin-top: 10px;">
					{{item.circuitName}}
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="inputDialog" background-color="#fff" :is-mask-click="true">
			<view class="type-popup">
				<view v-if="addType == 'add'" style="height: 120rpx;font-size: 36rpx;font-weight: bold;line-height: 120rpx;text-align: center;">
					新增到达站
				</view>
				<view v-if="addType == 'edit'" style="height: 120rpx;font-size: 36rpx;font-weight: bold;line-height: 120rpx;text-align: center;">
					编辑信息
				</view>
				<uni-forms :modelValue="addForm" ref="addForm" validateTrigger="bind" :rules="rules" label-width="130rpx"
					label-align="right">
					<uni-forms-item label="名称" required name="stationName">
						<uni-easyinput type="text" v-model="addForm.stationName" placeholder="请输入名称" />
					</uni-forms-item>
					<uni-forms-item label="运费" required name="freightRules">
						<uni-easyinput type="digit" v-model="addForm.freightRules" placeholder="请输入金额" />
					</uni-forms-item>
					<uni-forms-item label="落地费" required name="landingFeeRules">
						<uni-easyinput type="digit" v-model="addForm.landingFeeRules" placeholder="请输入金额" />
					</uni-forms-item>
					<uni-forms-item label="送货费" required name="deliveryFeeRules">
						<uni-easyinput type="digit" v-model="addForm.deliveryFeeRules" placeholder="请输入金额" />
					</uni-forms-item>
				</uni-forms>
				
				<view
					style="width: 100%;height: 90rpx;line-height: 90rpx;text-align: center;border-radius: 0;background: linear-gradient(90deg, #60F3FF, #088FEB);color: #fff;display: flex;">
					<view style="width:50%;" @tap="$refs.inputDialog.close()">取消</view>
					<view style="width:50%;background: linear-gradient(90deg, #FF9797, #FFC1E0);"
						@tap="onDotClick()">确认</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				staffList: [],
				addForm: {
					circuitId:"",
					deliveryFeeRules: '0',
					freightRules: '0',
					landingFeeRules: '0',
					stationName: ''
				},
				rules: {
					nickName: {
						rules: [{
							required: true,
							errorMessage: '名称不能为空'
						}]
					}
				},
				lineList:[],
				avatar: 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/unicloudlogo.png',
				size: 30,
				addType:"",
				type: '',
				pageNum: 1,
				pages: 1,
				
			};
		},
		onLoad() {
			this.type = uni.getStorageSync('type')
		},
		onShow() {
			this.getCircuitId("")
		},
		methods: {
			checkLine(id){
				this.addForm.circuitId = id
				this.getStaffList()
				this.$refs.linePopup.close()
			},
			getCircuitId(){
				uni.request({
					url: this.tui.interfaceUrl() + '/app/logisticsCompanyRoute/getLogisticsCompanyRoutePageList',
					data:{
						pageNum:1,
						pageSize:1000,
						companyId:uni.getStorageSync('transCompany'),
						companyName:"",
						circuitId:"",
						circuitName:""
					},
					header: {
						'content-type': 'application/json',
						'appWLToken': this.tui.getToken()
					},
					method: 'POST', //'GET','POST'
					dataType: 'json',
					success: (res) => {
						if(res.data.code == 401){
							uni.clearStorage()
							uni.navigateTo({
								url: '/package2/login/login'
							})
						}
						if (res.data.code == 200){
							if(res.data.result.records.length > 1){
								this.$refs.linePopup.open()
								this.lineList = res.data.result.records
							}else{
								this.addForm.circuitId = res.data.result.records[0].circuitId
								this.getStaffList()
							}
						}
					},
					fail: (res) => {
						this.tui.toast("网络不给力，请稍后再试~")
					}
				})
			},
			around(item){
				
				uni.navigateTo({
					url: '/package2/other/aroundStation?stationId='+item.id
				})
			},
			editPeople(type,item){
				this.addType = type
				if(item){
					this.addForm.stationName = item.stationName
					this.addForm.landingFeeRules = item.landingFeeRules
					this.addForm.freightRules = item.freightRules
					this.addForm.deliveryFeeRules = item.deliveryFeeRules
					this.addForm.id = item.id
				}else{
					this.addForm = {
						circuitId:this.addForm.circuitId,
						deliveryFeeRules: '0',
						freightRules: '0',
						landingFeeRules: '0',
						stationName: ''
					}
				}
				
				this.$refs.inputDialog.open()
			},
			delItem(item) {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确定要删除该到达站吗？',
					success(res1) {
						if (res1.confirm) {
							uni.request({
								url: that.tui.interfaceUrl() + '/app/logisticsStation/deleteLogisticsStation',
								data: {
									id: item.id
								},
								header: {
									'content-type': 'application/x-www-form-urlencoded',
									'appWLToken': that.tui.getToken()
								},
								method: 'POST', //'GET','POST'
								dataType: 'json',
								success: (res) => {
									if(res.data.code == 401){
										uni.clearStorage()
										uni.navigateTo({
											url: '/package2/login/login'
										})
									}
									if (res.data.code == 200) {
										that.tui.toast("删除成功")
										setTimeout(res1=>{
											that.getStaffList()
										},1000)
									}
								},
								fail: (res) => {
									that.tui.toast("网络不给力，请稍后再试~")
								}
							})
						}
					}
				})

			},
			onDotClick() {
				if (this.addType == 'edit') {
					if(this.addForm.stationName == '' ){
						this.tui.toast('到达站名称不能为空')
						return
					}
					uni.request({
						url: this.tui.interfaceUrl() + '/app/logisticsStation/updateLogisticsStation',
						data: this.addForm,
						header: {
							'content-type': 'application/json',
							'appWLToken': this.tui.getToken()
						},
						method: 'POST', //'GET','POST'
						dataType: 'json',
						success: (res) => {
							if(res.data.code == 401){
								uni.clearStorage()
								uni.navigateTo({
									url: '/package2/login/login'
								})
							}
							if(res.data.code == 200){
								this.tui.toast('操作成功')
								setTimeout(res1=>{
									this.getStaffList()
								},1000)
							}
							
						},
						fail: (res) => {
							this.tui.toast("网络不给力，请稍后再试~")
						}
					})
				} else if (this.addType == 'add') {
					this.$refs.addForm.validate().then(async () => {
						uni.request({
							url: this.tui.interfaceUrl() + '/app/logisticsStation/addLogisticsStation',
							data: this.addForm,
							header: {
								'content-type': 'application/json',
								'appWLToken': this.tui.getToken()
							},
							method: 'POST', //'GET','POST'
							dataType: 'json',
							success: (res) => {
								if(res.data.code == 401){
									uni.clearStorage()
									uni.navigateTo({
										url: '/package2/login/login'
									})
								}
								if(res.data.code == 200){
									this.tui.toast('操作成功')
									setTimeout(res1=>{
										this.getStaffList()
									},1000)
								}
							},
							fail: (res) => {
								this.tui.toast("网络不给力，请稍后再试~")
							}
						})
					}, (reason) => {
					
					})
				}
				this.$refs.inputDialog.close()
			},
			getStaffList() {
				uni.request({
					url: this.tui.interfaceUrl() + '/app/logisticsStation/getLogisticsStationPageList',
					data: {
						pageNum: this.pageNum,
						pageSize: "1000",
						circuitId: this.addForm.circuitId
					},
					header: {
						'content-type': 'application/json',
						'appWLToken': this.tui.getToken()
					},
					method: 'POST', //'GET','POST'
					dataType: 'json',
					success: (res) => {
						this.status = 'nomore';
						if(res.data.code == 401){
							uni.clearStorage()
							uni.navigateTo({
								url: '/package2/login/login'
							})
						}
						if (res.data.code == 200) {
							if (this.pages == 1) {
								this.staffList = res.data.result.records
							} else {
								this.staffList = [...this.staffList, ...res.data.result.records]
							}
							this.pages = res.data.result.pages
						}
					},
					fail: (res) => {
						this.tui.toast("网络不给力，请稍后再试~")
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.staff-name {
		padding: 0 15upx;
	}

	.tag-wrap {
		width: 100rpx;
		flex: 1;
		display: flex;

		view {
			margin: 0 2upx;
		}
	}

	.avatar-wrap {
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 24upx;
	}

	.type-popup {
		width: 500rpx;
		height: auto;
		background: #fff;
		border-radius: 10px;
		position: relative;
		padding: 20rpx;
	}
</style>