<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="handlePrint()">打印</el-button>
          <el-button type="danger" size="small" @click="openRevoke()">撤回</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><span style="padding-left: 30px;color: red" v-show="formobj.showValueCustomer">价值客户</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>

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

<script>
import { fetchDetailsBySid, revokeProcess, createPdf } from '@/api/anruifinmanagement/arrearsbilling'
import { getStorage } from '@/utils/auth'
import salesOrder from '../publicPage/salesOrder'
import paymentticket from '../publicPage/paymentticket'

export default {
  name: 'arrearsbillingYiBanInfo',
  components: {
    salesOrder,
    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: '',
        showValueCustomer: false,
        sid: '',
        staffName: '',
        staffSid: '',
        totalBillMoney: '',
        useOrgSid: '',
        createBySid: '',
        zjApplicationAppendxs: [],
        arrearsCollectionAppendxs: []
      },
      rules: {},
      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.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)
            })
          }
        }
      })
    },
    handlePrint() {
      createPdf({ sid: this.formobj.sid, userName: window.sessionStorage.getItem('name') }).then((resp) => {
        if (resp.success) {
          var xhr = new XMLHttpRequest()
          xhr.open('GET', process.env.VUE_APP_BASE_API + '/base/file/download?filePath=' + resp.data + '&outFileName=' + '财务', true)
          xhr.setRequestHeader('token', getStorage())
          xhr.responseType = 'blob'
          xhr.onload = function(e) {
            // 如果请求执行成功
            var blob = this.response
            var filename = resp.data.substring(resp.data.lastIndexOf('/') + 1)
            var a = document.createElement('a')
            // blob.type="application/octet-stream";
            // 创键临时url对象
            var url = URL.createObjectURL(blob)
            a.href = url
            a.download = filename
            a.click()
            // 释放之前创建的URL对象
            window.URL.revokeObjectURL(url)
          }
          // 发送请求
          xhr.send()
        }
      })
    },
    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
    },
    /** 确认撤回任务 */
    openRevoke() {
      this.$confirm('是否确认执行撤回操作', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleRevoke()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消撤回'
        })
      })
    },
    /** 撤回任务 */
    handleRevoke() {
      revokeProcess(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>
