<template>
  <w-dialog :border="false" closeFree width="600px" @ok="selectConfirm"  :title="title" v-model="visible">
    <div class="picker">
      <div class="candidate" v-loading="loading">
        <div class="role-header">
          <div>系统角色</div>
        </div>
        <div class="org-items">
          <el-empty :image-size="100" description="似乎没有数据" v-show="roleList.length === 0"/>
          <!-- 系统角色-->
          <div class="org-role-item" v-for="(roleItem , roleIndex) in roleList" :key="roleIndex"
               @click="selectChange(roleItem)">
            <el-checkbox v-model="roleItem.selected" v-if="showCheckbox" @change="selectChange(roleItem)"></el-checkbox>
            <i class="el-icon-user-solid" style="margin-left: 5px"></i>
            <span>{{ roleItem.roleName }}</span>
          </div>
        </div>
      </div>

      <div class="selected">
        <div class="count">
          <span>已选 {{ selectList.length }} 项</span>
          <span @click="clearSelected">清空</span>
        </div>
        <div class="org-items">
          <el-empty :image-size="100" description="请点击左侧列表选择数据" v-show="selectList.length === 0"/>
          <div v-for="(selectItem, selectIndex) in selectList" :key="selectIndex" class="org-role-item">
            <i class="el-icon-user-solid"></i>
            <span>{{ selectItem.roleName }}</span>
            <i class="el-icon-close" @click="noSelected(selectItem)"></i>
          </div>
        </div>
      </div>
    </div>
  </w-dialog>
</template>

<script>
import {getRole} from "@/api/org";

export default {
  name: "RolePicker",
  props: {
    selected: {
      default: () => {
        return [];
      },
      type: Array
    },
    multiple: { //是否多选
      default: true,
      type: Boolean
    },
    showCheckbox: { //是否显示左侧选择框
      default: true,
      type: Boolean
    }
  },
  computed: {
    _value: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit("input", val)
      }
    }
  },
  data() {
    return {
      visible: false,
      loading: false,
      title: "请选择",
      roleList: [],
      selectList: []
    };
  },
  created() {
  },
  methods: {
    //获取角色信息
    getRole() {
      getRole().then(res => {
        this.roleList= res.data.map(function (val){
          return {roleId:val.value,roleName:val.label}
        })
      });
    },
    //用于弹出角色选择器
    showRolePicker() {
      this.getRole();
      this.visible = true;
    },
    //选中角色
    selectChange(roleItem) {
      // 左侧有选择框
      if (this.showCheckbox) {
        // 左侧有选择框 + 多选
        if (this.multiple) {
          for (let i = 0; i < this.selectList.length; i++) {
            if (this.selectList[i].roleId === roleItem.roleId) {
              this.selectList.splice(i, 1);
              break;
            }
          }
          if (roleItem.selected) {
            this.selectList.push(roleItem);
          }
        } else {// 左侧有选择框 + 单选
          //用于左侧选择框选中取消,引起右侧数据变化
          for (let i = 0; i < this.selectList.length; i++) {
            if (this.selectList[i].roleId === roleItem.roleId) {
              this.selectList.splice(i, 1);
              break;
            }
          }
          if (roleItem.selected) {
            this.selectList = [roleItem];
          }
          for (let i = 0; i < this.roleList.length; i++) {
            for (let j = 0; j < this.selectList.length; j++) {
              if (this.roleList[i].roleId !== this.selectList[j].roleId) {
                this.roleList[i].selected = false;
              }
            }
          }
        }
      } else {// 左侧没有选择框
        // 左侧没有选择框 + 多选
        if (this.multiple) {
          for (let i = 0; i < this.selectList.length; i++) {
            if (this.selectList[i].roleId === roleItem.roleId) {
              this.selectList.splice(i, 1);
              break;
            }
          }
          this.selectList.push(roleItem);
        }else {// 左侧没有选择框 + 单选
          this.selectList = [roleItem];
        }
      }
      this._value = this.selectList
    },
    //右侧的×
    noSelected(selectItem) {
      for (let i = 0; i < this.selectList.length; i++) {
        if (this.selectList[i].roleId === selectItem.roleId) {
          this.selectList.splice(i, 1);
          break;
        }
      }
      selectItem.selected = false;
    },
    //清空
    clearSelected() {
      this.$confirm("您确定要清空已选中的项?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        for (let i = 0; i < this.selectList.length; i++) {
          if (this.selectList[i].selected === true) {
            this.selectList[i].selected = false;
          }
        }
        this.selectList = [];
      });
    },
    //确定按钮
    selectConfirm() {
      this.$emit("ok", this.selectList);
      this.visible = false;
    }
  }
};
</script>

<style lang="less" scoped>
@containWidth: 278px;
/deep/ .el-dialog__body {
  padding: 10px 20px;
}


.picker {
  height: 402px;
  position: relative;
  text-align: left;

  .candidate {
    left: 0;
    top: 0;
  }

  .role-header {
    padding: 10px !important;
    margin-bottom: 5px;
    border-bottom: 1px solid #e8e8e8;
  }
}

.candidate, .selected {
  position: absolute;
  display: inline-block;
  width: @containWidth;
  height: 400px;
  border: 1px solid #e8e8e8;
}

.selected {
  border-left: none;
  right: 0;
  top: 0;

  .count {
    width: calc(@containWidth - 20px);
    padding: 10px;
    display: inline-block;
    border-bottom: 1px solid #e8e8e8;
    margin-bottom: 5px;

    & > span:nth-child(2) {
      float: right;
      color: #c75450;
      cursor: pointer;
    }
  }

}


.org-items {
  overflow-y: auto;
  height: 350px;

  .el-icon-close {
    position: absolute;
    right: 5px;
    cursor: pointer;
    font-size: larger;
  }

  .org-role-item {
    padding: 7px 5px;
    display: flex;
    align-items: center;
    cursor: pointer;
    margin: 0 5px;
    border-radius: 5px;
    position: relative;

    &:hover {
      background: #f1f1f1;
    }

    > span {
      margin-left: 5px;
    }

  }
}


::-webkit-scrollbar {
  float: right;
  width: 4px;
  height: 4px;
  background-color: white;
}

::-webkit-scrollbar-thumb {
  border-radius: 16px;
  background-color: #efefef;
}
</style>
