<!--  -->
<template>
  <div>
    <main-top :crumb="crumbData"></main-top>
    <el-dialog
      title="用户列表"
      :visible.sync="userListvisible"
      width="30%"
      center>
      <ul v-if="userList.length>0">
        <li style="line-height:1.5" v-for="(item,i) in userList" :key="i">
          {{item}}
        </li>
      </ul>
      <div v-else>暂无用户</div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="userListvisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <div class="table-interface">
      <div class="top-body">
        <div class="info-top">
          <div class="info-left"></div>
          <div class="info-right">
            <el-button type="primary" v-if="authority['新增']" @click="$router.push({name:'edit-role',params:{type:0,id:'none'}})">新增角色</el-button>
          </div>
        </div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column
            v-for="(item, idx) in data"
            :key="idx"
            :prop="item.prop"
            :label="item.label"
          >
          <template slot-scope="scope">
            <span class="o-span" @click="showList(scope.row)" v-if="item.prop==='userName'">
              查看
              <!-- <li v-for="(sub,i) in scope.row[item.prop]" :key="i">{{sub}}</li> -->
            </span>

            <span v-else>{{scope.row[item.prop]}}</span>
          </template>
          </el-table-column>
          <el-table-column label="操作">
            <template  slot-scope="scope">
              <span class="o-span" @click="view(scope.row)" v-if="scope.row.roleKey === 'admin'">查看</span>
              <template v-else>
                <span class="o-span"  v-if="authority['修改']" @click="goto(scope.row)">修改</span>
                <span class="o-span"  v-if="authority['启/停用']" @click="changeState(scope.row)">{{scope.row.state?'启用':'停用'}}</span>
              </template>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
import MainTop from '@/components/main-top'
const crumbData = [
  {
    name: '角色管理',
    id: 9
  }
]

let zt = {
  0: '启用',
  1: '停用'
}

let urlType = {
  add: '/user/addUser',
  edit: '/user/updateUser'
}
export default {
  // import引入的组件需要注入到对象中才能使用
  components: {
    MainTop
  },
  data () {
    // 这里存放数据
    let data = [
      {
        prop: 'idx',
        label: '序号'
      },
      {
        prop: 'roleName',
        label: '角色名称'
      },
      {
        prop: 'description',
        label: '角色说明'
      },
      {
        prop: 'userName',
        label: '用户列表'
      },
      {
        prop: 'qiyong',
        label: '状态'
      }
    ]

    return {
      userListvisible: false,
      userList: [],
      crumbData,
      data,
      authority: {},
      infoName: '',
      tableData: []
    }
  },
  watch: {
    currentPage () {
      this.refresh()
    },
    size () {
      this.refresh()
    },
    region () {
      this.refresh()
    },
    inputKey () {
      this.refresh()
    },
    type (newVal) {
      this.editTitle = newVal === 'add' ? '新增用户' : '修改用户信息'
    }
  },
  mounted () {
    if (!window.localStorage.getItem('currentItem')) {
      return
    } else {
      let currentItem = JSON.parse(window.localStorage.getItem('currentItem'))
      let auth = ''
      if (currentItem.children) {
        auth = currentItem.children.find(item => item.name === '角色管理')
      }
      auth && auth.children.forEach(item => {
        this.authority[item.name] = item.authority
      })
      console.log(this.authority)
    }
    this.refresh()
  },
  methods: {
    showList (item) {
      this.userListvisible = !this.userListvisible
      this.userList = item.userName
      console.log(item)
    },
    goto (item) {
      this.$router.push({ name: 'edit-role', params: { type: 1, id: item.id } })
    },
    view (item) {
      this.$router.push({ name: 'edit-role', params: { type: 2, id: item.id } })
    },
    reset () {
      let data = {
        id: this.form.id
      }
      this.$confirm('此操作将重置该用户密码, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.post('/user/resetPassword', data).then(res => {
          if (res.code === 0) {
            this.$alert('重置成功', '提示', {
              confirmButtonText: '确定',
              callback: action => {
              }
            })
          } else {
            this.$notify.error({
              title: '错误',
              message: res.msg
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    refresh () {
      this.loading = true
      this.getInformation()
      this.loading = false
    },
    handleCurrentChange (val) {
      this.currentPage = val
    },
    del (item) {
      let data = {
        id: item.id
      }
      this.$confirm('此操作将注销并删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.post('/user/deleteUser', data).then(res => {
          if (res.code === 0) {
            this.$alert('注销成功', '提示', {
              confirmButtonText: '确定',
              callback: action => {
                this.refresh()
              }
            })
          } else {
            this.$notify.error({
              title: '错误',
              message: res.msg
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    save () {
      let {userName, roleId, phone, head, state} = this.form
      let data = {userName, roleId, phone, head, state}

      this.$http.post(urlType[this.type], data).then(res => {
        if (res.code === 0) {
          this.$alert('操作成功', '提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false
              this.refresh()
            }
          })
        } else {
          this.$notify.error({
            title: '错误',
            message: res.message
          })
        }
      })
    },

    show (item = '', type) {
      this.dialogFormVisible = true
      this.type = type
      this.form = {
        userName: '',
        roleId: 0,
        phone: '',
        head: '',
        password: '',
        repeatPassword: ''
      }

      if (type === 'edit') {
        this.form = item
      }
    },
    changeState (item) {
      let state = item.state === 1 ? 0 : 1
      let data = {
        id: item.id,
        state
      }

      this.$confirm('此操作将导致用户状态发生改变, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.post('/role/updateState', data).then(res => {
          if (res.code === 0) {
            this.$alert('修改成功', '提示', {
              confirmButtonText: '确定',
              callback: action => {
                this.refresh()
              }
            })
          } else {
            this.$notify.error({
              title: '错误',
              message: res.msg
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },

    async getInformation () {
      let params = {
        state: this.region,
        searchKey: this.inputKey,
        pageNum: this.currentPage,
        pageSize: this.size
      }

      let result = await this.$http({
        method: 'post',
        data: params,
        url: '/role/list'
      })

      if (result.code !== 0) {
        return
      }
      this.tableData = result.data.list
      this.total = result.data.total
      this.tableData.forEach((item, i) => {
        item['qiyong'] = zt[item['state']]
        item['idx'] = i + 1
      })
    }
  }
}
</script>

<style scoped>
/* 引入公共css类 */
@import url(./style);
</style>
