<template>
  <a-card size="small" :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="15">
          <a-col :md="6" :sm="24">
            <a-form-item label="用户名称">
              <a-input allowClear v-model="queryParam.name" placeholder="请输入用户名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="手机号码">
              <a-input allowClear v-model="queryParam.phone" placeholder="请输入手机号码"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="状态">
              <v-select code="CHECK_ENABLE_STATE" v-model="queryParam.loginFlag" allowClear placeholder="请选择状态"></v-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin-right: 10px;">查询</a-button>
            <a-button type="" @click="reset" id="roleList-reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button v-if="$hasPermissions('B_powerMD_user_add')" type="primary" class="button-error" @click="openModal">新增</a-button>
    </div>
    <s-table
      ref="table"
      size="default"
      rowKey="id"
      :columns="columns"
      :data="loadData"
      style="word-break: break-all;"
      bordered
    >
      <span slot="status" slot-scope="text, record">
        <a-switch v-if="$hasPermissions('B_powerMD_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-icon
          type="edit"
          title="编辑"
          :style="{fontSize: '20px',color:' #1890FF',padding:' 0 10px'}"
          v-if="$hasPermissions('B_powerMD_user_edit')"
          @click="handleEdit(record)"
        />
        <a-icon
          type="unlock"
          title="重置密码"
          v-if="record.loginFlag==1 && $hasPermissions('B_powerMD_user_restPwd')"
          :style="{fontSize: '20px',color:' #ffaa00',padding: '0 10px'}"
          @click="resetPassword(record)" />
        <a-icon
          type="delete"
          title="删除"
          :style="{fontSize: '20px',color: '#f00',padding: '0 10px'}"
          v-if="record.loginFlag==0 && $hasPermissions('B_powerMD_user_del')"
          @click="delect(record)"/>
      </span>
    </s-table>
    <userModal :visible="showModal" @refresh="$refs.table.refresh(true)" :data="itemData" @close="showModal = false"></userModal>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import userModal from './userModal.vue'
import { resetPSD, updateEnableStatus, getPowerUserList, delectUserPower } from '@/api/power-user.js'
export default {
  name: 'UserList',
  components: {
    STable, VSelect, userModal
  },
  data () {
    return {
      // 查询参数
      queryParam: {
        name: '',
        phone: '',
        loginFlag: ''
      },
      showModal: false,
      itemData: {}, // 编辑行数据
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'no',
          width: 60,
          align: 'center'
        },
        {
		  title: '创建时间',
          dataIndex: 'createDate',
          width: 150,
          align: 'center'
        },
        {
          title: '用户名称',
          dataIndex: 'name',
          width: 150,
          align: 'center'
        },
        {
          title: '手机号码',
          dataIndex: 'phone',
          width: 150,
          align: 'center'
        },
        {
          title: '角色',
          width: 200,
          align: 'center',
          dataIndex: 'roleNames'
        },
        {
          title: '状态',
          width: 150,
          align: 'center',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          width: 150,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getPowerUserList(Object.assign(parameter, this.queryParam)).then(res => {
          const no = (res.data.pageNo - 1) * res.data.pageSize
          for (let i = 0; i < res.data.list.length; i++) {
            const _item = res.data.list[i]
            _item.no = no + i + 1
            _item.loginFlag = _item.loginFlag + '' === '1'
          }
          return res.data
        })
      },
      optionAlertShow: false
    }
  },
  methods: {
    // 刷新列表
    refreshTable () {
      this.$refs.table.refresh(true)
    },
    // 新增
    openModal () {
      this.showModal = true
      this.itemData = {}
    },
    // 重置
    reset () {
      this.queryParam.name = ''
      this.queryParam.phone = ''
      this.queryParam.loginFlag = ''
      this.refreshTable()
    },
    // 重置密码
    resetPassword (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认重置密码吗？',
        centered: true,
        onOk () {
          resetPSD({
            id: row.id
          }).then(res => {
            if (res.status == 200) {
              this.$message.success(res.message)
            } else {
              this.$message.error(res.message)
            }
          }).catch(() => {
            this.$message.success(res.message)
          })
        }
      })
    },
    // 删除
    delect (row) {
      const _this = this
      this.$confirm({
        title: '警告',
        content: '删除后无法恢复，确认删除?',
        centered: true,
        onOk () {
          delectUserPower({
            id: row.id
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            } else {
              _this.$message.error(res.message)
            }
          }).catch(() => {
            _this.$message.success(res.message)
          })
        }
      })
    },
    getBizStr (storeBizList) {
      const str = []
      storeBizList.forEach(item => {
        str.push(item.bizTypeName)
      })
      return str.join(',')
    },
    handleEdit (row) {
      console.log(row, 'row')
      this.showModal = true
      this.itemData = row
    },
    // 修改状态
    changeFlagHandle (text, record) {
      const _data = {
        id: record.id,
        flag: record.loginFlag ? '1' : '0'
      }
      updateEnableStatus(_data).then(res => {
        if (res.status + '' === '200') {
          this.$message.success(res.message)
        } else {
          record.loginFlag = !record.loginFlag
        }
      })
    }
  }
}
</script>
<style scoped>
  .action-button{
    line-height: 40px;
    white-space: nowrap;
    padding: 5px 10px;
    background-color: #1890ff;
    border-radius: 4px;
    color: #fff;
    margin-right: 5px;
  }
  .red-text{
    background-color: red;
  }
  .reset-text {
    background-color: #31b50b;
  }
</style>
