<template>
	<view class="stock-wrap">
		<!-- 统计 -->
		<view class="panel-box" v-if="totalData&&$hasPermissions('M_stockTj_mobile')">
			<view class="item-box">
				<view>库存总数量(个)</view>
				<u-count-to :end-val="totalData&&totalData.totalCurrentStockQty ? totalData.totalCurrentStockQty : 0" separator=","></u-count-to>
			</view>
			<view class="item-box" v-if="$hasPermissions('M_showCostPrice_mobile')">
				<view>库存总成本(￥)</view>
				<u-count-to :end-val="totalData&&totalData.totalCurrentStockCost ? totalData.totalCurrentStockCost : 0" separator="," :decimals="2"></u-count-to>
			</view>
		</view>
		<!-- 库存查询 -->
		<view class="stock-con" v-if="$hasPermissions('M_stockQuery_mobile')">
			<view class="stock-tit flex align_center">
				<view class="u-line" :style="{background: $config('primaryColor')}"></view>
				库存查询
			</view>
			<u-form :model="form" ref="uForm" label-width="180">
				<u-form-item label="产品编码"><u-input v-model.trim="form.productCode" placeholder="请输入产品编码" input-align="right" /></u-form-item>
				<u-form-item label="原厂编码"><u-input v-model.trim="form.productOrigCode" placeholder="请输入原厂编码" input-align="right" /></u-form-item>
				<u-form-item label="产品名称"><u-input v-model.trim="form.productName" placeholder="请输入产品名称" input-align="right" /></u-form-item>
				<u-form-item label="产品品牌"><u-input v-model="brandName" @click="brandModal=true" clearable @clear="brandClean" type="select" placeholder="请选择产品品牌" input-align="right" /></u-form-item>
				<u-form-item label="产品分类"><u-input v-model="productTypeNameArr" @click="showTypeModal=true" clearable @clear="typeClean" type="select" placeholder="请选择产品分类" input-align="right" /></u-form-item>
				<u-form-item label="滞销天数">
					<u-input type="number" :min="0" :max="999999" v-model.trim="form.minUnsalableDays" placeholder="起始天数" input-align="center" />
					<text style="display: block;text-align: center;line-height: 32px;">至</text>
					<u-input type="number" :min="0" :max="999999" v-model.trim="form.maxUnsalableDays" placeholder="截止天数" input-align="center" />
				</u-form-item>
				<u-form-item label="只查看有库存" inputAlign="right"><u-switch slot="right" v-model="form.zeroQtyFlag"></u-switch></u-form-item>
				<u-form-item label="包括禁用产品" inputAlign="right"><u-switch slot="right" v-model="form.enableFlag"></u-switch></u-form-item>
			</u-form>
			<view class="form-footer-btn">
				<u-button size="medium" shape="circle" hover-class="none" @click="handleClean">清空</u-button>
				<u-button size="medium" hover-class="none" shape="circle" :custom-style="customBtnStyle" @click="handleSearch">查询</u-button>
			</view>
		</view>
		<!-- 产品品牌 -->
		<productBrand ref="productBrand" :openModal="brandModal" :itemSn="form.productBrandSn" @choose="brandChoose" @clean="brandClean" @close="brandModal=false" />
		<!-- 产品分类 -->
		<productType ref="productType" :openModal="showTypeModal" :nowData="nowData" @choose="typeChoose" @clean="typeClean" @close="showTypeModal=false" />
	</view>
</template>

<script>
	import productBrand from '@/pages/common/productBrand.vue'
	import productType from '@/pages/common/productType.vue'
	import { dealerProductTypeList } from '@/api/dealerProductType'
	import { stockCount } from '@/api/stock'
	export default{
		components: { productBrand, productType },
		data(){
			return {
				form: {
					productCode: '', //  产品编码
					productName: '', //  产品名称
					productOrigCode: '', //  原厂编码
					productBrandSn: undefined, //  产品品牌
					productTypeSn1: undefined, //  产品分类1
					productTypeSn2: undefined, //  产品分类2
					productTypeSn3: undefined, //  产品分类3
					zeroQtyFlag: false ,//  库存情况
					enableFlag: true, // 显示禁用产品
					minUnsalableDays: undefined, // 滞销天数最小值
					maxUnsalableDays: undefined // 滞销天数最大值
				},
				totalData: null,
				brandModal: false,
				brandName: '',
				showTypeModal: false,
				nowData: {
					data: [],
					indArr: [],
					nowInd: 0
				},
				customBtnStyle: {  //  自定义按钮样式
					backgroundColor: this.$config('primaryColor'),
					color: '#fff'
				},
			}
		},
		computed: {
			productTypeNameArr: function() {
				let str = ''
				this.nowData.data.map(item => {
					str += item.productTypeName+' / '
				})
				return str ? str.substr(0, str.length-3) : ''
			}
		},
		watch: {
		  'form.enableFlag' (newValue, oldValue) {
		    uni.setStorageSync('productEnabledFlag-' + this.$store.state.vuex_userData.orgId, newValue)
		  },
		},
		onShow() {
			// 是否默认包括禁用产品
			const a = uni.getStorageSync('productEnabledFlag-' + this.$store.state.vuex_userData.orgId)
			console.log(typeof a == 'boolean')
			this.form.enableFlag = typeof a == 'boolean' ? a : true
			this.getTotal()
		},
		methods: {
			$message(msg){
				uni.showToast({
					icon: 'none',
					title: msg
				})
			},
			// 校验滞销天数数值范围
			checkValueRange () {
			  const a =	!this.form.minUnsalableDays
			  const b = !this.form.maxUnsalableDays
			  
			  if(a && !b){
				  this.$message('请输入滞销天数的起始天数！')
				  return false
			  } 
			  if(b && !a){
				  this.$message('请输入滞销天数的截止天数！')
				  return false
			  }
			  console.log(this.form.minUnsalableDays , this.form.maxUnsalableDays,this.form.minUnsalableDays > this.form.maxUnsalableDays)
			  if (Number(this.form.minUnsalableDays) > Number(this.form.maxUnsalableDays)) {
			    this.$message('截止天数应大于起始天数！')
			    return false
			  }
			  this.form.minUnsalableDays = this.form.minUnsalableDays > 999999 ? 999999 : this.form.minUnsalableDays
			  this.form.maxUnsalableDays = this.form.maxUnsalableDays > 999999 ? 999999 : this.form.maxUnsalableDays
			  return true
			},
			// 合计
			getTotal () {
				stockCount({}).then(res => {
					if (res.status == 200 && res.data) {
						this.totalData = res.data
					} else {
						this.totalData = null
					}
				})
			},
			// 表单清空
			handleClean(){
				this.form.productCode = ''
				this.form.productName = ''
				this.form.productOrigCode = ''
				this.brandName = ''
				this.form.productBrandSn = undefined
				this.nowData = {
					data: [],
					indArr: [],
					nowInd: 0
				}
				this.form.productTypeSn1 = undefined
				this.form.productTypeSn2 = undefined
				this.form.productTypeSn3 = undefined
				this.form.zeroQtyFlag = false
				this.form.minUnsalableDays = undefined // 滞销天数最小值
				this.form.maxUnsalableDays = undefined // 滞销天数最大值
				this.$refs.productBrand.onClean()
			},
			// 查询
			handleSearch(){
				if (this.checkValueRange()) {
					let params = JSON.parse(JSON.stringify(this.form))
					params.zeroQtyFlag = params.zeroQtyFlag ? '0' : ''
					params.enableFlag = params.enableFlag ? '' : '1'
					uni.navigateTo({ url: "/pages/stock/stockSearch?data="+JSON.stringify(params) })
				}
			},
			// 选择品牌
			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
			}
		}
	}
</script>

<style lang="scss">
	.stock-wrap{
		width: 100%;
		padding: 0 30upx 30upx;
		.panel-box {
			background-color: #ffffff;
			padding: 20upx 20upx;
			border-radius: 20upx;
			box-shadow: 0 0 6upx #eee;
			margin-bottom: 20upx;
			.item-box{
				text-align: center;
				display: flex;
				align-items: center;
				justify-content: space-between;
			}
		}
		.stock-con{
			background-color: #ffffff;
			border-radius: 20upx;
			box-shadow: 0 0 6upx #eee;
			padding: 20upx;
			.stock-tit{
				font-weight: bold;
				.u-line{
					display: inline-block;
					width: 6upx;
					height: 30upx;
					vertical-align: bottom;
					margin: 0 10upx;
					border-radius: 5upx;
				}
			}
			.form-footer-btn{
				margin: 60upx 30upx 30upx;
				display: flex;
				justify-content: space-between;
			}
			.form-footer-btn uni-button{
				width: 40%;
				margin: 0 auto;
				font-size: 30upx;
			}
		}
	}
</style>
