<template>
      <div class="page">
        <el-form size="small" :inline="true" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
          <el-form-item prop="loginName">
            <el-input size="small" v-model="searchForm.loginName" placeholder="登录名" clearable></el-input>
          </el-form-item>
          <el-form-item prop="name">
            <el-input size="small" v-model="searchForm.name" placeholder="姓名" clearable></el-input>
          </el-form-item>
        <el-form-item>
          <el-button  type="primary" @click="refreshList()" size="small" icon="el-icon-search">查询</el-button>
          <el-button @click="resetSearch()" size="small" icon="el-icon-refresh-right">重置</el-button>
        </el-form-item>
        </el-form>
      <div class="bg-white top" style="padding-top: 0">
          <vxe-toolbar   :refresh="{query: refreshList}" >
              <template #buttons>
              </template>
          </vxe-toolbar>
          <div style="height: calc(100% - 80px);">
              <vxe-table
                  border="inner"
                  auto-resize
                  resizable
                  height="auto"
                  :loading="loading"
                  size="small"
                  ref="certTable"
                  show-header-overflow
                  show-overflow
                  highlight-hover-row
                  :print-config="{}"
                  :import-config="{}"
                  :export-config="{}"
                  @sort-change="sortChangeHandle"
                  :sort-config="{remote:true}"
                  :data="dataList"
                  >
                <vxe-column type="seq" width="60" title="序号"></vxe-column>
                <vxe-column type="checkbox"  width="40px"></vxe-column>
                <vxe-column  title="头像" field="photo">
                    <template slot-scope="scope">
                      <img :src="scope.row.photo === ''?'/static/img/avatar.png':scope.row.photo" style="height:35px"/>
                    </template>
                </vxe-column>
                <vxe-column  title="登录名" field="loginName" sortable>
                  <template slot-scope="scope">
                    <el-link  type="primary" :underline="false" v-if="hasPermission('sys:user:edit')" @click="edit(scope.row.id)">{{scope.row.loginName}}</el-link>
                    <el-link  type="primary" :underline="false" v-else-if="hasPermission('sys:user:view')"  @click="view(scope.row.id,)">{{scope.row.loginName}}</el-link>
                    <span v-else>{{scope.row.loginName}}</span>
                  </template>
                </vxe-column>

                <vxe-column  title="姓名" field="name" sortable></vxe-column>
                <vxe-column  title="手机号" field="mobile" sortable></vxe-column>
                <vxe-column  title="集团" field="companyDTO.name" sortable>
                  <template slot-scope="scope">
                    <el-tag>{{scope.row.companyDTO && scope.row.companyDTO.name}}</el-tag>
                  </template>
                </vxe-column>
                <vxe-column  title="部门" field="officeDTO.name" sortable>
                  <template slot-scope="scope">
                    <el-tag>{{scope.row.officeDTO && scope.row.officeDTO.name}}</el-tag>
                  </template>
                </vxe-column>
                <vxe-column  title="状态" field="loginFlag" sortable>
                  <template slot-scope="scope">
                    <el-tag v-if="scope.row.loginFlag === '1'" size="small" type="success">正常</el-tag>
                    <el-tag v-else-if="scope.row.loginFlag === '0'" size="small" type="danger">禁用</el-tag>
                  </template>
                </vxe-column>
                <vxe-column title="操作" width="100" fixed="right" align="center">
                  <template slot-scope="scope">
                    <el-button v-if="hasPermission('sys:user:edit')" type="text" size="small" icon="el-icon-edit" @click="edit(scope.row.id)">编辑</el-button>
                  </template>
                </vxe-column>
              </vxe-table>
              <vxe-pager
                background
                size="small"
                :current-page="tablePage.currentPage"
                :page-size="tablePage.pageSize"
                :total="tablePage.total"
                :page-sizes="[10, 20, 100, 1000, {label: '全量数据', value: 1000000}]"
                :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
                @page-change="currentChangeHandle">
              </vxe-pager>
          </div>
      <!-- 弹窗, 新增 / 修改 -->
      <cert-form  ref="certForm" @refreshDataList="refreshList"></cert-form>
      </div>
    </div>
</template>

<script>
  import CertForm from './CertForm'
  import UserService from '@/api/sys/UserService'
  import OfficeService from '@/api/sys/OfficeService'

  // import XEAjax from 'xe-ajax'
  // import XEUtils from 'xe-utils'

  export default {
    data () {
      return {
        searchForm: {
          loginName: '',
          name: '',
          companyDTO: {
            id: ''
          },
          officeDTO: {
            id: ''
          }
        },
        dataList: [],
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        loading: false
      }
    },
    components: {
      CertForm
    },
    userService: null,
    officeService: null,
    created () {
      this.userService = new UserService()
      this.officeService = new OfficeService()
    },
    activated () {
      this.refreshList()
    },
    watch: {
    },
    methods: {
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.userService.list({
          'current': this.tablePage.currentPage,
          'size': this.tablePage.pageSize,
          'orders': this.tablePage.orders,
          ...this.searchForm
        }).then(({data}) => {
          this.dataList = data.records
          this.tablePage.total = data.total
          this.loading = false
        })
      },
      // 当前页
      currentChangeHandle ({ currentPage, pageSize }) {
        this.tablePage.currentPage = currentPage
        this.tablePage.pageSize = pageSize
        this.refreshList()
      },
      // 排序
      sortChangeHandle (column) {
        this.tablePage.orders = []
        if (column.order != null) {
          if (column.property === 'officeDTO.name') {
            column.property = 'o.name'
          }
          if (column.property === 'companyDTO.name') {
            column.property = 'c.name'
          }
          if (column.property === 'postDTO.name') {
            column.property = 'p.name'
          }
          this.tablePage.orders.push({column: this.$utils.toLine(column.property), asc: column.order === 'asc'})
        }
        this.refreshList()
      },
      // 修改
      edit (id) {
        id = id || this.$refs.certTable.getCheckboxRecords().map(item => {
          return item.id
        })[0]
        this.$refs.certForm.init('edit', id)
      },
      // 查看
      view (id) {
        this.$refs.certForm.init('view', id)
      },
      resetSearch () {
        this.searchForm.companyDTO.id = ''
        this.searchForm.officeDTO.id = ''
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
    }
  }
</script>
<style lang="scss">
.el-card__body {
    overflow: auto;
}
</style>
