<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加收款用户
      </el-button>
    </div>
    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width:100%">
      <el-table-column label="序号" align="center" width="60">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column label="姓名" align="center">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="手机号" align="center">
        <template slot-scope="scope">{{ scope.row.mobile }}</template>
      </el-table-column>
      <el-table-column label="用户类型" align="center">
        <template slot-scope="scope">{{ scope.row.userTypeText }}</template>
      </el-table-column>
      <el-table-column label="收款小程序openid" align="center">
        <template slot-scope="scope">{{ scope.row.openid }}</template>
      </el-table-column>
      <el-table-column label="用户权限" align="center" min-width="180">
        <template slot-scope="scope">
          <el-tag v-for="item in scope.row.permissions" :key="item.id" v-text="item.permission" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { fetchList, deleteUser } from '@/api/caimei/finance/receipt-user'

export default {
  name: 'ReceiptUserList',
  data() {
    return {
      tableKey: 0,
      list: [],
      listLoading: true
    }
  },
  watch: {
    $route(route) {
      this.getList()
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList().then(response => {
        this.listLoading = false
        this.list = response.data
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleCreate() {
      this.$router.push({ path: '/finance/receipts/user/add' })
    },
    handleUpdate(row) {
      this.$router.push({ path: '/finance/receipts/user/edit', query: { id: row.id }})
    },
    handleDelete(row) {
      this.$confirm('是否要删除该收款用户', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUser(row.id).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          })
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped>
</style>
