<template>
  <Dialog draggable custom-show type="lg" title="通讯录" height="720px" @close="handleClose" @confirm="handleConfirm">
    <div class="content">
      <div class="left-content">
        <el-input v-model="queryParams1.deptName" class="custom-input" placeholder="组织架构搜索">
          <template #prefix>
            <el-icon class="el-input__icon"><search /></el-icon>
          </template>
        </el-input>
        <div class="tree-container">
          <div class="tree-box">
            <el-tree :data="treeData" :props="{ value: 'deptId', label: 'deptName', children: 'children' }" accordion @node-click="handleNodeClick" />
          </div>
        </div>
      </div>
      <div class="middle-content">
        <div class="user-box">
          <div class="user-table">
            <div class="tr">
              <div class="td2">
                <div :class="getCheckedClass()" @click="handleChecked"></div>
              </div>
              <div class="td">姓名</div>
              <div class="td3">职务</div>
            </div>
            <div class="table-content">
              <div v-for="(item, index) in userList" :key="index" class="tr2">
                <div class="td2">
                  <div :class="item.checked ? 'common-checked-active' : 'common-checked'" @click="handleChecked2(item)"></div>
                </div>
                <div class="td">{{ item.nickName }}</div>
                <div class="td3">
                  {{ item.deptName }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="select-box2">
        <div class="select-header">
          <div class="left-item">
            <div>已选择：</div>
            <div class="text">{{ selectList.length }}</div>
            <div>人</div>
          </div>
          <div class="clear-btn" @click="clearSelect">清空</div>
        </div>
        <div class="select-content">
          <div v-for="(item, index) in selectList" :key="index" class="box-item">
            <div class="box-left">
              <div class="line">
                <div class="text1">{{ item.nickName }}</div>
                <div class="text2">{{ item.duty }}</div>
              </div>
              <div class="line" style="margin-top: 6px">
                <div class="text2">{{ item.deptName }}</div>
              </div>
            </div>
            <div class="close-btn" @click="deleteItem(item)"></div>
          </div>
        </div>
      </div>
    </div>
  </Dialog>
</template>

<script lang="ts" setup name="Contact">
import { Search } from '@element-plus/icons-vue';
import { listDept } from '@/api/system/dept';
import { DeptVO } from '@/api/system/dept/types';
import { listUser } from '@/api/system/user';
defineProps({
  modelValue: Boolean
});
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const emits = defineEmits(['update:modelValue', 'close', 'confirm']);
let queryParams1 = ref({
  pageNum: 1,
  pageSize: 10,
  deptName: undefined,
  deptCategory: undefined,
  status: undefined
});
let queryParams2 = ref({
  pageNum: 1,
  pageSize: 100,
  deptId: ''
});
const treeData = ref([]);
const userList = ref([]);
const selectList = ref([]);
const getCheckedClass = () => {
  let res = 'common-checked';
  const len = userList.value.length;
  const len2 = selectList.value.length;
  if (len2 > 0 && len2 === len) {
    res = 'common-checked-active';
  } else if (len2 > 0) {
    res = 'common-checked-half';
  }
  return res;
};
const handleNodeClick = (data) => {
  queryParams2.value.deptId = data.deptId;
  getPerson();
};
const getPerson = () => {
  listUser(queryParams2.value).then((res) => {
    const data = res.rows;
    data.forEach((item) => {
      for (let i = 0; i < selectList.value.length; i++) {
        if (selectList.value[i].nickName === item.nickName) {
          item.checked = true;
          break;
        }
      }
    });
    userList.value = data;
  });
};
// 全选、全取消
const handleChecked = () => {
  const checkedClass = getCheckedClass();
  let flag = true;
  if (checkedClass === 'common-checked-active') {
    flag = false;
  }
  userList.value.forEach((item) => {
    item.checked = flag;
  });
  computedSelectList();
};
// 单个选中、取消选中
const handleChecked2 = (item) => {
  item.checked = !item.checked;
  computedSelectList();
};
const computedSelectList = () => {
  userList.value.forEach((item2) => {
    let index = 0;
    for (let i = 0; i < selectList.value.length; i++) {
      if (!!item2.checked && selectList.value[i].userId !== item2.userId) {
        index++;
      } else if (!item2.checked && selectList.value[i].userId === item2.userId) {
        selectList.value.splice(i, 1);
        break;
      }
    }
    if (!!item2.checked && selectList.value.length === index) {
      selectList.value.push(item2);
    }
  });
};
// 清空
const clearSelect = () => {
  userList.value.forEach((item) => {
    if (item.checked) {
      item.checked = false;
    }
  });
  selectList.value = [];
};
// 清空指定项
const deleteItem = (item) => {
  for (let i = 0; i < userList.value.length; i++) {
    if (item.userId === userList.value[i].userId) {
      userList.value[i].checked = false;
      break;
    }
  }
  computedSelectList();
};

// 弹窗关闭后
const handleClose = () => {
  emits('update:modelValue', false);
  emits('close');
};

//
const handleConfirm = () => {
  emits('confirm', selectList.value);
  emits('update:modelValue', false);
};

const initData = () => {
  listDept(queryParams1.value).then((res: any) => {
    const data = proxy?.handleTree<DeptVO>(res.data, 'deptId');
    treeData.value = data;
  });
};
onMounted(() => {
  initData();
});
</script>

<style lang="scss" scoped>
.content {
  display: flex;
  margin-top: 12px;
  .left-content {
    width: 303px;
    padding-right: 10px;
    border-right: 1px solid #2187ff;
  }
  .middle-content {
    width: 303px;
    padding: 0 10px;
    border-right: 1px solid #2187ff;
    .search-box {
      display: flex;
      .select-box {
        flex-shrink: 0;
        width: 176px !important;
        height: 56px;
        line-height: 56px;
        margin: 0 10px;
        color: #83a3be;
        font-size: 14px;
      }
    }
    .user-box {
      width: 100%;
      height: 100%;
      .user-table {
        padding: 15px 0;
        display: flex;
        flex-direction: column;
        font-size: 16px;
        color: #fbffff;
        .tr {
          background-color: #102e76;
        }
        .tr,
        .tr2 {
          display: flex;
          padding: 6px 0;
          .td {
            flex: 1;
            display: flex;
            align-items: center;
          }
          .td2 {
            width: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
          }
          .td3 {
            flex: 2;
            display: flex;
            align-items: center;
          }
        }
        .table-content {
          height: 520px;
          overflow-y: auto;
        }
        .tr2 {
          margin-top: 10px;
          background-color: #122868;
        }
        .phone-icon {
          width: 31px;
          height: 31.5px;
          background: url('@/assets/images/emergencyCommandMap/communication/phone.png');
          background-size: 100% 100%;
          cursor: pointer;
        }
      }
    }
  }

  .custom-select-popper {
    .el-scrollbar {
      .el-select-dropdown__item {
        color: #b1cae0;
        font-size: 14px;
        height: 56px;
        line-height: 56px;
      }
    }
  }
  .input {
    background: transparent;
    color: #83a3be;
    font-size: 14px;
    outline: none;
    appearance: none;
    height: 100%;
    border: none;
    &::placeholder {
      color: #83a3be;
    }
  }
  .tree-container {
    display: flex;
    .tree-box {
      width: 100%;
      height: 555px;
      overflow-y: auto;
      padding: 15px 8px;
      :deep(.el-tree) {
        height: 100%;
        background-color: transparent;
        color: #fbffff;
        font-size: 16px;
        .el-tree-node__content {
          height: auto;
          padding-top: 10px;
          padding-bottom: 10px;
          white-space: normal;
          word-break: break-all;
        }
        .el-tree-node__expand-icon {
          color: #297cfc;
        }
        .el-tree-node:focus > .el-tree-node__content,
        .el-tree-node__content:hover {
          background-color: transparent !important;
        }
      }
    }
  }
  .select-box2 {
    margin-left: 30px;
    width: 303px;
    height: 100%;
    .select-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      color: #fbffff;
      font-size: 14px;
      border-bottom: 1px solid #247dff;
      padding: 20px;
      .left-item {
        display: flex;
        align-items: center;
        .text {
          margin: 0 10px;
          color: #00e8ff;
          font-family: 'BEBAS-1';
        }
      }
      .clear-btn {
        color: #00e8ff;
        cursor: pointer;
      }
    }
    .select-content {
      height: 520px;
      overflow-y: auto;
      .box-item {
        border-bottom: 1px solid #247dff;
        padding: 7px;
        position: relative;
        display: flex;
        .box-left {
          flex: 1;
          margin-right: 5px;
        }
        .line {
          color: #fff;
          font-size: 16px;
          display: flex;
          .text1 {
            margin-right: 35px;
          }
          .text2 {
            color: #a7ccdf;
          }
        }
        .close-btn {
          flex-shrink: 0;
          cursor: pointer;
          width: 20px;
          height: 20px;
          background: url('@/assets/images/emergencyCommandMap/communication/close.png') no-repeat;
          background-size: 100% 100%;
        }
      }
    }
  }
}
</style>
