<template>
  <div class="bf-rolelist">
    <!-- 角色列表 -->
    <div class="cont">
      <div v-if="title" class="paren_header">
        <p class="manageTitle">{{ title }}</p>
      </div>
      <template v-if="dataList.length">
        <div :class="active ? 'bgActive' : 'bgActivecheckbox'" class="paren_content">
          <template v-if="!type">
            <el-row v-infinite-scroll="load" :infinite-scroll-distance="20" infinite-scroll-disabled="disabled" class="scCont scrollbar" :gutter="16">
              <el-checkbox-group @change="checkChange" v-model="checkList">
                <el-col :span="number" v-for="(item, index) in dataList" :key="index">
                  <div @click="handleBg(item, index)" :class="active && msg === index ? 'bgColor' : ''" class="cide">
                    <div class="cide_header">
                      <p :title="item.name">{{ item.name }}</p>
                      <el-checkbox :label="index"></el-checkbox>
                    </div>
                  </div>
                </el-col>
              </el-checkbox-group>
            </el-row>
          </template>
          <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-if="mainData.length">
        <div :class="active ? 'bgActive' : 'bgActivecheckbox'" class="paren_content">
          <template v-if="type">
            <el-row :gutter="16">
              <el-radio-group style="display: block" @change="radioChange" v-model="radio">
                <el-col :span="number" v-for="(item, index) in mainData" :key="index">
                  <div @click.stop="handleBg(index)" :class="active && msg === index ? 'bgColor' : ''" class="cide">
                    <div class="cide_header">
                      <p>{{ item.name }}</p>
                      <el-radio :label="index"></el-radio>
                    </div>
                  </div>
                </el-col>
              </el-radio-group>
            </el-row>
          </template>
        </div>
      </template>
      <template v-else>
        <NoData />
      </template>
    </div>
  </div>
</template>
<script>
import { GetAccountList } from '@/api/Account'
import { GetRoleByGroup } from '@/api/AccountGroup'
import { QueryRole } from '@/api/apiAuthority'
import NoData from "@/components/nodata"
export default {
  props: {
    title: {
      type: String,
      default: ''
    },
    type: {
      type: Boolean,
      default: false
    },
    number: {
      type: Number,
      default: 3
    },
    active: {
      type: Boolean,
      default: false
    },
    checkBoxList: {
      type: Array,
      default: () => []
    },
    mainData: {
      type: Array,
      default: () => []
    },
    radioCheck: {
      type: Number,
      default: 0
    },
    imageSize: {
      type: Number,
      default: 200
    },
    roleType: {
      type: String,
      default: ''
    },
    GroupIds: {
      type: Array,
      default: () => []
    }
  },
  components: { NoData },
  data () {
    return {
      radio: null,
      checkList: [],
      dataList: [],
      arrsList: [],
      msg: null,
      pageSize: 100,
      pageNum: 1,
      total: '',
      loading: false,
      asyncData: []
    }
  },
  watch: {
    radioCheck: {
      handler (num) {
        this.radio = num
      },
      deep: true
    },
    roleType: {
      handler (msg) {
        if (msg == 'account') {
          this.getRoleData('UserId', 'UserName')
        }
        if (msg == 'roleByUpId') {
          this.getRoleDataByUpId('roleId')
        }
        if (msg == 'onlyRole') {
          this.getRoleData('RoleId', 'RoleName')
        }
      },
      deep: true
    },
    dataList: {
      handler (arr) {
        if (arr && arr.length) {
          this.asyncData.push(true)
        }
      },
      deep: true
    },
    checkBoxList: {
      handler (arr) {
        if (arr && arr.length) {
          this.asyncData.push(true)
        }
        // if (this.roleType == "roleByUpId") {
        //   const datas = [];
        //   this.dataList.forEach((item, index) => {
        //     arr.forEach((p) => {
        //       if (item.RoleId == p.RoleId) {
        //         datas.push(index);
        //       }
        //     });
        //   });
        //   this.checkList = datas;
        // }
      },
      deep: true
    },
    asyncData: {
      handler (arr) {
        if (arr && arr.length == 2) {
          if (this.roleType == 'account') {
            this.checkBoxs('UserId')
          }
          if (this.roleType == 'onlyRole' || this.roleType == 'roleByUpId') {
            this.checkBoxs('RoleId')
          }
        }
      },
      deep: true
    },
    GroupIds: {
      handler () {
        this.getRoleDataByUpId()
      },
      deep: true
    }
  },
  computed: {
    noMore () {
      return this.pageNum >= this.total
    },
    disabled () {
      return this.loading || this.noMore
    }
  },
  methods: {
    // 选中
    handleBg (item, i) {
      this.msg = i
      this.$emit('checkClick', item)
    },
    //多选框
    checkChange (arr) {
      const datas = this.formatChecks(arr)
      // if (this.roleType == 'account') {
      //   datas = this.formatChecks(arr, 'UserId');
      // }
      this.$emit('checkChange', datas)
    },
    //单选框
    radioChange (val) {
      this.$emit('radioChange', val)
    },
    //格式化选中回调
    formatChecks (arr, key) {
      const datas = []
      // const ids = []
      this.dataList.forEach((item, index) => {
        arr.forEach(p => {
          if (index == p) {
            datas.push(item)
          }
        })
      })
      // datas.forEach(item => {
      //   ids.push(item[key])
      // })
      return datas
    },
    //多选框默认选中
    checkBoxs (ids) {
      const datas = []
      const type = typeof this.checkBoxList[0]
      if (type == 'object') {
        this.dataList.forEach((item, index) => {
          this.checkBoxList.forEach(p => {
            if (item[ids] == p[ids]) {
              datas.push(index)
            }
          })
        })
      } else {
        this.dataList.forEach((item, index) => {
          this.checkBoxList.forEach(p => {
            if (item[ids] == p) {
              datas.push(index)
            }
          })
        })
      }
      this.checkList = datas
    },
    checkRole () {
      const datas = []
      this.dataList.forEach((item, index) => {
        this.checkBoxList.forEach(p => {
          if (item.RoleId == p.RoleId) {
            datas.push(index)
          }
        })
      })
      this.checkList = datas
    },
    //获取列表
    async getRoleData (ids, names) {
      try {
        this.loading = true
        const obj = {
          QueryName: '',
          PageIndex: this.pageNum,
          PageSize: this.pageSize
        }
        let result = null
        if (this.roleType == 'account') {
          result = await GetAccountList(obj)
        }
        if (this.roleType == 'onlyRole') {
          result = await QueryRole(obj)
        }
        if (result.code === 0) {
          const datas = result.returnData.records
          const num = result.returnData.pages
          this.arrsList.push(datas)
          const arrs = this.arrsList.flat()
          const msgs = _.unionBy(arrs, ids)
          msgs.forEach(item => {
            item.name = item[names]
          })
          this.dataList = msgs
          this.total = num
          this.$emit('roleListChange', msgs)
          this.loading = false
        } else {
          this.$message.error(result.message)
          this.loading = false
        }
      } catch (error) {
        console.log('出错了', error)
        this.loading = false
      }
    },

    //获取列表2
    async getRoleDataByUpId () {
      try {
        this.loading = true
        const obj = {
          GroupIds: this.GroupIds
        }
        const result = await GetRoleByGroup(obj)
        if (result.code === 0) {
          const datas = result.returnData
          const msgs = []
          if (datas.length) {
            datas.forEach(item => {
              item.name = item.RoleName
              if (item.IsSelected) {
                msgs.push(item)
              }
            })
            this.dataList = msgs
          } else {
            this.dataList = msgs
          }
          this.$emit('roleListChange', msgs)
          // this.checkBoxs(this.dataList, "RoleId");
          this.loading = false
        } else {
          this.$message.error(result.message)
          this.loading = false
        }
      } catch (error) {
        console.log('出错了', error)
        this.loading = false
      }
    },

    //滚动加载数据
    load () {
      this.pageNum += 1
      if (this.roleType == 'account') {
        this.getRoleData('UserId', 'UserName')
      }
      if (this.roleType == 'onlyRole') {
        this.getRoleData('RoleId', 'RoleName')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import "./rolelist.scss";
.paren_content {
  ::v-deep .el-radio__label {
    display: none;
  }
  ::v-deep .el-checkbox__label {
    display: none;
  }
  .scCont {
    height: 65vh;
    overflow-x: hidden;
    overflow-y: auto;
  }
}
</style>
