<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.loginName"
        :placeholder="$t('pages.sysUserManager.filter.account')"
        style="width: 200px;"
        class="filter-item"
      />
      <el-input
        v-model="listQuery.nickname"
        :placeholder="$t('pages.sysUserManager.filter.nickname')"
        style="width: 200px;"
        class="filter-item"
      />
      <el-select
        v-model="listQuery.status"
        :placeholder="$t('pages.sysUserManager.filter.status')"
        clearable
        style="width: 90px"
        class="filter-item"
      >
        <el-option
          v-for="item in accountStatusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        :disabled="listLoading"
        @click="handleSearch"
      >
        {{ $t('pages.sysUserManager.action.search') }}
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="clickCreateUser"
      >
        {{ $t('pages.sysUserManager.action.add') }}
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('pages.sysUserManager.loading')"
      fit
      highlight-current-row
      @cell-mouse-enter="handleMouseEnter"
    >
      <el-table-column prop="id" label="ID" align="center" />
      <el-table-column prop="loginName" :label="$t('pages.sysUserManager.table.account')" align="center" />
      <el-table-column prop="nickname" :label="$t('pages.sysUserManager.table.nickname')" align="center" />
      <el-table-column prop="nickname" :label="$t('pages.sysUserManager.table.role')" align="center">
        <template slot-scope="scope">
          {{ scope.row.userRoles | joinRolesFilter }}
        </template>
      </el-table-column>
      <el-table-column
        prop="phone"
        :label="$t('pages.sysUserManager.table.phone')"
        width="110"
        align="center"
      />
      <el-table-column prop="email" :label="$t('pages.sysUserManager.table.email')" width="110" align="center" />

      <el-table-column
        prop="createTime"
        :label="$t('pages.sysUserManager.table.createdAt')"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>

      <el-table-column prop="nickname" :label="$t('pages.sysUserManager.table.status')" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
            :active-text="$t('pages.sysUserManager.status.normal')"
            :inactive-text="$t('pages.sysUserManager.status.disabled')"
            @change="clickSwitchChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('pages.sysUserManager.table.actions')" align="center" width="150">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="clickUpdateClick(scope.row)"
          >{{ $t('pages.sysUserManager.action.edit') }}</el-button>
          <el-button type="text" size="small" @click="clickRestPwd(scope.row)">{{ $t('pages.sysUserManager.action.resetPassword') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.cursor"
      :limit.sync="listQuery.limit"
      @pagination="renderData"
    />

    <edit-user
      v-if="editUserVisible"
      :update-data="thisActiveRow"
      @close="editUserVisible = false"
      @success="editUserSuccess"
    />

  </div>
</template>

<script>
import { pageUsers, switchUserStatus, restPassword } from '@/api/ops-system'
import Pagination from '@/components/Pagination'
import EditUser from './edit-user'
export default {
  name: 'UserManager',
  components: { Pagination, EditUser },
  filters: {
    joinRolesFilter(roles) {
      if (!roles || roles.length <= 0) {
        return ''
      }
      return roles.map(role => role.roleName).join(',')
    }
  },
  data() {
    return {
      editUserVisible: false,
      activeRow: null,
      list: [],
      total: 0,
      listQuery: {
        cursor: 1,
        limit: 20,
        loginName: '',
        nickname: '',
        status: ''
      },
      listLoading: false
    }
  },
  computed: {
    thisActiveRow() {
      return this.activeRow ? Object.assign({}, this.activeRow) : null
    },
    accountStatusOptions() {
      return [
        { value: 0, label: this.$t('pages.sysUserManager.status.normal') },
        { value: 1, label: this.$t('pages.sysUserManager.status.disabled') }
      ]
    }
  },
  created() {
    this.renderData(true)
  },
  methods: {
    renderData(isClean) {
      const that = this
      that.listLoading = true
      if (isClean === true) {
        that.listQuery.cursor = 1
        that.list = []
      }
      pageUsers(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.listLoading = false
      })
    },
    clickCreateUser() {
      const that = this
      that.editUserVisible = true
      that.activeRow = null
    },
    clickUpdateClick(row) {
      const that = this
      that.editUserVisible = true
    },
    handleSearch() {
      this.renderData(true)
    },
    clickSwitchChange(row) {
      switchUserStatus(row.id, row.status)
        .then(res => {})
        .catch(er => {
          if (row.status === 0) {
            row.status = 1
          } else if (row.status === 1) {
            row.status = 0
          }
        })
    },
    clickRestPwd(row) {
      this.$confirm(this.$t('pages.sysUserManager.confirm.resetPassword', { nickname: row.nickname }), this.$t('pages.sysUserManager.confirm.title'), {
        confirmButtonText: this.$t('pages.sysUserManager.confirm.confirm'),
        cancelButtonText: this.$t('pages.sysUserManager.confirm.cancel'),
        type: 'warning'
      }).then(() => {
        restPassword(row.id).then(res => {
          this.$opsMessage.success()
        })
      }).catch(() => {
        this.$opsMessage.info(this.$t('pages.sysUserManager.message.cancelled'))
      })
    },
    handleMouseEnter(row) {
      this.activeRow = row
    },
    editUserSuccess() {
      this.editUserVisible = false
      this.renderData()
    }
  }
}
</script>
