<template>
  <div class="payer-table">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="客户" prop="custId">
        <CustInput width="160px" :value="queryParams.custId"  :label="queryParams.custName" size="small" @change="handleCustChange" />
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input class="filter-input" v-model="queryParams.orderNo" 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">
        <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"
          >核销</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-plus"
            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="custName" sortable='custom' />
      <el-table-column label="费用名称" align="center" prop="itemName" sortable="custom" />
      <el-table-column label="费用" align="center" prop="itemCost" 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="lockStatus">
        <template slot-scope="scope">
          <el-tag :type="scope.row.lockStatus == 1 ? 'success' : ''" size="small">{{ scope.row.lockStatus == 1 ? '已对账' : '未对账'
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="已核销" align="center" prop="writeoffCost" 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="orderNo" sortable="custom" />
      <el-table-column label="日期" align="center" prop="inputDate" width="180" sortable='custom'>
        </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" 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 { pageListCost } from "@/api/amortize/cost";
import { autoWriteoffByCost, cancelWriteoffByCost } from "@/api/finance/writeoff";
import CustInput from "@/views/tm/components/CustInput";

export default {
  name: 'AmortizeList',
  dicts: ['sys_curr_type'],
  components: { CustInput },
  props: {
    // 0应收 1应付
    rpFlag: {
      type: String,
      default: '0',
    }
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        custId: null,
        custName: null,
        orderNo: null,
        beginDate: null,
        endDate: null,
        orderByColumn: 'createTime',
        isAsc: 'DESC',
      },
      selection: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleCustChange(custs) {
      const cust = custs[0] || {}
      this.queryParams.custId = cust.id
      this.queryParams.custName = cust.custName
      this.handleQuery()
      this.$emit('query', { custId: cust.id, custName: cust.custName });
    },
    /** 查询费用核销记录列表 */
    getList() {
      this.loading = true
      pageListCost({
        ...this.queryParams,
        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()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection = selection || [];
      this.$emit('change', 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();
    },
    // 核销
    handleWriteOff() {
      this.$emit('writeOff', this.selection);
      autoWriteoffByCost(this.selection[0]).then(res => {
        this.$modal.msgSuccess('核销成功');
        this.getList();
        this.$emit('complete');
      }).catch(e => {

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

      });
    },
  }
}
</script>
<style lang="scss" scoped>
.payer-table {
  width: 100%;
  height: 100%;
}
</style>
