<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <button-bar view-title="收款确认管理" ref="btnbar" :btndisabled="btndisabled" @btnhandle="btnHandle"/>
      <div class="main-content">
        <div class="searchcon">
          <el-button size="small" class="searchbtn" @click="clicksearchShow">{{ searchxianshitit }}</el-button>
          <div v-show="isSearchShow" class="search">
            <el-form ref="listQueryform" :inline="true" :model="listQuery" label-width="100px" class="tab-header">
              <el-form-item label="付款方:" class="searchlist">
                <el-input v-model="listQuery.params.payerName" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="收款方式:" class="searchlist">
                <el-select v-model="listQuery.params.collectionTypeKey" placeholder="请选择" filterable clearable>
                  <el-option v-for="item in collectionType_list" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey"/>
                </el-select>
              </el-form-item>
              <el-form-item label="收款银行:" class="searchlist">
                <el-input v-model="listQuery.params.collectionBank" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="收款日期:" class="searchlist">
                <el-date-picker value-format="yyyy-MM-dd" v-model="listQuery.params.collectionStartDate" clearable style="width: 160px" type="date" placeholder="开始日期"/>
                <span style="padding: 0 8px">至</span>
                <el-date-picker value-format="yyyy-MM-dd" v-model="listQuery.params.collectionEndDate" clearable style="width: 160px" type="date" placeholder="结束日期"/>
              </el-form-item>
            </el-form>
            <div class="btn" style="text-align: center;">
              <el-button type="primary" size="small" icon="el-icon-search" @click="handleFilter">查询</el-button>
              <el-button type="primary" size="small" icon="el-icon-refresh" @click="handReset">重置</el-button>
            </div>
          </div>
        </div>
        <div>
          <div class="listtop">
            <div class="tit">收款申请列表</div>
            <pageye v-show="list.length > 0" :total="listQuery.total" :page.sync="listQuery.current" :limit.sync="listQuery.size" class="pagination" @pagination="getList"/>
          </div>
          <div class="">
            <el-table :key="tableKey" v-loading="listLoading" :data="list" border style="width: 100%">
              <el-table-column fixed width="60" label="序号" type="index" :index="indexMethod" align="center"/>
              <el-table-column fixed label="操作" align="center" width="180">
                <template slot-scope="scope">
                  <el-button v-show="scope.row.auditState == 3" type="primary" size="mini" @click="handleDownLoad(scope.row)">下载</el-button>
                  <el-button type="danger" size="mini" v-show="scope.row.recallAtn" @click="toRevocation(scope.row)">撤回</el-button>
                  <el-button type="primary" size="mini" @click="handLook(scope.row)">查看</el-button>
                </template>
              </el-table-column>
              <el-table-column label="审核状态" align="center" width="90">
                <template slot-scope="scope">
                  <span>{{ scope.row.auditState == 1 ? '待审核' : scope.row.auditState == 2 ? '已驳回' : scope.row.auditState == 3 ? '已审核' : scope.row.auditState == 4 ? '已撤回' : '' }}</span>
                </template>
              </el-table-column>
              <el-table-column label="下载次数" align="center" width="90">
                <template slot-scope="scope">
                  <span>{{ scope.row.downNum }}</span>
                </template>
              </el-table-column>
              <el-table-column label="申请部门" align="center" width="90">
                <template slot-scope="scope">
                  <span>{{ scope.row.createDept }}</span>
                </template>
              </el-table-column>
              <el-table-column label="申请人" align="center" width="80">
                <template slot-scope="scope">
                  <span>{{ scope.row.createByName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="申请日期" align="center" width="100">
                <template slot-scope="scope">
                  <span>{{ scope.row.createDate }}</span>
                </template>
              </el-table-column>
              <el-table-column label="收款银行账号" align="center" width="140">
                <template slot-scope="scope">
                  <span>{{ scope.row.collectionBankNum }}</span>
                </template>
              </el-table-column>
              <el-table-column label="收款银行名称" align="center" width="130">
                <template slot-scope="scope">
                  <span>{{ scope.row.receivingName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="收款银行" align="center" width="130">
                <template slot-scope="scope">
                  <span>{{ scope.row.collectionBank }}</span>
                </template>
              </el-table-column>
              <el-table-column label="收款金额" align="center" width="150">
                <template slot-scope="scope">
                  <span>{{ scope.row.collectionMoney }}</span>
                </template>
              </el-table-column>
              <el-table-column label="收款日期" align="center" width="100">
                <template slot-scope="scope">
                  <span>{{ scope.row.collectionDate }}</span>
                </template>
              </el-table-column>
              <el-table-column label="收款方式" align="center" width="90">
                <template slot-scope="scope">
                  <span>{{ scope.row.collectionTypeValue }}</span>
                </template>
              </el-table-column>
              <el-table-column label="付款方" align="center" width="90">
                <template slot-scope="scope">
                  <span>{{ scope.row.payerName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="付款账号后4位" align="center" width="130">
                <template slot-scope="scope">
                  <span>{{ scope.row.payAccount }}</span>
                </template>
              </el-table-column>
              <el-table-column label="付款银行" align="center" width="100">
                <template slot-scope="scope">
                  <span>{{ scope.row.payBank }}</span>
                </template>
              </el-table-column>
              <el-table-column label="备注" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.remarks }}</span>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="pages">
            <!-- 翻页 -->
            <pagination v-show="list.length > 0" :total="listQuery.total" :page.sync="listQuery.current" :limit.sync="listQuery.size" class="pagination" @pagination="getList"/>
          </div>
        </div>
      </div>
    </div>
    <el-dialog center :visible.sync="dialogVisible" width="40%">
      <el-form class="formadd">
        <el-row style="border-top: 1px solid #E0E3EB">
          <el-col :span="6" class="tleftb">
            <span>打印机类型</span>
          </el-col>
          <el-col :span="18">
            <el-form-item>
              <el-radio-group v-model="print">
                <el-radio label="针孔式打印机">针孔式打印机</el-radio>
                <el-radio label="激光打印机">激光打印机</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" :disabled="visibleDisabled" type="primary" @click="handleConfirm">确定</el-button>
        <el-button size="small" :disabled="visibleDisabled" @click="dialogVisible = false">取消</el-button>
      </span>
    </el-dialog>
    <acknowledgementReceiptAdd v-show="viewState == 2" ref="divAdd" @doback="resetState" @reloadlist="getList"/>
    <acknowledgementReceiptInfo v-show="viewState == 3" ref="divInfo" @doback="resetState"/>
  </div>
</template>

<script>
import { createPdf, listPage, recallConfirm } from '@/api/anruifinmanagement/paymentConfirmation.js'
import Pagination from '@/components/pagination'
import ButtonBar from '@/components/ButtonBar'
import pageye from '@/components/pagination/pageye'
import { typeValues } from '@/api/jichuxinxi/dictcommons'
import acknowledgementReceiptAdd from './acknowledgementReceiptAdd.vue'
import acknowledgementReceiptInfo from './acknowledgementReceiptInfo.vue'
import { getStorage } from '@/utils/auth'

export default {
  name: 'shoukuanquerenguanli',
  components: {
    Pagination,
    pageye,
    ButtonBar,
    acknowledgementReceiptAdd,
    acknowledgementReceiptInfo
  },
  data() {
    return {
      dialogVisible: false,
      visibleDisabled: false,
      printSid: '',
      print: '',
      btndisabled: false,
      isSearchShow: false,
      searchxianshitit: '隐藏查询条件',
      viewState: 1,
      tableKey: 0,
      list: [],
      btnList: [
        {
          type: 'primary',
          size: 'small',
          icon: 'plus',
          btnKey: 'toAdd',
          btnLabel: '新增'
        },
        {
          type: 'info',
          size: 'small',
          icon: 'cross',
          btnKey: 'doClose',
          btnLabel: '关闭'
        }
      ],
      receiptBank_list: [],
      collectionType_list: [],
      listLoading: false,
      listQuery: {
        current: 1,
        size: 10,
        params: {
          payerName: '',
          collectionStartDate: '',
          collectionEndDate: '',
          collectionBank: '',
          collectionTypeKey: '',
          subscriptionState: '',
          useOrgSid: '',
          createBySid: '',
          state: '',
          orgPath: ''
        },
        total: 0
      }
    }
  },
  created() {
    this.getList()
    this.DataDictionary()
  },
  mounted() {
    this.$refs['btnbar'].setButtonList(this.btnList)
  },
  methods: {
    DataDictionary() {
      typeValues({ type: 'receiptBank' }).then((res) => {
        if (res.success) {
          this.receiptBank_list = res.data
        }
      })
      typeValues({ type: 'customerPayType' }).then((res) => {
        if (res.success) {
          this.collectionType_list = res.data
        }
      })
    },
    // 搜索条件效果
    clicksearchShow() {
      this.isSearchShow = !this.isSearchShow
      if (this.isSearchShow) {
        this.searchxianshitit = '隐藏查询条件'
      } else {
        this.searchxianshitit = '显示查询条件'
      }
    },
    btnHandle(btnKey) {
      console.log('XXXXXXXXXXXXXXX    ' + btnKey)
      switch (btnKey) {
        case 'toAdd':
          this.toAdd()
          break
        case 'doClose':
          this.doClose()
          break
        default:
          break
      }
    },
    // ----------功能打开结束--------
    // 序号
    indexMethod(index) {
      var pagestart = (this.listQuery.current - 1) * this.listQuery.size
      var pageindex = index + 1 + pagestart
      return pageindex
    },
    // 查询列表信息
    getList() {
      this.listLoading = true
      this.listQuery.params.createBySid = window.sessionStorage.getItem('userSid')
      this.listQuery.params.orgPath = window.sessionStorage.getItem('defaultOrgPath')
      this.listQuery.params.state = 0
      listPage(this.listQuery).then((response) => {
        this.listLoading = false
        if (response.code === '200') {
          this.listQuery.total = response.data.total
          this.list = response.data.records
        }
      })
    },
    // 查询按钮
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    handReset() {
      this.listQuery = {
        current: 1,
        size: 10,
        params: {
          collectionStartDate: '',
          collectionEndDate: '',
          collectionBank: '',
          collectionTypeKey: '',
          subscriptionState: '',
          useOrgSid: '',
          createBySid: '',
          state: '',
          orgPath: ''
        },
        total: 0
      }
      this.getList()
    },
    toAdd() {
      this.viewState = 2
      this.$refs['divAdd'].showAdd()
    },
    handUpdate(row) {
      this.viewState = 2
      this.$refs['divAdd'].showEdit(row)
    },
    handleDownLoad(row) {
      this.printSid = row.sid
      this.print = ''
      this.dialogVisible = true
    },
    toRevocation(row) {
      const tip = '请确认是否撤回该申请?'
      this.$confirm(tip, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        recallConfirm({ sid: row.sid, userSid: window.sessionStorage.getItem('userSid') }).then((resp) => {
          if (resp.success) {
            loading.close()
            this.$message({ showClose: true, type: 'success', message: '操作成功' })
            this.getList()
          } else {
            loading.close()
          }
        }).catch(() => {
          loading.close()
        })
      })
    },
    handleConfirm() {
      this.visibleDisabled = true
      createPdf({ skdSid: this.printSid, printerType: this.print }).then((resp) => {
        if (resp.success && resp.data.pdfPath !== '') {
          var xhr = new XMLHttpRequest()
          xhr.open('GET', process.env.VUE_APP_BASE_API + '/base/file/download?filePath=' + resp.data.pdfPath + '&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()
          this.dialogVisible = false
          this.printSid = ''
          this.visibleDisabled = false
          this.getList()
        } else {
          this.visibleDisabled = false
        }
      }).catch(() => {
        this.visibleDisabled = false
      })
    },
    handLook(row) {
      this.viewState = 3
      this.$refs['divInfo'].showInfo(row)
    },
    resetState() {
      this.viewState = 1
    },
    doClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    }
  }
}
</script>
<style scoped>
</style>
