<template>
  <div class="app-container">
    <div>
      <div class="tab-header webtop">
        <div>{{ viewTitle }}</div>
        <div>
          <el-button type="primary" size="small" @click="openTurnDo('转办')">转 办</el-button>
          <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>
    <div class="">
      <el-form ref="dataForm" :model="formobj" class="formaddcopy02" :rules="rules">
        <el-row style="border-top: 1px solid #DFE6EC">
          <el-col :span="8">
            <div class="span-sty">申请部门</div>
            <el-form-item><span class="addinputInfo">{{ formobj.dept }}</span></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">申请人</div>
            <el-form-item><span class="addinputInfo">{{ formobj.applyName }}</span></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">申请日期</div>
            <el-form-item><span class="addinputInfo">{{ formobj.applyDate }}</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.salesDept }}</span></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">销售专员</div>
            <el-form-item><span class="addinputInfo">{{ formobj.salesman }}</span></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">销售部经理</div>
            <el-form-item><span class="addinputInfo">{{ formobj.manage }}</span></el-form-item>
          </el-col>
        </el-row>
        <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>
        <div class="title">客户基本信息</div>
        <el-row>
          <el-col :span="16">
            <div class="span-sty">客户名称</div>
            <el-form-item><span class="addinputInfo">{{ formobj.customer }}</span></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">客户类型</div>
            <el-form-item><span class="addinputInfo">{{ formobj.customerType }}</span></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <div class="span-sty">联系电话</div>
            <el-form-item><span class="addinputInfo">{{ formobj.clientPhone }}</span></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">证件类型</div>
            <el-form-item><span class="addinputInfo">{{ formobj.documents }}</span></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <div class="span-sty">证件号码</div>
            <el-form-item><span class="addinputInfo">{{ formobj.clientNo }}</span></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">证件有效期</div>
            <el-form-item><span class="addinputInfo">{{ formobj.documentsDate }}</span></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16" class="tlineheightb">
            <div class="span-sty">证件地址</div>
            <el-form-item><span class="addinputInfo">{{ formobj.clientAddress }}</span></el-form-item>
          </el-col>
          <el-col :span="8">
            <div v-show="formobj.customerTypeKey == '1'">
              <div class="span-sty">是否挂靠运输公司</div>
              <el-form-item><span class="addinputInfo">{{ formobj.isAnchored = '1' ? '是' : '否' }}</span></el-form-item>
            </div>
          </el-col>
        </el-row>
        <div v-if="formobj.customerTypeKey === '2'">
          <div class="title">企业人员信息</div>
          <el-table :key="tableKey" v-loading="listLoading" :data="formobj.staffs" :index="index" border style="width: 100%">
            <el-table-column fixed width="80px" label="序号" type="index" :index="index + 1" align="center"/>
            <el-table-column label="姓名" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.name }}</span>
              </template>
            </el-table-column>
            <el-table-column label="身份" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.identityValue }}</span>
              </template>
            </el-table-column>
            <el-table-column label="手机号码" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.mobile }}</span>
              </template>
            </el-table-column>
            <el-table-column label="身份证号码" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.idNumber }}</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div v-show="formobj.customerTypeKey === '1' && formobj.isAnchored === '是'">
          <div class="title">挂靠运输公司信息</div>
          <el-row>
            <el-col :span="16">
              <div class="span-sty">企业名称</div>
              <el-form-item><span class="addinputInfo">{{ formobj.businessName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">统一社会信用代码</div>
              <el-form-item><span class="addinputInfo">{{ formobj.taxpayerNo }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="16">
              <div class="span-sty">负责人</div>
              <el-form-item><span class="addinputInfo">{{ formobj.leader }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">联系电话</div>
              <el-form-item><span class="addinputInfo">{{ formobj.phone }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="16">
              <div class="span-sty">法定代表人</div>
              <el-form-item><span class="addinputInfo">{{ formobj.legal }}</span></el-form-item>
            </el-col>
            <el-col :span="8" class="tlineheightb">
              <div class="span-sty">证件地址</div>
              <el-form-item><span class="addinputInfo">{{ formobj.address }}</span></el-form-item>
            </el-col>
          </el-row>
        </div>
        <div class="title">附件清单</div>
        <el-row v-if="formobj.customerTypeKey === '2' || (formobj.customerTypeKey === '1' && formobj.isAnchored === '是')">
          <el-col :span="24">
            <div class="span-sty span-sty-special" style="display: flex;flex-direction: row;justify-content: flex-end;align-items: center">营业执照</div>
            <el-form-item><el-image class="addinputInfo-special" style="width: 150px; height: 150px" v-for="(item, index) in formobj.license" :key="index" :src="item" :preview-src-list="formobj.license"/></el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="formobj.customerTypeKey === '2' || (formobj.customerTypeKey === '1' && formobj.isAnchored === '否')">
          <el-col :span="24">
            <div class="span-sty span-sty-special" style="display: flex;flex-direction: row;justify-content: flex-end;align-items: center">
              <span v-show="formobj.customerTypeKey === '2'">法人和实际控制人身份证</span>
              <span v-show="formobj.customerTypeKey === '1' && formobj.isAnchored === '否'">本人身份证</span>
            </div>
            <el-form-item><el-image class="addinputInfo-special" style="width: 150px; height: 150px" v-for="(item, index) in formobj.idCard" :key="index" :src="item" :preview-src-list="formobj.idCard"/></el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="formobj.customerTypeKey === '2'">
          <el-col :span="24">
            <div class="span-sty span-sty-special" style="display: flex;flex-direction: row;justify-content: flex-end;align-items: center">法人和实际控制人签署的《信息查询授权书》</div>
            <el-form-item><el-image class="addinputInfo-special" style="width: 150px; height: 150px" v-for="(item, index) in formobj.infoLetter" :key="index" :src="item" :preview-src-list="formobj.infoLetter"/></el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="formobj.customerTypeKey === '1' && formobj.isAnchored === '是'">
          <el-col :span="24">
            <div class="span-sty span-sty-special" style="display: flex;flex-direction: row;justify-content: flex-end;align-items: center">车队挂靠协议</div>
            <el-form-item><el-image class="addinputInfo-special" style="width: 150px; height: 150px" v-for="(item, index) in formobj.agreement" :key="index" :src="item" :preview-src-list="formobj.agreement"/></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="span-sty span-sty-special" style="display: flex;flex-direction: row;justify-content: flex-end;align-items: center">其他资料</div>
            <el-form-item><el-image class="addinputInfo-special" style="width: 150px; height: 150px" v-for="(item, index) in formobj.otherInfo" :key="index" :src="item" :preview-src-list="formobj.otherInfo"/></el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 选择待办人 的弹出框-->
    <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 v-show="operation == '加签'">
              <span class="icon">*</span>加签人员:
            </span>
            <span v-show="operation == '转办'">
              <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 req from '@/api/delinquentcustomerfiling/delinquentcustomerfiling'
import { selectStaffListss } from '@/api/Common/dictcommons'

export default {
  name: 'DelinquentCustomerFilingDaiBan',
  data() {
    return {
      viewTitle: '',
      tableKey: 0,
      index: 0,
      listLoading: false,
      formobj: {
        sid: '',
        userSid: '',
        applyName: '',
        applyDate: '',
        salesmanSid: '',
        salesman: '',
        dept: '',
        deptSid: '',
        salesDept: '',
        salesDeptSid: '',
        manage: '',
        manageSid: '',
        isAnchored: '',
        customerSid: '',
        customer: '',
        customerTypeKey: '',
        customerType: '',
        clientPhone: '',
        clientNo: '',
        documents: '',
        documentsDate: '',
        clientAddress: '',
        businessName: '',
        taxpayerNo: '',
        address: '',
        phone: '',
        leader: '',
        legal: '',
        orgPath: '',
        staffs: [],
        license: [],
        idCard: [],
        infoLetter: [],
        agreement: [],
        otherInfo: [],
        taskId: '',
        instanceId: '',
        remarks: ''
      },
      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: {
    showInfo(sid) {
      this.viewTitle = '欠款客户备案详情'
      req.fetchBySid(sid).then((resp) => {
        if (resp.success) {
          this.formobj = resp.data
        }
      })
      selectStaffListss().then((res) => {
        if (res.success) {
          this.options = res.data
        }
      })
    },
    // 转办
    openTurnDo(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
      })
    },
    // 加签
    openCountersign(val) {
      this.operation = val
      this.currentLink = true
      this.countersignLink = true
      this.dialogList.comment = ''
      req.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 = '同意'
      req.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 = ''
      req.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()
      } else if (this.operation === '转办') {
        this.handleAssignTask()
      }
    },
    // 转办
    handleAssignTask() {
      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
      req.assignTask(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
            }
          }, '*')
        }
      })
    },
    /** 加签 */
    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
      req.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
      req.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
      req.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
      req.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: 170px !important;
}
.addinputInfo {
  margin-left: 160px !important;
}
.span-sty-special {
  width: 300px !important;
}
.addinputInfo-special {
  margin-left: 290px !important;
}
.tlineheightb {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
}
/deep/ .tlineheightb .el-form-item .el-form-item__content .addinputInfo {
  line-height: 15px !important;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
}
.rowClass{
  border-top: 1px solid #E0E3EB;
}
</style>
