<template>
	<view class="create-page">
		<view class="create-title">创建新的订单</view>
		<view class="create-submit">完善创建订单用户与订单类型</view>
		<view class="create-from">
			<block v-if="createType === 'other'">
				<view class="create-user" @click="() => this.$Router.push({name: 'OrderAvailable'})">
					<view class="cover">
						<image class="cover-src" v-if="user != ''" :src="user.avatar" mode="aspectFill"></image>
						<u-icon v-else name="plus" color="#ddd" size="24"></u-icon>
					</view>
					<view class="nickname nowrap" :class="{'hide': user == ''}">{{user.nickname || '请选择办理用户'}}</view>
					<view class="icon">
						<u-icon class="icon-u" name="arrow-right" color="#ddd" size="20"></u-icon>
					</view>
				</view>
			</block>
			<view class="create-type">
				<view class="create-type-title">选择业务类型</view>
				<block v-for="(item, index) in businessArr" :key="index">
					<view class="create-type-item" :class="{'active': (item.business_id == businessId || item.synthesis_id == businessId) && item.self_type == selfType}" @click="onBusiness(item.business_id || item.synthesis_id, item.self_type)">
						<image class="create-type-icon" :src="item.cover_url || item.cover" mode="aspectFill"></image>
						<view class="create-type-text">
							<view class="title nowrap">{{item.title || '-'}}</view>
							<view class="submit nowrap">{{item.self_type == 'synthesis' ? '相关个人法律咨询服务' : item.subtitle || '-'}}</view>
						</view>
					</view>
				</block>
			</view>
			<view class="create-btn">
				<button size="default" @click="onNext()">下一步</button>
			</view>
		</view>
	</view>
</template>

<script>
	import { business } from '@/apis/interfaces/business.js'
	import { synthList } from '@/apis/interfaces/synthesis.js'
	export default {
		data() {
			return {
				createType : '',
				businessArr: [],
				businessId : '',
				selfType   : '',
				user	   : ''
			};
		},
		onShow(){
			let user = this.$store.getters.getUser
			this.createType = this.$Route.query.type
			if(user != ''){
				this.user = user
				this.$store.commit('setUser', '')
			}
		},
		created() {
			
			uni.showLoading({
				title: '加载中...',
				mask : true 
			})
			
			Promise.all([business(), synthList()]).then(res => {
				let businessData = res[0]
				let synthListData = res[1]
				
				this.businessArr = this.createType === 'other' ? businessData : businessData.concat(synthListData)
				this.businessId	 = this.businessArr[0].business_id
				this.selfType	 = this.businessArr[0].self_type
			}).catch(err => {
				uni.showToast({
					title: err.message,
					icon : 'none'
				})
			}).finally(() => {
				uni.hideLoading()
			})
		},
		methods: {
			// 选择业务类型
			onBusiness(id, type){
				this.selfType	= type
				this.businessId = id
			},
			// 创建业务单
			onNext(){
				if(this.createType == 'other' && !this.user.user_id){
					uni.showToast({
						title: '请选择办理用户',
						icon : 'none'
					})
					return
				}
				// 个人综法业务
				if(this.selfType === 'synthesis'){
					let synthesisArr = []
					let synthesisObj = {}
					for(let val of this.businessArr){
						if(val.self_type == 'synthesis'){
							synthesisArr.push(val)
						}
					}
					synthesisObj = synthesisArr.find(val => val.synthesis_id === this.businessId)
					
					if(synthesisObj.can.buy){
						this.$Router.replace({
							name	: 'FeeWrite',
							params	: {
								synthesisId	: synthesisObj.synthesis_id
							}
						})
						return
					}
					uni.showModal({
						title		: '提示',
						content		: '开通个人365会员，尊享全年不限次数免费法律咨询，是否立即开通？',
						confirmText	: '开通会员',
						cancelText	: '稍后开通',
						success 	: modalRes => {
							if(modalRes.confirm){
								this.$Router.replace({
									name: 'Individual'
								})
							}
						}
					})
					return
				}
				// 金法业务
				let busines = this.businessArr.find(val => val.business_id === this.businessId)
				this.$Router.replace({
					name	: 'WorkGenerate',
					params	: {
						businessTitle	: busines.title,
						businessId		: busines.business_id,
						serviceUser		: this.user.user_id,
						createType		: this.createType
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.create-page{
		padding: 50rpx;
		box-sizing: border-box;
		min-height: 100vh;
		overflow: hidden;
		.create-title{
			text-align: center;
			font-weight: bold;
			font-size: 44rpx;
		}
		.create-submit{
			text-align: center;
			font-size: 30rpx;
			color: gray;
			padding-top: 10rpx;
		}
		.create-from{
			padding-top: 50rpx;
		}
		// 目标用户
		.create-user{
			background: #f8f8f8;
			border-radius: $radius;
			padding: 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			.cover{
				width: 88rpx;
				height: 88rpx;
				background: white;
				border-radius: $radius;
				margin-right: 20rpx;
				border: dashed 1rpx #ddd;
				display: flex;
				justify-content: center;
				align-items: center;
				overflow: hidden;
				.cover-src{
					width: 100%;
					height: 100%;
				}
			}
			.nickname{
				width: calc(100% - 188rpx);
				line-height: 70rpx;
				font-size: 32rpx;
				&.hide{
					color: gray;
				}
			}
			.icon{
				width: 80rpx;
				text-align: right;
				.icon-u{
					display: inline-block;
				}
			}
		}
		// 选择业务类型
		.create-type{
			padding-top: 50rpx;
			.create-type-title{
				text-align: center;
				line-height: 50rpx;
				color: gray;
				font-size: 30rpx;
			}
			.create-type-item{
				background: #f8f8f8;
				border-radius: $radius;
				padding: 30rpx;
				margin-top: 30rpx;
				border:solid 1rpx #f8f8f8;
				box-sizing: border-box;
				display: flex;
				align-items: center;
				.create-type-icon{
					width: 88rpx;
					height: 88rpx;
					margin-right: $margin;
					background-color: white;
					border-radius: $radius;
				}
				.create-type-text{
					line-height: 40rpx;
					.title{
						font-size: 30rpx;
					}
					.submit{
						color: gray;
						font-size: 26rpx;
					}
				}
				&.active{
					border:solid 1rpx $main-color;
				}
			}
		}
		// 按钮
		.create-btn{
			margin-top: 90rpx;
			button[size="default"]{
				height: 100rpx;
				line-height: 100rpx;
				background: $main-color;
				font-size: 32rpx;
				color: white;
				font-weight: bold;
				&::after{
					border: none;
				}
			}
		}
	}
</style>
