<template>
	<view class="affirm-page">
		<view class="affirm-view overscroll">
			<!-- 门店信息 -->
			<form>
				<view class="module-com">
					<view class="commodity-view">
						<view class="commodity-list">
							<view
								class="commodity-item"
								v-for="(item, index) in selectList"
								:key="index"
							>
								<image class="item-icon" :src="item.cover" alt="商品图" />

								<view class="item-info">
									<view class="info-line">
										<text class="item-name">{{ item.itemName }}</text>
										<text class="item-price">
											<text class="unit">¥</text>

											<text class="price">
												{{ getSelectItemPrice(item) }}
											</text>
										</text>
									</view>
									<view class="info-line">
										<text class="item-type">{{ item.rank }}级</text>
										<text class="item-count">
											<text v-if="item.bigCount > 0 || item.smallCount > 0">{{
												`${item.bigCount}`
											}}</text>
											<text v-if="item.smallCount > 0">/</text>
											<text v-if="smallCount > 0">{{ `${smallCount}` }}</text>
										</text>
									</view>
								</view>
							</view>
						</view>
						<view class="summary-bar">
							<view class="operate-view" @click="gobackEvent">
								<text class="iconfont icongouwuche"></text>
								重选花材
							</view>
							<view class="describe-view">
								共{{ allCount }}种,
								<!-- 共16扎9支， -->
								合计 ¥<text class="price">{{ allPrice }}</text>
							</view>
						</view>
					</view>
				</view>
				<!-- 登录信息 -->
				<view class="module-com input-line-wrap">
					<!-- <tui-list-cell class="line-cell" :hover="false" :arrow="true">
						<view class="tui-title ">客户</view>
						<view class="tui-input" v-if="form.customId">
							{{ form.customName }}
						</view>
						<view class="tui-placeholder" v-else @click="showCustomer = true"
							>请选择客户</view
						>
					</tui-list-cell> -->
					<appBindInfo :nameTetx="'昵称'" @bindInfo="bindInfoEvent"></appBindInfo>
					<appFormSend :form.sync="form"> </appFormSend>
				</view>
			</form>
		</view>
		<view class="under-bar">
			<view class="price-view">
				<text class="price-title">收款</text>
				<text class="price-number">
					¥ <text class="large">{{ allPrice }}</text>
				</text>
			</view>
			<button class="admin-button-com blue middle" @click="formSubmit">
				下单
			</button>
		</view>
	</view>
</template>

<script>
import TuiListCell from "@/components/plugin/list-cell";
import AppAvatarModule from "@/components/module/app-avatar";
import SimpleAddress from "@/components/plugin/simple-address";
import AppAreaSel from "@/components/app-area-sel";
import productMins from "@/mixins/product";
import appFormRadioBtn from "@/components/app-formRadio-btn";
import appAddressGroup from "@/components/app-address-group";
import appSendTime from "@/components/app-send-time";
import appBindInfo from "@/components/app-bind-info";
import appFormSend from "@/components/app-form-send";
const formUtil = require("@/utils/formValidation.js");
import { createOrder } from "@/api/order/index";
import { mapActions, mapGetters } from "vuex";

export default {
	name: "BillingAffirm", // 开单确认
	components: {
		TuiListCell,
		AppAvatarModule,
		SimpleAddress,
		AppAreaSel,
		appFormRadioBtn,
		appAddressGroup,
		appSendTime,
		appFormSend,
		appBindInfo
	},
	mixins: [productMins],
	data() {
		return {
			pageType: "customer",
			autoLoad: false,
			form: {
				shopId: "",
				shopName: "",
				receiveProvince: "",
				receiveCity: "",
				receiveFloor: "",
				receiveAddress: "",
				receiveLong: "",
				receiveLat: "",
				sendType: "3",
				sendSide: "2",
				sendDate: "",
				sendTime: "",
				sendCost: "",
				customId: "",
				product: "",
				nickname: "",
				mobile: ""
			},

			// 详细地址
			// showRegion: false,
			showCustomer: false
		};
	},
	onLoad(options) {
		const { shopId } = options;
		if (shopId) {
			this.shopId = shopId;
		}
	},
	computed: {
		...mapGetters(["getLoginInfo"])
	},
	methods: {
		init() {
			this._getDet();
		},
		// api
		_getDet() {
			let data = uni.getStorageSync("modifyShopB");
			if (this.$util.isEmpty(data)) return;
			Object.keys(this.form).forEach((i, index) => {
				// if (i != 'password') {
				this.form[i] = data[i];
				// }
			});
		},

		changeCustomerFn(info) {
			console.log("changeCustomerFn", info);
			this.form.customId = info.id;
			this.form.customName = info.name;
		},
		confirmFn(options) {
			createOrder(options).then(res => {
				// this.$msg("下单成功");
				this.resetSelectInfoByType(this.pageType);
				const {
					data: { orderId, orderSn }
				} = res;
				// 如果是客户跳转到客户成功页pagesClient/official/clientApply   供货员工下单到 /admin/billing/result
				let url = `/admin/billing/result?orderId=${orderId}&orderSn=${orderSn}`;
				uni.redirectTo({
					url: url,
					success: result => {},
					fail: () => {},
					complete: () => {}
				});
			});
		},
		bindInfoEvent(info) {
			this.form.nickname = info.nickName;
			this.form.mobile = info.mobile;
		},
		// 表单验证
		async formSubmit(e) {
			try {
				// 表单规则
				let rules = [
					{
						name: "nickname",
						rule: ["required"],
						msg: ["请输入昵称"]
					},
					{
						name: "mobile",
						rule: ["required"],
						msg: ["请输入手机号"]
					},
					{
						name: "sendTime",
						rule: ["required"],
						msg: ["请输入具体时间"]
					}
				];
				// 进行表单检查
				let formData = this.form;

				if (formData.sendType == "1") {
				} else {
					let temp = [
						{
							name: "receiveProvince",
							rule: ["required"],
							msg: ["请选择地址"]
						},
						{
							name: "receiveAddress",
							rule: ["required"],
							msg: ["请填写详细地址"]
						},
						{
							name: "receiveFloor",
							rule: ["required"],
							msg: ["请输入门牌号"]
						}
					];
					rules = rules.concat(temp);
				}

				const product = this.selectList.map(ele => {
					return { productId: ele.id, num: ele.bigCount, smallNum: ele.smallCount };
				});
				formData.product = JSON.stringify(product);

				formData.shopId = this.shopId;
				formData.customId = this.getLoginInfo.id;
				if (this.waySele == 1) {
					formData.sendCost = "免运费";
				}
				console.log(9999, formData);
				let checkRes = formUtil.validation(formData, rules);
				// 验证通过!
				if (!checkRes) {
					this.confirmFn(formData);
				} else {
					this.$msg(checkRes);
				}
			} catch (error) {
				console.log(999999, error);
			}
		},
		gobackEvent() {
			uni.navigateBack({
				delta: 1
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.affirm-page {
	position: relative;
	height: 100%;
	display: flex;
	flex-direction: column;

	.affirm-view {
		flex: 1;
		padding: 20px;
		background-color: #f0f2f6;

		.commodity-view {
			display: flex;
			flex-direction: column;
			.commodity-list {
				padding: 20px;
				.commodity-item {
					display: flex;
					margin-bottom: 20px;
					.item-icon {
						flex-shrink: 0;
						width: 140px;
						height: 140px;
					}
					.item-info {
						display: flex;
						flex-direction: column;
						justify-content: center;
						flex: 1;
						margin-left: 20px;
						.info-line {
							margin-bottom: 20px;
							display: flex;
							justify-content: space-between;
							align-items: flex-end;
							.item-name {
								color: #666;
								font-size: 28px;
							}
							.item-price {
								color: #333;
								font-size: 22px;
								.price {
									font-size: 32px;
									font-weight: bold;
								}
							}
							.item-type {
								color: #999;
								font-size: 24px;
							}
							.item-count {
								color: #666;
								font-size: 24px;
							}
						}
					}
				}
			}
			.summary-bar {
				padding: 0 20px;
				height: 90px;
				display: flex;

				align-items: center;
				justify-content: space-between;
				border-top: 1px solid #eeeeee;
				.operate-view {
					display: flex;

					align-items: center;
					color: #3385ff;
					font-size: 26px;
					.iconfont {
						margin-right: 20px;
						font-size: 40px;
					}
				}
				.describe-view {
					display: flex;

					align-items: center;
					color: #333;
					font-size: 24px;
					.price {
						font-weight: bold;
						font-size: 36px;
					}
				}
			}
		}

		.module-com {
			background-color: #fff;
			margin-bottom: 20px;

			border-radius: 10px;
			overflow: hidden;
			.member-wrap {
				.member-det {
					font-size: 24px;
					margin-left: 20px;
				}
			}
			.remark-wrap {
				align-items: flex-start;
				.remark {
					height: 240px;
				}
			}
			// .tui-placeholder,
			// .tui-input {
			// 	padding-right: 30px;
			// 	width: 100%;
			// 	text-align: right;
			// }
		}
	}
	.under-bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 20px;
		width: 100%;
		height: 100px;
		background-color: #fff;

		box-shadow: 0px -1px 6px 0px rgba(4, 0, 0, 0.06);
		.price-view {
			display: flex;
			align-items: center;
			font-size: 24px;
			.price-title {
				color: #333;
			}
			.price-number {
				margin: 0 20px;
				color: #ff2842;
				.large {
					font-size: 40px;
				}
			}
		}
		.admin-button-com {
			width: 200px;
		}
	}
}
</style>
