<!--
 * @Author: your name
 * @Date: 2021-11-29 14:37:54
 * @LastEditTime: 2022-03-18 10:41:39
 * @LastEditors: your name
 * @Description: 角色管理
 * @FilePath: \Foshan4A2.0\src\views\authorityManagement\components\authorityRole.vue
-->
<template>
  <div class="authorityRoleHome">
    <!--状态和搜索-->
    <div class="authority-head flex">
      <div class="status flex-wrap">
        <div class="status1"><span class="icon"></span>存在互斥</div>
        <div class="status2"><span class="icon"></span>不存在互斥</div>
      </div>
      <Search @getSearchData="getSearchData" @clearSearchData="clearSearchData" :isSlot="true">
        <button v-is="['role_add']" @click="addRole" class="btnAdd">新增角色</button>
      </Search>
    </div>
    <!--列表-->
    <template v-if="arrs.length">
      <div class="role-content scrollbar infinite-list-wrapper"  v-infinite-scroll="load" :infinite-scroll-distance="20" infinite-scroll-disabled="disabled">
        <el-row :gutter="24">
          <el-col v-for="(item,index) in arrs" class="account-left-content-teams" :lg="{ span:'5-5' }" :key="index">
            <div class="team">
              <div :class="'status'+ item.IsDef" class="bg"></div>
              <div class="list">
                <div class="flex info">
                  <div class="info-avoutr">
                    <div class="msg flex-wrap">
                      <p :title="item.RoleName" class="name">{{item.RoleName}}</p>
                      <div v-is="['role_edit']" @click="editRole(item)" class="cap cap-edit"></div>
                    </div>
                  </div>
                  <div v-is="['role_delete']" class="info-close">
                    <i @click="checkRemove(item,index)" class="icon el-icon-close"></i>
                  </div>
                </div>
                <div class="flex-wrap time">
                  <div class="time-msg">账号数：</div>
                  <div class="time-info glr">{{item.UserCount}}</div>
                </div>
                <div class="flex-wrap ip">
                  <div class="ip-msg">权限项数：</div>
                  <div class="ip-info glr">{{item.AuthCount}}</div>
                </div>
                <div class="flex details">
                  <div class="details-msg">
                    状态：<span :class="item.Status == 1 ? 'success' : 'error'">{{item.Status == 1 ? '启用' : '停用'}}</span>
                  </div>
                  <div v-is="['role_state switch']" class="details-info">
                    <el-switch v-model="item.isauto" @change="handleChange(item)" active-color="#6F81BC">
                    </el-switch>
                  </div>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
        <template v-if="total > 1">
          <p class="center" v-if="loading">加载中...</p>
          <p class="center" v-if="noMore">没有更多数据了~</p>
        </template>
      </div>
    </template>
    <template v-else>
      <el-empty description="暂无数据"></el-empty>
    </template>
    <!--删除弹框-->
    <Dialog :flag="flag">
      <div class="airportInfoDialog">
        <div class="title">删除角色</div>
        <div class="content">是否确认删除{{ title }}？</div>
        <div class="foot right t30">
          <el-button size="medium" class="r24" @click="remove" type="danger">删除</el-button>
          <el-button size="medium" @click="flag = false">取消</el-button>
        </div>
      </div>
    </Dialog>
  </div>
</template>

<script>
import Search from '@/layout/components/Search'
import Dialog from '@/layout/components/Dialog'
import roleData from '../minixs/roleData'
import { UpdateStatus } from '@/api/apiAuthority'
export default {
  name: 'AuthorityRole',
  components: { Search, Dialog },
  mixins: [roleData],
  data () {
    return {
      arrs: [], //卡片数据
      arr: [],
      arrsCopy: [],
      flag: false,
      title: '',
      index: null,
      num: null,
      pageNum: 1,
      PageSize: 20,
      loading: false,
      keyWords: '',
      total: null,
      search: null
    }
  },
  created () {
    const { keyWords } = this.$route.query
    if (keyWords) {
      this.keyWords = keyWords
    }
    this.gueryRole({
      QueryName: this.keyWords,
      PageSize: this.PageSize,
      PageIndex: this.pageNum
    });
  },
  computed: {
    noMore () {
      return this.pageNum >= this.total
    },
    disabled () {
      return this.loading || this.noMore
    }
  },
  methods: {
    //删除
    checkRemove (item, index) {
      this.flag = true
      this.index = item
      this.title = item.RoleName
      this.num = index
    },
    //确认删除
    remove () {
      this.deleteRole(this.index.RoleId, this.num)
    },
    //角色新增
    addRole () {
      this.$router.push('/role/addRole')
    },
    //角色编辑
    editRole (item) {
      this.$router.push({ path: '/role/editRole', query: { RoleId: item.RoleId, Status: item.Status } })
    },
    //查询
    getSearchData (val) {
      if (val) {
        this.$router.replace({ path: '/role', query: { keyWords: val } })
      } else if (this.$route.query) {
        this.$router.replace('/role')
      }
      this.arrs = []
      this.dataList = []
      this.pageNum = 1
      this.keyWords = val
      this.gueryRole({
        QueryName: val,
        PageSize: this.PageSize,
        PageIndex: this.pageNum
      })
    },
    //清除查询
    clearSearchData () {
      this.$router.replace('/role')
      this.arrs = []
      this.dataList = []
      this.pageNum = 1
      this.keyWords = ''
      this.gueryRole({
        QueryName: this.keyWords,
        PageSize: this.PageSize,
        PageIndex: this.pageNum
      })
    },
    //状态变更
    async handleChange (item) {
      try {
        const { RoleId, isauto } = item
        const Status = isauto ? 1 : 0
        const res = await UpdateStatus({
          RoleId: RoleId,
          RoleStatus: Status
        })
        if (res.code === 0) {
          this.$message.success(res.message)
          this.arrs.forEach(item => {
            if (item.RoleId == RoleId) {
              item.isauto = isauto
              item.Status = Status
            }
          })
        } else {
          this.$message.error(res.message)
        }
      } catch (error) {
        console.log('出错了', error)
      }
    },
    //滚动加载数据
    load () {
      this.pageNum += 1
      this.gueryRole({
        QueryName: this.keyWords,
        PageSize: this.PageSize,
        PageIndex: this.pageNum
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.authorityRoleHome {
  padding: 0 64px;
  padding-top: 32px;
}
.authority-head {
  line-height: 48px;
  .status {
    & > div {
      margin-right: 28px;
      .icon {
        width: 16px;
        height: 16px;
        background: #6f81bc;
        border-radius: 2px;
        display: inline-block;
        vertical-align: middle;
        margin-right: 10px;
        position: relative;
        top: -2px;
      }
      &:last-child {
        margin-right: 0;
      }
    }
    .status2 {
      .icon {
        background: #cfd6e2;
      }
    }
  }
}
.role-content {
  margin-top: 32px;
  height: 72vh;
  overflow-y: auto;
  overflow-x: hidden;
  > p {
    margin: 0;
  }
  .el-col-lg-5-5 {
    width: 20%;
  }
  .account-left-content-teams {
    position: relative;
    margin-bottom: 24px;
    .info-close {
      cursor: pointer;
      .icon {
        color: #303133;
        font-weight: 600;
      }
    }
    .list {
      .info {
        line-height: normal;
        margin-bottom: 19px;
        .name {
          font-size: 16px;
          font-weight: bold;
        }
        .cap {
          width: 24px;
          height: 24px;
          display: inline-block;
          background-repeat: no-repeat;
          background-size: cover;
          transition: all 0.3s;
          margin-left: 16px;
          cursor: pointer;
          &:first-child {
            margin-right: 0;
          }
        }
        .cap-plus {
          background-image: url("../../../assets/status/ic_plus.png");
          &:hover {
            background-image: url("../../../assets/status/ic_plus_hovar.png");
          }
        }
        .cap-edit {
          background-image: url("../../../assets/status/ic_edit.png");
          &:hover {
            background-image: url("../../../assets/status/ic_edit_hovar.png");
          }
        }
        .cap-sub {
          background-image: url("../../../assets/status/ic_subordinate.png");
          &:hover {
            background-image: url("../../../assets/status/ic_subordinate_hovar.png");
          }
        }
      }
    }
    .team {
      background: #fff;
      border-radius: 5%;
      box-shadow: 0px 6px 7px 0px rgba(0, 0, 0, 0.06);
      overflow: hidden;
      .bg {
        height: 4px;
        background: #6f81bc;
      }
      .status1 {
        background: #6f81bc;
      }
      .status2 {
        background: #cfd6e2;
      }
    }
    .list {
      padding: 31px 24px 22px 32px;
      position: relative;
      min-width: 264px;
      .info {
        margin-bottom: 19px;
        .info-avoutr {
          display: flex;
          .avoutr {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #303133;
            img {
              max-width: 100%;
            }
          }
          .msg {
            p {
              margin: 0;
              padding: 0;
              height: 20px;
              line-height: 20px;
            }
            .name {
              font-weight: bold;
              color: #303133;
              font-size: 18px;
              max-width: 180px;
              white-space: nowrap;
              text-overflow: ellipsis;
              overflow: hidden;
            }
            .cap {
              position: relative;
              top: -3px;
            }
            .group {
              font-size: 14px;
              font-family: Microsoft YaHei;
              font-weight: 400;
              color: #303133;
            }
          }
        }
        .icon {
          font-size: 16px;
          cursor: pointer;
        }
      }
      .time,
      .ip {
        height: 16px;
        line-height: 16px;
        font-size: 16px;
        .glr {
          color: #909399;
        }
      }
      .ip {
        margin-top: 14px;
        margin-bottom: 14px;
      }
      .details {
        height: 24px;
        line-height: 24px;
        .success {
          color: #6f80bc;
        }
        .error {
          color: #f25555;
        }
      }
    }
  }
}
</style>
