<template>
	<!-- 入库详情 -->
	<view class="ex-page">
		<view class="main-view overscroll">
			<view class="space-view ex-info">
				<view class="info-cell">
					<view class="cell-label"> 收货仓库</view>
					<view class="cell-value"> {{ putData.shopName || "" }}</view>
				</view>
				<view class="info-cell">
					<view class="cell-label"> 日期</view>
					<view class="cell-value"> {{ putData.updateTime || "" }}</view>
				</view>
				<view class="info-cell">
					<view class="cell-label"> 制单人</view>
					<view class="cell-value"> {{ putData.adminName || "" }}</view>
				</view>
				<view class="info-cell">
					<view class="cell-label"> 状态</view>
					<view
						:class="[
							'cell-value',
							{ warning: putData.status == PUT_STATUS.CONFIRM },
							{ success: putData.status == PUT_STATUS.DONE }
						]"
					>
						{{ getItemStatusName(putData.status) || "" }}</view
					>
				</view>
				<view class="info-cell">
					<view class="cell-label"> 备注</view>
					<view class="cell-value">
						{{ putData.remark || "" }}
					</view>
				</view>
			</view>
			<view class="space-view ex-table">
				<block v-if="!$util.isEmpty(putData.itemInfo)">
					<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 putData.itemInfo" :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.itemCover" 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 == 'stock'">
									<text > {{ item.bigNumStock }}/ </text>
									<text>
										{{ item.smallNumStock }}
									</text>
								</view>
								<view v-else-if="column.custom == 'put'">
									<text > {{ item.bigNum }}/ </text>
									<text>
										{{ item.smallNum }}
									</text>
								</view>
								<view v-else>
									{{ item[column.dataIndex] || "" }}
								</view>
							</t-td>
						</t-tr>
					</t-table>
				</block>
				<block v-else>
					<AppWrapperEmpty
						title="暂无数据"
						:is-empty="$util.isEmpty(putData.itemInfo)"
					/>
				</block>
			</view>
		</view>
		<view class="bar-view">
			<view class="info-view"> 入库数量：{{ sumCount }} </view>
			<view class="btn-view" v-if="putData.status == PUT_STATUS.CONFIRM">
				<button class="admin-button-com middle" @click="confirmPutEvent">
					入库
				</button>
			</view>
		</view>
	</view>
</template>
<script>
import { PUT_STATUS } from "@/utils/declare";
import { getStockInDetailApi, confirmOrderApi } from "@/api/storehouse/stock";
import AppWrapperEmpty from "@/components/app-wrapper-empty";

export default {
	components: { AppWrapperEmpty },
	data() {
		return {
			PUT_STATUS,
			orderSn: "",
			putData: {},
			columns: [
				{
					name: "花材名称",
					custom: "icon"
				},
				{
					name: "库存",
					custom: "stock",
					// color: "success",
					width: 120,
					align: "center"
				},
				{
					name: "入库数量",
					custom: "put",
					// color: "warn",
					width: 120,
					align: "center"
				}
			]
		};
	},
	onLoad(options) {
		const { orderSn } = options;
		if (orderSn) {
			this.orderSn = orderSn;
			this.initData();
		}
	},
	computed: {
		sumCount() {
			let big = 0;
			let small = 0;
			if (this.putData.itemInfo) {
				for (const item of this.putData.itemInfo) {
					big += item.bigNum;
					small += item.smallNum;
				}
			}
			return `${big}/${small}`;
		}
	},
	methods: {
		async initData() {
			try {
				const { data } = await getStockInDetailApi(this.orderSn);
				this.putData = data;
			} catch (error) {}
		},
		getItemStatusName(status) {
			let name = "";
			switch (status) {
				case PUT_STATUS.CONFIRM:
					name = "待入库";
					break;
				case PUT_STATUS.DONE:
					name = "已入库";
					break;
				case PUT_STATUS.CANCEL:
					name = "已取消";
					break;

				default:
					break;
			}
			return name;
		},
		confirmPutEvent(){
			let self = this;
			uni.showModal({
				title: '提示',
				content: '确定入库',
				success: function (res) {
					if (res.confirm) {
						self.putEvent();
					}
				}
			});
		},
		async putEvent() {
			try {
				await confirmOrderApi(this.orderSn);
				await this.initData();
				wx.showToast({
					title: "入库成功"
				});
			} catch (error) {}
		}
	}
};
</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;
		}
		.ex-info {
			padding: 30upx;
			.info-cell {
				display: flex;
				align-items: center;
				font-size: 28upx;
				&:not(:first-child) {
					margin-top: 20upx;
				}
				.cell-label {
					width: 110upx;
					color: #666666;
				}
				.cell-value {
					margin-left: 40upx;
					color: #333;
					&.warning {
						color: #ffaf1d;
					}
					&.success {
						color: #27c325;
					}
				}
			}
		}
		.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: space-between;
		align-items: center;
		padding: 0 30upx;
		width: 100%;
		height: 100upx;

		box-shadow: 0px -1px 6px 0px rgba(4, 0, 0, 0.06);
		.info-view {
			font-size: 26upx;
		}
		.btn-view {
			display: flex;
			.admin-button-com {
				margin: 0 10upx;
			}
		}
	}
}
</style>
