<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" id="userList-form" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="用户名称">
                <a-input allowClear id="userList-name" v-model.trim="queryParam.name" placeholder="请输入用户名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="手机号码">
                <a-input allowClear id="userList-phone" v-model.trim="queryParam.phone" placeholder="请输入手机号码"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="状态">
                <v-select code="ENABLE_FLAG" id="userList-loginFlag" v-model="queryParam.loginFlag" allowClear placeholder="请选择状态"></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-button type="primary" id="userList-refresh" @click="$refs.table.refresh(true)" style="margin-right: 10px;">查询</a-button>
              <a-button type="" @click="reset" id="userList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 操作按钮 -->
        <div class="table-operator">
          <a-button type="primary" id="userList-add" v-if="$hasPermissions('B_power_user_add')" @click="openModal">新增</a-button>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          size="small"
          :style="{ height: tableHeight+70+'px' }"
          rowKey="id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          style="word-break: break-all;"
          :defaultLoadData="false"
          bordered
        >
          <span slot="status" slot-scope="text, record">
            <!-- 超级管理员不可删除不可编辑不可启禁用 -->
            <a-switch
              :id="'userList-loginFlag-btn-'+record.id"
              v-if="record.superAdmin!=1 && $hasPermissions('B_power_user_enable')"
              checkedChildren="启用"
              unCheckedChildren="禁用"
              v-model="record.loginFlag"
              @change="changeFlagHandle(text, record)"/>
            <span v-else>{{ record.loginFlag==1?'已启用':'已禁用' }}</span>
          </span>
          <span slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              :id="'userList-edit-btn-'+record.id"
              class="button-info"
              v-if="record.superAdmin!=1 &&$hasPermissions('B_power_user_edit')"
              @click="handleEdit(record)"
            >
              编辑
            </a-button>
            <a-button
              size="small"
              type="link"
              class="button-info"
              :id="'userList-resetPsd-btn-'+record.id"
              v-if="record.loginFlag==1 && record.superAdmin!=1 &&$hasPermissions('B_power_user_resetPsd')"
              @click="resetPassword(record)"
            >
              重置密码
            </a-button>

            <a-button
              size="small"
              type="link"
              class="button-error"
              :id="'userList-del-btn-'+record.id"
              v-if="record.loginFlag==0 && record.superAdmin!=1 &&$hasPermissions('B_power_user_del')"
              @click="delect(record)"
            >
              删除
            </a-button>
          </span>
        </s-table>
      </a-spin>
      <userModal :visible="showModal" @refresh="$refs.table.refresh(true)" :nowData="itemData" @close="closeModal"></userModal>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import userModal from './userModal.vue'
// 接口
import { resetPSD, updateEnableStatus, getPowerUserList, delectUserPower } from '@/api/power-user.js'
export default {
  name: 'PowerUserList',
  mixins: [commonMixin],
  components: {
    STable, VSelect, userModal
  },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      // 查询参数
      queryParam: {
        name: '', // 用户名称
        phone: '', // 手机号码
        loginFlag: undefined// 状态
      },
      showModal: false, // 新增用户弹窗
      itemData: null, // 编辑行数据
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '用户名称', dataIndex: 'name', align: 'center', width: '23%', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '手机号码', dataIndex: 'phone', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '用户账号', dataIndex: 'loginName', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        // { title: '所属分区', dataIndex: 'allSubareaName', width: '8%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '角色', dataIndex: 'roleNames', width: '23%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '状态', width: '6%', align: 'center', scopedSlots: { customRender: 'status' } },
        { title: '操作', width: '10%', align: 'center', scopedSlots: { customRender: 'action' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        // 获取用户数据  有分页
        return getPowerUserList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            // 计算表格序号
            const no = (data.pageNo - 1) * data.pageSize
            for (let i = 0; i < data.list.length; i++) {
              const _item = data.list[i]
              _item.no = no + i + 1
              _item.loginFlag = _item.loginFlag + '' === '1'
            }
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    // 新增
    openModal () {
      this.showModal = true
      this.itemData = null
    },
    // 关闭新增用户弹窗
    closeModal () {
      this.showModal = false
      this.itemData = null
    },
    // 重置
    reset () {
      this.queryParam.name = ''
      this.queryParam.phone = ''
      this.queryParam.loginFlag = undefined
      this.$refs.table.refresh(true)
    },
    // 重置密码
    resetPassword (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认重置密码吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          resetPSD({
            id: row.id
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    // 删除
    delect (row) {
      const _this = this
      this.$confirm({
        title: '警告',
        content: '删除后无法恢复，确认删除?',
        centered: true,
        onOk () {
          _this.spinning = true
          delectUserPower({
            id: row.id
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.$message.error(res.message)
              _this.spinning = false
            }
          })
        }
      })
    },
    // 编辑
    handleEdit (row) {
      this.showModal = true
      this.itemData = row
    },
    // 修改状态
    changeFlagHandle (text, record) {
      const _data = {
        id: record.id,
        flag: record.loginFlag ? '1' : '0'
      }
      this.spinning = true
      updateEnableStatus(_data).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
          this.$refs.table.refresh()
          this.spinning = false
        } else {
          this.$refs.table.refresh()
          this.spinning = false
        }
      })
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 计算列表高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 240
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.reset()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.reset()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  }
}
</script>
