<template>
	<view class="ex-page">
		<view class="main-view overscroll">
			<view class="space-view ex-info">
				<form>
					<view class="module-com input-line-wrap">
						<TuiListCell class="line-cell" :hover="false" :arrow="true">
							<view class="tui-title ">收货门店</view>
							<view class="tui-input">
								<DorpdownSelect
									:menuObj.sync="menuObj"
									top="0"
									:isShowMore="false"
									:selectItemId.sync="form.inShopId"
									@selectSussess="selectSussess"
								></DorpdownSelect>
							</view>
						</TuiListCell>
						<TuiListCell class="line-cell" :hover="false" :arrow="true">
							<view class="tui-title ">日期</view>
							<AppDatePicker :dateVal.sync="form.date" :placeholder="'请选择日期'" />
						</TuiListCell>
						<TuiListCell class="line-cell" :hover="false" :arrow="false">
							<view class="tui-title ">备注</view>
							<input
								v-model="form.remark"
								placeholder-class="phcolor"
								class="tui-input"
								placeholder="请输入"
								maxlength="50"
								type="text"
							/>
						</TuiListCell>
					</view>
				</form>
			</view>
			<view class="space-view ex-table">
				<block v-if="!$util.isEmpty(selectList)">
					<t-table :headerBackgroundColor="'#F0F2F6'">
						<t-tr header>
							<t-th v-for="(item, index) in columns" :key="index" :align="item.align">
								{{ item.name }}
							</t-th>
						</t-tr>
						<t-tr v-for="(item, index) in selectList" :key="index">
							<t-td v-for="(column, colIndex) in columns" :key="colIndex" :align="column.align" :color="column.color">
								<view v-if="column.custom == 'icon'" class="list-icon">
									<image class="icon-image" :src="item.cover" alt="商品图" />
									<view class="icon-info">
										<view class="info-name">
											{{ item.itemName }}
										</view>
										<view class="info-type"> {{ item.rank }}级 </view>
									</view>
								</view>
								<view v-else-if="column.custom == 'excount'">
									<text v-if="item.bigCount > 0 || item.smallCount > 0">{{
										`${item.bigCount}`
									}}</text>
									<text v-if="item.smallCount > 0">/</text>
									<text v-if="item.smallCount > 0">{{ `${item.smallCount}` }}</text>
								</view>
								<view v-else>
									{{ item[column.dataIndex] || "" }}
								</view>
							</t-td>
						</t-tr>
					</t-table>
				</block>
				<view class="btn-bar">
					<button class="admin-button-com big blue" @click="gotoSelectPage">
						选择商品
					</button>
				</view>
			</view>
		</view>
		<view class="bar-view" v-if="!$util.isEmpty(selectList)">
			<view class="btn-view">
				<button class="admin-button-com middle" @click="createStockOutOrder">
					直接出库
				</button>
				<button class="admin-button-com middle" @click="confirmStockOutOrder">
					保存草稿
				</button>
			</view>
		</view>
	</view>
</template>
<script>
import { getMainProfile } from "@/api/statistics";

import TuiListCell from "@/components/plugin/list-cell";
import productMins from "@/mixins/product";
import AppDatePicker from "@/components/app-date-picker";
import DorpdownSelect from "@/components/module/dorpdownSelect";
import { selectShop } from "@/mixins";
import { createStockOutOrderApi } from "@/api/storehouse/stock";
import {mapActions, mapGetters} from "vuex";
const formUtil = require("@/utils/formValidation.js");

export default {
	components: { TuiListCell, AppDatePicker, DorpdownSelect },
	mixins: [productMins, selectShop],
	computed: {		...mapGetters(["getLoginInfo"])	},
	data() {
		return {
			pageType: "allotEx",
			autoLoad: false,
			form: {
				date: "",
				remark: "",
				inShopId: ""
			},
			columns: [
				{
					name: "花材名称",
					custom: "icon"
				},
				{
					name: "库存",
					dataIndex: "stock",
					// color: "success",
					width: 120,
					align: "center"
				},
				{
					name: "出库数量",
					custom: "excount",
					// color: "warn",
					width: 120,
					align: "center"
				}
			]
		};
	},
	onShow() {
		console.log('拿到选择商品列表', this.selectList);
	},
	onLoad(){
		this.form.inShopId = this.getLoginInfo.shopId;
	},
	methods: {
		...mapActions(["resetSelectInfoByType"]),
		/**
			* 清楚页面选中的商品列表  姜枫 2021.03.27
			* */
		clearSelectList(){
			this.resetSelectInfoByType(this.pageType)
		},
		async init() {
			// this.form.inShopId = this.userInfo.id;

		},
		gotoSelectPage() {
			uni.navigateTo({
				url: `/pagesStorehouse/allot/exSelect`,
				success: result => {},
				fail: () => {},
				complete: () => {}
			});
		},

		selectSussess(val) {
			this.form.inShopId = val.selectItem.id;
		},
		createStockOutOrder() {
			try {
				let rules = [
					{
						name: "date",
						rule: ["required"],
						msg: ["请选择日期"]
					},
					{
						name: "inShopId",
						rule: ["required"],
						msg: ["请选择门店"]
					}
				];
				let formData = this.form;
				const itemInfo = this.selectList.map(ele => {
					return {
						productId: ele.id,
						bigNum: ele.bigCount,
						smallNum: ele.smallCount
					};
				});
				formData.itemInfo = JSON.stringify(itemInfo);

				formData.action = "confirm";

				let checkRes = formUtil.validation(formData, rules);
				// 验证通过!
				if (!checkRes) {
					let self = this;
					uni.showModal({
						title: '提示',
						content: '确定直接出库？',
						success: function (res) {
							if (res.confirm) {
								self.confirmStockOut(formData)
							}
						}
					});
					// await createStockOutOrderApi(formData);
					// self.$msg("出库成功");
					// self.clearSelectList();
					// uni.navigateBack({
					// 	delta: 1
					// });

				} else {
					this.$msg(checkRes);
				}
			} catch (error) {}
		},
		confirmStockOutOrder() {
			try {
				let rules = [
					{
						name: "date",
						rule: ["required"],
						msg: ["请选择日期"]
					},
					{
						name: "inShopId",
						rule: ["required"],
						msg: ["请选择门店"]
					}
				];
				let formData = this.form;
				const itemInfo = this.selectList.map(ele => {
					return {
						productId: ele.id,
						bigNum: ele.bigCount,
						smallNum: ele.smallCount
					};
				});
				formData.itemInfo = JSON.stringify(itemInfo);

				formData.action = "wait";

				let checkRes = formUtil.validation(formData, rules);
				// 验证通过!
				if (!checkRes) {
					let self = this;
								self.confirmStockOut(formData)
				} else {
					this.$msg(checkRes);
				}
			} catch (error) {}
		},
		async confirmStockOut(formData){
			let { data } = await createStockOutOrderApi(formData);
			this.$msg("确认成功");
			this.clearSelectList();
			uni.navigateTo({url:'/pagesStorehouse/allot/exDetails?orderSn=' + data.orderSn});
			// uni.navigateBack({delta: 1});
		},
	}
};
</script>
<style lang="scss" scoped>
.ex-page {
	height: 100%;
	display: flex;
	flex-direction: column;
	.main-view {
		flex: 1;
		background-color: #f9fbfc;
		.space-view {
			margin-top: 15upx;
			background-color: #fff;
			.btn-bar {
				margin-top: 50upx;
				width: 100%;
				text-align: center;
			}
		}
		.ex-info {
			// padding: 0 30upx;
			.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;
				// }
			}
		}
		.ex-table {
			/deep/.list-icon {
				display: flex;
				.icon-image {
					width: 80upx;
					height: 80upx;
					margin-right: 20upx;
				}
				.icon-info {
					display: flex;
					flex-direction: column;
					justify-content: space-around;
					.info-name {
						font-size: 28upx;
						font-weight: bold;
						color: #333333;
					}
					.info-type {
						font-size: 24upx;
						color: #666666;
					}
				}
			}
		}
	}
	.bar-view {
		display: flex;
		justify-content: flex-end;
		align-items: center;
		padding: 0 30upx;
		width: 100%;
		height: 100upx;

		box-shadow: 0px -1px 6px 0px rgba(4, 0, 0, 0.06);

		.btn-view {
			display: flex;
			.admin-button-com {
				margin: 0 10upx;
			}
		}
	}
}
</style>
