<template>
  <div class="app-container">
    <!--标题按钮部分开始-->
    <div v-show="viewState == 1">
      <div class="tab-header webtop">
        <!--标题-->
        <div>{{ viewTitle }}</div>
        <!--start 新增修改按钮-->
        <div>
          <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>
        <!--end 添加修改按钮-->
      </div>
      <!--按钮部分结束-->
      <div class="">
        <div class="titwu">欠款开票申请单</div>
        <el-form ref="form_obj" :model="formobj" :rules="rules" class="formaddcopy02">
          <el-row style="border-top: 1px solid #E0E3EB">
            <el-col :span="8">
              <div class="span-sty">发起人</div>
              <el-form-item><span class="addinputInfo">{{ formobj.createByName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">发起部门</div>
              <el-form-item><span class="addinputInfo">{{ formobj.deptName }}</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.staffName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">销售部门</div>
              <el-form-item><span class="addinputInfo">{{ formobj.saleDeptName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">销售类型</div>
              <el-form-item><span class="addinputInfo">{{ formobj.saleTypeValue }}</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.contractNo }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">开票单位</div>
              <el-form-item><span class="addinputInfo">{{ formobj.openTickUnitName }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">客户名称</div>
              <el-form-item><span class="addinputInfo">{{ formobj.customerName }}</span></el-form-item>
            </el-col>
          </el-row>
          <div class="title titleOne">
            <div>主车开票信息</div>
            <div><el-button type="primary" size="mini" class="btntopblueline" @click="handleLookByDingDan()">查看销售订单</el-button></div>
          </div>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">单台开票金额</div>
              <el-form-item><span class="addinputInfo">{{ formobj.oneBillMoney }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">是否领取发票原件</div>
              <el-form-item><span class="addinputInfo">{{ formobj.isLQOrigInvoice }}</span></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">开票名称默认</div>
              <el-form-item><span class="addinputInfo">{{ formobj.openTickName }}</span></el-form-item>
            </el-col>
          </el-row>
          <el-table :key="tableKey" :data="formobj.finBillVehicles" :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.vinNo }}</span>
              </template>
            </el-table-column>
            <el-table-column label="合格证状态" align="center" width="110">
              <template slot-scope="scope">
                <span class="bluezi" @click="handleLook(scope.row.certificatePhoto)">{{ scope.row.certificateStateValue }}</span>
              </template>
            </el-table-column>
            <el-table-column label="开票类型" align="center" width="150">
              <template slot-scope="scope">
                <span>{{ scope.row.billTypeValue }}</span>
              </template>
            </el-table-column>
            <el-table-column label="开票名称" align="center" width="160">
              <template slot-scope="scope">
                <span>{{ scope.row.openTickName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="开票信息" align="center" min-width="200">
              <template slot-scope="scope">
                <span>{{ scope.row.openTickRemarks }}</span>
              </template>
            </el-table-column>
            <el-table-column label="开票金额" align="center" width="120">
              <template>
                <span>{{ formobj.oneBillMoney }}</span>
              </template>
            </el-table-column>
            <el-table-column label="款、票情况" align="center" width="200">
              <template slot-scope="scope">
                <span class="bluezi" @click="lookPayment(scope.row)">查看</span>
              </template>
            </el-table-column>
          </el-table>
          <el-row>
            <el-col :span="12">
              <div class="span-sty">欠款金额合计</div>
              <el-form-item><span class="addinputInfo">{{ formobj.arrearsMoney }}</span></el-form-item>
            </el-col>
            <el-col :span="12">
              <div class="span-sty">承诺回款日期</div>
              <el-form-item><span class="addinputInfo">{{ formobj.returnedMoneyDate }}</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.arrearsOpenYYValue }}</span></el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item><span class="addinputInfo" style="margin-left: 0px !important;">{{ formobj.remarks }}</span></el-form-item>
            </el-col>
          </el-row>
          <div v-if="formobj.finBillTrailers !== null">
            <div class="title">挂车开票信息(挂车的开票类型、开票名称与主车相同)</div>
            <el-table :key="billingKey" :data="formobj.finBillTrailers" :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.gcVinNo }}</span>
                </template>
              </el-table-column>
              <el-table-column label="主车车架号" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.vinNo }}</span>
                </template>
              </el-table-column>
              <el-table-column label="开票名称" align="center" width="160">
                <template slot-scope="scope">
                  <span>{{ scope.row.openTickName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="开票信息" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.invoiceInfo }}</span>
                </template>
              </el-table-column>
              <el-table-column label="开票金额" align="center" width="120">
                <template slot-scope="scope">
                  <span>{{ scope.row.oneBillMoney }}</span>
                </template>
              </el-table-column>
              <el-table-column label="合格证" align="center" width="100">
                <template slot-scope="scope">
                  <span class="bluezi" @click="handleLook(scope.row.hgzImageUrl)">查看</span>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="title">证件(营业执照或身份证)</div>
          <el-row>
            <el-col :span="24">
              <el-image style="width: 178px;height: 178px;padding-left: 5px" v-for="(item,index) in image_list1" :key="index" :src="item" :preview-src-list="image_list1"></el-image>
            </el-col>
          </el-row>
          <div class="title">客户开票申请书</div>
          <el-row>
            <el-col :span="24">
              <el-image style="width: 178px;height: 178px;padding-left: 5px" v-for="(item,index) in image_list2" :key="index" :src="item" :preview-src-list="image_list2"></el-image>
            </el-col>
          </el-row>
          <div class="title">款项确认书(贷款业务上传)</div>
          <el-row>
            <el-col :span="24">
              <el-form-item>
                <el-image style="width: 178px;height: 178px;padding-left: 5px" v-for="(item,index) in image_list3" :key="index" :src="item" :preview-src-list="image_list3"></el-image>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="title">车辆登记合同(开票名称与客户名称不一致时上传)</div>
          <el-row>
            <el-col :span="24">
              <el-form-item>
                <el-image style="width: 178px;height: 178px;padding-left: 5px" v-for="(item,index) in image_list4" :key="index" :src="item" :preview-src-list="image_list4"></el-image>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="title">欠款领票相关附件</div>
          <el-row>
            <el-col :span="24">
              <el-form-item>
                <el-image style="width: 178px;height: 178px;padding-left: 5px" v-for="(item,index) in image_list5" :key="index" :src="item" :preview-src-list="image_list5"></el-image>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>

    <el-dialog :visible.sync="dialogVisible" :append-to-body="true">
      <el-image style="width: 178px;height: 178px;padding-left: 5px" v-for="(item,index) in srcList" :key="index" :src="item" :preview-src-list="srcList"></el-image>
    </el-dialog>

    <!-- 选择待办人 的弹出框-->
    <el-dialog title="填写审批意见" :visible.sync="nodeDialogVisible" width="80%">
      <el-form class="formadd" >
        <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>

    <!-- 查看销售订单详情 -->
    <xiaoshoudingdanInfo v-show="viewState == 2" ref="divInfo" @doback="resetState"/>
    <paymentticket v-show="viewState == 3" ref="divPay" @doback="resetState"/>
  </div>
</template>

<script>
import { fetchDetailsBySid, completeByDivision, complete, breakProcess, reject, getNextNodesForSubmit, getPreviousNodesForReject } from '@/api/anruifinmanagement/arrearsbilling'
import xiaoshoudingdanInfo from '../../workFlow/kaipiaoguanliFlow/relation/xiaoshoudingdanInfo'
import paymentticket from '../../workFlow/kaipiaoguanliFlow/relation/paymentticket'

export default {
  name: 'arrearsbillingByDivisionDaiBanInfo',
  components: {
    xiaoshoudingdanInfo,
    paymentticket
  },
  data() {
    return {
      viewTitle: '',
      viewState: 1,
      tableKey: 0,
      billingKey: 1,
      index: 0,
      dialogVisible: false,
      srcList: [],
      image_list1: [],
      image_list2: [],
      image_list3: [],
      image_list4: [],
      image_list5: [],
      formobj: {
        applyDate: '',
        arrearsMoney: '',
        arrearsOpenYYKey: '',
        arrearsOpenYYValue: '',
        billNo: '',
        billStateKey: '',
        billStateValue: '',
        cldjhtApplicationAppendxs: [],
        contractNo: '',
        contractSid: '',
        createByName: '',
        customerName: '',
        customerSid: '',
        deptName: '',
        deptSid: '',
        finBillTrailers: [],
        finBillVehicles: [],
        isDelayBill: '',
        isHighAndLowOpen: '',
        isLQOrigInvoice: '',
        khkpsqsApplicationAppendxs: [],
        kpxxqrsApplicationAppendxs: [],
        modelName: '',
        modelSid: '',
        nodeSid: '',
        nodeState: '',
        oneBillMoney: '',
        openTickName: '',
        openTickSid: '',
        openTickTypeKey: '',
        openTickTypeValue: '',
        openTickUnitName: '',
        openTickUnitSid: '',
        orgSidPath: '',
        procDefId: '',
        procInstSid: '',
        remarks: '',
        returnedMoneyDate: '',
        saleDeptName: '',
        saleDeptSid: '',
        saleTypeKey: '',
        saleTypeValue: '',
        sid: '',
        staffName: '',
        staffSid: '',
        totalBillMoney: '',
        useOrgSid: '',
        createBySid: '',
        zjApplicationAppendxs: [],
        arrearsCollectionAppendxs: []
      },
      rules: {},
      operation: '', // 点击操作按钮
      dialogList: {
        comment: ''
      },
      startTask: true,
      current: {
        taskDefKey: '',
        taskName: '' // 当前环节名称
      },
      nextNode: {}, // 下一环节
      nodeDialogVisible: false,
      currentLink: true,
      linkByParameter: {
        businessSid: '',
        comment: '',
        instanceId: '',
        taskId: '',
        orgSidPath: '',
        taskDefKey: '',
        userSid: ''
      }
    }
  },
  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.showInfo(obj.businessSid)
  },
  mounted() {
    window.parent.postMessage({
      cmd: 'returnHeight',
      params: {
        // 告诉父级页面，子页面的弹框高度。
        code: 2,
        data: 550 + 'px'
      }
    }, '*')
  },
  methods: {
    showInfo(sid) {
      this.viewTitle = '欠款开票详情'
      fetchDetailsBySid(sid).then((res) => {
        if (res.success) {
          this.formobj = res.data
          if (this.formobj.zjApplicationAppendxs.length > 0) {
            this.formobj.zjApplicationAppendxs.forEach((e) => {
              this.image_list1.push(e.fileUrl)
            })
          }
          if (this.formobj.khkpsqsApplicationAppendxs.length > 0) {
            this.formobj.khkpsqsApplicationAppendxs.forEach((e) => {
              this.image_list2.push(e.fileUrl)
            })
          }
          if (this.formobj.kpxxqrsApplicationAppendxs.length > 0) {
            this.formobj.kpxxqrsApplicationAppendxs.forEach((e) => {
              this.image_list3.push(e.fileUrl)
            })
          }
          if (this.formobj.cldjhtApplicationAppendxs.length > 0) {
            this.formobj.cldjhtApplicationAppendxs.forEach((e) => {
              this.image_list4.push(e.fileUrl)
            })
          }
          if (this.formobj.arrearsCollectionAppendxs.length > 0) {
            this.formobj.arrearsCollectionAppendxs.forEach((e) => {
              this.image_list5.push(e.fileUrl)
            })
          }
        }
      })
    },
    handleLook(row) {
      this.srcList = []
      this.dialogVisible = true
      this.srcList = row
    },
    handleLookByDingDan() {
      this.viewState = 2
      this.$refs['divInfo'].showInfo(this.formobj.busSalesOrderSid)
    },
    // 查看款、票情况
    lookPayment(row) {
      this.viewState = 3
      const params = {
        vinSid: row.vehSid
      }
      this.$refs['divPay'].showInfo(this.formobj.contractNo, params)
    },
    resetState() {
      this.viewState = 1
    },
    // 同意
    openAgree(val) {
      this.operation = val
      this.currentLink = 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
      })
    },
    // 驳回
    openReject(val) {
      this.operation = val
      this.currentLink = true
      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.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()
        }
      }
    },
    /** 同意任务 */
    handleAgree() {
      this.linkByParameter.comment = this.dialogList.comment
      if (this.formobj.isLQOrigInvoice === '否') {
        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
              }
            }, '*')
          }
        })
      } else {
        completeByDivision(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>
.titwu {
  font-size: 28px;
  text-align: center;
  padding: 30px 0 20px 0;
}
.titleOne {
  padding: 7px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
.span-sty {
  width: 100px !important;
}
.addinputInfo {
  margin-left: 90px !important;
}
</style>
