<template>
	<view class="container">
		<view class="list" v-if="data&&data.goodsName">
			<view class="item u-flex u-col-center u-row-between">
				<view class="l">
					商品名称
				</view>
				<view class="r">
					{{data.goodsName}}
				</view>
			</view>
			<view class="item u-flex u-col-center u-row-between">
				<view class="l">
					{{type == 0 ? '销售' : '求购'}}商品数量
				</view>
				<view class="r">
					{{data.quantity}}
				</view>
			</view>
			<view class="item u-flex u-col-center u-row-between">
				<view class="l">
					单位规格
				</view>
				<view class="r">
					{{data.measureUnit}}
				</view>
			</view>
			<view class="item u-flex u-col-center u-row-between">
				<view class="l">
					商品类型
				</view>
				<view class="r">
					{{data.goodsType}}
				</view>
			</view>
			<view class="item u-flex u-col-center u-row-between">
				<view class="l">
					地区
				</view>
				<view class="r">
					{{data.address}}
				</view>
			</view>
			
			<view class="item u-flex u-col-center u-row-between">
				<view class="l">
					经营主体名称
				</view>
				<view class="r">
					{{data.companyname}}
				</view>
			</view>
			<view class="item u-flex u-col-center u-row-between">
				<view class="l">
					联系人
				</view>
				<view class="r">
					{{data.contact}}
				</view>
			</view>
			<view class="item u-flex u-col-center u-row-between">
				<view class="l">
					联系人电话
				</view>
				<view class="r">
					{{data.contactPhone}}
				</view>
			</view>
			<view class="item u-flex u-col-center u-row-between">
				<view class="l">
					备注
				</view>
				<view class="r">
					{{data.remark||''}}
				</view>
			</view>
		</view>
		<view class="f-btn" @click="submit" v-if="!add">
			删除
		</view>
	</view>
</template>

<script>
	import {salesDemand,purchaseDemand,deletionSale,deletePurchase,commodityCategory } from '@/api/home.js'
	import {enterpriseTpye ,sysUnit} from '@/api/dictionary.js'
	export default {
		data() {
			return {
				id: '',
				data: {

				},
				type: 0,
				list1:[],
				list2:[],
				// 判断是否从首页的销售,求购页面以及我得买卖里面求购信息进入
				add:false,
			}
		},
		onLoad(o) {
			this.id = o.id
			this.type = o.type
			this.add = o.add
			commodityCategory().then(res=>{
				this.list2 = res.data
			})
			sysUnit().then(res=>{
				this.list1 = res.data
			})
			this.getData()

		},
		onReady() {
			uni.setNavigationBarTitle({
				title: this.type == 0 ? '销售需求详情' : '求购需求详情'
			});
		},
		methods: {
			getData() {
				// let url = this.type == 0 ? `/managelef/demandManage/getSalesDemand/${this.id}` :
				// 	`/managelef/demandManage/getPurchaseDemand/${this.id}`
				if(this.type == 1){
					
					salesDemand(
					this.id
					).then(res => {
						if (res.code == 200) {
							console.log(res)
							this.data = res.data
							// 对销售规格,商品类型进行回显
							this.list1.forEach(item=>{
								if(item.dictValue == this.data.measureUnit){
									this.data.measureUnit = item.dictLabel
								}
							})
							this.list2.forEach(item=>{
								if(item.id == this.data.goodsType){
									this.data.goodsType = item.name
								}
							})
						} else {
							uni.showToast({
								title: res.msg,
								icon: "none",
								duration: 1500
							});
						}
					})
				}else{
					
					purchaseDemand(
					this.id
					).then(res => {
						if (res.code == 200) {
							console.log(res)
							this.data = res.data
							this.list1.forEach(item=>{
								if(item.dictValue == this.data.measureUnit){
									this.data.measureUnit = item.dictLabel
								}
							})
							this.list2.forEach(item=>{
								if(item.id == this.data.goodsType){
									this.data.goodsType = item.name
								}
							})
						} else {
							uni.showToast({
								title: res.msg,
								icon: "none",
								duration: 1500
							});
						}
					})
				}
				console.log(this.data,'23')
				
			},
			submit(){
				uni.showModal({
					title: '温馨提示',
					content: '确认要删除这条需求吗?',
					success: (r) => {
						if (r.confirm) {
							console.log('用户点击确定');
							if(this.type == 0){
								deletePurchase(this.id).then(res => {
									if (res.code == 200) {
										uni.showToast({
											title: '删除成功',
											icon: "none",
											duration: 1500
										});
										setTimeout(() => {
											uni.navigateBack({
												delta: 1
											})
										}, 1500)
									} else {
										uni.showToast({
											title: res.msg,
											icon: "none",
											duration: 1500
										});
									}
									console.log(res)
								
								})
							}else{
								deletionSale(this.id).then(res => {
									if (res.code == 200) {
										uni.showToast({
											title: '删除成功',
											icon: "none",
											duration: 1500
										});
										setTimeout(() => {
											uni.navigateBack({
												delta: 1
											})
										}, 1500)
									} else {
										uni.showToast({
											title: res.msg,
											icon: "none",
											duration: 1500
										});
									}
									console.log(res)
								
								})
							}
						
						} else if (r.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	$color: #4892FD;

	.container {

		.f-btn {
			position: fixed;
			bottom: 60rpx;
			left: 30rpx;
			right: 30rpx;
			height: 90rpx;
			line-height: 90rpx;
			text-align: center;
			background-color: $color;
			color: #fff;
			font-size: 28rpx;
			border-radius: 20rpx;
		}

		padding: 30rpx;

		.list {
			background-color: #fff;
			border-radius: 20rpx;
			padding: 10rpx 30rpx;
			width:100%;
			.item {
				padding: 15rpx 0;
				border-bottom: 1rpx solid #eee;

				&:last-child {
					border-bottom: 0;
				}

				.l {
					font-size: 30rpx;
					font-weight: bold;
					color: #333333;
					line-height: 60rpx;
					height: 60rpx;
					width: 200rpx;
				}

				.r {
					flex: 1;
					width: 1rpx;
					font-size: 28rpx;
					color: #333333;
					line-height: 40rpx;
					text-align: right;

				}
			}
		}

	}
</style>