<template>
	<view class="productSearch-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="180">
				<u-form-item label="车架号(VIN)" prop="vinCode" class="form-item">
					<u-input v-model.trim="form.vinCode" :maxlength="17" placeholder="请输入车架号" input-align="right" />
					<u-upload :custom-btn="true" ref="uUpload" :show-upload-list="false" :action="action" :form-data="{savePathType: 'local'}" @on-success="uploadSuccess" :max-size="2 * 1024 * 1024">
						<view slot="addBtn" class="slot-btn" hover-class="slot-btn__hover" hover-stay-time="150">
							<u-icon name="camera-fill" color="#c8c0cc" size="34" style="margin-left: 14upx;"></u-icon>
						</view>
					</u-upload>
				</u-form-item>
				<u-form-item label="产品编码1" prop="productCode" class="form-item">
					<u-input v-model.trim="form.productCode" placeholder="请输入产品编码" input-align="right" />
				</u-form-item>
				<u-form-item label="原厂编码" prop="productOrigCode" class="form-item">
					<u-input v-model.trim="form.productOrigCode" placeholder="请输入原厂编码" input-align="right" />
				</u-form-item>
				<u-form-item label="产品名称" prop="productName" class="form-item">
					<u-input v-model.trim="form.productName" placeholder="请输入产品名称" input-align="right" />
				</u-form-item>
				<u-form-item label="产品品牌" prop="productBrandSn" class="form-item">
					<u-input v-model="brandName" @click="brandModal=true" disabled placeholder="请选择产品品牌" input-align="right" />
				</u-form-item>
				<u-form-item label="产品分类" prop="productTypeSn" class="form-item">
					<u-input v-model="productTypeNameArr" @click="showTypeModal=true" disabled placeholder="请选择产品分类" input-align="right" />
				</u-form-item>
				<u-form-item label="仓库" prop="warehouseSn" class="form-item">
					<u-input v-model="warehouseName" @click="warehouseModal=true" disabled placeholder="请选择仓库" class="form-item-inp" input-align="right" />
					<u-icon v-show="warehouseName.length != 0" name="close-circle-fill" color="#c8c0cc" size="32" class="close-circle" @click="clearWarehouse"></u-icon>
				</u-form-item>
				<u-form-item label="成本价">
					<u-switch slot="right" v-model="form.cost"></u-switch>
				</u-form-item>
				<u-form-item label="市级价">
					<u-switch slot="right" v-model="form.cityPrice"></u-switch>
				</u-form-item>
			</u-form>
			<view class="uni-list-btns">
				<u-button class="handle-btn" size="medium" hover-class="none" shape="circle" @click="resetForm">重置</u-button>
				<u-button class="handle-btn search-btn" size="medium" shape="circle" hover-class="none" :custom-style="customBtnStyle" @click="handleSearch">查询</u-button>
			</view>
		</u-popup>
		<!-- 产品品牌 -->
		<productBrand :openModal="brandModal" :itemSn="form.productBrandSn" @choose="brandChoose" @clean="brandClean" @close="brandModal=false" />
		<!-- 产品分类 -->
		<productType :openModal="showTypeModal" :nowData="nowData" @choose="typeChoose" @clean="typeClean" @close="showTypeModal=false" />
		<!-- 选择收款方式 -->
		<u-picker v-model="warehouseModal" @confirm="warehouseChange" :range="warehouseList" range-key="name" mode="selector"></u-picker>
	</view>
</template>

<script>
	import productBrand from '@/pages/common/productBrand.vue'
	import productType from '@/pages/common/productType.vue'
	import { warehouseAllList, vinCodeParse } from '@/api/sales'
	const baseUrl = getApp().globalData.baseUrl
	export default{
		components: { productBrand, productType },
		props: {
			openModal: { //  弹框显示状态
				type: Boolean,
				default: false
			},
			defaultParams: {  //  默认查询条件
				type: Object,
				default: () => {
					return null
				}
			}
		},
		data(){
			return{
				isShow: this.openModal,
				form: {
					vinCode: '',
					productCode: '', //  产品编码
					productName: '', //  产品名称
					productOrigCode: '', //  原厂编码
					productBrandSn: undefined, //  产品品牌
					productTypeSn1: undefined, //  产品分类1
					productTypeSn2: undefined, //  产品分类2
					productTypeSn3: undefined, //  产品分类3
					warehouseSn: undefined, //  仓库
					cost: false,  // 成本价
					cityPrice: false,  // 市级价
				},
				customBtnStyle: {  //  自定义按钮样式
					borderColor: this.$config('primaryColor'),
					backgroundColor: this.$config('primaryColor'),
					color: '#fff'
				},
				warehouseName: '',
				warehouseModal: false,
				warehouseList: [],
				brandModal: false,
				brandName: '',
				showTypeModal: false,
				nowData: {
					data: [],
					indArr: [],
					nowInd: 0
				},
				action: baseUrl + 'vinIdentify/ocr',
			}
		},
		computed: {
			productTypeNameArr: function() {
				let str = ''
				this.nowData.data.map(item => {
					str += item.productTypeName+' / '
				})
				return str ? str.substr(0, str.length-3) : ''
			}
		},
		mounted() {
			this.getWarehouse()
		},
		methods: {
			//  初始化数据
			init(){
				console.log(this.defaultParams)
				this.form.vinCode = this.defaultParams && this.defaultParams.vinCode || ''
				this.form.productCode = this.defaultParams && this.defaultParams.productCode || ''
				this.form.productName = this.defaultParams && this.defaultParams.productName || ''
				this.form.productOrigCode = this.defaultParams && this.defaultParams.productOrigCode || ''
				this.form.productBrandSn = this.defaultParams && this.defaultParams.productBrandSn || undefined
				this.form.productTypeSn1 = this.defaultParams && this.defaultParams.productTypeSn1 || undefined
				this.form.productTypeSn2 = this.defaultParams && this.defaultParams.productTypeSn2 || undefined
				this.form.productTypeSn3 = this.defaultParams && this.defaultParams.productTypeSn3 || undefined
				this.form.warehouseSn = this.defaultParams && this.defaultParams.warehouseSn || undefined
				this.form.cost = this.defaultParams && this.defaultParams.cost || false
				this.form.cityPrice = this.defaultParams && this.defaultParams.cityPrice || false
				this.brandName = this.defaultParams && this.defaultParams.brandName || ''
				if(this.defaultParams && this.defaultParams.nowData){
					this.nowData = this.defaultParams.nowData
				}else{
					this.nowData = {
						data: [],
						indArr: [],
						nowInd: 0
					}
				}
				this.warehouseName = this.defaultParams && this.defaultParams.warehouseName || ''
			},
			// 查询
			handleSearch() {
				let params = JSON.parse(JSON.stringify(this.form))
				let other = {
					brandName: this.brandName,
					nowData: this.nowData,
					warehouseName: this.warehouseName
				}
				this.$emit('refresh', Object.assign(params, other))
				this.isShow = false
			},
			// 重置
			resetForm() {
				this.warehouseName = ''
				this.nowData = {
					data: [],
					indArr: [],
					nowInd: 0
				}
				this.$refs.uForm.resetFields()
				this.$emit('refresh', {})
				this.isShow = false
				this.brandClean()
				this.typeClean()
			},
			uploadSuccess(res){
				console.log(res)
				if(res.status == 200){
					this.form.vinCode = res.data
				}else{
					this.toashMsg(res.message)
				}
			},
			// 选择品牌
			brandChoose(data){
				this.brandName = data.brandName
				this.form.productBrandSn = data.brandSn
			},
			// 清空品牌
			brandClean(){
				this.brandName = ''
				this.form.productBrandSn = undefined
			},
			// 选择分类
			typeChoose(obj){
				this.nowData = {
					data: obj.data,
					indArr: obj.indArr,
					nowInd: obj.nowInd
				}
				this.form.productTypeSn1 = this.nowData.data[0]&&this.nowData.data[0].productTypeSn ? this.nowData.data[0].productTypeSn : undefined
				this.form.productTypeSn2 = this.nowData.data[1]&&this.nowData.data[1].productTypeSn ? this.nowData.data[1].productTypeSn : undefined
				this.form.productTypeSn3 = this.nowData.data[2]&&this.nowData.data[2].productTypeSn ? this.nowData.data[2].productTypeSn : undefined
			},
			// 清空分类
			typeClean(){
				this.nowData = {
					data: [],
					indArr: [],
					nowInd: 0
				}
				this.form.productTypeSn1 = undefined
				this.form.productTypeSn2 = undefined
				this.form.productTypeSn3 = undefined
			},
			//  清空仓库
			clearWarehouse(){
				this.warehouseName = ''
				this.form.warehouseSn = undefined
			},
			// 仓库  change
			warehouseChange(ind){
				this.form.warehouseSn = this.warehouseList[ind].warehouseSn || undefined
				this.warehouseName = this.warehouseList[ind].name || ''
			},
			// 仓库列表
			getWarehouse () {
				warehouseAllList().then(res => {
					if(res.status == 200 && res.data){
						this.warehouseList = res.data
						// 仓库有值时,匹配对应name
						if(this.defaultParams && (this.defaultParams.warehouseSn || this.defaultParams.warehouseSn==0)){
							this.warehouseList.map(item => {
								if(item.warehouseSn == this.defaultParams.warehouseSn){
									this.warehouseName = item.name
								}
							})
						}
					}else{
						this.warehouseList = []
					}
				})
			},
		},
		watch: {
			//  父页面传过来的弹框状态
			openModal (newValue, oldValue) {
				this.isShow = newValue
			},
			//  重定义的弹框状态
			isShow (newValue, oldValue) {
				if (!newValue) {
					this.$emit('close')
				}else{
					this.init()
				}
			}
		}
	}
</script>

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