<template>
  <el-row>
    <el-row>
      <el-form ref="searchForm" :model="searchForm" label-width="80px">
        <search-white-board :show-more="true" @search="search">
          <div slot="base">
            <el-row style="line-height: 40px; text-align: right;">
              <el-col :span="2" :offset="1"><label class="search-label">昵称</label></el-col>
              <el-col :span="5">
                <el-input v-model.trim="searchForm.nickname" placeholder="请输入昵称" clearable @keyup.enter.native="search" />
              </el-col>
            </el-row>
          </div>

          <div slot="more">
            <el-row style="line-height: 40px; text-align: right;">
              <el-col :span="2"><label class="search-label">是否删除</label></el-col>
              <el-col :span="5" style="text-align: left;">
                <el-switch v-model="searchForm.isDel" active-color="#13ce66" inactive-color="#ff4949" @change="search" />
              </el-col>
            </el-row>
          </div>
        </search-white-board>
      </el-form>
    </el-row>

    <el-row>
      <white-board>
        <el-row>
          <el-col :span="2" :offset="22" style="text-align: right;">
            <el-button icon="el-icon-plus" type="success" plain @click="handleAdd">新增</el-button>
          </el-col>
        </el-row>

        <el-row style="padding: 10px 0px;">
          <el-table :data="tableData" style="width: 100%;" highlight-current-row stripe>
            <el-table-column prop="identifier" label="用户名" show-overflow-tooltip />
            <el-table-column prop="nickname" label="昵称" show-overflow-tooltip />
            <el-table-column label="性别">
              <template slot-scope="scope">
                <label v-if="scope.row.gender === 0">未知</label>
                <label v-if="scope.row.gender === 1">男</label>
                <label v-if="scope.row.gender === 2">女</label>
              </template>
            </el-table-column>
            <el-table-column label="类型">
              <template slot-scope="scope">
                <label v-if="scope.row.type === 1">平台</label>
                <label v-if="scope.row.type === 2">租户管理员</label>
                <label v-if="scope.row.type === 3">普通账号</label>
              </template>
            </el-table-column>
            <el-table-column prop="deptName" label="部门" show-overflow-tooltip />
            <el-table-column label="状态">
              <template slot-scope="scope">
                <span>{{ scope.row.isLocked === true ? '已锁定' : '已启用' }}</span>
              </template>
            </el-table-column>
            <el-table-column label="更新时间" width="135">
              <template slot-scope="scope">
                <el-row>{{ scope.row.updateByName }}</el-row>
                <el-row>{{ scope.row.updateAt }}</el-row>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="150" fixed="right">
              <template slot-scope="scope">
                <el-button type="primary" plain @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button v-if="!scope.row.isDel" type="danger" plain @click="handleIsDel(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-row>

        <el-row>
          <el-pagination :current-page="searchForm.page" :page-sizes="[10, 20, 50, 100]" :page-size="50" layout="total, sizes, prev, pager, next, jumper" :total="total" small @size-change="changeSizeChange" @current-change="changeCurrentChange" />
        </el-row>
      </white-board>
    </el-row>
    <edit-account-dialog :visible="dialogVisible" :dialog-title="dialogTitle" :form="form" @cancelDialog="cancelDialog" @refresh="list" />
  </el-row>
</template>

<script>
import EditAccountDialog from '@/views/platform/system/account/editAccountDialog'
import accountApi from '@/api/platform/accountApi'

export default {
  name: 'AccountIndex',
  components: {
    'edit-account-dialog': EditAccountDialog
  },
  data() {
    return {
      searchForm: {
        nickname: '',
        isDel: false,
        page: 1,
        size: 50
      },
      total: 0,
      tableData: [],
      dialogTitle: '',
      dialogVisible: false,
      form: {
        id: '',
        identifier: '',
        credential: '',
        nickname: '',
        gender: 0,
        type: [],
        dept: [],
        isLocked: 0,
        endDateTime: ''
      }
    }
  },
  mounted() {
    this.list()
  },
  methods: {
    cancelDialog(visible) {
      this.dialogVisible = visible
    },
    search() {
      this.searchForm.page = 1
      this.list()
    },
    list() {
      this.tableData = []
      accountApi.page(this.searchForm).then(data => {
        this.total = data.total
        this.tableData = data.data
      })
    },
    handleAdd() {
      this.form = {
        id: '',
        identifier: '',
        credential: '',
        nickname: '',
        gender: 0,
        type: [],
        dept: [],
        isLocked: 0,
        endDateTime: ''
      }
      this.dialogTitle = '新增'
      this.dialogVisible = true
    },
    handleEdit(index, row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogTitle = '编辑'
      this.dialogVisible = true
    },
    handleIsDel(index, row) {
      accountApi.handleIsDel(row.id).then(data => {
        this.list()
      })
    },
    changeSizeChange(val) {
      this.searchForm.size = val
      this.list()
    },
    changeCurrentChange(val) {
      this.searchForm.page = val
      this.list()
    }
  }
}
</script>

<style scoped>
</style>
