<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="custName">
        <el-input
          v-model="queryParams.custName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收支" prop="ie">
        <el-select v-model="queryParams.ie" placeholder="请选择收支" clearable>
          <el-option
            v-for="dict in dict.type.ie_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="费用名称" prop="chargeName">
        <el-input
          v-model="queryParams.chargeName"
          placeholder="请输入费用名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算方式" prop="payMethod">
        <el-select v-model="queryParams.payMethod" placeholder="请输入结算方式" clearable>
          <el-option
            v-for="dict in dict.type.pay_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="autoWriteoff"
          v-hasPermi="['cash:writeoff:add']"
        >自动核销</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="removeWriteoff"
          v-hasPermi="['cash:writeoff:add']"
        >退回核销</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bill:charge:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chargeList"
              @sort-change="handleSortChange" @selection-change="handleSelectionChange" @row-click="handleRowClick">
      <el-table-column type="selection" width="55" align="center" fixed="left"/>
      <el-table-column label="客户名称" align="center" prop="custName" sortable='custom' width="120" fixed="left"/>
      <el-table-column label="业务类型" align="center" prop="bizType" sortable='custom' width="100" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_type" :value="scope.row.bizType"/>
        </template>
      </el-table-column>
      <el-table-column label="收支" align="center" prop="ie" sortable='custom'>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ie_type" :value="scope.row.ie"/>
        </template>
      </el-table-column>
      <el-table-column label="费用名称" align="center" prop="chargeName" sortable='custom' width="100"/>
      <el-table-column label="结算方式" align="center" prop="payMethod" sortable='custom' width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_method" :value="scope.row.payMethod"/>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import * as api from '@/api/fmbillcharge.js'
import RightToolbar from '@/components/RightToolbar'

export default {
  components: { RightToolbar },
  dicts: ['ie_type', 'biz_type', 'pay_method'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 客户ID
      custId: undefined,
      // 排序参数
      sortField: '',
      sortOrder: '',
      // 导出参数
      exportParams: {},
      // 接收的费用ID
      receivedChargeId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chargeName: undefined,
        custName: undefined,
        ie: undefined,
        payMethod: undefined,
        bizType: undefined
      },
      // 费用列表
      chargeList: [],
      // 总条数
      total: 0
    }
  },
  methods: {
    // 自动核销
    autoWriteoff() {
      this.$confirm('是否确认自动核销？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return api.autoWriteoff(this.ids)
      }).then(response => {
        this.$message.success('自动核销成功')
        this.getList()
        this.$emit('chargeTriggerRefresh')
      }).catch(() => {
        this.$message.info('已取消操作')
      })
    },
    // 退回核销
    removeWriteoff() {
      this.$confirm('是否确认退回核销？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return api.removeWriteoff(this.ids)
      }).then(response => {
        this.$message.success('退回核销成功')
        this.getList()
        this.$emit('chargeTriggerRefresh')
      }).catch(() => {
        this.$message.info('已取消操作')
      })
    },
    // 查询按钮点击事件
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮点击事件
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 选择数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 行点击事件
    handleRowClick(row) {
      this.selectObj = row
      this.$emit('passChargeId', row.id)
    },
    // 排序
    handleSortChange(ev) {
      this.sortField = ev.prop
      this.sortOrder = ev.order === 'ascending' ? 'asc' : 'desc'
      this.getList()
    },
    // 导出
    handleExport() {
      this.download('bill/charge/export', {
        ...this.queryParams
      }, `charge_${new Date().getTime()}.xlsx`)
    },
    // 获取费用列表
    getList() {
      this.loading = true
      api.fmbillchargeListApi({
        ...this.queryParams,
        sortField: this.sortField,
        sortOrder: this.sortOrder
      }).then(response => {
        this.chargeList = response.rows
        this.total = response.total
        this.loading = false
      })
    }
  },
  created() {
    this.getList()
  }
}
</script>
