<template>
	<view class="content">
		<u-cell-group>
			<u-cell-item title="显示价格">
				<u-switch slot="right-icon" @change="changeShowPrice" v-model="showPrice"></u-switch>
			</u-cell-item>
			<u-cell-item title="显示价格类型" v-if="showPrice" :value="priceTypeName||'请选择'" @click="showPriceType=true"></u-cell-item>
			<u-cell-item title="员工是否可见进货价" v-if="showPrice&&priceType == 'PURCHASES_PRICE'">
				<u-switch slot="right-icon" @change="changeShowJhPrice" v-model="showJhPrice"></u-switch>
			</u-cell-item>
		</u-cell-group>
		<!-- 价格类型列表 -->
		<u-action-sheet :list="priceTypeList" v-model="showPriceType" @click="clickAction"></u-action-sheet>
	</view>
</template>

<script>
	import { findPriceShow, savePriceShow } from '@/api/shelf'
	export default {
		data() {
			return {
				showPrice: false,
				showJhPrice: false,
				showPriceType: false,
				priceTypeList:[],
				priceType:'',
				priceTypeName:''
			}
		},
		onLoad() {
			this.priceTypeList = this.$store.state.vuex_priceTypeList
			this.priceTypeList.map(item => {
				item.text = item.dispName
				item.fontSize = 30
			})
		},
		onReady() {
			this.pageInit()
		},
		methods: {
			async pageInit(){
				await findPriceShow({paramCode:'PRICE_SHOW'}).then(res => {
					this.showPrice = res.data ? res.data.paramValue=='1' : false
					this.$store.state.vuex_showPrice[0] = this.showPrice
				})
				await findPriceShow({paramCode:'PRICE_SHOW_TYPE'}).then(res => {
					if(res.data&&res.data.paramValue){
						this.priceType = res.data.paramValue
						this.priceTypeName = this.priceTypeList.find(item => item.code == res.data.paramValue).dispName
						this.$store.state.vuex_showPrice[1] = this.priceType
					}
				})
				await findPriceShow({paramCode:'PRICE_SHOW_EMPLOYEE'}).then(res => {
					this.showJhPrice = res.data ? res.data.paramValue=='1' : false
					this.$store.state.vuex_showPrice[2] = this.showJhPrice
				})
			},
			savePrice(data,value){
				savePriceShow({
					paramCode:data,
					paramValue: value
				}).then(res => {
					 if(res.status == 200){
						 uni.showToast({
						 	icon:'none',
							title: res.message
						 })
					 }
					 this.pageInit()
				})
			},
			changeShowPrice(v){
				this.savePrice('PRICE_SHOW',v?'1':'0')
			},
			clickAction(index){
				this.savePrice('PRICE_SHOW_TYPE',this.priceTypeList[index].code)
			},
			changeShowJhPrice(v){
				this.savePrice('PRICE_SHOW_EMPLOYEE',v?'1':'0')
			}
		}
	}
</script>

<style lang="less">
	.content{
		width: 100%;
		height: 100vh;
	}
</style>
