<template>
  <view class="HomeItem">
    <view class="goWorkAll">
			<view class="title">{{taskName}}</view>
			<view class="goWorkSelect history">
			  <view class="goWorkSelect-list">
					<view class="list-title">申请科室：</view>
					<view @click="searchDept('applyDept')" :class="applyDeptId ? 'black' : 'gray'">
						{{applyDept}}
					</view>
					<text class="right-icon newicon newicon-weibiaoti2010104" @click="searchDept('applyDept')"></text>
					<text class="toolbar-icon newicon newicon-saoma" @click="scan()"></text>
				</view>
				<view class="goWorkSelect-list">
					<view class="list-title">起点科室：</view>
					<view @click="searchDept('startDept')" :class="startDeptId ? 'black' : 'gray'">
						{{startDept}}
					</view>
					<text v-if="startTarget==202 || startTarget==204 || startTarget==205" class="right-icon newicon newicon-weibiaoti2010104" @click="searchDept('startDept')"></text>
				</view>
				<view class="goWorkSelect-list">
					<view class="list-title">终点科室：</view>
					<view @click="searchDept('endDept')" :class="endDeptId ? 'black' : 'gray'">
						{{endDept}}
					</view>
					<text v-if="endTarget==202 || endTarget==204 || endTarget==205" class="right-icon newicon newicon-weibiaoti2010104" @click="searchDept('endDept')"></text>
				</view>
				<view class="goWorkSelect-list besides" v-if="remarksSwitch==1">
					<view class="list-title">备注信息：</view>
					<textarea v-model="remark" :placeholder="remarksPrompts"/>
				</view>
			</view>
    </view>
    <view class="foot_btn_spe">
			<view class="btn3" @click="submit">建单</view>
      <view class="btn3" @click="goBack">返回</view>
    </view>
		
		<uni-popup ref="alertDialog" type="dialog" :mask-click="false">
			<uni-popup-dialog type="info" cancelText="取消" confirmText="确定" title="提示" :content="content" @confirm="dialogConfirm"
				@close="dialogClose"></uni-popup-dialog>
		</uni-popup>
  </view>
</template>

<script>
  import {
    get,
    post,
    webHandle,
		SM
  } from "../../http/http.js";
	
  export default {
    data() {
      return {
				list:[],
				hosId: uni.getStorageSync('userData').user.currentHospital.id,
				SMFlag:true,
				applyDept:"请选择申请科室",
				applyDeptId:null,
				startDept:"请选择起点科室",
				startDeptId:null,
				endDept:"请选择终点科室",
				endDeptId:null,
				remark:null,
				itemTaskType:null,
				remarksSwitch:null,
				remarksPrompts:null,
				startTarget:null,
				endTarget:null,
				taskTypeData:null, //点击的任务类型数据
				taskTypeId: null,
				content:null,
				tipsCreateOder:null
      };
    },
    methods: {
			// 清空缓存
			setSess(){
				uni.setStorageSync('applyDept', null)
				uni.setStorageSync('taskStartDept', null)
				uni.setStorageSync('taskEndDept', null)
				uni.setStorageSync('sponsorTaskBuildData', null)
			},
      // 返回
      goBack() {
				this.setSess();
				uni.navigateTo({
					url:'/pages/sponsorTask/sponsorTask'
				})
      },
			// 建单
			submit(){
				if(!this.applyDeptId){
					uni.showToast({
					  icon: 'none',
					  title: '申请科室不能为空'
					});
					return
				}
				if(!this.startDeptId){
					uni.showToast({
					  icon: 'none',
					  title: '起点科室不能为空'
					});
					return
				}
				if(!this.endDeptId){
					uni.showToast({
					  icon: 'none',
					  title: '终点科室不能为空'
					});
					return
				}
				this.content = `您将前往【${this.startDept}】执行【${this.taskName}】,送往【${this.endDept}】,确定建单吗？`
				this.$refs.alertDialog.open()
			},
			
			// 确定中转
			dialogConfirm(){
				uni.showLoading({
				  title: "加载中",
				  mask: true,
				});
				let data = {
					workOrder:{
						tipsCreateOder: null,
						worker:{
							id: uni.getStorageSync('userData').user.id
						},
						createDept: this.applyDeptId,
						startDept:{
							id: this.startDeptId
						},
						endDepts:{
							id: this.endDeptId
						},
						sourceId: 4,
						workOrderRemark: this.remark,
						taskType:{
							id : this.taskTypeId
						}
					}
				}
				if(this.tipsCreateOder == 1){
					data.tipsCreateOder = 1
				}else{
					data.tipsCreateOder = undefined
				}
				post("/api/startOrder", data).then((res2) => {
				  uni.hideLoading();
				  if (res2.status == 200) {
						this.tipsCreateOder = null
						uni.showToast({
						  icon: 'none',
						  title: '操作成功'
						});
						this.setSess()
						setTimeout(_=>{
							uni.redirectTo({
								url:'/pages/receiptpage/receiptpage'
							})
						},1500)
				  } else if(res2.status == 1000033){
						this.tipsCreateOder = 1
						this.content = res2.msg
						this.$refs.alertDialog.close()
						setTimeout(_=>{
							this.$refs.alertDialog.open()
						},500)
				  } else {
						uni.showToast({
						  icon: 'none',
						  title: res2.msg || '请求数据失败！'
						});
					}
				});
			},
			// 取消
			dialogClose(){
				this.tipsCreateOder = null
				this.$refs.alertDialog.close()
			},
			// 搜索科室
			searchDept(type){
				if(type=='startDept'){
					if(this.startTarget==201 || this.startTarget==203){
						return
					}
				}
				if(type=='endDept'){
					if(this.endTarget==201 || this.endTarget==203){
						return
					}
				}
				let data = {
					applyDept:this.applyDept,
					applyDeptId:this.applyDeptId,
					startDept:this.startDept,
					startDeptId:this.startDeptId,
					endDept:this.endDept,
					endDeptId:this.endDeptId,
					remark:this.remark
				}
				uni.setStorageSync('sponsorTaskBuildData', JSON.stringify(data))
				uni.navigateTo({
				  url: `../taskTypeSearch/taskTypeSearch?type=${type}&startTarget=${this.startTarget}&endTarget=${this.endTarget}&taskTypeData=${JSON.stringify(this.taskTypeData)}&taskTypeId=${this.taskTypeId}&taskName=${this.taskName}`
				});
			},
			// 扫描科室
			scan(){
				if (!this.SMFlag) {
				  return;
				}
				this.SMFlag = false;
				// let content = '87bdf1c7-1514-4fa8-8fa4-07f37c06bec7'
				SM().then((content) => {
					uni.showLoading({
					  title: "加载中",
					  mask: true,
					});
					let data = {
						getDeptInfo: true,
						content: content
					}
					post("/dept/scanning" , data).then((res2) => {
						this.SMFlag = true;
					  uni.hideLoading();
					  if (res2.state == 200) {
							this.applyDept = res2.data.dept
							this.applyDeptId = res2.data.id
							if(this.startTarget==201){
								this.startDept = res2.data.dept
								this.startDeptId = res2.data.id
							}
							if(this.endTarget==201){
								this.endDept = res2.data.dept
								this.endDeptId = res2.data.id
							}
					  } else {
					    uni.showToast({
					      icon: 'none',
					      title: res2.msg || '请求数据失败！'
					    });
					  }
					});
				}).catch(err => {
				  this.SMFlag = true;
				});
			},
			getTaskType(id){
				uni.showLoading({
				  title: "加载中",
				  mask: true,
				});
				let postData= {
					taskTypeId: id
				}
				post("/ser/getdeptList", postData).then((res) => {
					uni.hideLoading();
				  this.taskTypeData = res
					this.startTarget = this.taskTypeData.startStatus
					this.endTarget = this.taskTypeData.endStatus
					if(this.startTarget==203){
						this.startDept = this.taskTypeData.startDept[0].dept
						this.startDeptId = this.taskTypeData.startDept[0].id
					}
					if(this.endTarget==203){
						this.endDept = this.taskTypeData.endDept[0].dept
						this.endDeptId = this.taskTypeData.endDept[0].id
					}
					this.remarksSwitch = this.taskTypeData.taskType.remarksSwitch
					this.remarksPrompts = this.taskTypeData.taskType.remarksPrompts
				});
			}
    },
		onUnload(){
			this.setSess();
		},
		onShow(options){
			
		},
    onLoad(options) {
			console.log(111, options)
			let sponsorTaskBuildData = uni.getStorageSync('sponsorTaskBuildData')
			let applyDept = uni.getStorageSync('applyDept')
			let taskStartDept = uni.getStorageSync('taskStartDept')
			let taskEndDept = uni.getStorageSync('taskEndDept')
			if(applyDept){
				// 科室类型：201：默认发起科室
				// 科室类型：202：固定科室范围
				// 科室类型：203：固定科室
				// 科室类型：204：自主填写
				// 科室类型：205：固定科室类型
				let data = JSON.parse(applyDept)
				this.applyDept = data.dept
				this.applyDeptId = data.id
				if(options.startTarget){
					this.startTarget = options.startTarget
				}
				if(options.endTarget){
					this.endTarget = options.endTarget
				}
				console.log(456, this.startTarget)
				if(this.startTarget==201){
					this.startDept = data.dept
					this.startDeptId = data.id
				}
				if(this.endTarget==201){
					this.endDept = data.dept
					this.endDeptId = data.id
				}
			}
			 if (taskStartDept){
				 let data = JSON.parse(taskStartDept)
				 if(this.startTarget!=201){
					 this.startDept = data.dept
					 this.startDeptId = data.id
				 }
			}
			 if (taskEndDept){
				 let data = JSON.parse(taskEndDept)
				 if(this.endTarget!=201){
						this.endDept = data.dept
						this.endDeptId = data.id
				 }
			}
			// 之前有填写过数据-回显
			if(sponsorTaskBuildData){
				let data = JSON.parse(sponsorTaskBuildData)
				console.log(777, data)
				if(!applyDept){
					if(data.applyDept){
						this.applyDept = data.applyDept
						this.applyDeptId = data.applyDeptId
					}
				}
				
				if(!taskStartDept){
					if(data.startDept && this.startTarget!=201){
						this.startDept = data.startDept
						this.startDeptId = data.startDeptId
					}
				}
				
				if(!taskEndDept){
					if(data.endDept && this.endTarget!=201){
						this.endDept = data.endDept 
						this.endDeptId = data.endDeptId 
					}
				}

				if(data.remark){
					this.remark = data.remark
				}
			}
			
      if(options.data){
        options.data = JSON.parse(options.data);
				this.getTaskType(options.data.id);
				this.taskTypeId = options.data.id;
				this.taskName = options.data.taskName;
			}
			
			if(options.taskTypeId){
				this.taskTypeId = options.taskTypeId;
				this.taskName = options.taskName;
				this.getTaskType(options.taskTypeId);
			}
    },
  };
</script>

<style scoped>
	/deep/ .uni-textarea-wrapper{
	border: 1rpx solid #E5E5E5;
	padding-left: 6rpx;
}
/deep/ .uni-textarea-placeholder{
	padding: 6rpx;
}
/deep/ .uni-button-color{
	color: #64BD7B;
}
</style>
<style lang="less" scoped>
	.black{
		color: #000;
		width: 400rpx;
		// overflow:hidden;/*内容超出后隐藏*/
		// text-overflow:ellipsis;/*超出内容显示为省略号*/
		// white-space:nowrap;
	}
	.gray{
		color: #7b7b7b;
		width: 400rpx;
		// overflow:hidden;/*内容超出后隐藏*/
		// text-overflow:ellipsis;/*超出内容显示为省略号*/
		// white-space:nowrap;
	}
  .HomeItem {
    .foot_btn_spe {
      width: 100%;
      position: fixed;
      bottom: 30rpx;
      left: 0;
      line-height: 88rpx;
      height: 88rpx;
      text-align: center;
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
    
      &::after {
        content: '';
        flex: 1;
      }
    
      view {
        height: 88rpx;
        width: 48%;
        margin: 0 1%;
        background-image: linear-gradient(to right, #72c172, #3bb197);
        color: #fff;
        border-radius: 8rpx;
        font-size: 32rpx;
        margin-top: 16rpx;
      }
    }

    .login {
      height: 420rpx;
      padding: 0 32rpx;
      padding-top: 164rpx;
      position: relative;
      z-index: 999;

      .savePassword {
        margin-top: 32rpx;
      }

      /deep/ uni-checkbox:not([disabled]) .uni-checkbox-input:hover {
        border-color: #42b983 !important;
      }

      .login_input {
        margin-top: 32rpx;
        background-color: #ffffff;
        height: 72rpx;
        box-sizing: border-box;
        padding: 16rpx;
      }

      .title {
        font-size: 36rpx;
        color: #42b983;
        text-align: center;
      }

      .tips {
        font-size: 28rpx;
        color: red;
        margin-top: 16rpx;
      }

      .page_item_btn {
        height: 88rpx;
        background-image: linear-gradient(to right, #72c172, #3bb197);
        border-radius: 8rpx;
        line-height: 88rpx;
        color: #fff;
        font-size: 36rpx;
        font-weight: 700;
        margin-top: 64rpx;
        text-align: center;
      }
    }

    //上班页面
    .goWorkAll {
      overflow-y: auto;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      /deep/ .uni-radio-input-checked {
        background-color: #42b983 !important;
        border-color: #42b983 !important;
      }
      /deep/ .uni-checkbox-input-checked {
        color: #42b983 !important;
      }
			
			.title{
				text-align: center;
				color: #64BD7B;
				line-height: 80rpx;
				border-bottom: 1rpx solid #E5E5E5;
			}
			
      .goWorkSelect {
        &.history {
          padding-bottom: 16rpx;
        }

        &.combination {
          .goWorkSelect-head {}

          .goWorkSelect-list {}
        }

        .goWorkSelect-head {
          font-size: 28rpx;
          line-height: 80rpx;
          border-bottom: 2rpx solid #E5E5E5;
          text-align: center;
        }
				
				.besides{
					align-items: start !important;
				}
				
        .goWorkSelect-list {
					display: flex;
					flex-wrap: nowrap;
					align-items: center;
					padding: 20rpx;
					border-bottom: 1rpx solid #E5E5E5;
					position: relative;
					
					.list-title{
						width: 180rpx;
					}
					
					.input-sign{
						user-select: none; 
						pointer-events: none;
					}
					
					.right-icon{
						font-size: 38rpx !important;
						margin: 0 40rpx 0 10rpx;
						position: relative;
						top: -2rpx;
					}
					
					.toolbar-icon{
						position: absolute;
						right: 20rpx;
					}
					
					.newicon{
						font-size: 32rpx;
						color: #b8b8b8;
					}
					
					.input-num {
					  height: 60rpx;
					  border: 2rpx solid #E5E5E5;
					  border-radius: 5rpx;
					  // width: 100%;
					  padding-left: 10rpx;
					  // padding-right: 10rpx;
					}
					
          .goWorkSelect-item {
            height: 52rpx;
            display: flex;
            align-items: center;
            border-bottom: 2rpx solid #e5e9ed;
            padding: 16rpx;

            &.relative {
              position: relative;

              .picker {
                position: absolute;
                width: 100%;
                padding-left: 64rpx;
              }
            }

            button {
              font-size: 32rpx;
              height: 52rpx;
              line-height: 52rpx;
              margin: 0;
              margin-left: 16rpx;
              color: rgb(7, 134, 60);
              font-weight: 700;
            }
          }
        }
				
				.goWorkSelect-list:last-child {
					border-bottom: none;
				}
      }
    }

    .goWork {
      margin: 0 auto 48rpx;
      width: 240rpx;
      height: 240rpx;

      .goWork_btn_E {
        width: 100%;
        height: 100%;
        background: #bee1a7;
        border-radius: 30%;

        .goWork_btn_W {
          width: 75%;
          height: 75%;
          background-image: linear-gradient(to right, #72c172, #3bb197);
          border-radius: 30%;
          margin: 0 auto;
          position: relative;
          top: 12.5%;
          line-height: 180rpx;
          color: #fff;
          font-size: 36rpx;
          text-align: center;
        }
      }
    }

    .goWork_text {
      width: 100%;

      view {
        text-align: center;
      }

      .goWork_text-p {
        font-size: 36rpx;
        margin-bottom: 48rpx;
      }
    }

    .botImg {
      height: 600rpx;
      width: 100%;
      position: fixed;
      bottom: 0;

      .img {
        height: 100%;
        background: url("../../static/img/BG.png") no-repeat center center;
        background-size: 100% 100%;
      }
    }
  }
</style>
