<template>
  <div class="payee-table">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="单据号" prop="billNo">
        <el-input class="filter-input" v-model="queryParams.billNo" placeholder="请输入单据号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="开始时间" prop="beginDate">
        <el-date-picker class="filter-input" v-model="queryParams.beginDate" value-format="yyyy-MM-dd" type="date" placeholder="请选择开始时间" />
      </el-form-item>
      <el-form-item label="结束时间" prop="endDate">
        <el-date-picker class="filter-input" v-model="queryParams.endDate" value-format="yyyy-MM-dd" type="date" placeholder="请选择结束时间" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
          <el-switch
            v-model="queryParams.rpFlag"
            active-value="1"
            inactive-value="0"
            active-text="支"
            inactive-text="收">
          </el-switch>
        </el-col> -->
      <el-col :span="1.5">
        <span class="text-gray" style="line-height: 28px;font-weight: 500;">{{ rpFlag === '0' ? '现金收入列表：' : '现金支出列表：' }}</span>
      </el-col>
      <el-col :span="1.5">
        <el-popconfirm title="确定核销选中的账单吗？" @confirm="handleWriteOff">
          <el-button
            v-hasPermi="['finance:writeoff_pay:add']"
            slot="reference"
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            :disabled="selection.length <= 0 || !writeOffAble"
          >核销</el-button>
        </el-popconfirm>
      </el-col>
      <el-col :span="1.5">
        <el-popconfirm title="确定反核销选中的账单吗？" @confirm="handleWriteOffBack">
          <el-button
            v-hasPermi="['finance:writeoff_pay:edit']"
            slot="reference"
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="selection.length <= 0"
          >反核销</el-button>
        </el-popconfirm>
      </el-col>
    </el-row>

    <el-table
      ref="table"
      v-loading="loading"
      :data="list"
      :highlight-current-row="true"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
      @select="handleSelect"
      @select-all="handleSelectAll"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" type="index" width="55" align="center" />
      <el-table-column label="单据号" align="center" prop="billNo" sortable="custom" />
      <el-table-column label="收/付款单位" align="center" prop="custName" sortable="custom" />
      <el-table-column label="收/支" align="center" prop="rpFlag">
        <template slot-scope="scope">
          <el-tag :type="scope.row.rpFlag == 1 ? 'success':''" size="small">{{ scope.row.rpFlag == 0 ? '收' : '支' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="收支金额" align="center" prop="paidMoney" sortable="custom" />
      <el-table-column label="币种" align="center" prop="currency" sortable="custom">
        <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_curr_type" :value="scope.row.currency" />
          </template>
      </el-table-column>
      <el-table-column label="已核销金额" align="center" prop="hxAmount" sortable="custom" />
      <el-table-column label="未核销金额" align="center" prop="whxAmount" sortable="custom" />
      <el-table-column label="收支时间" align="center" prop="paidTime" width="180" sortable="custom">
      </el-table-column>
      <!-- <el-table-column label="经办人" align="center" prop="userId" sortable="custom" /> -->
    </el-table>

    <pagination
      hide-on-single-page
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { pageListCash } from "@/api/finance/cash";
import { autoWriteoffByCash, cancelWriteoffByCash } from "@/api/finance/writeoff";
export default {
  name: 'CashList',
  dicts: ['sys_curr_type'],
  props: {
    // 可核销
    writeOffAble: {
      type: Boolean,
      default: false
    },
    // 0应收 1应付
    rpFlag: {
      type: String,
      default: '0',
    },
    // 额外查询条件
    extraQuery: {
      type: Object,
      default: null,
    },
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 费用核销记录表格数据
      list: [],
      
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        custId: null,
        custName: null,
        billNo: null,
        bizId: null,
        beginDate: null,
        endDate: null,
        orderByColumn: 'paidTime',
        isAsc: 'DESC',
      },
      selection: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询费用核销记录列表 */
    getList() {
      this.loading = true
      pageListCash({
        ...this.queryParams,
        ...this.extraQuery,
        rpFlag: this.rpFlag,
      }).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      }).catch(e => {
        this.loading = false
      })
    },
    // 排序
    handleSortChange(col) {
      this.$sortBy(col, this.queryParams)
      this.getList()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 核销
    handleWriteOff() {
      this.$emit('writeOff', this.selection);
      autoWriteoffByCash(this.selection[0]).then(res => {
        this.$modal.msgSuccess('核销成功');
        this.getList();
        this.$emit('complete');
      }).catch(e => {

      });
    },
    // 反核销
    handleWriteOffBack() {
      this.$emit('writeOffBack', this.selection);
      cancelWriteoffByCash(this.selection[0]).then(res => {
        this.$modal.msgSuccess('反核销成功');
        this.getList();
        this.$emit('complete');
      }).catch(e => {

      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection = selection || [];
      this.$emit('selectionChange', this.selection);
    },
    // 单选点击
    handleSelect(selection, row) {
      selection.forEach(val => {
        if (val.id !== row.id) {
          this.$nextTick(() => {
            this.$refs.table.toggleRowSelection(val, false);
          });
        }
      });
    },
    // 全选点击
    handleSelectAll() {
      // 单选清空数据
      this.$refs.table.clearSelection();
    },
  }
}
</script>
<style lang="scss" scoped>
.payee-table {
  height: 100%;
  width: 100%;
}
</style>
