<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <div class="tab-header webtop">
        <div>{{ viewTitle }}</div>
        <div>
          <el-button type="primary" size="small" @click="handleDownload()">下载</el-button>
          <el-button type="info" size="small" @click="handleReturn()">关闭</el-button>
        </div>
      </div>
      <div class="listconadd">
        <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.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="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">附件</div>
              <el-form-item>
                <el-image class="addinputInfo" style="width: 150px;height: 150px" v-for="(item, index) in formobj.files" :key="index" :src="item" :preview-src-list="formobj.files"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <span style="color: red;font-weight: bold">注：因间还未及时在资方认款而造成同一期实还金额合计为负数或零时不在此申请中显示</span>
            </el-col>
          </el-row>
          <div class="title">
            <div>当月还款记录</div>
          </div>
          <el-table :key="tableKey" :data="formobj.records" :index="index" border style="width: 100%">
            <el-table-column fixed width="80" label="序号" type="index" :index="index + 1" align="center"/>
            <el-table-column prop="loanContractNo" label="贷款合同编号" align="center" width="160" />
            <el-table-column prop="vinNo" label="车架号" align="center" width="120"/>
            <el-table-column prop="bankName" label="资方" align="center" width="100" />
            <el-table-column prop="bankContractNo" label="资方合同编号" align="center" width="160" />
            <el-table-column prop="customer" label="客户名称" align="center" width="100" />
            <el-table-column prop="borrowerName" label="贷款人" align="center" />
            <el-table-column prop="period" label="期数" align="center" width="100" />
            <el-table-column prop="actualMoney" label="实还金额" align="center" width="140"/>
            <el-table-column label="还款状态" align="center" width="140">
              <template slot-scope="scope">
                <span>{{ scope.row.payState == '0' ? '正常还款' : '逾期还款'}}</span>
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import req from '@/api/deduct/deduct'
import { getStorage } from '@/utils/auth'

export default {
  name: 'DeductInfo',
  data() {
    return {
      viewTitle: '',
      viewState: 1,
      submitdisabled: false,
      tableKey: 0,
      index: 0,
      formobj: {
        sid: '',
        userSid: '',
        dept: '',
        deptSid: '',
        applyName: '',
        applyDate: '',
        remarks: '',
        orgPath: '',
        taskId: '',
        instanceId: '',
        files: [],
        records: []
      },
      rules: {}
    }
  },
  methods: {
    showInfo(row) {
      this.viewTitle = '划扣申请'
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      req.fetchBySid({ sid: row.sid }).then((res) => {
        if (res.success) {
          this.formobj = res.data
          if (this.formobj.files.length > 0) {
            const aa = []
            this.formobj.files.forEach((e) => {
              aa.push(e.url)
            })
            this.formobj.files = aa
          }
        }
      })
    },
    handleDownload() {
      req.buckleCreatePdf({ sid: this.formobj.sid }).then((res) => {
        if (res.success) {
          var xhr = new XMLHttpRequest()
          xhr.open('GET', process.env.VUE_APP_BASE_API + '/base/file/download?filePath=' + res.data + '&outFileName=' + '月还划扣', true)
          xhr.setRequestHeader('token', getStorage())
          xhr.responseType = 'blob'
          xhr.onload = function(e) {
            // 如果请求执行成功
            var blob = this.response
            var filename = '月还划扣.pdf'
            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()
        }
      }).catch(() => {})
    },
    handleReturn(isreload) {
      if (isreload === 'true') this.$emit('reloadlist')
      this.formobj = {
        sid: '',
        userSid: '',
        dept: '',
        deptSid: '',
        applyName: '',
        applyDate: '',
        remarks: '',
        orgPath: '',
        taskId: '',
        instanceId: '',
        files: [],
        records: []
      }
      this.$emit('doback')
    }
  }
}
</script>

<style scoped>
.addinputInfo {
  margin-left: 60px !important;
}
</style>
