<template>
  <div class="app-container">
    <div class="filter-container">
      <sticky style="margin-bottom: 10px;">
        <div class="sub-navbar">
          <el-input ref="seaialNumberF" v-model="listQuery.serialNumber" placeholder="收费单号" clearable style="width: 260px" @keyup.enter.native="handleFilter" />
          <el-input v-model="listQuery.inspectedUnit" placeholder="缴款单位" clearable style="width: 360px" @keyup.enter.native="handleFilter" />
          <el-button type="primary" icon="el-icon-search" @click="handleFilter">
            查询
          </el-button>
          <el-button type="success" @click="updateJfState">
            <svg-icon icon-class="charge" />确认收费
          </el-button>
        </div>
      </sticky>
    </div>
    <el-table
      ref="list"
      v-adaptive="{bottomOffset: 50}"
      height="0"
      :data="list"
      element-loading-text="拼命加载中"
      border
      fit
      highlight-current-row
      stripe
      width="100%"
      :row-class-name="tableRowFileState"
      @row-click="onRowClick"
      @selection-change="handleSelectionChange"
      @sort-change="sortChange"
    >
      <el-table-column fixed="left" type="selection" width="40" />
      <el-table-column fixed="left" align="center" label="序号" width="60" type="index" />
      <el-table-column align="center" label="创建日期" prop="createTime" width="160" />
      <el-table-column align="center" :formatter="formatter.formatEquipment" label="设备种类" prop="shebeizhongleidaima" width="80" />
      <el-table-column align="center" label="收费单号" prop="serialNumber" width="160" />
      <!--      <el-table-column align="center" label="缴款码单号" prop="paymentCode" width="160" />-->
      <el-table-column align="center" label="使用单位" prop="inspectedUnit" sortable="custom" />
      <el-table-column align="center" label="缴款单位" prop="invoiceHeader" sortable="custom" />
      <el-table-column align="center" prop="bjdCount" label="报检台数" width="80" />
      <el-table-column align="center" label="补交状态" prop="supplyState" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.supplyState" type="danger">
            是
          </el-tag>
          <el-tag v-else type="success">
            否
          </el-tag>
        </template>
      </el-table-column>
      <!-- el-table-column align="center" label="当前状态" prop="fileState" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.fileState === 0" type="info">
            待资料上传
          </el-tag>
          <el-tag v-else-if="scope.row.fileState === 1" type="primary">
            待审核
          </el-tag>
          <el-tag v-else-if="scope.row.fileState === 2" type="danger">
            已驳回
          </el-tag>
          <el-tag v-else-if="scope.row.fileState === 3" type="success">
            已通过
          </el-tag>
        </template>
      </el-table-column -->
      <el-table-column align="center" label="备注" prop="record" />
      <!-- el-table-column align="center" label="缴费方式" prop="payMode" width="155" /-->
      <el-table-column :formatter="formatter.moneyFormat" fixed="right" align="center" label="缴款金额" prop="totalAmount" width="140">
        <template slot-scope="{row}">
          {{ row.supplyState ? '补交:' + row.supplyAmount : row.totalAmount }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" width="140">
        <template slot-scope="scope">
          <el-button size="mini" title="预览交费凭据" icon="el-icon-view" circle @click="viewJFPZ(scope.row.fileName)" />
          <el-button size="mini" title="详情" icon="el-icon-info" circle @click="showDetailsInit(scope.row)" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-row.sync="listQuery.pageSize" @pagination="getList" />
    <el-dialog title="驳回当前收费单" :modal-append-to-body="false" :visible.sync="dialogFileStateVisible" width="450px" @close="dialogFileStateVisible = false">
      <el-form ref="form" :model="bill" label-width="80px">
        <el-form-item label="驳回原因">
          <el-input v-model="bill.record" type="textarea" placeholder="请填写驳回的原因" />
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button type="primary" @click="updateFileState">
          提交
        </el-button>
      </div>
    </el-dialog>
    <el-dialog title="填写发票相关信息" :modal-append-to-body="false" :visible.sync="dialogChargeBillSupplementVisible" width="450px" @close="closeChargeBillSupplement">
      <el-form ref="chargeBillSupplement" :model="chargeBillSupplement" label-width="80px">
        <el-form-item label="缴款单位" prop="invoiceHeader" required>
          <el-input v-model="chargeBillSupplement.invoiceHeader" placeholder="请填写缴款单位" />
        </el-form-item>
        <el-form-item label="缴款码" prop="paymentCode" :rules="[{ required: true, message: '请输入缴款码', trigger: 'blur' },{ min: 20, max:24, message: '请输入20位缴款码', trigger: ['blur', 'change'] }]">
          <el-input v-model="chargeBillSupplement.paymentCode" placeholder="请填写缴款码" maxlength="24" show-word-limit />
        </el-form-item>
        <el-form-item label="缴费方式" prop="payMode" required>
          <el-select v-model="chargeBillSupplement.payMode">
            <el-option label="山东非税统缴平台" value="山东非税统缴平台" />
            <el-option label="财政过度账号(汇款)" value="财政过度账号(汇款)" />
            <el-option label="转账支票填写" value="转账支票填写" />
          </el-select>
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button type="primary" @click="updateRenwuSfStateByBillId">
          提交
        </el-button>
      </div>
    </el-dialog>
    <detail v-if="detailListVisible" :charge-bill="chargeBill" :data="tableDetailsList" :editor="editor" :version="version" />
  </div>
</template>
<script>
import Sticky from '@/components/Sticky'
import Pagination from '@/components/Pagination'
import Detail from '@/views/chargeBill/components/detail'

export default {
  name: 'JiaoFeiDanXianXia', // 待收费单位
  components: { Sticky, Pagination, Detail },
  data() {
    return {
      total: 0,
      listQuery: {
        pageNum: 1, // 页码
        pageSize: 20, // 每页条数
        // printState: true,
        // paymentCodeIsNull: true,
        isUploadCredential: false,
        jfState: false,
        order: undefined, // 排序字段
        sort: undefined // 排序方式
      },
      bill: {
        record: ''
      },
      detailListVisible: false,
      chargeBill: {},
      tableDetailsList: [],
      editor: false,
      multipleSelection: [],
      dialogFileStateVisible: false,
      list: [],
      bjd: [],
      dialogChargeBillSupplementVisible: false,
      chargeBillSupplement: {}
    }
  },
  created() {
    this.getList()
    const that = this
    this.common.$on('CloseChargeBillDetailList', function() {
      that.detailListVisible = false
    })
  },
  mounted() {
    this.$refs.seaialNumberF.focus()
  },
  methods: {
    handleFilter() {
      // 查询事件
      this.listQuery.pageNum = 1
      this.getList()
    },
    getList() {
      this.apibjd({
        url: '/charge/pageList',
        method: 'get',
        params: this.listQuery
      }).then(data => {
        this.list = data.list
        this.total = data.total
        this.common.switchInspection(this.list)
      })
    },
    onRowClick(row) {
      this.$refs.list.toggleRowSelection(row)
    },
    handleSelectionChange: function(val) {
      this.multipleSelection = val
    },
    sortChange(column) {
      this.listQuery.order = column.prop
      this.listQuery.sort = column.order.replace('ending', '')
      this.getList()
    },
    viewJFPZ(fileName) {
      // 缴费凭证预览查看
      if (fileName) {
        const path = process.env.VUE_APP_BASE_API + 'showImage/voucher/' + fileName
        this.$router.push({ name: 'ImagePreview', params: { src: path }})
      } else {
        this.$message.warning('暂无上传交费凭据！')
      }
    },
    updateJfState() {
      if (this.multipleSelection.length !== 1) {
        this.$message({ message: '只允许一条缴费单进行确认。', type: 'warning' })
        return false
      }
      this.$confirm('确认要将该收费单设置为已收费吗?', '提示', {
        confirmButtonText: '确定',
        showCancelButton: true,
        type: 'warning'
      }).then(() => {
        this.dialogChargeBillSupplementVisible = true
        const chargeBill = this.multipleSelection[0]
        this.$set(this.chargeBillSupplement, 'billId', chargeBill.id)
        this.$set(this.chargeBillSupplement, 'invoiceHeader', chargeBill.invoiceHeader)
        this.$set(this.chargeBillSupplement, 'paymentCode', chargeBill.paymentCode)
        this.$set(this.chargeBillSupplement, 'payMode', chargeBill.payMode)
      })
    },
    updateRenwuSfStateByBillId() {
      this.apibjd({
        url: '/charge/updateRenwuSfStateByBillId',
        method: 'get',
        params: this.chargeBillSupplement
      }).then(() => {
        this.getList()
        this.$message({ message: '收费成功。', type: 'success' })
        this.dialogChargeBillSupplementVisible = false
        this.$refs.chargeBillSupplement.resetFields()
      })
    },
    bohuiSfd() {
      if (this.multipleSelection.length !== 1) {
        this.$message({ message: '只允许一条缴费单进行驳回。', type: 'warning' })
        return false
      }
      this.dialogFileStateVisible = true
    },
    updateFileState() {
      if (this.multipleSelection.length !== 1) {
        this.$message({ message: '只允许一条缴费单进行驳回。', type: 'warning' })
        return false
      }
      this.apibjd({
        url: '/charge/updateRenwuFileStateByBillId',
        method: 'get',
        params: {
          billId: this.multipleSelection[0].id,
          record: this.bill.record
        }
      }).then(data => {
        this.getList()
        this.dialogFileStateVisible = false
        this.$message({ message: '驳回成功。', type: 'success' })
      })
    },
    tableRowFileState({ row, rowIndex }) {
      if (row.fileState === 2) {
        return 'bohui-row'
      }
      return ''
    },
    getVersionBySbzl(sbzl) {
      switch (sbzl) {
        case '3000' :
        case '4000' :
        case '5000' :
          this.version = 16
          break
        default :
          this.version = 18
          break
      }
    },
    /**
     * 获得收费通知单详情列表
     */
    showDetailsInit(row) {
      this.getVersionBySbzl(row.shebeizhongleidaima)
      this.apibjd({
        url: '/charge/getDetailByBillId/' + row.id,
        method: 'get',
        params: { version: this.version }
      }).then(data => {
        this.chargeBill = row
        this.tableDetailsList = data
        this.detailListVisible = true
      })
    },
    closeChargeBillSupplement() {
      this.dialogChargeBillSupplementVisible = false
      this.$refs.chargeBillSupplement.resetFields()
    }
  }
}
</script>

<style>
  .el-table .bohui-row {
    color: #ff0913;
  }
</style>
