<template>
  <div>
    <BackBar title="待办处理-个贷" lefttext="返回" />
    <van-form>
      <van-cell-group inset>
        <van-field label="项目编号" v-model="personal.orderId" name="orderId" readonly placeholder="-" />
        <van-field label="坐落" v-model="personal.location" name="location" readonly type="textarea" placeholder="-" />
        <van-field label="当前节点" v-model="currentNode.nodeName" name="nodeName" readonly :error="true" placeholder="-" />
        <van-field label="处理人" v-model="currentNode.handlerName" name="handlerName" readonly placeholder="-" />
        <van-field label="面积" v-model="personalTarget.acreage" name="acreage" readonly placeholder="-">
          <template #button>平米</template>
        </van-field>
        <van-field label="单价" v-model="personalTarget.price" name="price" readonly placeholder="-">
          <template #button>元</template>
        </van-field>
        <van-field label="总价" v-model="personalTarget.amount" name="amount" readonly placeholder="-">
          <template #button>元</template>
        </van-field>
        <van-field label="已出产品" name="feedback" readonly>
          <template #input>
            <van-tag type="primary"
              v-for="(p, index) in feedback">{{ p == 'STATEMENT' ? '价值意见书' : (p == 'REPORT' ? '报告' : (p == 'LETTER'? '复评函':'')) }}</van-tag>
          </template>
        </van-field>
        <van-field label="客户名称" v-model="personal.clientName" name="clientName" readonly placeholder="-" />
        <van-field label="业务来源" v-model="personal.clientSubName" name="clientSubName" readonly placeholder="-" />
        <van-field label="中介" v-model="personal.agent" name="agent" readonly placeholder="-" />
        <van-field label="联系人" v-model="personal.contactName" name="contactName" readonly placeholder="-" />
        <van-field label="联系电话" v-model="personal.contactTel" name="contactTel" readonly placeholder="-" />
        <van-field label="委托人1" v-model="personal.bailorA" name="bailor" readonly placeholder="-" />
        <van-field label="委托人1电话" v-model="personal.bailoraTel" name="bailor" readonly placeholder="-" />
        <van-field label="委托人2" v-model="personal.bailorB" name="bailor" readonly placeholder="-" />
        <van-field label="委托人2电话" v-model="personal.bailorbTel" name="bailor" readonly placeholder="-" />
        <van-field label="下单时间" v-model="personal.created" name="created" readonly placeholder="-" />
        <van-field label="节点时间" v-model="currentNode.nodeTime" name="nodeTime" readonly placeholder="-" />
        <van-field label="最新备注" v-model="lastestComments" name="lastestComments" readonly type="textarea" />
        <van-field label="我的备注" v-model="commitInfo.comments" name="comments" type="textarea" placeholder="请填入当前节点提交的备注"/>
      </van-cell-group>
    </van-form>
    <van-form v-show="currentNode.nodeCode=='QUOTATION_FEEDBACK' || currentNode.nodeCode=='STATEMENT_FEEDBACK' || currentNode.nodeCode=='REPORT_FEEDBACK' || currentNode.nodeCode=='LETTER_FEEDBACK'">
      <van-cell-group inset>
        <VanSinglePicker label="反馈信息" v-model="productionType" v-bind:columns="productionTypes" name="productionType"
          placeholder="选择反馈信息" clearable :rules="[{ required: true, message: '请选择反馈信息' }]" :required="true"
          @change="doFeedback()" />
      </van-cell-group>
    </van-form>
    <div class="buttons-div">
      <van-button class="button" type="warning"  @click="commitNode('REVERSE')" v-if="currentNode.reversible">退回流程</van-button>
      <van-button class="button" type="primary"  @click="openTransferDialog()">任务转交</van-button>
      <van-button class="button" type="success"  @click="commitNode('PASS')">提交流程</van-button>
    </div>

    <van-popup v-model:show="taskTransforDialog" :style="{ padding: '10px', width:'80%' }" round >
      <van-form>
        <van-cell-group inset>
          <van-radio-group v-model="transforUserId" direction="horizontal">
            <van-radio :name="u.id" class="radio-class" v-for="(u,index) in users">{{ u.name }}</van-radio>
          </van-radio-group>
        </van-cell-group>
        <van-button round block  size="small" type="primary" native-type="submit" @click="doTransfer()">
          提交
        </van-button>
      </van-form>
    </van-popup>
  </div>
</template>

<script>
import { showConfirmDialog } from 'vant';
import { showNotify } from 'vant';
import { getPersonalTodoDetail, getPersonalTargetDetail } from '@/api/personal';
import { currentNode } from '@/api/workFlowNodeInstance';
import { commit } from '@/api/workflow';
import { doFeedback } from '@/api/personalTarget';
import { postUser } from '@/api/user';
import { taskTransfer } from '@/api/workRecordTransfer';

export default {
  data() {
    return {
      // 个贷详情
      personal: {},
      personalTarget: {},
      currentNode: {},
      // 节点提交信息
      commitInfo: {},
      productionType: null,
      feedback: [],
      lastestComments:null,
      users:[],
      transforUserId:null,
      taskTransforDialog:false
    };
  },
  created() {
    this.personal.id = this.$route.query.id;
    this.personal.orderId = this.$route.query.orderId;
    this.lastestComments = this.$route.query.lastestComments;
    this.getTodoDetail();
    this.getTargetDetail();
    this.getCurrentNode();
  },
  computed: {
    productionTypes() {
      if (this.currentNode.nodeCode == 'QUOTATION_FEEDBACK') {
        return [
          { text: '出价值意见书', value: 'STATEMENT' },
          { text: '出报告', value: 'REPORT' },
          { text: '出复评函', value: 'LETTER' },
          { text: '结束', value: 'NONE' },
        ]
      }
      if (this.currentNode.nodeCode == 'STATEMENT_FEEDBACK') {
        return [
          { text: '修改意见书', value: 'CHANGE_STATEMENT' },
          { text: '出报告', value: 'REPORT' },
          { text: '出复评函', value: 'LETTER' },
          { text: '结束', value: 'NONE' },
        ]
      }
      if (this.currentNode.nodeCode == 'REPORT_FEEDBACK') {
        return [
          { text: '修改报告', value: 'CHANGE_REPORT' },
          { text: '出复评函', value: 'LETTER' },
          { text: '结束', value: 'NONE' },
        ]
      }
      if (this.currentNode.nodeCode == 'LETTER_FEEDBACK') {
        return [
          { text: '修改复评函', value: 'CHANGE_LETTER' },
          { text: '结束', value: 'NONE' },
        ]
      }
    }
  },
  methods: {
    // 获取待办详情
    getTodoDetail() {
      if (this.personal.id) {
        getPersonalTodoDetail(this.personal.id).then((res) => {
          if (res.code === 200) {
            this.personal = res.data;
          }
        });
      }
    },
    // 获取评估对象详情
    getTargetDetail() {
      if (this.personal.id) {
        getPersonalTargetDetail(this.personal.id).then((res) => {
          if (res.code === 200) {
            this.personalTarget = res.data;
            const feedback = res.data.feedback;
            if (feedback){
              this.feedback = JSON.parse(res.data.feedback);
            }
          }
        });
      }
    },
    // 获取当前节点实例
    getCurrentNode() {
      if (this.personal.id) {
        currentNode({ mainBusiness: 'PERSONAL_BUSINESS', businessId: this.personal.id, businessSubId: this.personal.orderId }).then((res) => {
          if (res.code === 200) {
            this.currentNode = res.data;
            this.currentNode.handlerName = this.$route.query.handlerName;
            this.currentNode.nodeTime = this.$route.query.nodeTime;
          }
        });
      }
    },
    // 提交节点
    commitNode(state) {
      showConfirmDialog({
        title: '提交节点',
        message: '确认提交流程节点?',
      }).then(() => {
        this.commitInfo.instanceNodeId = this.currentNode.instanceId;
        this.commitInfo.state = state;
        this.commitInfo.businessSubId = this.personal.orderId;
        //检查提交的节点
        const valid = this.checkNodeCommit();
        if (valid) {
          commit(this.commitInfo).then((res) => {
            if (res.code === 200 && res.data) {
              showNotify({ type: 'success', message: '节点提交成功' });
              history.back();
            }
          });
        }
      });
    },
    //报价客户反馈提交
    doFeedback() {
      let taskData = new Object();
      taskData.id = this.personalTarget.id;
      taskData.personalId = this.personalTarget.personalId;
      this.feedback.push(this.productionType);
      this.feedback = Array.from(new Set(this.feedback));
      taskData.feedback = JSON.stringify(this.feedback);
      let feedback = new Object();
      feedback.taskData = taskData;
      feedback.recordId = this.currentNode.tasks[0].recordId;
      doFeedback(feedback).then((res) => {
        if (res.code === 200 && res.data) {
          showNotify({ type: 'success', message: '反馈提交成功。' });
        }
        this.getTargetDetail();
      });
    },
    checkNodeCommit() {
      //提交报价客户反馈节点需要的参数
      if (this.currentNode.nodeCode == 'QUOTATION_FEEDBACK') {
        if (!this.feedback || this.feedback.length==0) {
          showNotify({ type: 'danger', message: '请选择反馈信息' });
          return false;
        }
        this.commitInfo.production = this.feedback;
        this.commitInfo.ccId = this.personal.clienteleId;
        this.commitInfo.ifFeedback = false;
      }
      if (this.currentNode.nodeCode == 'STATEMENT_FEEDBACK') {
        const filter = this.feedback.filter(item => item != 'STATEMENT');
        if (filter.length==0) {
          showNotify({ type: 'danger', message: '请选择反馈信息' });
          return false;
        }
        this.commitInfo.production = this.feedback;
        this.commitInfo.ccId = this.personal.clienteleId;
        this.commitInfo.ifFeedback = false;
      }
      if (this.currentNode.nodeCode == 'REPORT_FEEDBACK') {
        let filter = this.feedback.filter(item => item != 'STATEMENT');
        filter = filter.filter(item => item != 'CHANGE_STATEMENT');
        filter = filter.filter(item => item != 'REPORT');
        if (filter.length==0) {
          showNotify({ type: 'danger', message: '请选择反馈信息' });
          return false;
        }
        this.commitInfo.production = this.feedback;
        this.commitInfo.ccId = this.personal.clienteleId;
        this.commitInfo.ifFeedback = false;
      }
      if (this.currentNode.nodeCode == 'LETTER_FEEDBACK') {
        let filter = this.feedback.filter(item => item != 'STATEMENT');
        filter = filter.filter(item => item != 'CHANGE_STATEMENT');
        filter = filter.filter(item => item != 'REPORT');
        filter = filter.filter(item => item != 'CHANGE_REPORT');
        filter = filter.filter(item => item != 'LETTER');
        if (filter.length==0) {
          showNotify({ type: 'danger', message: '请选择反馈信息' });
          return false;
        }
        this.commitInfo.production = this.feedback;
        this.commitInfo.ccId = this.personal.clienteleId;
        this.commitInfo.ifFeedback = false;
      }
      return true;
    },
    openTransferDialog(){
       this.taskTransforDialog = true;
       postUser("个贷外业岗").then(res=>{
        if (res.code == 200){
            this.users = res.data;
        }
       })
    },
    doTransfer(){
      let params = new Object();
      params.recordId = this.currentNode.tasks[0].recordId;
      params.toId = this.transforUserId;
      taskTransfer(params).then(res=>{
        if (res.code == 200){
          showNotify({ type: 'success', message: '任务转交成功' });
          this.taskTransforDialog = false;
          history.back();
        }
      })
    }
  },
};
</script>

<style scoped>
.button-style {
  display: inline-block;
  margin: 20px;
  width: 100px;
}
.buttons-div{
  width: 100%;
  display: flex;
  padding: 17px;
}

.button{
  width: 90px;
  margin-right: 50px;
}

.radio-class{
  margin-bottom: 10px;
}
</style>