<!--
 * @Author: your name
 * @Date: 2021-11-29 14:37:54
 * @LastEditTime: 2022-03-03 10:29:05
 * @LastEditors: Please set LastEditors
 * @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="manageTitle">角色管理</div>
        <!-- <div class="status1"><span class="icon"></span>存在互斥</div>
        <div class="status2"><span class="icon"></span>不存在互斥</div> -->
      </div>
      <Search @getSearchData="getSearchData" @clearSearchData="clearSearchData" :isTitle="false" :isSlot="true">
        <button @click="addRole" class="btnAdd">新增</button>
      </Search>
    </div>
    <!--列表-->
    <div class="role-content scrollbar infinite-list-wrapper">
      <template v-if="arrs.length">
        <el-row v-infinite-scroll="load" :infinite-scroll-distance="20" infinite-scroll-disabled="disabled" :gutter="24">
          <el-col v-for="(item, index) in arrs" class="account-left-content-teams" :lg="4" :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.role_name" class="name">{{ item.role_name }}</p>
                      <div @click="editRole(item)" class="cap cap-edit"></div>
                      <div @click="authRole(item)" class="cap cap-auth"></div>
                    </div>
                  </div>
                  <div 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="#2D67E3">
                    </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>
      </template>
      <template v-else>
        <NoData />
      </template>
    </div>
    <!--删除弹框-->
    <Dialog :flag="flag">
      <div class="airportInfoDialog">
        <div class="title del-title">删除角色</div>
        <div class="content del-content">
          <span class="el-icon-error error r10"></span>您是否确认删除<span class="error l10">{{ title }}</span>
          ？
        </div>
        <div class="foot right Delfoot 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>
    <!--新增/编辑-->
    <Dialog :flag="editDialogVisible" width="508px" :show-flag="true">
      <div class="airportInfoDialog dialog-public-background">
        <div class="title">{{ editDialogTitle }}</div>
        <div class="content">
          <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="130px" class="demo-ruleForm">
            <el-form-item label="角色名称" prop="role_name">
              <el-input v-model="ruleForm.role_name" size="medium" placeholder="请输入节点名称（必填）" />
            </el-form-item>
            <el-form-item label="角色描述" prop="role_comment">
              <el-input v-model="ruleForm.role_comment" size="medium" type="textarea" :rows="3" placeholder="请输入跟踪节点描述" />
            </el-form-item>
          </el-form>
        </div>
        <div class="foot center t30">
          <el-button size="medium" type="primary" class="r25 r26" @click="submitClickHandler()">提交</el-button>
          <el-button size="medium" class="r26" @click="resetForm('ruleForm')">取消</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 NoData from '@/components/nodata';
import { Query, GeneralDataReception } from "@/api/dataIntegration";
export default {
  name: "AuthorityRole",
  components: { Search, Dialog, NoData },
  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,
      editDialogVisible: false,
      editDialogTitle: "新增角色",
      editType: "add",
      ruleForm: {
        // 协议信息表单
        role_name: "",
        role_comment: "",
      },
      rules: {
        // 协议信息表单验证
        role_name: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
      },
      ruleFormObj: {},
      roleId: "",
      needPage: 1,
    };
  },
  created () {
    if (this.$route.query.keyWords) {
      keyWords = this.$route.query.keyWords;
      this.keyWords = keyWords;
    }
    this.queryData();
  },
  computed: {
    noMore () {
      return this.pageNum >= this.total;
    },
    disabled () {
      return this.loading || this.noMore;
    },
  },
  methods: {
    queryData () {
      let data = {
        id: DATACONTENT_ID.roleTableId,
        dataContent: [this.keyWords],
        needPage: this.needPage,
      };
      this.gueryRole(data);
    },
    //删除
    checkRemove (item, index) {
      this.flag = true;
      this.index = item;
      this.title = item.role_name;
      this.num = index;
    },
    //确认删除
    remove () {
      this.index.event = 3;
      this.editRoles(this.index);
    },
    //角色新增
    addRole () {
      this.editDialogTitle = "新增角色";
      this.editDialogVisible = true;
      this.editType = "add";
      // this.$router.push('/role/addRole')
    },
    //角色编辑
    editRole (item) {
      this.editDialogVisible = true;
      this.editType = "edit";
      this.editDialogTitle = "编辑角色";
      this.roleId = item.role_id;
      this.ruleForm = JSON.parse(JSON.stringify(item));
    },
    pageInit () {
      this.needPage = 1
      this.arrs = []
      this.queryData()
    },
    //新增角色
    async editRoles (params) {
      try {
        const res = await GeneralDataReception({
          serviceId: SERVICE_ID.roleMsgId,
          dataContent: JSON.stringify(params),
        });
        if (res.code === "0") {
          this.flag = false;
          this.$message.success(res.message);
          this.pageInit();
        } else {
          this.flag = false;
          this.$message.error(res.message);
        }
      } catch (error) {
        this.flag = false;
        console.log("出错了", error);
      }
    },
    // 角色授权
    authRole (item) {
      this.$router.push({
        path: "/role/editRole",
        query: { RoleId: item.role_id },
      });
    },
    //查询
    getSearchData (val) {
      this.arrs = [];
      this.needPage = 1;
      this.keyWords = val;
      this.queryData()
    },
    //清除查询
    clearSearchData () {
      this.arrs = [];
      this.needPage = 1;
      this.keyWords = "";
      this.queryData()
    },
    // 新增/编辑-确认
    submitClickHandler () {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          if (this.editType == "add") {
            this.ruleForm.event = 1;
            this.editRoles(this.ruleForm);
          } else {
            this.ruleForm.event = 2;
            this.editRoles(this.ruleForm);
          }
          this.resetForm("ruleForm");
        } else {
          return false;
        }
      });
    },
    // 重置
    resetForm (formName) {
      this.ruleForm = [];
      this.$refs[formName].resetFields();
      this.editDialogVisible = false;
    },
    //滚动加载数据
    load () {
      this.needPage += 1
      this.queryData()
    },
  },
};
</script>

<style lang="scss" scoped>
.authorityRoleHome {
  // padding: 0 64px;
  padding: 16px 32px 32px 32px;
}
.authority-head {
  line-height: 32px;
  .status {
    & > div {
      .icon {
        width: 14px;
        height: 14px;
        background: #2d67e3;
        border-radius: 2px;
        display: inline-block;
        vertical-align: middle;
        margin-right: 10px;
        position: relative;
        top: -2px;
      }
      &:last-child {
        margin-right: 0;
      }
    }
    .status1 {
      font-size: 14px;
      margin-right: 28px;
    }
    .status2 {
      font-size: 14px;
      .icon {
        background: #afb4bf;
      }
    }
  }
}
.role-content {
  margin-top: 24px;
  height: 72vh;
  overflow-y: auto;
  overflow-x: hidden;
  .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: 14px;
          height: 14px;
          display: inline-block;
          background-repeat: no-repeat;
          background-size: cover;
          transition: all 0.3s;
          margin-left: 24px;
          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_default.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");
          }
        }
        .cap-auth {
          margin-left: 12px;
          background-image: url("../../../assets/status/ic_edit_authorization.png");
          &:hover {
            background-image: url("../../../assets/status/ic_edit_authorization_hover.png");
          }
        }
      }
    }
    .team {
      background: #fff;
      border-radius: 4px;
      box-shadow: 0px 3px 3px 0px rgba(0, 0, 0, 0.06);
      overflow: hidden;
      .bg {
        height: 4px;
        background: #2d67e3;
      }
      .status1 {
        background: #2d67e3;
      }
      .status2 {
        background: #afb4bf;
      }
    }
    .list {
      padding: 24px;
      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: 16px;
              max-width: 180px;
              white-space: nowrap;
              text-overflow: ellipsis;
              overflow: hidden;
            }
            .cap {
              position: relative;
              top: 2px;
            }
            .group {
              font-size: 14px;
              font-family: Microsoft YaHei;
              font-weight: 400;
              color: #303133;
            }
          }
        }
        .icon {
          font-size: 14px;
          cursor: pointer;
        }
      }
      .time,
      .details,
      .ip {
        height: 14px;
        line-height: 14px;
        font-size: 14px;
        .glr {
          color: #909399;
        }
      }
      .ip {
        margin-top: 14px;
        margin-bottom: 14px;
      }
      .details {
        height: 24px;
        line-height: 24px;
        .success {
          color: #2d67e3;
        }
        .error {
          color: #afb4bf;
        }
      }
    }
  }
}
</style>
