<template>
	<view>
		<view class="address" @click="address()">
			<view class="con" v-if="item.addressName">
				<view class="title omit">{{ item.addressName }}</view>
				<view class="desc">
					<text>{{ item.name }}</text>
					<text>{{ item.phone }}</text>
				</view>
			</view>
			<view class="con" v-else>
				<view class="title omit">你还没添加收货地址</view>
				<view class="desc">
					<text>点击添加地址</text>
				</view>
			</view>
			<view class="icon">&#xe8f2;</view>
		</view>
		<view class="forms">
			<view class="form_group">
				<view class="lable re">互市区</view>
				<picker :range="tradeAreaList" @change="picker($event, 'tradeAreaName')" range-key="name">
					<input placeholder="请选择" v-model="item.tradeAreaName" :disabled="true" />
					<view class="icon more">&#xe8f2;</view>
				</picker>
			</view>
			<view class="form_group">
				<view class="lable re">收购商品</view>
				<picker @click="selectGoods()" :disabled="true">
					<input placeholder="请选择" v-model="item.goodsName" :disabled="true" />
					<view class="icon more">&#xe8f2;</view>
				</picker>
			</view>
			<view class="form_group">
				<view class="lable">商品编号</view>
				<input placeholder="请输入" v-model="item.goodsNo" />
			</view>
			<view class="form_group">
				<view class="lable re">计价单位</view>
				<input placeholder="请输入" v-model="item.goodsUnit" :disabled="true" />
			</view>
			<view class="form_group">
				<view class="lable re">数量</view>
				<input type="number" placeholder="请输入" v-model="item.goodsQuantity" />
			</view>
			<view class="form_group">
				<view class="lable re">到货日期</view>
				<picker mode="date" @change="picker($event, 'arrivalTime')">
					<input placeholder="请选择" v-model="item.arrivalTime" :disabled="true" />
					<view class="icon more">&#xe8f2;</view>
				</picker>
			</view>
		</view>
		<view class="mfooter">
			<view class="flex">
				<view class="f">
					<button class="save btn" @click="save(0)">保存</button>
				</view>
				<view class="f">
					<button class="btn" @click="save(1)">一键发布</button>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			item: {},
			tradeAreaList: []
		};
	},
	onLoad(e) {
		if (e.id) {
			this.http.request({
				url: '/level-two-server/app/TbGoodsDemand/getById?id=' + e.id,
				success: res => {
					this.item = res.data.data;
					uni.setNavigationBarTitle({ title: '编辑求购信息' });
				}
			});
		}
		this.getTradeAreaList();
		//选择地址回显
		uni.$on('selectAddress', res => {
			this.item.addressName = res.province + ' ' + res.city + ' ' + res.area + ' ' + res.street;
			this.item.name = res.name;
			this.item.phone = res.phone;
			this.$forceUpdate();
		});
		//选择商品回显
		uni.$on('selects', res => {
			this.item.goodsName = res.name;
			this.item.levelOneGoodsId = res.id;
			this.item.goodsImg = res.avatar;
			this.item.goodsNo = res.code;
			this.item.goodsUnit = res.unit;
			this.$forceUpdate();
		});
	},
	methods: {
		// 获取贸易区域列表
		getTradeAreaList() {
			this.http.request({
				url: '/level-one-server/app/TbTradeArea/getList',
				success: res => {
					this.tradeAreaList = res.data.data;
				}
			});
		},
		//选择地址
		address() {
			uni.navigateTo({ url: '/pages/market/two/purchaser/address/list?select=true' });
		},
		//选择商品
		selectGoods() {
			if (this.item.tradeAreaId) {
				uni.navigateTo({ url: '/pages/market/one/merchant/goods/selects?tradeAreaId=' + this.item.tradeAreaId + '&tradeAreaName=' + this.item.tradeAreaName });
			} else {
				uni.showModal({ content: '请先选择互市区', showCancel: false });
				return;
			}
		},
		picker(e, tag) {
			if (tag == 'arrivalTime') {
				this.item[tag] = e.detail.value;
			}
			if (tag == 'tradeAreaName') {
				this.item.tradeAreaName = this.tradeAreaList[e.detail.value].name;
				this.item.tradeAreaId = this.tradeAreaList[e.detail.value].id;
			}
			this.$forceUpdate();
		},
		//保存或发布
		save(isRelease) {
			let rule = [
				{ name: 'tradeAreaName', checkType: 'notnull', errorMsg: '请选择互市区' },
				{ name: 'goodsName', checkType: 'notnull', errorMsg: '请选择商品' },
				{ name: 'addressName', checkType: 'notnull', errorMsg: '请选择地址' },
				{ name: 'goodsQuantity', checkType: 'notnull', errorMsg: '请输入数量' },
				{ name: 'arrivalTime', checkType: 'notnull', errorMsg: '请选择到货日期' }
			];
			if (!this.verify.check(this.item, rule)) {
				uni.showModal({ content: this.verify.error, showCancel: false });
				return false;
			}
			this.item.isRelease = isRelease;
			delete this.item.createTime;
			delete this.item.updateTime;
			let action = this.item.id ? 'update' : 'add';
			this.http.request({
				url: '/level-two-server/app/TbGoodsDemand/' + action,
				method: 'POST',
				data: this.item,
				success: res => {
					uni.showToast({ title: '操作成功' });
					setTimeout(() => {
						uni.$emit('purchaser_buy_list');
						uni.navigateBack();
					}, 1000);
				}
			});
		}
	}
};
</script>

<style lang="scss">
.address {
	border-bottom: 1px solid $line;
}
.btn {
	width: 100%;
	border-radius: 5px;
}
</style>
