<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>
          <el-button type="warning" @click="bohuiSfd">
            驳回
          </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="updateTime" width="160" />
      <el-table-column align="center" label="设备种类" :formatter="formatter.formatEquipment" prop="shebeizhongleidaima" width="100" />
      <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" label="缴费方式" prop="payMode" width="155" />
      <el-table-column align="center" label="备注" prop="record" />
      <el-table-column :formatter="formatter.moneyFormat" align="center" label="缴款金额" prop="totalAmount" width="140">
        <template slot-scope="{row}">
          {{ row.supplyState ? '补交:' + row.supplyAmount : row.totalAmount }}
        </template>
      </el-table-column>
      <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 fixed="right" align="center" label="操作" width="140">
        <template slot-scope="scope">
          <el-button type="success" size="small" circle icon="el-icon-view" title="预览缴费凭证" @click="viewJFPZ(scope.row.fileName)" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="totalCount>0" :total="totalCount" :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>
  </div>
</template>
<script>
import Sticky from '@/components/Sticky'
import Pagination from '@/components/Pagination'
import axios from 'axios'

export default {
  name: 'JiaoFeiDanDaiShouFei', // 待收费单位
  components: { Sticky, Pagination },
  data() {
    return {
      totalCount: 0,
      listQuery: {
        pageNum: 1, // 页码
        pageSize: 20, // 每页条数
        paymentCodeIsNull: false, // 是否查看缴款码为空的数据
        isUploadCredential: false, // 是否已经上传缴费凭证
        jfState: false,
        order: 'updateTime', // 排序字段
        sort: 'desc' // 排序方式
      },
      bill: {
        record: ''
      },
      multipleSelection: [],
      dialogFileStateVisible: false,
      list: [],
      bjd: []
    }
  },
  created() {
    this.getList()
  },
  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.totalCount = 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.apibjd({
          url: '/charge/updateRenwuSfStateByBillId',
          method: 'get',
          params: {
            billId: this.multipleSelection[0].id
          }
        }).then(data => {
          this.getList()
          this.$message({ message: '收费成功。', type: 'success' })
        })
      })
    },
    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' })
        this.sendSmsReject(this.multipleSelection[0].inspectionPhone, this.bill.record)
      })
    },
    tableRowFileState({ row, rowIndex }) {
      if (row.fileState === 2) {
        return 'bohui-row'
      }
      return ''
    },
    sendSmsReject(phone, reason) {
      axios.defaults.headers.common['area'] = 'jinan'
      axios.get(`${process.env.VUE_APP_SMS_PAY_REJECT_URL}` + phone + `?reason=` + reason).then((response) => {
        console.log(response)
      }).catch(() => {
        this.$message.warning('短信发送失败！！！')
      })
    }
  }
}
</script>

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