<template>
  <div class="app-container">
    <div class="filter-container">
        <el-input :size="sizeType" v-model="search.clientName" placeholder="客户名称" style="width: 200px;" class="filter-item"/>
      <el-button :size="sizeType" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>

      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="getList"/>
        </el-tooltip>
      </div>
    </div>
    <el-table
      :size="sizeType"
      :data="tableData"
      v-loading="listLoading"
      row-key="id"
      border
      style="margin-top: 10px"
      ref="orderTable"
      max-height="600"
      default-expand-all>
      <el-table-column label="操作" width="160px">
        <template slot-scope="{row}">
          <el-button size="mini" type="danger" @click="cancelPay(row)">取消</el-button>
          <el-button size="mini" type="warning" @click="resetPay(row)">重收</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作人" prop="userName"></el-table-column>
      <el-table-column label="交易编号" prop="tradeCode" width="110">
      <template slot-scope="{row}">
        <el-tooltip :content="row.tradeCode" placement="top-start" effect="light">
          <span v-if="row.tradeCode">{{ plusXing(row.tradeCode, 6, 4) }}</span>
        </el-tooltip>
      </template>
    </el-table-column>
      <el-table-column label="客户名称" prop="printOrderList[0].orderName" show-overflow-tooltip></el-table-column>
      <el-table-column label="应收金额" prop="payableMoney" w>
        <template slot-scope="{row}">
          <span>{{ row.payableMoney / 100 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款金额" prop="paiedMoney" width="100">
        <template slot-scope="{row}">
          <span>{{ row.paiedMoney / 100 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="二维码地址" prop="qrCodeUrl">
        <template slot-scope="{row}">
          <el-link type="primary" :href="row.qrCodeUrl" target="_blank">二维码地址</el-link>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" prop="billStatus" width="80">
        <template slot-scope="{row}">
          <el-tag v-if="row.paymentChannel == 1" type="info">银联</el-tag>
          <el-tag v-else-if="row.paymentChannel == 2" type="primary">支付宝</el-tag>
          <el-tag v-else-if="row.paymentChannel == 4" type="success">微信</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" prop="billStatus">
        <template slot-scope="{row}">
          <el-tag v-if="row.billStatus == 0" type="danger">未支付</el-tag>
          <el-tag v-else-if="row.billStatus == 1" type="success">已支付</el-tag>
          <el-tag v-else-if="row.billStatus == 2" type="warning">手动终止</el-tag>
          <el-tag v-else-if="row.billStatus == 3" type="warning">超时终止</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="过期时间" prop="expireTime" width="130">
        <template slot-scope="{row}">
          <span v-if="row.expireTime">{{ row.expireTime | getTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" prop="payTime" width="130">
        <template slot-scope="{row}">
          <span v-if="row.payTimeLong>0">{{ row.payTime | getTime }}</span>
          <span v-else> </span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getList"/>
    <el-dialog title="订单详情" :visible.sync="detailVisible" class="orderDetailDialog" width="1200px" top="10px">
      <order-detail ref="orderDetail"></order-detail>
    </el-dialog>
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog" :noDoubleClick="true"></cancledialog>
    <order-step ref="orderStep"></order-step>
  </div>
</template>

<script>
import OrderDetail from '@/components/orderDetail/orderDetail'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import axios from "axios";
import {changeMoneyToChinese, doLocalPrint} from '@/utils/index'
import OrderStep from "@/components/orderStep/orderStep";
import {exportData} from "@/api/orderCharge/orderCollection";
import {formatMoney} from '@/filters/index'
import {cancelOrderPayRequest, ifPermission, reOrderPayRequest} from "@/api/order/orderList";
import {getPaymentBillByClient} from "@/api/order/clientPayBillList";
const  searchForm = {
  clientName: '',
  financeCode: '',
  chargeCode: '',
  orderCode: '',
  accountName: '',
  voucherCode: '',
  chargeMan: '',
  platformOrderID: '',
  dataKind: '',
  beginTime: '',
  endTime: '',
  receiveCode: '',
  payMan:'',
}
export default {
  name: 'collectionRecords',
  components: {
    OrderStep,
    OrderDetail,Cancledialog
  },
  data() {
    return {
      combinedQuery: false,
      permissionFlag1:false,
      sizeType: 'mini',
      detailVisible: false,
      search: {
        clientName: '',
      },
      choosedList: [],
      tableData: [],
      collection: {},

      listLoading: false,
      listData: {
        total: 0,
        page: 1,
        pageSize: 20
      },
      searchTime: [],

      stateOptions: [
        {
          label: '订单定金',
          value: 0
        },
        {
          label: '订单收款',
          value: 1
        },
      ],
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.checkPermission('PlDel:admin')
  },
  methods: {
    // 判断权限
    checkPermission(permissionStr) {
      const _that = this
      var data = {
        permission: permissionStr
      }
      ifPermission(data).then(res => {
        // console.log(res)
        if (res.data == 1) {
          if (permissionStr == 'PlDel:admin') {
            _that.permissionFlag1 = true
          }
        }
      })
    },
    /**
     * 显示字符串中间加星号
     */
    plusXing(str, frontLen, endLen) {
      // var len = str.length - frontLen - endLen
      var xing = ''
      for (var i = 0; i < 6; i++) {
        xing += '*'
      }
      if(str.length>10){
      return str.substr(0, frontLen) + xing + str.substr(str.length - endLen)
      }else{
        return str
      }
    },

    resetForm() {
      this.search = Object.assign({}, searchForm)
      this.searchTime = []
    },
    getList() {
      var _that = this
      var params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }
      if (_that.listData.pageSize == 10) {
        _that.sizeType = 'medium'
      } else {
        _that.sizeType = 'mini'
      }
      // console.log(data);
      getPaymentBillByClient(data).then(response => {
        // console.log(response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        _that.collection = response.collection
        _that.listData.total = response.total
        // console.log(_that.tableData)
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },

    getSummaries(param) {
      var _that = this
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return
        }
        if (column.label == '折扣金额') {
          sums[index] = formatMoney(_that.collection.agioMoneySum)
        } else if (column.label == '收款金额') {
          sums[index] = formatMoney(_that.collection.receivableMoneySum)
        } else {
          sums[index] = ''
        }
      })
      return sums
    },

    // 刷新表格
    tableRefresh() {
      // 刷新变革
      this.getList()
    },

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getList()
    },

    // 收款单取消
    cancelPay(row) {
      const _that = this
      var data = {
        idBill: row.id
      }
      cancelOrderPayRequest(data).then(res => {
        _that.paymentBillVisible = false
        _that.$message({
          type: 'success',
          message: '取消成功!'
        })
      })
    },
    // 收款单重收
    resetPay(row) {
      const _that = this
      var data = {
        idBill: row.id
      }
      reOrderPayRequest(data).then(res => {
        _that.paymentBillVisible = false
        _that.$notify({
          title: '提示',
          message: '<img src="' + res.data.qrCodeImageUrl + '"width="100px" height="100px" onclick="window.open(this.src)">',
          duration: 0,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 导出
    exportBtn() {
      const _that = this
      var data = {
        code: 'OrderChargeData_Exporter',
        exportName: '收款记录导出',
        params: _that.search
      }
      exportData(data).then(res =>{
        console.log(res);
        if(res.httpCode == 200){
          _that.$message.success('导出成功，请前至系统管理-我的任务中进行查看和下载')
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.filter-item{
  padding: 5px 0;
}
</style>
