<template>
	<view class="salesSearch-wrap">
		<u-popup v-model="isShow" class="search-popup" mode="right" @close="isShow=false" length="85%">
			<u-form class="form-content" :model="form" ref="uForm" label-width="150">
				<u-form-item label="下单时间" prop="date" class="form-item">
					<u-input clearable v-model="form.date" disabled placeholder="请选择下单时间区间" class="form-item-inp" @click="showDate=true" />
					<u-icon v-show="form.date.length != 0" name="close-circle-fill" color="#c8c0cc" size="32" class="close-circle" @click="clearTime"></u-icon>
				</u-form-item>
				<u-form-item label="产品编码" prop="productCode" class="form-item">
					<u-input v-model="form.productCode" :maxlength="30" placeholder="请输入产品编码" />
				</u-form-item>
				<u-form-item label="产品名称" prop="productName" class="form-item">
					<u-input v-model="form.productName" :maxlength="30" placeholder="请输入产品名称" />
				</u-form-item>
				<u-form-item label="订单状态" prop="billState" class="form-item">
					<u-input v-model="settleStyleName" @click="settleStyleModal=true" disabled placeholder="请选择订单状态" class="form-item-inp" />
					<u-icon v-show="settleStyleName.length != 0" name="close-circle-fill" color="#c8c0cc" size="32" class="close-circle" @click="clearSettleStyle"></u-icon>
				</u-form-item>
			</u-form>
			<view class="uni-list-btns">
				<u-button class="handle-btn" size="medium" shape="circle" hover-class="none" @click="resetForm">重置</u-button>
				<u-button class="handle-btn search-btn" shape="circle" size="medium" hover-class="none" :custom-style="customBtnStyle" @click="handleSearch">查询</u-button>
			</view>
		</u-popup>
		<!-- 选择日期范围 -->
		<u-calendar v-model="showDate" @change="dateChange" mode="range"></u-calendar>
		<!-- 选择收款方式 -->
		<u-picker v-model="settleStyleModal" @confirm="settleStyleChange" :range="billStatusOpt" range-key="dispName" mode="selector"></u-picker>
	</view>
</template>

<script>
	export default{
		props: {
			openModal: { //  弹框显示状态
				type: Boolean,
				default: false
			},
			defaultParams: {  //  默认查询条件
				type: Object,
				default: () => {
					return {}
				}
			},
			billStatusOpt: {
				type: Array,
				default: () => {
					return []
				}
			}
		},
		data(){
			return{
				isShow: this.openModal,
				form: {
					date: '',
					orderStartDate: '',
					orderEndDate: '',
					productCode: '', //  产品编码
					productName: '', //  产品名称
					billState: undefined //  订单状态
				},
				customBtnStyle: {  //  自定义按钮样式
					borderColor: this.$config('primaryColor'),
					backgroundColor: this.$config('primaryColor'),
					color: '#fff'
				},
				showDate: false, // 是否显示时间弹窗
				settleStyleName: '',
				settleStyleModal: false,
			}
		},
		mounted() {
			this.init()
		},
		methods: {
			//  初始化数据
			init(){
				if(this.defaultParams.orderStartDate && this.defaultParams.orderEndDate){
					this.form.date = this.defaultParams.orderStartDate + ' ~ ' + this.defaultParams.orderEndDate
				}else{
					this.form.date = ''
				}
				this.form.orderStartDate = this.defaultParams.orderStartDate ? this.defaultParams.beginDate : ''
				this.form.orderEndDate = this.defaultParams.orderEndDate ? this.defaultParams.orderEndDate : ''
				this.form.productCode = this.defaultParams.productCode ? this.defaultParams.productCode : ''
				this.form.productName = this.defaultParams.productName ? this.defaultParams.productName : ''
				this.form.billState = this.defaultParams.billState ? this.defaultParams.billState : ''
			},
			// 查询
			handleSearch() {
				let params = JSON.parse(JSON.stringify(this.form))
				delete params.date
				this.$emit('refresh', params)
				this.isShow = false
			},
			// 重置
			resetForm() {
				this.clearSettleStyle()
				this.form = {
					date: '',
					orderStartDate: '',
					orderEndDate: '',
					productCode: '', //  产品编码
					productName: '', //  产品名称
					billState: undefined //  订单状态
				}
				this.$refs.uForm.resetFields()
				this.clearTime()
				this.handleSearch()
			},
			//  清空订单状态
			clearSettleStyle(){
				this.settleStyleName = ''
				this.form.billState = undefined
			},
			// 订单状态  change
			settleStyleChange(ind){
				this.form.billState = this.billStatusOpt[ind].code || undefined
				this.settleStyleName = this.billStatusOpt[ind].dispName || ''
			},
			// 订单状态
			getTypeList () {
				// 订单状态有值时,匹配对应name
				if(this.defaultParams.billState || this.defaultParams.billState==0){
					this.billStatusOpt.map(item => {
						if(item.code == this.defaultParams.billState){
							this.settleStyleName = item.dispName
						}
					})
				}
			},
			// 时间  change
			dateChange(date){
				this.form.date = date.startDate + ' ~ ' + date.endDate
				this.form.orderStartDate = date.startDate
				this.form.orderEndDate = date.endDate
			},
			//  清空创建时间
			clearTime(){
				this.form.date = ''
				this.form.orderStartDate = ''
				this.form.orderEndDate = ''
			}
		},
		watch: {
			//  父页面传过来的弹框状态
			openModal (newValue, oldValue) {
				this.isShow = newValue
			},
			//  重定义的弹框状态
			isShow (newValue, oldValue) {
				if (!newValue) {
					this.$emit('close')
				}else{
					this.getTypeList()
				}
			}
		}
	}
</script>

<style lang="scss">
	.salesSearch-wrap{
		.search-popup {
			.form-content {
				display: block;
				padding: 0 20upx;
				box-sizing: content-box;
				.form-item-inp{
					display: inline-block;
					width: 88%;
					vertical-align: top;
				}
			}
			.uni-list-btns {
				display: flex;
				padding: 20upx;
				margin-top: 200upx;
				.handle-btn {
					font-size: 28upx;
					margin: 0 30upx;
					width: 100%;
					flex: 1;
				}
			}
		}
	}
</style>
