<template>
	<view class="total-page page-box">
		<Nav :title="title" :genre="1" :opacity="scrollTop" is_fixed :bgHeight="bgHeight"></Nav>
		<view class="operate_body page-env-160" :style="{top:`${$tools.topHeight()}px`}">
			<view class="sys-background-fff m-lr30 m-t30 m-b20 p-lr30 r-20">
				<view class="" v-if="is_handle">
					<view class="row-justify-sb center p-tb30 bor-bottom-1 size-28">
						<view class="wh-text"><text></text>下一阶段处理</view>
						<view class="row-c flex"  @click="openPopupObj(1)">
<!--							<input class="wh-input sys-size-28 text-color-12 m-l20 flex"-->
<!--								placeholder-class="sys-size-28 text-color-999 sys-weight-400" placeholder="请选择"-->
<!--								v-model="inputValue"></input>-->
              {{stageData.stageName?stageData.stageName:'请选择任务阶段'}}
						</view>
						<uni-icons type="forward" size="18" color="#D8D8D8"></uni-icons>
					</view>
					<view class="row-justify-sb center p-tb30 bor-bottom-1 size-28"  v-if="selectType<2">
						<view class="wh-text"><text></text>下一执行人员</view>
						<view class="row-c flex" @click="openPopupObj(2)">
<!--							<input class="wh-input sys-size-28 text-color-12 m-l20 flex"-->
<!--								placeholder-class="sys-size-28 text-color-999 sys-weight-400" placeholder="请选择"-->
<!--								v-model="inputValue"></input>-->
              {{stageData.userName?stageData.userName:'请选择执行人员'}}
						</view>
						<uni-icons type="forward" size="18" color="#D8D8D8"></uni-icons>
					</view>
          <WhInput :name="'回款金额'"  v-if="stageData.operate_ids.indexOf('2')>-1" v-model="businessData.returned_total"  type="digit" placeholder="请输入回款金额" ></WhInput>
          <WhInput :name="'出款金额'"  v-if="stageData.operate_ids.indexOf('1')>-1" v-model="businessData.out_total"  type="digit" placeholder="请输入出款金额" ></WhInput>
          <WhInput :name="'银行出款'"  v-if="stageData.operate_ids.indexOf('4')>-1" v-model="businessData.bank_total"  type="digit" placeholder="请输入银行出款" ></WhInput>
          <view  v-if="stageData.operate_ids.indexOf('3')>-1">
            <WhInput :name="'回款姓名'"   v-model="businessData.returned_name"  type="text" placeholder="请输入回款人姓名" ></WhInput>
<!--            此处应该是select-->
            <WhInput :name="'银行出款'"  v-model="businessData.returned_bank_id"  :localData="bankData" placeholder="请选择还款银行" ></WhInput>
          </view>

				</view>
<!--				<view class="" v-else>-->
<!--					<WhInput :name="'冲销类型'" :is_border="false" is_select disabled></WhInput>-->
<!--				</view>-->
			</view>
			<view class="sys-background-fff m-lr30 r-20">
				<SendChat  v-model="msgData"></SendChat>
			</view>
		</view>
    <view v-show="showExecute">
      <uni-data-picker class="" :popup-title="popupTitle" :localdata="popupData" ref="popupObj" @change="setNewExecute"
                       :border="false" :clear-icon="false" @popupclosed="setPopupClosed">
      </uni-data-picker>
    </view>
		<EnButton text="提交" @onSubmit="submit"></EnButton>
	</view>
</template>

<script>
	import WhInput from "@/components/en-from/en-input/en-input.vue"
	import SendChat from "@/page_task/task_details/module/send_chat.vue"
  import tools from "@/service/tools";
  import {
    auditTask, getReturnedBankList,
    getTaskStage,
    setExecuteTask, setStageOperate
  } from "@/api/task";
  import {getStageUser} from "@/api/task";
	export default {
		components: {
			WhInput,
			SendChat
		},
		data() {
			return {
				bgHeight: 110,
				scrollTop: 0,
				is_handle: true,
        title:'任务反馈',
        businessId:'',
        type:'',
        stageData: {
          operate_ids:[]
        },
        businessData: {
          'id': '',
          'manage_id': '',
          'stage_id': '',
          'audit_type': '',
          'msg': '',
          'msg_img': [],
          'file_list': [],
          'returned_total':'',
          'out_total':'',
          'bank_total':'',
          'returned_name':'',
          'returned_bank_id':'',
        },
        msgData:{
          'msg': '',
          'msg_img': [],
          'file_list': [],
        },
        users: [],
        stageList: [],
        isAudit: false,
        showExecute: false,
        popupData: [],
        popupType: 1,
        auditType: 0,
        popupTitle: '',
        previewList: [],
        current: 0,
        selectType: 1,
        //新增阶段操作数据
        operateType:1,
        stageId:0,
        operateArr:[],
        operateData:{
          'returned_total':'',
          'out_total':'',
          'returned_bank':'',
          'returned_bank_id':'',
        },
        bankData:[]
			}
		},
    watch:{
      'msgData':{
        handler() {
          this.businessData.msg=this.msgData.msg
          this.businessData.msg_img=this.msgData.msg_img
          this.businessData.file_list=this.msgData.file_list
        },
        deep: true
      }
    },
    onLoad(ret) {
      console.log(ret)
      if(ret.businessId===undefined){
        tools.leftClick()
      }else {
        this.businessId=ret.businessId
        this.type=ret.type
        console.log( this.type==='2')
        if(ret.auditType)this.businessData.audit_type=ret.auditType*1
        if(this.type==='1'){
          this.title='任务反馈'
        }else if(this.type==='2'){
          tools.success('ok')
          this.title='任务审核'
        }else {
          this.title='任务冲销'
        }
        this.getTaskStage();
      }
    },
		onPageScroll(res) {
			this.scrollTop = res.scrollTop / 120
		},
		methods: {
      openPopupObj(type) {
        this.popupType = type;
        if (type === 1) {
          if (this.selectType > 2) {
            tools.error('此阶段不能修')
            return false;
          }
          this.popupTitle = '请选择执行阶段'
          this.popupData = this.stageList
        } else {
          if (this.selectType > 1) {
            tools.error('此阶段不能修改')
            return false;
          }
          this.popupTitle = '请选择执行人'
          this.popupData = this.users
        }
        this.showExecute = true;
        this.$refs.popupObj.show();
      },
      setPopupClosed() {
        //关闭选择器
        this.showExecute = false;
      },
      submitOperate(){
        this.operateData.businessId=this.businessData.id
        this.operateData.stageId=this.stageId
        setStageOperate(this.operateData).then((res)=>{
          if(res.code===1){
            this.operateData={
              'returned_total':'',
              'out_total':'',
            }
            this.operateType=1
            tools.success(res.msg)
            this.$emit('endTaskSet')
          }else {
            tools.error(res.msg)
          }
        })
      },
      setNewExecute(e) {
        //设置新执行人
        let newData = e.detail.value[0]
        this.showExecute = false;
        if (this.popupType === 1) {
          this.businessData.stage_id = newData.value
          this.stageData.stageName = newData.text
          this.getStageUser(this.businessData.stage_id)
          this.stageList.forEach((stage) => {
            if (stage.value === newData.value) {
              if (stage.type !== 2  ) {
                this.selectType = 2
              }else {
                this.selectType = 1
              }
            }
          })
        } else {
          this.businessData.manage_id = newData.value
          this.stageData.userName = newData.text
        }
      },
      getStageUser(stage_id){
        getStageUser({'stage_id':stage_id}).then((res)=>{
          if(res.code===1){
            this.users=res.data
            this.businessData.manage_id =  this.users[0].value
            this.stageData.userName = this.users[0].text
          }
        })
      },
      submit() {
        if (this.type==='2') {
          this.auditTask()
        } else {
          this.setExecuteTask()
        }
      },
      getTaskStage() {
        getTaskStage({
          'id': this.businessId
        }).then((res) => {
          if (res.code === 1) {
            this.stageData = res.data.stageData;
            console.log(this.stageData.operate_ids)
            if(this.stageData.operate_ids.indexOf('3')>-1){
              //获取回款银行信息
              getReturnedBankList().then((bankRes)=>{
                if(bankRes.code===1){
                  this.bankData=bankRes.data
                }
              })
            }
            this.businessData.id = res.data.business.id;
            this.businessData.manage_id = res.data.business.manage_id;
            this.businessData.stage_id = res.data.business.stage_id;
            this.stageId = res.data.business.stage_id;
            this.users = res.data.users;
            this.stageList = res.data.stageList;
            let isNext=false
            this.stageList.forEach((item)=>{
              if(res.data.business.stage_id===item.value){
                isNext=true
              }else if(isNext){
                this.businessData.stage_id=item.value
                this.stageData.stageName=item.text
                this.getStageUser(this.businessData.stage_id)
                if (item.type > 2 ) {
                  this.selectType = 2
                }else {
                  this.selectType = 1
                }
                isNext=false
              }
            })

            if (this.stageData.operateNum > 1) {
              this.selectType = 3;
            }
          } else {
            tools.error('阶段信息异常')
          }
        })
      },
      auditTask() {
        uni.showModal({
          title: '提示',
          content: this.businessData.audit_type === 1 ? '是否通过当前任务？' : '是否驳回当前任务？',
          success: (res) => {
            if (res.confirm) {
              auditTask(this.businessData).then((auditRes) => {
                if (auditRes.code === 1) {
                  tools.success(auditRes.msg)
                  uni.$emit('newTaskList')
                  setTimeout(()=>{
                    tools.leftClick()
                  },500)
                } else {
                  tools.error(auditRes.msg)
                }
              })
            }
          }
        });
      },
      setExecuteTask() {
        setExecuteTask(this.businessData).then((auditRes) => {
          if (auditRes.code === 1) {
            tools.success(auditRes.msg)
            // this.$emit('endTaskSet')
            uni.$emit('newTaskList')
            setTimeout(()=>{
              tools.leftClick()
            },1000)
          } else {
            tools.error(auditRes.msg)
          }
        })
      },


		}
	}
</script>

<style lang="scss" scoped>
	.operate_body {
		width: 100%;
		position: absolute;
	}

	.wh-input {
		width: 100%;
		text-align: right;
		direction: rtl;
	}
</style>
