<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="productCode" class="form-item">
					<u-input v-model.trim="form.productCode" :maxlength="17" placeholder="请输入车架号" input-align="right" />
					<u-icon @click="uploadFun" :name="vinLoading?'loading':'camera-fill'" name="camera-fill" color="#c8c0cc" size="34" style="margin-left: 14upx;"></u-icon>
					<!-- <u-upload ref="uUpload" :custom-btn="true" :action="action" :form-data="{savePathType: 'local'}" @on-success="uploadSuccess" :max-size="2 * 1024 * 1024" max-count="1">
						<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="产品编码" 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="cost"></u-switch>
				</u-form-item>
				<u-form-item label="市级价">
					<u-switch slot="right" v-model="cityPrice"></u-switch>
				</u-form-item>
			</u-form>
			<view class="uni-list-btns">
				<u-button class="handle-btn" size="medium" hover-class="none" @click="resetForm">重置</u-button>
				<u-button class="handle-btn search-btn" size="medium" 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>
		<input type="file" id="filed" accept="image/jpeg,image/png" hidden="" @change="filePreview">
	</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 {}
				}
			}
		},
		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
				},
				vinLoading: false,
			}
		},
		computed: {
			productTypeNameArr: function() {
				let str = ''
				this.nowData.data.map(item => {
					str += item.productTypeName+' / '
				})
				return str ? str.substr(0, str.length-3) : ''
			}
		},
		mounted() {
			this.init()
			this.getWarehouse()
		},
		methods: {
			//  初始化数据
			init(){
				// if(this.defaultParams.beginDate && this.defaultParams.endDate){
				// 	this.form.date = this.defaultParams.beginDate + ' ~ ' + this.defaultParams.endDate
				// }else{
				// 	this.form.date = ''
				// }
				// this.beginDate = this.defaultParams.beginDate ? this.defaultParams.beginDate : ''
				// this.endDate = this.defaultParams.endDate ? this.defaultParams.endDate : ''
				// this.form.buyerName = this.defaultParams.buyerName ? this.defaultParams.buyerName : ''
				// this.form.salesBillNo = this.defaultParams.salesBillNo ? this.defaultParams.salesBillNo : ''
				// this.form.purchaseBillNo = this.defaultParams.purchaseBillNo ? this.defaultParams.purchaseBillNo : ''
				// this.form.warehouseSn = this.defaultParams.warehouseSn ? this.defaultParams.warehouseSn : ''
			},
			// 查询
			handleSearch() {
				let params = JSON.parse(JSON.stringify(this.form))
				delete params.date
				this.$emit('refresh', params)
				this.isShow = false
			},
			// 重置
			resetForm() {
				this.warehouseName = ''
				this.form.beginDate = ''
				this.form.endDate = ''
				this.$refs.uForm.resetFields()
				this.$emit('refresh', {})
				this.isShow = false
			},
			uploadFun () {
				console.log(111,document.getElementById('filed'))
				document.getElementById('filed').click()
				console.log(333)
			},
			filePreview (e) {
				console.log(222)
				const _this = this
				var files = e.target.files[0]
				const formData = new FormData()
				formData.append('savePathType', 'local')
				formData.append('file', files)
				this.vinLoading = true
				vinCodeParse(formData).then(res => {
					if (res.type == 'application/json') {
						var reader = new FileReader()
						reader.addEventListener('loadend', function () {
						const obj = JSON.parse(reader.result)
						if (obj.status == 200) {
							_this.form.vinCode = obj.data || ''
							// 移除表单项的校验结果
							// _this.$refs.uForm.clearValidate('vinCode')
						} else {
							this.toashMsg(obj.message)
						}
						_this.vinLoading = false
						document.getElementById('filed').value = ''
					})
					reader.readAsText(res)
			    } else {
					_this.vinLoading = false
			    }
			  })
			},
			// 选择品牌
			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.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')
				}
			}
		}
	}
</script>

<style lang="scss">
	.productSearch-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: 100upx;
				.handle-btn {
					font-size: 28upx;
					margin: 0 30upx;
					width: 100%;
					flex: 1;
				}
			}
		}
	}
</style>
