<template>
	<view class="mxpopup-container">
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
					title="移位审核人调整" @clickLeft="handleBack" />
		<scroll-view class="cpck-content" scroll-y="true">
			<uni-list>
				<uni-list-item 
					v-for="item in listData" 
					:show-extra-icon="true" 
					:extra-icon="setDefaultSettingsList.settingCode == item.userId ? {size: '22',type: 'star-filled'} : {size: '22',type: 'star'}" 
					:title="item.nickName" 
					@click="goProDetail(item)"
					:style="getItemStyle(item)"
					link
				></uni-list-item>
			</uni-list>
		</scroll-view>
		<view class="page-bottom">
			<view class="p-b-btn" @click="examine">
				<text class="box-text" ><uni-icons type="star" size="24"></uni-icons></text>
			</view>
			<view class="p-b-btn" @click="del">
				<text class="box-text" ><uni-icons type="trash" size="24"></uni-icons></text>
			</view>
			<view class="p-b-btn" @click="choiceUser">
				<text class="box-text"><uni-icons type="plusempty" size="24"></uni-icons></text> 
			</view> 
		</view>
		
		<!-- 审核人 -->
		<shrPopup ref="shrPopup" :title="shrTitle" :idxFlag="shrFlag" :formData="formData" @sendData="getSendData"/> 
	</view>
</template>

<script>
import shrPopup from './popup/commonpopup.vue' // 审核人
import { moveReviewerEdit, getMoveReviewerList, listUser } from '@/api/wms/stockMove.js'
import storage from '@/utils/storage'
	
	export default {
		components: {
			shrPopup
		},
		data() {
			return {
				drawerWidth: 300,
				listData:[],
				formData:{},
				// 审核人基础资料
				shrTitle: "审核人",
				shrFlag: "shr",
				// 审核人设置主子表信息
				setDefaultSettings : { //主表信息
					"billCode" : "stockMove",
					"settingClass": "4",
				},
				setDefaultSettingsList : { //子表信息
					"settingFlag": "审核人",
                    "settingCode": "",
                    "settingName": "",
                    "settingValues": ""
				},
				//选中数据
				selectedItemUserid : "",
			};
		},
		created(){
		  let that = this;
		  uni.getSystemInfo({
		  	success:(res=>{
		  		that.drawerWidth = res.windowWidth;
		  	})
		  });
		  this.getList();
		},
		mounted(){},
		methods:{
			examine(){ //设置选中人为显示值和实际值（默认审核人）
				if(this.selectedItemUserid!=undefined && this.selectedItemUserid!=null && this.selectedItemUserid!="" && this.selectedItemUserid!=this.setDefaultSettingsList.settingCode){
					//查询选中行的值
					let isSave = false
					for(let index in this.listData){
						if( this.listData[index].userId == this.selectedItemUserid ){
							this.setDefaultSettingsList.settingCode = this.listData[index].userId;
							this.setDefaultSettingsList.settingName = this.listData[index].nickName;
							isSave = true;
							break;
						}
					}
					//更改后查询
					let params = this.setDefaultSettings;
					params.setDefaultSettingsList = [this.setDefaultSettingsList];
					//添加后刷新
					moveReviewerEdit(params).then(res => {
						if(res.code === 200){
							this.getList();
							this.selectedItemUserid ="";
						}
					})
				}
			},
			del(){ //删除
				if(this.selectedItemUserid!=undefined && this.selectedItemUserid!=null && this.selectedItemUserid!="" /*&& this.selectedItemUserid!=this.setDefaultSettingsList.settingCode*/){
					//删除
					this.listData = this.listData.filter(item => item.userId !== this.selectedItemUserid);
					//删除后的values
					let userIds = this.listData.map(item => item.userId);
					let settingValues = userIds.join(',');
					this.setDefaultSettingsList.settingValues = settingValues;
					//更改后查询
					let params = this.setDefaultSettings;
					params.setDefaultSettingsList = [this.setDefaultSettingsList];
					//删除后刷新
					moveReviewerEdit(params).then(res => {
						if(res.code === 200){
							this.getList();
							this.selectedItemUserid ="";
						}
					})
				}
			},
			choiceUser(){ //添加按钮
				this.$refs.shrPopup.showPopup();
			},
			getSendData(res){ //添加人后刷新list
				//settingValues有值，userid拼接逗号
				let userId = res.selectData.userId;
				if( this.setDefaultSettingsList.settingValues && this.setDefaultSettingsList.settingValues.length>0 && userId!=undefined && userId!=null && userId!=""){
					userId = ','+userId
				}
				//userid有值，添加到settingValues
				if(userId){
					this.setDefaultSettingsList.settingValues += userId;
				}
				let params = this.setDefaultSettings;
				params.setDefaultSettingsList = [this.setDefaultSettingsList];
				//添加后刷新
				moveReviewerEdit(params).then(res => {
					if(res.code === 200){
						this.getList();
					}
				})
			},
			async getList(){
				let userids = "";
				await getMoveReviewerList().then(res => {
					if(res.code === 200){
						if(res && res.rows && res.rows[0] && res.rows[0].setDefaultSettings){
							if(res.rows[0].setDefaultSettings.settingFlag == "审核人"){
								this.setDefaultSettingsList = res.rows[0].setDefaultSettings;
								userids = res.rows[0].setDefaultSettings.settingValues;
							}
						}
					}
				});
				
				if(userids){
					let params = {
						status: '0', //(固定)
						params: {
							userids: userids,
							limit: 200
						}
					}
					await listUser(params).then(res => {
						if(res.code === 200){
							this.listData = res.rows;
						}
					})
				}
			},
			goProDetail(item){
				try{
					this.selectedItemUserid = item.userId;
				}catch{
					this.selectedItemUserid = "";
				}
			},
			handleBack(){
				uni.showModal({
					title: '提示',
					content: '确认返回?',
					success: function (res) {
						if (res.confirm) {
							uni.navigateBack();
						} else if (res.cancel) {
						
						}
					}
				});
			},
			getItemStyle(item) {
			  // 根据 item.userId 是否与 selectedItemUserid 相等来返回不同的样式对象
			  return item.userId === this.selectedItemUserid ? { backgroundColor: '#f8edb8' } : {};
			},
		}
	}
</script>

<style lang="less">
	
@import "@/static/scss/stock.css";
		
</style>