<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div class="filter-container">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="账号" prop="username">
          <el-input v-model="searchForm.username" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="fetchData">搜索</el-button>
          <router-link to="/systemManagement/accountCreate"><el-button class="ml-10" type="primary" icon="el-icon-edit">新增</el-button></router-link>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <el-table v-loading="listLoading" :data="firmList" border fit  highlight-current-row style="width: 100%;">
      <el-table-column label="账号" prop="username" align="center"></el-table-column>
      <el-table-column label="昵称" prop="fullname" align="center"></el-table-column>
      <el-table-column label="创建时间" prop="create_time" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <router-link :to="'/systemManagement/accountEdit/'+scope.row.user_id">
            <el-button type="primary" size="mini">编辑</el-button>
          </router-link>
          <el-button class="ml-10" size="mini" type="danger" @click="handleDelete(scope.row.user_id)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 翻页 -->
    <pagination v-show="total>0" 
      :total="total" 
      :page.sync="searchForm.page" 
      :page_len.sync="searchForm.page_len"
      :limit.sync="searchForm.limit" 
      @pagination="fetchData" />
  </div>
</template>

<script>
import { listAdmin, deleteAdmin } from '@/api/user/index'

export default {
  data() {
    return {
      firmList: [],  // 列表
      total: 0,  // 总页数
      listLoading: true,
      // 搜索相关数据
      searchForm: {
        page: 1,
        page_len: 10
      },
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取企业列表
    fetchData(obj={}) {
      if(obj.page) {
        this.searchForm.page = obj.page
        this.searchForm.page_len = obj.page_len
      }
      this.listLoading = true
      this.$http(listAdmin(this.searchForm), (res) => {
        this.firmList = res.data.result
        this.total = res.data.data_sum
        this.listLoading = false
      })
    },
    // 删除
    handleDelete(id) {
      this.$confirm('删除', `确认删除吗`, () => {
        this.$http(deleteAdmin(id), res => {
          if(res.code == 0) {
            this.$message.success(res.message)
            this.fetchData()
          }
        })
      })
    },
  }
}
</script>

<style lang='scss' scoped>
</style>
