<template>
  <view class="task-set">
    <!-- <headNav :isBackShow="true" :textContents="'任务设置'" :navBackgroundColor="'#fff'"></headNav> -->
    <view class="nav">
      任务设置
    </view>
<!--    <view class="task-tab" v-if="stageData.operate_ids.length>0">-->
<!--      <view class="tab-item" :class="{'tab-select':operateType===1}" @click="setOperateType(1)">任务操作</view>-->
<!--      <view class="tab-item" :class="{'tab-select':operateType===2}" @click="setOperateType(2)">阶段操作</view>-->
<!--    </view>-->
    <view v-if="true">
      <view class="content">
        <view class="choose-box">
          <view class="choose-box-left">
            下一处理阶段
          </view>
          <view class="choose-box-right" @click="openPopupObj(1)">
            <view class="choose-box-right-content">
              {{stageData.stageName?stageData.stageName:'请选择任务阶段'}}
            </view>
            <image class="choose-box-right-img" src="@/static/add-client/choose.png" mode=""></image>
          </view>
        </view>
        <view class="choose-box choose-box-two" v-if="selectType<2">
          <view class="choose-box-left">
            下一执行人员
          </view>
          <view class="choose-box-right" @click="openPopupObj(2)">
            <view class="choose-box-right-content">
              {{stageData.userName?stageData.userName:'请选择执行人员'}}
            </view>
            <image class="choose-box-right-img" src="@/static/add-client/choose.png" mode=""></image>
          </view>
        </view>
        <view class="choose-box-stage">
          <en-input v-if="stageData.operate_ids.indexOf('2')>-1" label="回款金额" v-model="businessData.returned_total" type="digit" placeholder="请输入回款金额" right-text="万元"></en-input>
          <en-input v-if="stageData.operate_ids.indexOf('1')>-1" label="出款金额" v-model="businessData.out_total" type="digit" placeholder="请输入出款金额"  right-text="万元"></en-input>
          <en-input v-if="stageData.operate_ids.indexOf('4')>-1" label="银行出款" v-model="businessData.bank_total" type="digit" placeholder="请输入银行出款"  right-text="万元"></en-input>
          <view v-if="stageData.operate_ids.indexOf('3')>-1">
            <en-input label="回款姓名" v-model="businessData.returned_name" type="text" placeholder="请输入回款人姓名"></en-input>
            <en-select  label="回款银行" v-model="businessData.returned_bank_id" :localData="bankData"  placeholder="请选择还款银行"></en-select>
          </view>
        </view>
        <add-msg v-model="msgData"></add-msg>
      </view>
      <view class="submit-box" @click="submit">
        <view class="submit">
          提交
        </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>
    </view>
    <view class="operate-box" v-else>
      <view class="content-box">
        <en-input v-if="stageData.operate_ids.indexOf('1')>-1" label="回款金额" v-model="operateData.returned_total" type="number" placeholder="请输入回款金额"></en-input>
        <en-input v-if="stageData.operate_ids.indexOf('2')>-1" label="出款金额" v-model="operateData.out_total" type="number" placeholder="请选择还款银行"></en-input>
      </view>
      <view class="submit-box" @click="submitOperate">
        <view class="submit">
          提交
        </view>
      </view>
    </view>


  </view>
</template>

<script>
import {
  auditTask, getReturnedBankList,
  getTaskStage,
  setExecuteTask, setStageOperate
} from "@/api/task";
  import tools from "@/service/tools";
  import PreviewImage from "@/components/PreviewImage/index";
  import {getStageUser} from "@/api/task";
  import AddMsg from "./module/add-msg";
  import EnInput from "@/components/en-from/en-input/en-input";
import EnSelect from "@/components/en-from/en-select/en-select";

  export default {
    props: {
      businessId: {
        default: 0
      },

    },
    components: {
      EnSelect,
      EnInput,
      AddMsg,
      PreviewImage
    },
    data() {
      return {
        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
      }
    },
    mounted() {
      this.getTaskStage();
    },
    methods: {
      setOperateType(operateType){
        if(this.operateType!==operateType){
          this.operateType=operateType
        }
      },
      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)
          }
        })
      },
      setPopupClosed() {
        //关闭选择器
        this.showExecute = false;
      },
      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
          }
        })
      },
      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();
      },
      startStage() {
        this.isAudit = false;
        this.getTaskStage();
      },
      startAuditType(auditType) {
        this.isAudit = true;
        this.businessData.audit_type = auditType;
        this.getTaskStage();
      },
      submit() {
        // console.log('sadsadsajkhhjasd---------------------')
        // uni.$emit('updateMemberInfo')
        // console.log('newReceiving:---------------------')
        if (this.isAudit) {
          this.auditTask()
        } else {
          this.setExecuteTask()
        }
      },
      setExecuteTask() {
        setExecuteTask(this.businessData).then((auditRes) => {
          if (auditRes.code === 1) {
            tools.success(auditRes.msg)
            this.$emit('endTaskSet')
            uni.$emit('newTaskList')
          } else {
            tools.error(auditRes.msg)
          }
        })
      },
      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)
                  this.$emit('endTaskSet')
                  uni.$emit('newTaskList')
                  console.log('newReceiving:---------------------')
                } else {
                  tools.error(auditRes.msg)
                }
              })
            }
          }
        });
      },
      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('阶段信息异常')
          }
        })
      },
      leftClick() {
        uni.navigateBack({
          delta: 1
        })
      },
    },
  }
</script>
<style lang="scss" scoped>


  .task-set {
    width: 100%;
    min-height: 50vh;
    max-height: 70vh;
    background: #F6F7FB;
    overflow: auto;
    border-radius: 20rpx 20rpx 0rpx 0rpx;
    .nav {
      font-size: 18px;
      font-weight: bold;
      text-align: center;
      padding: 30rpx 0 15rpx;
      background: #fff;
    }
    .task-tab{
      display: flex;
      justify-content: space-between;
      height: 88rpx;
      .tab-item{
        width: 50%;
        height: 100%;
        font-size: 14px;
        color: #333333;
        line-height: 88rpx;
        text-align: center;
        background: #fff;
        border: 2rpx solid #3169FA;
        box-sizing: border-box;
      }
      .tab-select{
        background: #3169FA;
        color: #FFFFFF;
        border:none;
      }
    }

    .content {
      width: 100%;
      height: auto;
      padding: 22rpx 0 106rpx 0;

      .choose-box {
        width: 100%;
        height: auto;
        padding: 34rpx 35rpx;
        box-sizing: border-box;
        background: #fff;
        border-top: 1px solid #F0F0F0;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .choose-box-left {
          font-size: 16px;
          color: #666666;
        }

        .choose-box-right {
          display: flex;
          align-items: center;

          .choose-box-right-content {
            font-size: 16px;
            font-weight: 400;
            color: #232A35;
            margin: 0 18rpx 0 0;
          }

          .choose-box-right-img {
            width: 12rpx;
            height: 20rpx;
          }
        }
      }

      .choose-box-stage{
        background: #fff;
        padding: 0 22rpx;
        margin-bottom: 22rpx;
      }

      .choose-box-two {
        margin: 0 0 22rpx 0;
      }


    }

    .submit-box {
      width: 100%;
      height: auto;
      border-top: 1px solid #F0F0F0;
      background: #fff;
      position: fixed;
      left: 0;
      bottom: 0;
      padding: 12rpx 32rpx;
      box-sizing: border-box;

      .submit {
        width: 100%;
        height: auto;
        background: #3169FA;
        border-radius: 4px;
        font-size: 16px;
        color: #FFFFFF;
        padding: 20rpx 0;
        text-align: center;
      }
    }
    .operate-box{
      .content-box {
        margin-top: 20rpx;
        height: auto;
        padding: 0 20rpx;
        background: #fff;
      }
    }
  }
</style>
