<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <div class="tab-header webtop">
        <div>欠款出库申请详情</div>
        <div>
          <el-button type="primary" size="small" @click="openCountersign('加签')">加 签</el-button>
          <el-button type="primary" size="small" @click="openAgree('同意')">同 意</el-button>
          <el-button type="danger" size="small" @click="openReject('驳回')">驳 回</el-button>
          <el-button type="danger" size="small" @click="openStop('终止')">终 止</el-button>
        </div>
      </div>
      <div class="">
        <div class="titwu">欠款出库申请</div>
        <el-form ref="dataForm" :model="formobj" class="formaddcopy02" :rules="rules">
          <el-row style="border-top: 1px solid #E0E3EB">
            <el-col :span="8" class="tlineheightb">
              <div class="span-sty">发起部门</div>
              <el-form-item><span class="addinputInfo">{{ formobj.pcApplyDept }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">发起人</div>
              <el-form-item><span class="addinputInfo">{{ formobj.pcApplyName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">发起日期</div>
              <el-form-item><span class="addinputInfo">{{ formobj.createTime }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8" class="tlineheightb">
              <div class="span-sty">销售部门</div>
              <el-form-item><span class="addinputInfo">{{ formobj.staffDeptName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">销售专员</div>
              <el-form-item><span class="addinputInfo">{{ formobj.staffName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">销售类型</div>
              <el-form-item><span class="addinputInfo">{{ formobj.paymentMethod }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">合同编号</div>
              <el-form-item><span class="addinputInfo">{{ formobj.contractId }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">客户名称</div>
              <el-form-item><span class="addinputInfo">{{ formobj.name }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">联系电话</div>
              <el-form-item><span class="addinputInfo">{{ formobj.mobile }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="formobj.paymentMethodKey == '2'">
            <el-col :span="12">
              <div class="span-sty">贷款人</div>
              <el-form-item><span class="addinputInfo">{{ formobj.loanName }}</span></el-form-item>
            </el-col>
            <el-col :span="12">
              <div class="span-sty">联系电话</div>
              <el-form-item><span class="addinputInfo">{{ formobj.loanMobile }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">中介人1</div>
              <el-form-item><span class="addinputInfo">{{ formobj.pcDistributorName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">单车中介费1</div>
              <el-form-item><span class="addinputInfo">{{ formobj.pcDistributorPrice }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">中介服务费1合计</div>
              <el-form-item><span class="addinputInfo">{{ formobj.pcDistributorPriceAll }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">中介人2</div>
              <el-form-item><span class="addinputInfo">{{ formobj.pcDistributorNameTwo }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">单车中介费2</div>
              <el-form-item><span class="addinputInfo">{{ formobj.pcDistributorPriceTwo }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">中介服务费2合计</div>
              <el-form-item><span class="addinputInfo">{{ formobj.pcDistributorPriceTwoAll }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8" class="tlineheightb">
              <div class="span-sty">车型</div>
              <el-form-item><span class="addinputInfo">{{ formobj.modelName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">成交价</div>
              <el-form-item><span class="addinputInfo">{{ formobj.price }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">实际购车人</div>
              <el-form-item><span class="addinputInfo">{{ formobj.type }}</span></el-form-item>
            </el-col>
          </el-row>
          <!--Start 预订车辆信息-->
          <div class="title">
            <div>车辆信息</div>
          </div>
          <el-table :key="tableKey" v-loading="listLoading" ref="multipleTable" :data="formobj.carList" border style="width: 100%" :index="index">
            <el-table-column fixed align="center" label="序号" type="index" :index="index + 1" width="60"/>
            <el-table-column label="车架号" align="center" width="100">
              <template slot-scope="scope">
                <span>{{ scope.row.realBuyer.vin }}</span>
              </template>
            </el-table-column>
            <el-table-column label="挂车/上装" align="center" width="100">
              <template slot-scope="scope">
                <span v-show="scope.row.showHandCarBtn" class="bluezi" @click="handleLook(scope.row)">查看</span>
              </template>
            </el-table-column>
            <el-table-column label="实际购车人" align="center" width="120">
              <template slot-scope="scope">
                <span v-if="formobj.typeKey == '2'" class="bluezi" @click="lookUser(scope.row.realBuyer)">{{ scope.row.realBuyer.name }}</span>
                <span v-else>{{ scope.row.realBuyer.name }}</span>
              </template>
            </el-table-column>
            <el-table-column label="实际购车人联系电话" align="center" width="170">
              <template slot-scope="scope">
                <span>{{ scope.row.realBuyer.mobile }}</span>
              </template>
            </el-table-column>
            <el-table-column label="赠送套餐" align="center" width="100">
              <template slot-scope="scope">
                <span>{{ price(scope.row) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="销售折让" align="center" width="120">
              <template slot-scope="scope">
                <span>{{ scope.row.discount }}</span>
              </template>
            </el-table-column>
            <el-table-column label="备注" align="center" min-width="200">
              <template slot-scope="scope">
                <span>{{ remarks(scope.row) }}</span>
              </template>
            </el-table-column>
            <el-table-column v-if="formobj.paymentMethodKey == '2'" label="放款资料是否齐全" align="center" width="170">
              <template slot-scope="scope">
                <span>{{ scope.row.realBuyer.zlStateValue }}</span>
              </template>
            </el-table-column>
            <el-table-column v-if="formobj.paymentMethodKey == '2'" label="资方放款审核结果" align="center" width="170">
              <template slot-scope="scope">
                <span>{{ scope.row.realBuyer.bankStateValue }}</span>
              </template>
            </el-table-column>
          </el-table>
          <el-row>
            <el-col :span="24">
              <div class="span-sty">备注</div>
              <el-form-item><span class="addinputInfo">{{ formobj.remarks }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty" style="display: flex;flex-direction: row;justify-content: flex-end;align-items: center"><span>责任人担保承诺书</span></div>
              <el-form-item>
                <el-image style="width: 178px;height: 178px;padding-left: 5px" class="addinputInfo" v-for="(item,index) in formobj.promiseList" :key="index" :src="item" :preview-src-list="formobj.promiseList"></el-image>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty" style="display: flex;flex-direction: row;justify-content: flex-end;align-items: center"><span>担保承诺书</span></div>
              <el-form-item>
                <el-image style="width: 178px;height: 178px;padding-left: 5px" class="addinputInfo" v-for="(item,index) in formobj.danFileList" :key="index" :src="item" :preview-src-list="formobj.danFileList"></el-image>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="span-sty" style="display: flex;flex-direction: row;justify-content: flex-end;align-items: center"><span>车辆登记合同</span></div>
              <el-form-item>
                <el-image style="width: 178px;height: 178px;padding-left: 5px" class="addinputInfo" v-for="(item,index) in formobj.carFileList" :key="index" :src="item" :preview-src-list="formobj.carFileList"></el-image>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <user-info-look v-show="viewState == 2" ref="divUserInfo" @doback="resetState"/>
    <guacheInfo v-show="viewState == 3" ref="divCheInfo" @doback="resetState"/>
    <shangzhuangInfo v-show="viewState == 4" ref="divZhuangInfo" @doback="resetState"/>
    <!-- 选择待办人 的弹出框-->
    <el-dialog title="填写审批意见" :visible.sync="nodeDialogVisible" width="80%">
      <el-form class="formadd" >
        <el-row v-show="countersignLink" style="border-top: 1px solid #e0e3eb">
          <el-col :span="4" class="tleftb">
            <span><span class="icon">*</span>加签人员:</span>
          </el-col>
          <el-col :span="20">
            <el-form-item>
              <el-select v-model="countersign.assignee" placeholder="请选择" filterable>
                <el-option v-for="item in options" :key="item.userSid" :label="item.staffName" :value="item.userSid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-show="currentLink" style="border-top: 1px solid #e0e3eb">
          <el-col :span="4" class="tleftb">
            <span>当前环节:</span>
          </el-col>
          <el-col :span="20">
            <el-form-item><span>{{ current.taskName }}->{{ nextNode.name }}</span></el-form-item>
          </el-col>
        </el-row>
        <el-row :class="{rowClass:!currentLink}">
          <el-col :span="4" class="tleftb">
            <span>意见:</span>
          </el-col>
          <el-col :span="20">
            <el-form-item><el-input size="small" v-model="dialogList.comment" placeholder="审批意见" class="addinputw" type="textarea" :autosize="{ minRows: 1, maxRows: 10}" clearable ></el-input></el-form-item>
          </el-col>
        </el-row>
        <div style="text-align:center;margin-top: 20px;">
          <el-button type="primary" size="mini" @click="reject">确 定</el-button>
          <el-button type="info " size="mini" @click="nodeDialogVisible = false">取 消</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getRetrievalApplyInfoBySid, trailerList, complete, breakProcess, reject, delegate, getPreviousNodesForReject, getNextNodesForSubmit } from '@/api/tesheshenpi/debtrelease'
import userInfoLook from '../chukuguanliFlow/chukuguanli/relation/userInfo'
import guacheInfo from '../chukuguanliFlow/chukuguanli/relation/guacheInfo'
import shangzhuangInfo from '../chukuguanliFlow/chukuguanli/relation/shangzhuangInfo'
import { selectStaffListss } from '@/api/dictcommons/dictcommons'

export default {
  name: 'DebtReleaseDaiBan',
  components: {
    userInfoLook,
    guacheInfo,
    shangzhuangInfo
  },
  data() {
    return {
      tableKey: 0,
      index: 0,
      // --按钮菜单-------
      viewState: 1,
      listLoading: false,
      formobj: {
        carFileList: [],
        carList: [],
        configSid: '',
        contractId: '',
        createTime: '',
        customerSid: '',
        danFileList: [],
        loanMobile: '',
        loanName: '',
        loanSid: '',
        mobile: '',
        modelName: '',
        modelSid: '',
        name: '',
        orgPath: '',
        paymentMethod: '',
        paymentMethodKey: '',
        pcApplyDept: '',
        pcApplyDeptSid: '',
        pcApplyName: '',
        pcApplySid: '',
        pcDistributorName: '',
        pcDistributorNameTwo: '',
        pcDistributorPrice: '',
        pcDistributorPriceAll: '',
        pcDistributorPriceTwo: '',
        pcDistributorPriceTwoAll: '',
        price: '',
        procInstId: '',
        remarks: '',
        sid: '',
        staffDeptName: '',
        staffDeptSid: '',
        staffName: '',
        pcStaffSid: '',
        promiseList: [],
        taskId: '',
        type: '',
        typeKey: '',
        userSid: '',
        discountLeave: []
      },
      rules: {},
      options: [],
      operation: '', // 点击操作按钮
      dialogList: {
        comment: ''
      },
      startTask: true,
      current: {
        taskDefKey: '',
        taskName: '' // 当前环节名称
      },
      nextNode: {}, // 下一环节
      nodeDialogVisible: false,
      currentLink: true,
      countersignLink: false,
      // 环节所需参数
      linkByParameter: {
        businessSid: '',
        comment: '',
        instanceId: '',
        taskId: '',
        orgSidPath: '',
        taskDefKey: '',
        userSid: ''
      },
      // 加签按钮所需参数
      countersign: {
        taskId: '',
        assignee: '',
        userSid: '',
        instanceId: '',
        views: ''
      }
    }
  },
  created() {
    console.log('url:' + window.location.href)
    var one = window.location.href.indexOf('&data') + 6
    const data = window.location.href.substr(one) // url解码unescape()已从web中移除,尽量不使用
    const obj = JSON.parse(decodeURIComponent(data))
    console.log('iframe页面获取的obj:', obj)
    // 点击（同意、终止、驳回、驳回）操作时所需的参数
    this.linkByParameter.businessSid = obj.businessSid
    this.linkByParameter.instanceId = obj.instanceId
    this.linkByParameter.taskId = obj.taskId
    this.linkByParameter.taskDefKey = obj.taskDefKey
    // this.linkByParameter.orgSidPath = window.sessionStorage.getItem('orgSidPath')
    this.linkByParameter.userSid = window.sessionStorage.getItem('userSid')
    this.current.taskDefKey = obj.taskDefKey
    this.current.taskName = obj.taskName
    // 加签参数
    this.countersign.taskId = obj.taskId
    this.countersign.userSid = window.sessionStorage.getItem('userSid')
    this.countersign.instanceId = obj.instanceId
    // 加载表单数据
    this.showInfo(obj.businessSid)
  },
  mounted() {
    window.parent.postMessage({
      cmd: 'returnHeight',
      params: {
        // 告诉父级页面，子页面的弹框高度。
        code: 2,
        data: 500 + 'px'
      }
    }, '*')
  },
  methods: {
    // ------------ 页面初始化 ------------
    resetState() {
      this.viewState = 1
    },
    showInfo(sid) {
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      getRetrievalApplyInfoBySid({ sid: sid }).then((response) => {
        if (response.success) {
          this.formobj = response.data
        }
      })
      selectStaffListss().then((res) => {
        if (res.success) {
          this.options = res.data
        }
      })
    },
    // 车辆信息表--赠送套餐
    price(row) {
      var aa = 0
      for (var i = 0; i < row.discountUse.length; i++) {
        aa = parseInt(aa) + (parseInt(row.discountUse[i].discountPrice) * parseInt(row.discountUse[i].discountNum))
      }
      return aa
    },
    // 车辆信息表--备注内容拼接（优惠套餐说明 + 赠送数量）
    remarks(row) {
      var aa = ''
      for (var i = 0; i < row.discountUse.length; i++) {
        if (row.discountUse[i].discountNum !== 0) {
          aa = aa + row.discountUse[i].discountName + row.discountUse[i].discountInfo + 'X' + row.discountUse[i].discountNum + ','
        }
      }
      if (aa !== '') {
        aa = aa.substring(0, aa.lastIndexOf(','))
      }
      return aa
    },
    handleLook(row) {
      trailerList({ contractNo: this.formobj.contractId, vinSid: row.vinSid }).then((resp) => {
        if (resp.success) {
          if (resp.data.list.length > 0) {
            this.viewState = 3
            this.$refs['divCheInfo'].showInfo(resp.data)
          } else {
            this.viewState = 4
            this.$refs['divZhuangInfo'].showInfo(resp.data)
          }
        }
      })
    },
    // 查看用户人信息
    lookUser(row) {
      this.viewState = 2
      this.$refs['divUserInfo'].showInfo(this.formobj.contractId, row)
    },
    // ------------ 功能实现 ------------
    // 加签
    openCountersign(val) {
      this.operation = val
      this.currentLink = true
      this.countersignLink = true
      this.dialogList.comment = ''
      getNextNodesForSubmit({ taskDefKey: this.current.taskDefKey, businessSid: this.linkByParameter.businessSid }).then((resp) => {
        if (resp.success) {
          var arr = resp.data
          this.nextNode = arr[0]
          this.nodeDialogVisible = true
        }
        this.submitdisabled = false
      })
    },
    // 同意
    openAgree(val) {
      this.operation = val
      this.currentLink = true
      this.countersignLink = false
      this.dialogList.comment = '同意'
      getNextNodesForSubmit({ taskDefKey: this.current.taskDefKey, businessSid: this.linkByParameter.businessSid }).then((resp) => {
        if (resp.success) {
          var arr = resp.data
          this.nextNode = arr[0]
          this.nodeDialogVisible = true
        }
        this.submitdisabled = false
      })
    },
    // 驳回
    openReject(val) {
      this.operation = val
      this.currentLink = true
      this.countersignLink = false
      this.dialogList.comment = ''
      getPreviousNodesForReject({ taskDefKey: this.current.taskDefKey, businessSid: this.linkByParameter.businessSid }).then((resp) => {
        if (resp.success) {
          var arr = resp.data
          this.nextNode = arr[0]
          this.nodeDialogVisible = true
        }
        this.submitdisabled = false
      })
    },
    // 终止
    openStop(val) {
      this.operation = val
      this.currentLink = false
      this.countersignLink = false
      this.dialogList.comment = ''
      this.nodeDialogVisible = true
    },
    reject() {
      if (this.operation === '同意') {
        this.handleAgree()
      } else if (this.operation === '驳回') {
        if (this.dialogList.comment === '') {
          this.$message({ showClose: true, type: 'error', message: '请填写审批意见' })
        } else {
          this.handleReject()
        }
      } else if (this.operation === '终止') {
        if (this.dialogList.comment === '') {
          this.$message({ showClose: true, type: 'error', message: '请填写审批意见' })
        } else {
          this.handleStop()
        }
      } else if (this.operation === '加签') {
        this.handleCountersign()
      }
    },
    /** 加签 */
    handleCountersign() {
      if (this.countersign.assignee === '') {
        this.$message({ showClose: true, type: 'error', message: '请选择加签人员' })
        return
      }
      if (this.dialogList.comment === '') {
        this.$message({ showClose: true, type: 'error', message: '请填写审批意见' })
        return
      }
      this.countersign.views = this.dialogList.comment
      delegate(this.countersign).then((response) => {
        if (response.success) {
          this.$notify({
            title: '提示',
            message: '执行成功',
            type: 'success',
            duration: 2000
          })
          this.nodeDialogVisible = false
          // 子页面向父级页面传递值
          window.parent.postMessage({
            cmd: 'returnHeight',
            params: {
              // 操作成功，告诉父级页面关闭弹框
              code: 1
            }
          }, '*')
        }
      })
    },
    /** 同意任务 */
    handleAgree() {
      this.linkByParameter.comment = this.dialogList.comment
      complete(this.linkByParameter).then((response) => {
        if (response.success) {
          this.$notify({
            title: '提示',
            message: '执行成功',
            type: 'success',
            duration: 2000
          })
          this.nodeDialogVisible = false
          // 子页面向父级页面传递值
          window.parent.postMessage({
            cmd: 'returnHeight',
            params: {
              // 操作成功，告诉父级页面关闭弹框
              code: 1
            }
          }, '*')
        }
      })
    },
    /** 驳回任务 */
    handleReject() {
      this.linkByParameter.comment = this.dialogList.comment
      reject(this.linkByParameter).then((response) => {
        if (response.success) {
          this.$notify({
            title: '提示',
            message: '执行成功',
            type: 'success',
            duration: 2000
          })
          this.nodeDialogVisible = false
          // 子页面向父级页面传递值
          window.parent.postMessage({
            cmd: 'returnHeight',
            params: {
              // 操作成功，告诉父级页面关闭弹框
              code: 1
            }
          }, '*')
        }
      })
    },
    /** 终止任务 */
    handleStop() {
      this.linkByParameter.comment = this.dialogList.comment
      breakProcess(this.linkByParameter).then((response) => {
        if (response.success) {
          this.$notify({
            title: '提示',
            message: '执行成功',
            type: 'success',
            duration: 2000
          })
          this.nodeDialogVisible = false
          // 子页面向父级页面传递值
          window.parent.postMessage({
            cmd: 'returnHeight',
            params: {
              // 操作成功，告诉父级页面关闭弹框
              code: 1
            }
          }, '*')
        }
      })
    }
  }
}
</script>
<style scoped>
.span-sty {
  width: 120px !important;
}
.addinputInfo {
  margin-left: 110px !important;
}
.tlineheightb {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
}
/deep/ .tlineheightb .el-form-item .el-form-item__content {
  line-height: 15px !important;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
}
/*表格列设置fixed后固定列出现下边框的设置*/
/deep/ .el-table__fixed {
  height: 100% !important;
}
/*表格列设置fixed后固定列出现下边框的设置*/
/deep/ .el-table__fixed-right {
  height: 100% !important;
}
.titwu {
  font-size: 28px;
  text-align: center;
  padding: 30px 0 20px 0;
}
.rowClass{
  border-top: 1px solid #E0E3EB;
}
</style>
