<template>
  <div class="app-container">
    <!-- 搜索区域START -->
    <div class="filter-container">
      <div class="filter-control">
        <span>启用状态：</span>
        <el-select
          v-model="listQuery.status"
          placeholder="启用状态"
          clearable
          style="width: 200px"
          class="filter-item"
          @change="getList()"
        >
          <el-option label="全部" value="" />
          <el-option label="已启用" :value="0" />
          <el-option label="未启用" :value="1" />
        </el-select>
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="navigationTo('accounts/add?type=add')">添加用户</el-button>
      </div>
    </div>
    <!-- 搜索区域END -->
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      border
      fit
      class="table-cell"
      header-row-class-name="tableHeader"
    >
      <el-table-column :index="indexMethod" label="序号" type="index" width="80" align="center" />
      <!-- <el-table-column prop="title" label="头像" align="center" width="60">
        <template slot-scope="{ row }">
          <img :src="row.avatar" class="avatar" alt="">
        </template>
      </el-table-column> -->
      <el-table-column prop="loginAccount" label="登录名" align="center" />
      <el-table-column prop="linkMan" label="联系人" align="center" />
      <el-table-column label="手机号" align="center">
        <template slot-scope="{row}">
          <span>{{ row.mobile || '—' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色描述" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.roleDesc || '—' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="启用状态" align="center" width="100">
        <template slot-scope="{ row }">
          <el-switch v-model="row.status" :active-value="1" :inactive-value="0" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" :disabled="!row.id" @click="navigationTo(`accounts/edit?type=edit&id=${row.id}`)">编辑</el-button>
          <el-button type="danger" size="mini" :disabled="!row.id" @click="removeUser(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { fetchUserList, deleteUser } from '@/api/system'
export default {
  data() {
    return {
      listLoading: false,
      listQuery: {
        status: '',
        pageSize: 10,
        pageNum: 1
      },
      list: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    indexMethod(index) {
      return index + this.listQuery.pageSize * (this.listQuery.pageNum - 1) + 1
    },
    // 获取列表
    getList() {
      this.listQuery.pageNum = 1
      this.list = []
      this.fetchUserList()
    },
    // 获取用户列表
    fetchUserList() {
      fetchUserList(this.listQuery).then(res => {
        console.log(res)
        this.list = [...this.list, ...res.data.list]
      })
    },
    // 删除用户
    removeUser(row) {
      deleteUser(row.id).then(res => {
        this.$message.success('删除用户成功')
        this.getList()
      })
    }
  }
}
</script>

<style scoped lang="scss">
.avatar{
  display: block;
  width: 40px;
  height: 40px;
}
</style>
