<template>
  <div class="form-wrap">
    <div class="searchBox">
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        label-width="100px">
        <el-form-item
          label="名称"
          prop="loginName">
          <el-input v-model="form.loginName"></el-input>
        </el-form-item>
      </el-form>
      <div class="btn-wrap">
        <el-button
          type="primary"
          @click="getAccountList">搜索</el-button>
        <el-button
          type="primary"
          v-if="canAddUsers"
          @click="showDialog('add')">新增账号</el-button>
      </div>
    </div>
    <div class="tableBox">
      <!-- <div class="block-title">
        账号列表
      </div> -->
      <el-table
        v-loading="loading"
        stripe
        row-class-name="is-center"
        :data="tableData">
        <el-table-column
          prop="loginName"
          label="账号名"
          width="140">
        </el-table-column>

        <el-table-column
          prop="roles"
          label="角色">
          <template slot-scope="scope">
            <el-tag
              v-for="(item,index) in scope.row.roleList"
              :key="index">
              {{ item.name }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="resourceList"
          label="权限列表">
          <template slot-scope="scope">
            <el-tag
              v-for="(item,index) in scope.row.resourceList"
              :key="index">
              {{ item.name }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          prop="lastLoginTime"
          label="最近一次登录时间"
          width="140">
        </el-table-column>

        <el-table-column
          fixed="right"
          label="操作"
          width="120">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="showDialog('edit',scope.row)">编辑</el-button>
            <el-button
              type="text"
              @click="deleteAccount(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 账号弹框 -->
    <ElDialog
      title="添加账号"
      width="660px"
      v-model="addVisible">
      <AddDialog @updateList="getAccountList"></AddDialog>
    </ElDialog>

    <ElDialog
      title="编辑账号"
      width="660px"
      v-model="editVisible">
      <EditDialog
        :account-item="currentItem"
        @updateList="getAccountList"></EditDialog>
    </ElDialog>

    <el-pagination
      background
      :page-size="10"
      @current-change="handlePage"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getAccountList, deleteAccount } from '@/api/systemSetting/account'
import ElDialog from '@/components/Dialog'
import AddDialog from './account/AddDialog'
import EditDialog from './account/EditDialog'

export default {
  components: {
    ElDialog,
    AddDialog,
    EditDialog
  },
  computed: {
    canAddUsers () {
      return true
      // return this.$store.state.user.permissionList.includes('system_settings:add_users')
    }
  },

  created () {
    this.getAccountList()
  },
  data () {
    return {
      loading: false,
      addVisible: false,
      editVisible: false,
      tableData: [],
      total: 0,
      form: {
        loginName: '', // 账号名
        pageNum: 1,
        pageSize: 10
      },
      // 当前账号对象
      currentItem: {}
    }
  },
  methods: {
    showDialog (type, item) {
      this[`${type}Visible`] = true
      this.currentItem = item
    },
    getAccountList (goFirst) {
      this.loading = true
      if (goFirst) {
        this.form.pageNum = 1
      }
      getAccountList(this.form).then(res => {
        this.loading = false
        this.tableData = res.data.records.map(item => {
          item.permissionList = (item.roleList || []).reduce((acc, role) => {
            return acc.concat(role.resourceList || [])
          }, [])
          return item
        }) || []
        this.total = res.data.total
      })
    },
    deleteAccount (id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAccount(id).then(res => {
          this.$message.success('删除成功')
          this.getAccountList(true)
        })
      })
    },
    handlePage (pageNum) {
      this.form.pageNum = pageNum
      this.getAccountList()
    }
  }
}
</script>

<style scoped>

</style>
