<template>
  <el-dialog custom-class="custom-dialog" class="border" :border="false" closeFree width="600px" :title="title"
             :visible.sync="visible" v-model="visible" append-to-body :close-on-click-modal="true"
             :destroy-on-close="true">
    <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>
    <div slot="footer">
      <el-button size="mini" @click="visible = false">取 消</el-button>
      <el-button size="mini" type="primary" @click="selectConfirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
import {getRole} from "@/api/workflow/process-user";
import {computed, defineProps, defineExpose} from "vue";
import {ElMessageBox} from "element-plus";

const emit = defineEmits();

const props = defineProps({
  selected: {
    default: () => {
      return [];
    },
    type: Array
  },
  multiple: { //是否多选
    default: true,
    type: Boolean
  },
  showCheckbox: { //是否显示左侧选择框
    default: true,
    type: Boolean
  }
});
const _value = computed({
  get() {
    return this.value;
  },
  set(val) {
    emit("input", val);
  }
});
const visible = ref(false);
const loading = ref(false);
const title = ref("请选择");
const selectList = ref([]);
const roleList = ref([]);

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

<style lang="scss" scoped>
$containWidth: 264px;
.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(var($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>
