<template>
  <view class="container">
		<view class="title">标本收取限制配置</view>
		<view class="content">
			<view class="list" v-if="this.list.collectDeptList && this.list.collectDeptList.length>0">
				<view class="top">
					<view class="title">起点科室</view>
					<text class="right-icon newicon newicon-weibiaoti2010104" @click="search('startDept')"></text>
				</view>
				<view class="content" v-for="(item, index) in startDept" :key="index">
					<view class="name">{{item.dept}}</view>
					<text class="right-icon newicon newicon-lajitong" v-if="index!=0" @click="delect(index, '1')"></text>
				</view>
			</view>
			<view class="list">
				<view class="top">
					<view class="title">检验科室</view>
					<text class="right-icon newicon newicon-weibiaoti2010104" @click="search('checkoutDept')"></text>
				</view>
				<view class="content" v-for="(item, index) in checkoutDept" :key="index">
					<view class="name">{{item.dept}}</view>
					<text class="right-icon newicon newicon-lajitong" @click="delect(index, '2')"></text>
				</view>
			</view>
			<view class="list">
				<view class="top">
					<view class="title">试管类型</view>
					<text class="right-icon newicon newicon-weibiaoti2010104" @click="search('testTube')"></text>
				</view>
				<view class="content" v-for="(item, index) in testTube" :key="index">
					<view class="name">{{item.name}}</view>
					<text class="right-icon newicon newicon-lajitong" @click="delect(index, '3')"></text>
				</view>
			</view>
			<view class="list">
				<view class="top">
					<view class="title">标本状态</view>
					<text class="right-icon newicon newicon-weibiaoti2010104" @click="search('speState')"></text>
				</view>
				<view class="content" v-for="(item, index) in speStateData" :key="index">
					<view class="name">{{item.name}}</view>
					<text class="right-icon newicon newicon-lajitong" @click="delect(index, '4')"></text>
				</view>
			</view>
			<view class="list">
				<view class="top">
					<view class="title">紧急普通</view>
					<checkbox-group @change="checkboxChange">
						<label v-for="item in urgencyData" :key="item.value">
							<view>
								<checkbox :value="item.value" :checked="item.checked" color="#42b983"/>
							</view>
							<view>{{item.name}}</view>
						</label>
					</checkbox-group>
				</view>
			</view>
			<view class="list">
				<view class="top">
					<view class="title">重复收取</view>
					<radio-group @change="radioChange">
						<label v-for="(item, index) in repetition" :key="item.value">
							<view>
								<radio :value="item.value" :checked="index === current" color="#42b983"/>
							</view>
							<view>{{item.name}}</view>
						</label>
					</radio-group>
				</view>
			</view>
		</view>
		<view class="footer">
			<view class="btn cancel" hover-class="seimin-btn-hover" @click="goBack()">取消</view>
			<view class="btn affirm" hover-class="seimin-btn-hover" @click="submit()">确定</view>
		</view>
  </view>
</template>

<script>
  import { get, post} from "../../http/http.js";
  export default {
    data() {
      return {
        hosId: uni.getStorageSync('userData').user.currentHospital.id,
        list: [],
				startDept:[],
				checkoutDept:[],
				speStateData:[],
				testTube:[],
				urgencyData:[
					{value:'1',name:'紧急',checked:false},
					{value:'0',name:'普通',checked:false},
				],
				repetition:[
					{value:'1',name:'是'},
					{value:'0',name:'否'},
				],
				current:null,
				gdId:null,
				id:null,
				detailHosId:null,
				back:0
      };
    },
		onLoad(options) {
			console.log('options111111', options);
			this.gdId = options.id
			if(options.back){
				this.back = 1
			}else{
				this.back = 0
			}
			this.getSpeDetail()
			let startDept = uni.getStorageSync('startDept')
			let checkoutDept = uni.getStorageSync('checkoutDept')
			let testTube = uni.getStorageSync('testTube')
			let speState = uni.getStorageSync('speState')
			if(startDept){ //需要追加
				this.startDept = JSON.parse(startDept)
			}
			 if (checkoutDept){ //需要追加
				this.checkoutDept = JSON.parse(checkoutDept)
			}
			 if (testTube){
				this.testTube = JSON.parse(testTube)
			}
			 if (speState){
				this.speStateData = JSON.parse(speState)
			}
		},
    methods:{
			getSpeDetail(){
				post("/simple/data/fetchDataList/speOrderCollectInfo", {
					idx: 0,
					sum: 9999,
					speOrderCollectInfo:{
						gdId: this.gdId,
						hosId: this.hosId
					}
				}).then((result) => {
				  uni.hideLoading();
				  if (result.status == 200) {
						this.list = result.list[0] || [];
						this.id = result.list[0].id
						this.detailHosId = result.list[0].hosId
						this.createTime = result.list[0].createTime
						if(result.list[0].collectDeptList){
							for(let i of this.startDept){
								result.list[0].collectDeptList.push(i)
							}
							let data = result.list[0].collectDeptList
							let newList2 = []
							let newList3 = []
							data.forEach((item) => {
								if (!newList2.includes(item.id)) {
									newList2.push(item.id)
								}
							})
							newList2.forEach(i=>{
								newList3.push(data.find(x=> x.id == i))
							})
							this.startDept = newList3
						}
						if(result.list[0].testDeptList){
							for(let i of this.checkoutDept){
								result.list[0].testDeptList.push(i)
							}
							let data = result.list[0].testDeptList
							let newList2 = []
							let newList3 = []
							data.forEach((item) => {
								if (!newList2.includes(item.id)) {
									newList2.push(item.id)
								}
							})
							newList2.forEach(i=>{
								newList3.push(data.find(x=> x.id == i))
							})
							this.checkoutDept = newList3
						}else{
							let data = this.checkoutDept
							let newList2 = []
							let newList3 = []
							data.forEach((item) => {
								if (!newList2.includes(item.id)) {
									newList2.push(item.id)
								}
							})
							newList2.forEach(i=>{
								newList3.push(data.find(x=> x.id == i))
							})
							this.checkoutDept = newList3
						}
						if(result.list[0].tubeTypeList){
							if(this.back == 0 || this.testTube.length==0){
								this.testTube = this.testTube.concat(result.list[0].tubeTypeList) 
							}
						}
						if(result.list[0].speStateList){
							if(this.back == 0 || this.speStateData.length==0){
								this.speStateData = this.speStateData.concat(result.list[0].speStateList) 
							}
						}
						if(result.list[0].urgent){
							let urgent = result.list[0].urgent.split(',')
							this.urgencyData.forEach(i=>{
								for(let x of urgent){
									if(i.value == x){
										i.checked = true
									}
								}
							})
						}
						result.list[0].repeatCollect = result.list[0].repeatCollect+''
						if(result.list[0].repeatCollect){
							for (let i = 0; i < this.repetition.length; i++) {
									if (this.repetition[i].value == result.list[0].repeatCollect) {
											this.current = i;
											break;
									}
							}
						}
				  } else {
				    uni.showToast({
				      icon: "none",
				      title: result.msg || "接口获取数据失败！",
				    });
				  }
				});
			},
      goBack() {
        uni.navigateBack();
      },
			search(type){
				uni.navigateTo({
				  url: `../searchSqType/searchSqType?type=${type}&id=${this.gdId}`
				});
			},
			delect(index, type){
				if(type=='1'){
					this.startDept.splice(index, 1)
					uni.setStorageSync('startDept', JSON.stringify(this.startDept))
				}else if (type=='2'){
					this.checkoutDept.splice(index, 1)
					uni.setStorageSync('checkoutDept', JSON.stringify(this.checkoutDept))
				}else if (type=='3'){
					this.testTube.splice(index, 1)
					uni.setStorageSync('testTube', JSON.stringify(this.testTube))
				}else if (type=='4'){
					this.speStateData.splice(index, 1)
					uni.setStorageSync('speState', JSON.stringify(this.speStateData))
				}
			},
			submit(){
				uni.showLoading({
				  title: '加载中',
				  mask: true,
				})
				let data = {
					id: this.id,
					gdId: this.gdId,
					hosId: this.detailHosId,
					createTime: this.createTime,
					collectDept: this.startDept && this.startDept.length>0 ? this.startDept.map(i => { return i.id}).join(',') : undefined,
					repeatCollect: this.repetition[this.current] && this.repetition[this.current].value || undefined,
					speState: this.speStateData && this.speStateData.length>0 ? this.speStateData.map(i => { return i.id}).join(',') : undefined,
					testDept: this.checkoutDept && this.checkoutDept.length>0 ? this.checkoutDept.map(i => { return i.id}).join(',') : undefined,
					tubeType: this.testTube && this.testTube.length>0 ? this.testTube.map(i => { return i.id}).join(',') : undefined,
					urgent: null
				}
				let value = []
				this.urgencyData.forEach(i=>{
					if(i.checked){
						value.push(i.value)
					}
				})
				if(value.length>0){
					data.urgent = value.join(',')
				}else{
					delete data.urgent
				}
				post("/simple/data/updData/speOrderCollectInfo", data).then((result) => {
				  uni.hideLoading();
				  if (result.status == 200) {
						uni.removeStorageSync('startDept')
						uni.removeStorageSync('checkoutDept')
						uni.removeStorageSync('testTube')
						uni.removeStorageSync('speState')
						let sessData = uni.getStorageSync('gdData')
						let data = null
						if(sessData){
							data = JSON.parse(sessData)
						}else{
							data = null
						}
						uni.navigateTo({
						  url: `../scanning_Result/scanning_Result?${data}&intercept=1&isShow=0`,
						});
				  } else {
				    uni.showToast({
				      icon: "none",
				      title: result.msg || "接口获取数据失败！",
				    });
				  }
				});
			},
			radioChange(evt) {
					for (let i = 0; i < this.repetition.length; i++) {
							if (this.repetition[i].value == evt.detail.value) {
									this.current = i;
									break;
							}
					}
			},
			checkboxChange(e){
				var items = this.urgencyData,
						values = e.detail.value;
					for (var i = 0, lenI = items.length; i < lenI; ++i) {
						const item = items[i]
						if(values.includes(item.value)){
							this.$set(item,'checked',true)
						}else{
							this.$set(item,'checked',false)
						}
					}
			},
      selectItem(user){
        uni.redirectTo({
          url: `/pages/transferWorkOrder/transferWorkOrderPatient?userId=${user.id}&userName=${user.name}&userAccount=${user.account}`,
        });
      },
      getList(keyword){
        uni.showLoading({
          title: "加载中",
        });
        post("/simple/data/fetchDataList/user", {
          idx: 0,
          sum: 25,
          user: {
            hosId: this.hosId,
            name: keyword || undefined,
            online: true,
          }
        }).then((result) => {
          uni.hideLoading();
          if (result.status == 200) {
            this.list = result.list || [];
          } else {
            uni.showToast({
              icon: "none",
              title: result.msg || "接口获取数据失败！",
            });
          }
        });
      },
    }
  }
</script>

<style scoped>
	uni-checkbox-group,
	uni-radio-group{
		display: flex;
	}
	.uni-label-pointer{
		display: flex;
		margin-left: 20rpx;
	}
/* 	uni-radio-group{
		
	} */
</style>
<style lang="less" scoped>
.container{
  width: 100%;
	height: 100vh;
	.title{
		font-size: 32rpx;
		font-weight: bold;
		color: #49B856;
		text-align: center;
		padding: 20rpx 0;
	}
	.content{
		height: 82vh;
		overflow-y: auto;
		.list{
			.top{
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 20rpx;
				border-bottom: 1px solid #E6E6E6;
				.title{
					font-size: 32rpx;
					font-weight: bold;
					color: #000;
				}
				.right-icon{
					color: #888;
				}
			}
			.content{
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 20rpx;
				height: 80rpx;
				.name{
					
				}
				.right-icon{
					color: #888;
				}
			}
		}
	}
  .footer{
		padding: 0 20rpx;
    margin-top: 24rpx;
    margin-bottom: 24rpx;
    line-height: 80rpx;
    height: 80rpx;
    background: #fff;
    display: flex;
		justify-content: space-between;
    align-items: center;
    .btn {
			width: 48%;
      height: 80rpx;
      border-radius: 8rpx;
      font-size: 32rpx;
      text-align: center;
    }
		.affirm{
			background-color: #49B856;
			color: #fff;
		}
		.cancel{
			background-color: #AFAFAF;
			color: #fff;
		}
  }
}

</style>
