<template>
	<view class="page">

		<navbar :title="titleList[titleIndex]" leftClick @leftClick="$utils.navigateBack" />

		<view class="frame">
			<view class="title">
				<span
					style="width: 10rpx;height: 40rpx;background-color: #1f1c39;border-radius: 10rpx;overflow: hidden;"></span>
				<span>{{ $t('other.aluminumProducts') }}</span>
			</view>

			<view class="basic-info">

				<!--供应商名称-->
				<!-- <view v-if="titleIndex == 1" class="item">
					<view>{{ $t('other.supplierName') }}</view>
					<view>华南铝业有限公司</view>
				</view> -->

				<!--商品主图-->
				<view class="item">
					<view>商品主图</view>
					<view class="">
						<uv-upload 
						:fileList="fileList" 
						:maxCount="1" 
						multiple 
						width="150rpx" 
						height="150rpx"
						name="fileList"
						@delete="deleteImage" 
						@afterRead="afterRead"
						:previewFullImage="true"></uv-upload>
					</view>
				</view>

				<!--商品详情-->
				<view class="item">
					<view>详情图片</view>
					<view class="">
						<uv-upload 
						:fileList="contentFileList" 
						:maxCount="3" 
						multiple 
						width="150rpx" 
						height="150rpx"
						name="contentFileList"
						@delete="deleteImage" 
						@afterRead="afterRead"
						:previewFullImage="true"></uv-upload>
					</view>
				</view>
				<view class="item">
					<view>详情视频</view>
					<view class="">
						<uv-upload 
						:fileList="contentFileVideoList" 
						:maxCount="3" 
						multiple 
						accept="video"
						width="150rpx" 
						height="150rpx"
						name="contentFileVideoList"
						@delete="deleteImage" 
						@afterRead="afterRead"
						:previewFullImage="true"></uv-upload>
					</view>
				</view>

				<!--商品规格-->
				<view class="item"
				@click="$refs.unitListPicker.open()">
					<view>{{ $t('components.productSpe') }}</view>
					<view>
						{{ unit.specsName }}
					</view>
					<view class="icon">
						<uv-icon name="arrow-right" size="30rpx"></uv-icon>
					</view>
				</view>


				<view class="addressA" @click="openAddress">
					<view class="title">提货地点</view>
					<view class="address" style="width: 70%">
						<image mode="" src="/static/image/address/selectIcon.png"></image>
						<view class="">
							{{ address.name }}
						</view>
						<view class="icon">
							<uv-icon name="arrow-right" size="30rpx"></uv-icon>
						</view>
					</view>
				</view>


				<!--提货地点-->
				<!--<view class="currentRegion">-->
				<!--	<view>{{ $t('components.pickupLocation') }}</view>-->
				<!--	<view @click.stop="selectAddr">-->
				<!--		<input class="input" disabled v-model="form.currentRegion"-->
				<!--			:placeholder=" $t('components.selectRegion')">-->
				<!--		</input>-->
				<!--		<view class="orientation">-->
				<!--			<img src="../../static/image/address/selectIcon.png"-->
				<!--				style="width:30rpx;height: 30rpx;margin:5rpx 5rpx 5rpx 5rpx;">-->
				<!--		</view>-->
				<!--	</view>-->
				<!--</view>-->

				<!--详细地址-->
				<view v-if="address.addressDetail" class="item">
					<view>{{ $t('components.detailedAddress') }}</view>
					<view>
						<view class="">
							{{ address.address + ' ' + address.addressDetail }}
							<!--</view>-->
							<!--<view class="">-->
							<!--  {{ address.addressDetail }}-->
						</view>
					</view>
				</view>
				
				<view class="item"
				@click="$refs.startDateRef.open()">
					<view>{{ $t('components.deliveryDate') }}</view>
					<view>
						{{ form.transactionTime }}
						<uv-datetime-picker 
						ref="startDateRef" 
						v-model="form.transactionTime" mode="date"
						@confirm="startDateChange"></uv-datetime-picker>
					</view>
					<view class="icon">
						<uv-icon name="arrow-right" size="30rpx"></uv-icon>
					</view>
				</view>

				<!--暂定数量-->
				<view class="item">
					<view>{{ $t('other.tentativeQuantity') }}</view>
					<view>
						<uv-number-box v-model="form.num"></uv-number-box>
					</view>
				</view>

				<!--单价-->
				<view class="item">
					<view>{{ $t('components.unitPrice') }}</view>
					<view>
						<input v-model="form.price" clearable
						class="input-color"
						placeholder="请输入单价"></input>
					</view>
				</view>
				
				<!-- 检测报告 -->
				<view class="item">
					<view>检测报告</view>
					<view class="">
						<uv-upload 
						:fileList="reportFileList" 
						:maxCount="3" 
						multiple 
						width="150rpx" 
						height="150rpx"
						name="reportFileList"
						@delete="deleteImage" 
						@afterRead="afterRead"
						:previewFullImage="true"></uv-upload>
					</view>
				</view>
				<view class="item">
					<view>检测视频</view>
					<view class="">
						<uv-upload 
						:fileList="reportFileVideoList" 
						:maxCount="3" 
						multiple 
						accept="video"
						width="150rpx" 
						height="150rpx"
						name="reportFileVideoList"
						@delete="deleteImage" 
						@afterRead="afterRead"
						:previewFullImage="true"></uv-upload>
					</view>
				</view>

				<!--提交报价和提交审核-->
				<view class="btns">
					<span @click="confirmBtn" class="ljxd">
						{{ titleIndex == 0 ? $t('other.submitQuotation') : $t('other.submitReview') }}
					</span>
					<span class="lxwm" @click="contactUs">
						{{ $t('components.contactUs') }}
					</span>
					<span class="tip">如有疑问请联系我们</span>
				</view>

			</view>
		</view>

		<!-- 联系客服弹框 -->
		<customerServicePopup ref="customerServicePopup" />


		<!-- 地址选择弹框 -->
		<uv-popup ref="addressPopup" :round="30">
			<addressList ref="addressList" height="60vh" @select="selectAddress" />
			<view class="uni-color-btn"
			@click="toAddress">
				添加地址
			</view>
		</uv-popup>
		
		<uv-picker ref="unitListPicker" 
		keyName="specsName"
		:columns="unitList" 
		itemHeight="80"
		@confirm="unitListConfirm"></uv-picker>

	</view>
</template>

<script>
	import topbar from "@/components/base/topbar.vue";
	import tabber from "@/components/base/tabbar.vue";
	import customerServicePopup from "@/components/config/customerServicePopup.vue";
	import Position from "@/utils/position";
	import dayjs from "dayjs";
	import AddressList from "@/pages_order/components/address/addressList.vue";

	export default {
		name: "offer",
		components: {
			AddressList,
			customerServicePopup,
			tabber,
			topbar
		},
		onLoad(options) {
			this.titleIndex = options.titleIndex;
		},
		data() {
			return {
				titleIndex: 0,
				titleList: [
					// 0 供应商报价
					this.$t('pages.index.index.supplierQuotation'),
					this.$t('pages.index.index.supplierBillLading')
				],
				form: {
					"addressId": "",
					"num": 1,
					"price": '',
					"transactionTime": dayjs(new Date()).format("YYYY-MM-DD")
				},
				address: {
					name: '请选择地址',
					addressDetail: '',
				},
				addressTotal: 0,
				fileList : [],//商品主图
				reportFileList : [],//检测报告
				reportFileVideoList : [],//检测视频
				contentFileList : [],//商品内容
				contentFileVideoList : [],//商品详情视频
				unitList : [],//规格列表
				unit : {//选中的规格
					specsName : '请选择规格'
				},
			}
		},
		onShow() {
			this.getUnitList()
			this.getAddressDefault()
		},
		onLoad() {
		},
		methods: {
			//获取默认地址
			getAddressDefault() {
				return new Promise((success, fail) => {
					this.$api('addressList', {
						pageNo: 1,
						pageSize: 1,
					}, res => {
						if (res.code == 200) {
							this.address = res.result.records[0];
						}
					})
				})
			},
			
			// 查询规格
			getUnitList(){
				this.$api('specsList', {
					pageNo : 1,
					pageSize : 9999999,
				}, res => {
					if(res.code == 200){
						this.unitList = [res.result.records]
					}
				})
			},

			//获取地址列表
			getAddressList() {
				this.$refs.addressList
				.getAddressList()
				.then(res => {
					if (res.total == 0) {
						this.toAddress()
					}
				})
			},
			// 跳转到添加地址页面
			toAddress(){
				this.$refs.addressPopup.close()
				return uni.navigateTo({
					url: '/pages_order/center/addressListManage?type=back'
				})
			},

			// 提交报价/提交审核
			confirmBtn() {
				//TODO
				
				this.form.addressId = this.address.id
				this.form.specsId = this.unit.id

				// 商品主图
				this.form.pic = this.fileList.map(n => n.url).join(',')
				// 检测报告
				this.form.report = this.reportFileList.map(n => n.url).join(',')
				// 检测视频
				this.form.reportVideo = this.reportFileVideoList.map(n => n.url).join(',')
				// 商品内容-详情图片
				this.form.picDetail = this.contentFileList.map(n => n.url).join(',')
				// 商品内容-详情视频
				this.form.video = this.contentFileVideoList.map(n => n.url).join(',')

				if (this.$utils.verificationAll(this.form, {
						"pic": '请上传主图',
						// "picDetail": '请上传内容图片',
						specsId : "请选择规格",
						"addressId": '请选择提货地点',
						"num": '请选择吨数',
						"price": '请输入单价',
						"transactionTime": '请选择交货时间',
					})) {
					return
				}

				this.$api('addProduct', this.form, res => {
					if (res.code == 200) {
						uni.showToast({
							title: '提交成功',
							icon: 'none'
						})
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}, 1000)
					}
				})
			},
			
			// 选中规格时
			unitListConfirm(e){
				this.unit = e.value[0]
			},

			openAddress() {
				this.$refs.addressPopup.open('bottom')
				// this.getAddressList()

				this.$nextTick(() => {
					this.getAddressList()
				})
			},
			// 选择地址
			selectAddress(e) {
				this.address = e
				this.$refs.addressPopup.close()
			},


			startDateChange(val) {
				this.$nextTick(() => {
					this.form.transactionTime = dayjs(val.value).format("YYYY-MM-DD")
				});
			},
			startDateOpen() {
				this.$refs.startDateRef.open();
			},

			// 联系我们
			contactUs() {
				this.$refs.customerServicePopup.open()
			},

			//地图上选择地址
			selectAddr() {
				Position.getLocation(res => {
					Position.selectAddress(res.longitude, res.latitude, success => {
						this.setAddress(success)
					})
				})
				Position.getLocationDetail(res => {
					console.log(res);
					this.form.address = res
				})
			},
			
			
			// 上传图片
			afterRead(e) {
				let self = this
				e.file.forEach(file => {
					self.$Oss.ossUpload(file.url).then(url => {
						self[e.name].push({
							url
						})
					})
				})
			},
			// 删除图片
			deleteImage(e) {
				this[e.name].splice(e.index, 1)
			},
		},
	}
</script>


<style scoped lang="scss">
	.page {
		height: 100vh;

		.frame {
			background-color: #FFF;

			.title {
				display: flex;
				font-size: 30rpx;
				font-weight: 700;
				padding: 0rpx 0 0rpx 20rpx;
				margin: 20rpx 0 0 0;

				>span:nth-of-type(1) {
					margin: 4rpx 0 0 8rpx;
					background-color: #FFF;
				}

				>span:nth-of-type(2) {
					margin: 0 0 0 8rpx;
					background-color: #FFF;
				}
			}

			.basic-info {
				display: flex;
				flex-direction: column;
				gap: 10rpx;
				//margin-top: 20rpx;
				padding: 20rpx 20rpx 20rpx 20rpx;

				.addressA {
					display: flex;
					align-items: center;
					background-color: #FFF;
					height: 80rpx;

					.title {
						display: flex;
						align-items: center;
						justify-content: flex-start;
						font-weight: 400;
						width: 30%;
					}

					.address {
						width: 70%;
						display: flex;
						padding: 20rpx;
						background-color: #fff;

						image {
							width: 30rpx;
							height: 30rpx;
							margin: 10rpx;
						}

						view {

							//margin: 20rpx;
							overflow: hidden; //超出的文本隐藏
							text-overflow: ellipsis; //溢出用省略号显示
							white-space: nowrap; //溢出不换行
						}

						.icon {
							display: flex;
							justify-content: center;
							align-items: center;
							margin-left: auto;
						}
					}
				}


				.Tip {
					display: flex;
					align-items: center;
					justify-content: center;

					.span {
						color: #999;
						font-size: 24rpx;
					}
				}

				.btns {
					width: 100%;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					gap: 20rpx;

					.ljxd {
						display: flex;
						align-items: center;
						justify-content: center;
						width: 500rpx;
						height: 70rpx;
						border-radius: 40rpx;
						color: #FFF;
						font-size: 28rpx;
						margin: 20rpx 10rpx 0 0;
						background: $uni-color;
						//margin-top: 20rpx;
						border-radius: 40rpx;
					}

					.lxwm {
						display: flex;
						align-items: center;
						justify-content: center;
						width: 500rpx;
						height: 70rpx;
						border-radius: 40rpx;
						color: #000000;
						font-size: 28rpx;
						margin: 20rpx 10rpx 0 0;
						background: #f1f1f1;
						//margin-top: 20rpx;
						border-radius: 40rpx;
					}

					.tip {
						display: flex;
						align-items: center;
						justify-content: center;
						font-size: 24rpx;
						color: #999;
						//width: 500rpx;
					}
				}

				.item {
					display: flex;
					align-items: center;
					background-color: #FFF;
					margin: 10rpx 0 0 0;
					padding: 10rpx 20rpx;

					>view:nth-of-type(1) {
						width: 30%;
						// font-weight: 700;
					}

					>view:nth-of-type(2) {
						flex: 1;
						border-radius: 10rpx;
						overflow: hidden;
						input {
							background-color: #00000011;
							font-size: 28rpx;
							padding: 16rpx 8rpx 16rpx 15rpx;
						}
					}
				}

			}
		}
		// .input-color{
		// 	padding: 20rpx;
		// 	border-radius: 20rpx;
		// }
	}
</style>