<template>
  <el-card>
    <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent>
      <el-form-item label="采购单号" prop="contractCode">
        <el-input v-model="queryParams.contractCode" placeholder="请输入采购单号" size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="付款状态" prop="settlementStatus">
        <el-select v-model="queryParams.settlementStatus" placeholder="请选择付款状态" size="small" style="width: 100%" @change="handleQuery">
          <el-option v-for="item in status" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey" />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商" prop="supplyName">
        <el-input v-model="queryParams.supplyName" placeholder="请输入供应商" size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button size="mini" @click="handleQuery" class="searchBtn">搜索</el-button>
        <el-button size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableList" :cell-style="{ padding: '0' }" :row-style="{ height: '35px' }" v-loading="loading" header-row-class-name="tableHeader">
      <el-table-column label="采购单号" prop="code" width="160" />
      <el-table-column label="供应商" prop="supplyName" min-width="140" />
      <el-table-column label="结算方式" prop="payType" width="140" :formatter="payTypeFormat" />
      <el-table-column label="发票" prop="invoiceType" min-width="140" :formatter="invoiceTypeFormat" />
      <el-table-column label="付款状态" align="center" width="100" :formatter="statusFormat" />
      <el-table-column label="采购总金额(含税)" align="right" width="130">
        <template slot-scope="scope">
          <span v-if="scope.row.purchasePrice">{{ moneyFormat(scope.row.purchasePrice, 3) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退货总金额" align="right" width="110">
        <template slot-scope="scope">
          <span v-if="scope.row.returnAmount">{{ moneyFormat(scope.row.returnAmount, 3) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应付总金额" align="right" width="110">
        <template slot-scope="scope">
          <span v-if="scope.row.meetAmount">{{ moneyFormat(scope.row.meetAmount, 3) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已结清金额" align="right" width="110">
        <template slot-scope="scope">
          <span v-if="scope.row.settledAmount">{{ moneyFormat(scope.row.settledAmount, 3) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已抵扣金额" align="right" width="110">
        <template slot-scope="scope">
          <span v-if="scope.row.deductionAmount">{{ moneyFormat(scope.row.deductionAmount, 3) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="未结清金额" align="right" width="110">
        <template slot-scope="scope">
          <span v-if="scope.row.unclearedAmount && scope.row.unclearedAmount > 0" style="color: #d70606">{{ moneyFormat(scope.row.unclearedAmount, 3) }}</span>
          <span v-else>{{ scope.row.unclearedAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="80" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" v-if="scope.row.unclearedAmount && scope.row.unclearedAmount > 0" @click="clickPay(scope.row)" v-db-click>付款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </el-card>
</template>

<script>
import * as API from '@/api/shengde/productionSystem/purchase/payment'
import Utils from '@/util/transit'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractCode: '',
        supplyName: '',
        settlementStatus: '',
      },
      loading: false,
      tableList: [],
      total: 0,
      status: [
        { dictKey: '0', dictValue: '未结清' },
        { dictKey: '1', dictValue: '已结清' },
      ],
      payType: [],
      invoiceType: [],
    }
  },
  created() {
    this.payType = this.dictData.filter((item) => item.code === 'purchase_payment_pay_type')[0].children
    this.invoiceType = this.dictData.filter((item) => item.code === 'invoice_type')[0].children
  },
  mounted() {
    this.getList()
    Utils.$on('refreshPay', () => {
      this.getList()
    })
  },
  computed: mapGetters(['dictData']),
  methods: {
    getList() {
      this.loading = true
      API.getPurchasePaymentList(this.queryParams).then(
        (res) => {
          this.tableList = res.data.data.records
          this.total = res.data.data.total
          this.loading = false
        },
        (err) => {
          console.log('getPurchasePaymentList: ' + err)
          this.loading = false
        }
      )
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    statusFormat(row) {
      return this.selectConstantsLabel(this.status, row.settlementStatus)
    },
    payTypeFormat(row) {
      return this.selectConstantsLabel(this.payType, row.payType)
    },
    invoiceTypeFormat(row) {
      return this.selectConstantsLabel(this.invoiceType, row.invoiceType)
    },
    clickPay(row) {
      let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
      let maxPos = $chars.length
      let random = ''
      for (let i = 0; i < 32; i++) {
        random += $chars.charAt(Math.floor(Math.random() * maxPos))
      }
      this.$router.push({
        path: '/purchase/payment',
        query: {
          id: row.id,
          random: random,
        },
      })
    },
  },
}
</script>

<style lang="scss" scoped>
* {
  font-size: 12px;
}
.searchBtn {
  background: #20b2aa;
  color: #fff;
  border: 1px solid #20b2aa;
}

::v-deep {
  .el-input__inner {
    border-radius: 1px;
  }
  .el-button--mini {
    border-radius: 1px;
  }
  .tableHeader th {
    background-color: #edf0f5;
    height: 35px;
    padding: 0;
  }
}
</style>
