<template>
	<view class="sales-list-wrap">
		<u-dropdown class="search-top-box" id="tjCons">
			<u-dropdown-item v-model="searchForm.billStatus" @change="handleChange('bill')" title="业务状态" :options="billStatusOpt"></u-dropdown-item>
			<u-dropdown-item v-model="searchForm.financialStatus" @change="handleChange('financial')" title="财务状态" :options="financialStatusOpt"></u-dropdown-item>
		</u-dropdown>
		<!-- 销售单列表 -->
		<view class="list-box">
			<listComponent ref="salesList" :height="listHeight" :params="searchParams" />
		</view>
		<!-- 查询右侧弹框 -->
		<salesSearch :openModal="openModal" :defaultParams="searchForm" @refresh="refresh" @close="openModal=false" />
	</view>
</template>

<script>
	import ListComponent from './listComponent.vue'
	import SalesSearch from './salesSearch.vue'
	import { getLookUpItem } from '@/api/data'
	import { salesList } from '@/api/sales'
	export default{
		components: { ListComponent, SalesSearch },
		data(){
			return{
				listData: [],
				pageNo: 1,
				pageSize: 6,
				totalNum: 0,
				noDataText: '暂无数据',
				billStatusOpt: [],
				financialStatusOpt: [],
				searchForm: {
					billStatus: null,
					financialStatus: null
				},
				searchParams: {},
				listHeight: 200,
				openModal: false
			}
		},
		onNavigationBarButtonTap(e){  // 标题栏 按钮操作
			this.openModal = true
		},
		onBackPress() {
			if(this.openModal){
				this.openModal = false
				return true
			}
		},
		onReady() {
			const query = uni.createSelectorQuery().in(this);
			query.select('#tjCons').boundingClientRect(data => {
			  this.listHeight = Math.floor(data.height + 10)
			}).exec();
		},
		onLoad() {
			const _this = this
			this.getLookUpItem('SALES_BILL_STATUS')
			this.getLookUpItem('FINANCIAL_RECEIVE_STATUS')
			this.$nextTick(function(){
				// 监听整改完成后刷新事件
				uni.$on('refreshSalesBL', function(data){
					_this.$refs.salesList.getList(1)
				})
			})
		},
		onUnload() {
			uni.$off('refreshSalesBL')
		},
		methods: {
			// 获取查询参数 刷新列表
			refresh(params){
				const _this = this
				const data = {}
				if(this.searchForm.billStatus){
					data.billStatus = this.searchForm.billStatus
				}
				if(this.searchForm.financialStatus){
					data.financialStatus = this.searchForm.financialStatus
				}
				
				if(params){
					if(params.beginDate){
						data.beginDate = params.beginDate
					}
					if(params.endDate){
						data.endDate = params.endDate
					}
					if(params.buyerName){
						data.buyerName = params.buyerName
					}
					if(params.salesBillNo){
						data.salesBillNo = params.salesBillNo
					}
					if(params.purchaseBillNo){
						data.purchaseBillNo = params.purchaseBillNo
					}
					if(params.settleStyleSn){
						data.settleStyleSn = params.settleStyleSn
					}
				}
				
				this.searchParams = data
				this.searchParams.beginDate = data.beginDate ? (data.beginDate + ' 00:00:00') : ''
				this.searchParams.endDate = data.endDate ? (data.endDate + ' 23:59:59') : ''
				console.log(this.searchParams,'this.searchParams')
				this.$nextTick(function(){
					_this.$refs.salesList.refash()
				})
			},
			// 状态  change
			handleChange(type){
				if(type == 'bill'){
					this.searchParams.billStatus = this.searchForm.billStatus
				}else if(type == 'financial'){
					this.searchParams.financialStatus = this.searchForm.financialStatus
				}
				this.$refs.salesList.refash()
			},
			// 配送方式
			getLookUpItem (type) {
				getLookUpItem({ lookupCode: type, pageNo: 1, pageSize: 1000 }).then(res => {
					if (res.status == 200 && res.data && res.data.list) {
						res.data.list.map(item => {
							item.label = item.dispName
							item.value = item.code
						})
						res.data.list.splice(0, 0, { label: '全部', value: null })
						if(type == 'SALES_BILL_STATUS'){
							this.billStatusOpt = res.data.list
						}else if(type == 'FINANCIAL_RECEIVE_STATUS'){
							this.financialStatusOpt = res.data.list
						}
					} else {
						if(type == 'SALES_BILL_STATUS'){
							this.billStatusOpt = []
						}else if(type == 'FINANCIAL_RECEIVE_STATUS'){
							this.financialStatusOpt = []
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.sales-list-wrap{
		width: 100%;
		.search-top-box{
			background-color: #fff;
		}
		.list-box{
			padding: 0 25upx;
		}
	}
</style>
