<template>
	<view class="order-page">
		<view scroll-y class="order-list">
			<block v-if="!$util.isEmpty(list.data)">
				<OrderItem v-for="(item, index) in list.data" :key="index" :info="item" @click="gotoDetails(item)"></OrderItem>
			</block>
			<block v-else>
				<AppWrapperEmpty title="暂无数据" :is-empty="$util.isEmpty(list.data)" />
			</block>
		</view>

		<view class="app-footer open_btn">
			<view @click="addEvent" class="admin-button-com middle blue">新增采购</view>
		</view>
	</view>
</template>
<script>
import Tabs from "@/components/plugin/tabs";
import OrderItem from "./orderItem";
import AppWrapperEmpty from "@/components/app-wrapper-empty";

import { list } from "@/mixins";
import { PURCHASE_ORDER_STATUS } from "@/utils/declare";
import { getPurchaseListApi } from "@/api/purchase/index";
export default {
	components: {
		Tabs,
		OrderItem,
		AppWrapperEmpty
	},
	mixins: [list],

	data() {
		return {
			PURCHASE_ORDER_STATUS,
			tabIndex: 0,
			tabs: [
				{
					name: "全部",
					key: "",
					value: "25"
				},
				{
					name: "待确认",
					key: "1",
					value: "25"
				},
				{
					name: "待配送",
					key: "2",
					value: "25"
				},
				{
					name: "配送中",
					key: "3",
					value: "25"
				},
				{
					name: "已完成",
					key: "4",
					value: "25"
				}
			]
		};
	},
	onPullDownRefresh() {
		this.resetList();
		this._list().then(res => {
			uni.stopPullDownRefresh();
		});
	},
	methods: {
		async init() {
			this._list();
		},
		_list() {
			let options = {
				page: this.list.page,
				pageSize: this.list.pageSize,
				status: this.tabs[this.tabIndex].key
			};
			return getPurchaseListApi(options)
				.then(res => {
					const {
						data: { allNum, unPayNum, unSendNum, sendingNum, finishNum }
					} = res;

					this.completes(res);
					for (let index = 0; index < this.tabs.length; index++) {
						const element = this.tabs[index];
						if (element.key == "") {
							// this.tabs[index].value = allNum;
							this.$set(this.tabs, index, {
								...this.tabs[index],
								value: allNum
							});
						} else if (element.key == PURCHASE_ORDER_STATUS.CONFIRM) {
							this.tabs[index].value = unPayNum;
						} else if (element.key == PURCHASE_ORDER_STATUS.UNDIS) {
							this.tabs[index].value = unSendNum;
						} else if (element.key == PURCHASE_ORDER_STATUS.DIS) {
							this.tabs[index].value = sendingNum;
						} else if (element.key == PURCHASE_ORDER_STATUS.DONE) {
							this.tabs[index].value = finishNum;
						}
					}
				})
				.catch(err => {
					console.log(3333, err);
				});
		},
		changeTabEvent(info) {
			const { index } = info;
			if (this.tabIndex == index) {
				return false;
			} else {
				this.tabIndex = index;
				this.resetList();
				this._list();
			}
		},
		getItemStatusName(status) {
			let name = "";
			switch (status) {
				case OUT_STATUS.CONFIRM:
					name = "待出库";
					break;
				case OUT_STATUS.DONE:
					name = "已出库";
					break;

				default:
					break;
			}
			return name;
		},
		addEvent() {
			uni.navigateTo({
				url: `/pagesPurchase/add`,
				success: result => {},
				fail: () => {},
				complete: () => {}
			});
		},
		gotoDetails(item) {
			uni.navigateTo({
				url: `/pagesPurchase/info?orderSn=${item.orderSn}`,
				success: result => {},
				fail: () => {},
				complete: () => {}
			});
		}
	}
};
</script>
<style lang="scss" scoped>
.order-page {
	height: 100%;
	display: flex;
	flex-direction: column;
	background-color: #f9fbfc;
	.order-list {
		padding-top: 20upx;
		flex: 1;
		// padding-bottom: 50upx;
		.allot-item {
			margin-bottom: 1px;
			padding: 30upx;
			display: flex;
			align-items: center;
			box-shadow: 0px 1px 0px 0px #eeeeee;
			background-color: #fff;
			.info {
				flex: 1;
				margin-right: 20px;
				.info-order {
					font-size: 32upx;
					font-weight: 600;
					color: #333333;
				}
				.info-name {
					margin: 40upx 0 20upx;
					font-size: 28upx;
					font-weight: 400;
					color: #666666;
				}
				.info-time {
					margin-right: 30upx;
					font-size: 28upx;
					font-weight: 400;
					color: #666666;
				}
			}
			.status {
				display: flex;
				flex-direction: column;
				align-items: center;
				flex-shrink: 0;
				width: 100upx;
				color: #cccccc;
				.status-name {
					margin-top: 10upx;
					font-size: 24upx;
				}
				.iconfont {
					font-size: 46upx;
				}
				&.primary {
					color: #3385ff;
				}
				&.gray {
					color: #cccccc;
				}
				&.warn {
					color: #ffaf1d;
				}
			}
		}
	}
	.bar-view {
		flex-shrink: 0;
		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;
				width: 140upx;
			}
		}
	}
}
</style>
