<template>
	<view class="page-body">
		<view class="scroll-list">
			 <view
			 class="check-order-list" 
			 v-for="(item,index) in list" 
			 :key="item.id" 
			 >
			 	<view class="flex align_center justify_between">
			 		<view class="u-name">
			 			<text style="margin-right: 0.5rem;">{{item.userName}}</text>
			 		</view>
			 		<view class="u-mobile">
			 			<text v-if="item.isManager == 1">门店负责人 / </text>
						<text v-else>{{item.roleName?item.roleName + '/':''}}</text>
			 			{{item.phone}}
			 		</view>
			 	</view>
			 	<view class="price-item">
			 		<u-checkbox-group>
						<u-checkbox 
							@change="(e)=>priceChange(e,item)"
							v-for="(priceItem, priceIndex) in item.priceTypeList" :key="priceItem.id" 
							v-model="priceItem.checked" 
							:name="priceItem.val"
						>{{priceItem.name}}</u-checkbox>
					</u-checkbox-group>
			 	</view>
			 	<view class="price-show-list flex align_center" v-if="item.priceTypeList.length == 2&&item.showListPrice">
			 		<text>列表显示</text>
					<view>
						<u-radio-group v-model="item.showListPriceVal" @change="(e)=>priceShowChange(e,item)">
							<u-radio 
								v-for="(priceItem, priceIndex) in priceShowType" :key="priceItem.id" 
								:name="priceItem.code"
							>
								{{priceItem.dispName}}
							</u-radio>
						</u-radio-group>
					</view>
			 	</view>
			  </view>
			  <view style="padding:0 30upx 30upx;">
			 	 <u-empty :src="`/static/nodata.png`" icon-size="180" :text="noDataText" img-width="120" v-if="list.length==0 && status!='loading'" mode="list"></u-empty>
			 	 <u-loadmore v-if="(total>=list.length&&list.length)||status=='loading'" :status="status" />
			  </view>
		</view>
	</view>
</template>

<script>
	import {searchEmployee, delEmployee } from '@/api/employee'
	import { queryQplsConfig, queryShelfUserParam, updateShelfUserParam } from '@/api/shelf.js'
	import moment from 'moment'
	export default{
		name:'personnel',
		data(){
			return{
				status: 'loading',
				noDataText: '暂无数据',
				// 查询条件
				list: [],
				total: 0,
				shelfSn: null,
				priceType:[{
					val: 'PURCHASES_PRICE',
					name: '进货价',
					show: false,
					checked: false
				},
				{
					val: 'CAR_OWNER_PRICE',
					name: '车主价',
					show: false,
					checked: false
				}],
				priceShowType:[]
			}
		},
		computed:{
			hasShelf(){
				return this.$store.state.vuex_storeShelf
			},
			configPrice(){
				return this.$store.state.vuex_configPrice
			}
		},
		onLoad() {
			this.shelfSn = this.hasShelf.shelfSn
			this.priceShowType = this.$store.state.vuex_priceTypeList
			this.getPriceCofig()
		},
		onShow() {
			this.getRow()
		},
		methods:{
			// 获取价格配置
			getPriceCofig(){
				this.priceType[0].show = this.configPrice.shelf_cost_show == '1'
				this.priceType[1].show = this.configPrice.shelf_price_show == '1'
			},
			priceChange(e,item){
				console.log(e,item)
				const row = item.storeParamList.find(k => k.paramCode == e.name)
				this.updateData({id:row.id,paramValue:e.value?'1':'0'})
			},
			priceShowChange(e,item){
				console.log(e,item)
				const row = item.storeParamList.find(k => k.paramCode == 'PRICE_SHOW_TYPE')
				this.updateData({id:row.id,paramValue:e})
			},
			// 修改数据
			updateData(params){
				uni.showLoading({
					title:'正在修改...'
				})
				updateShelfUserParam(params).then(res => {
					if(res.status == 200){
						this.getRow()
					}
					uni.hideLoading()
				})
			},
			// 格式化数据
			formatData(){
				this.list.map(item => {
					const a = item.storeParamList
					const pst = a.find(b=>b.paramCode=='PRICE_SHOW_TYPE') // 列表显示价格值
					item.showListPriceVal = pst?pst.paramValue:''
					const cop = a.find(b=>b.paramCode=='CAR_OWNER_PRICE') // 车主价值
					item.carPriceVal = cop?cop.paramValue:''
					const pp = a.find(b=>b.paramCode=='PURCHASES_PRICE') // 进货价值
					item.ppPriceVal = pp?pp.paramValue:''
					item.priceTypeList = JSON.parse(JSON.stringify(this.priceType.filter(item => item.show)))
					// 赋值
					item.priceTypeList.map(key => {
						key.id = item.userId + '-' + key.val
						// 车主价
						if(key.val == 'CAR_OWNER_PRICE'){
							key.checked = item.carPriceVal == '1'
						}
						// 进货价
						if(key.val == 'PURCHASES_PRICE'){
							key.checked = item.ppPriceVal == '1'
						}
					})
					// 列表显示价格一行是否显示
					const showListPrice = item.priceTypeList.filter(item => item.checked)
					item.showListPrice = showListPrice.length == 2
				})
				this.list.splice()
				console.log(this.list)
			},
			// 查询列表
			getRow () {
			  let _this = this
			  this.status = "loading"
			  queryShelfUserParam({shelfSn: this.shelfSn}).then(res => {
				if (res.code == 200 || res.status == 204 || res.status == 200) {
				  _this.list = res.data || []
				  _this.total = res.data.length
				} else {
				  _this.list = []
				  _this.total = 0
				  _this.noDataText = res.message
				}
				_this.formatData()
				_this.status = _this.total>=_this.list.length ? "nomore" : 'loadmore'
			  })
			},
		}
	}
</script>

<style lang="less">
	.page-body{
		height: 100vh;
		padding:0;
		.scroll-list{
			height: 100vh;
			overflow: auto;
			.check-order-list{
				background: #ffffff;
				padding: 10upx 20upx;
				margin: 25rpx;
				border-radius: 20upx;
				box-shadow: 1px 1px 3px #EEEEEE;
				> view{
					padding: 0.8rem 0.5rem;
					.u-name{
						font-size: 32rpx;
					}
					.u-mobile{
						color: #999;
					}
				}
			}
			.price-item{
				border-top: 1rpx solid #eee;
				padding-left: 5%;
			}
			.price-show-list{
				margin-left: 5%;
				border-top: 1rpx solid #eee;
				>view{
					padding:0 20rpx;
				}
			}
		}
		.footer{
			padding: 0.6rem;
			background: #ffffff;
		}
	}
</style>