<template>
  <div class="communication-container">
    <div class="common-title gradient-text">前方通信</div>
    <div class="tabs">
      <div v-for="(item, index) in menu" :key="index" :class="activeIndex === index ? 'tab tab_active' : 'tab'">
        {{ item.name }}
      </div>
    </div>
    <!--视频会商-->
    <div v-show="activeIndex === 0" class="content">
      <div class="left-content">
        <div class="search-box">
          <el-input v-model="queryParams.label" class="custom-input" placeholder="组织架构搜索" style="width: 214px" @keyup.enter="getTree">
            <template #prefix>
              <el-icon class="el-input__icon"><search /></el-icon>
            </template>
          </el-input>
          <el-select
            v-model="queryParams.value1"
            class="custom-select select-box"
            placeholder="全部"
            popper-class="custom-select-popper"
            :teleported="false"
          >
            <el-option v-for="level in options" :key="level.value" :label="level.name" :value="level.value"></el-option>
          </el-select>
          <el-input v-model="queryParams.keyword" class="custom-input" placeholder="组织架构搜索">
            <template #prefix>
              <el-icon class="el-input__icon"><search /></el-icon>
            </template>
          </el-input>
        </div>
        <div class="tree-container">
          <div class="tree-box">
            <div style="overflow-y: auto; height: 100%">
              <el-tree :data="treeData" accordion @node-click="handleNodeClick" />
            </div>
          </div>
          <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.name }}</div>
                  <div class="td3">
                    {{ item.duty }}
                    <div class="phone-icon"></div>
                  </div>
                </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="line">-->
<!--              <div class="text1">{{ item.name }}</div>-->
<!--              <div class="text2">{{ item.duty }}</div>-->
<!--            </div>-->
<!--            <div class="line" style="margin-top: 20px">-->
<!--              <div class="text2">{{ item.dept }}</div>-->
<!--            </div>-->
<!--            <div class="close-btn" @click="deleteItem(item)"></div>-->
<!--          </div>-->
<!--        </div>-->
<!--      </div>-->
      <div class="btn-box">
        <div class="btn" @click="handleJoinMeeting">
          <div class="icon1"></div>
          <div class="text">会议号入会</div>
        </div>
        <div class="btn" @click="handleStartCall">
          <div class="icon2"></div>
          <div class="text">电话呼叫</div>
        </div>
        <div class="btn" @click="handleStartMeeting">
          <div class="icon3"></div>
          <div class="text">发起会议</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { Search } from '@element-plus/icons-vue';
import { getAvconDeptList, getAvconDeptTree, getStartMiniParan } from '@/api/emergencyCommandMap/communication';

const proxy = getCurrentInstance()?.proxy;

let activeIndex = ref(0);
const options = ref([{ name: '全部', value: '全部' }]);
const menu = ref([{ name: '视频会商' }, { name: '无人机' }, { name: '单兵设备' }]);
const treeData = ref([]);
const userList = ref([]);
const queryParams = reactive({
  label: '',
  value1: ''
});

const getTree = () => {
  getAvconDeptTree(queryParams).then((res) => {
    treeData.value = res.data;
  });
};
const selectList = computed(() => {
  const data = [];
  userList.value.forEach((item) => {
    if (item.checked) {
      data.push(item);
    }
  });
  return data;
});
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 = (item) => {
  // if (item.isLeaf) {
  //   debugger
    getAvconDeptList(item.id).then((res) => {
      res.data.forEach((item) => {
        item.checked = false;
      });
      userList.value = res.data;
    });
  // }
};
// 全选、全取消
const handleChecked = () => {
  const checkedClass = getCheckedClass();
  let flag = true;
  if (checkedClass === 'common-checked-active') {
    flag = false;
  }
  userList.value.forEach((item) => {
    item.checked = flag;
  });
};
// 单个选中、取消选中
const handleChecked2 = (item) => {
  item.checked = !item.checked;
};
// 清空
const clearSelect = () => {
  userList.value.forEach((item) => {
    if (item.checked) {
      item.checked = false;
    }
  });
};
// 清空指定项
const deleteItem = (item) => {
  for (let i = 0; i < userList.value.length; i++) {
    if (item.id === userList.value[i].id) {
      userList.value[i].checked = false;
      break;
    }
  }
};
// 会议号入会
const handleJoinMeeting = () => {
  proxy?.$modal.msgError('融合通信新开发功能，暂未对接');
  return false;

  const screenWidth = window.screen.width * window.devicePixelRatio;
  const screenHeight = window.screen.height * window.devicePixelRatio;
  const data = {
    "userid": "", // 空表示后台获取当前用户对应融合通信dev_id
    "password": "123",
    roomid: '715724498', // 会议号
    windowpos: { "x": 0, "y": 0, "width": screenWidth, "height": screenHeight, "top": true },
    members: { num: 1} // 不用邀请别人
  };
  getStartMiniParan(data).then((res) => {
    // 创建一个a标签元素
    const a = document.createElement('a');
    // 设置a标签的href属性
    a.href = res.data;
    // 触发点击事件
    a.click();
  });
}

// 电话呼叫
const handleStartCall = () => {
  let dev_list = [];
  userList.value.forEach((item)=>{
    if(item.checked && item.mobile != '') {
      dev_list.push({id: item.mobile, avtype: 'a'}); // a 音频 v 视频 默认 av
    }
  });
  if (dev_list.length == 0) {
    proxy?.$modal.msgError('请勾选人员');
    return false;
  }
  const screenWidth = window.screen.width * window.devicePixelRatio;
  const screenHeight = window.screen.height * window.devicePixelRatio;
  const data = {
    "userid": "", // 空表示后台获取当前用户对应融合通信dev_id
    "password": "123",
    roomid: '', // 空表示新会议室
    windowpos: { "x": 0, "y": 0, "width": screenWidth, "height": screenHeight, "top": true },
    members: {
      num: dev_list.length + 2, // 配置多少个座位，一般就是邀请人多少个就多少个
      "dev-list": dev_list,
      // "sip-list": [ { "id": "60002" }, { "id": "60008", "avtype": "a" }, { "id": "60003", "avtype": "v" } ]
    }
  };
  getStartMiniParan(data).then((res) => {
    // 创建一个a标签元素
    const a = document.createElement('a');
    // 设置a标签的href属性
    a.href = res.data;
    // 触发点击事件
    a.click();
  });
};
// 发起会议
const handleStartMeeting = () => {
  const data = {
    userid: 'mmyj0006@mm.zw.yj',
    password: '123',
    roomid: '',
    'dev-list': [{ id: 'hh@mm.zw.yj', avtype: 'av' }]
  };
  getStartMiniParan(data).then((res) => {
    // 创建一个a标签元素
    const a = document.createElement('a');
    // 设置a标签的href属性
    a.href = res.data;
    // 触发点击事件
    a.click();
  });
};

onMounted(() => {
  getTree();
})
</script>

<style lang="scss" scoped>
.communication-container {
  width: 527px;
  height: 366px;
  background: url('@/assets/images/emergencyCommandMap/communication/communicationBg.png') no-repeat;
  background-size: 100% 100%;
  position: relative;
  padding-top: 47px;
  padding-left: 10px;
  animation-name: slideLeft;
  animation-duration: 2s;
  .tabs {
    display: flex;
    .tab {
      font-size: 16px;
      font-family: YouSheBiaoTiHei;
      text-align: center;
      color: #b6bbcc;
      width: 121px;
      height: 30px;
      line-height: 33px;
      background: url('@/assets/images/emergencyCommandMap/communication/tab.png') no-repeat;
      background-size: 100% 100%;
      cursor: pointer;
      &:hover {
        color: #ffffff;
        background: url('@/assets/images/emergencyCommandMap/communication/tabActive.png') no-repeat;
        background-size: 100% 100%;
      }
    }

    .tab_active {
      color: #ffffff;
      background: url('@/assets/images/emergencyCommandMap/communication/tabActive.png') no-repeat;
      background-size: 100% 100%;
    }
  }
  .content {
    margin-top: 12px;
    .left-content {
      .search-box {
        display: flex;
        .custom-input {
          width: 140px;
        }
        .select-box {
          width: 77px !important;
          height: 24px;
          line-height: 24px;
          margin: 0 10px;
          color: #83a3be;
          font-size: 14px;
        }
        .custom-select-popper {
          .el-scrollbar {
            .el-select-dropdown__item {
              color: #b1cae0;
              font-size: 14px;
              line-height: 24px;
            }
          }
        }
        .input {
          background: transparent;
          color: #83a3be;
          font-size: 32px;
          outline: none;
          appearance: none;
          height: 100%;
          border: none;
          &::placeholder {
            color: #83a3be;
          }
        }
      }
    }
    .tree-container {
      margin-top: 15px;
      display: flex;
      .tree-box {
        width: 255px;
        height: 180px;
        background: url('@/assets/images/emergencyCommandMap/communication/treeBg.png') no-repeat;
        padding: 15px 8px;
        overflow: hidden;
        :deep(.el-tree) {
          background-color: transparent;
          color: #fbffff;
          font-size: 14px;
          .el-tree-node__content {
            height: auto;
            padding-top: 5px;
            padding-bottom: 5px;
            white-space: normal;
            word-break: break-all;
          }
          .el-tree-node__expand-icon {
            color: #297cfc;
            font-size: 12px;
          }
          .el-tree-node:focus > .el-tree-node__content,
          .el-tree-node__content:hover {
            background-color: transparent !important;
          }
        }
      }
      .user-box {
        margin-left: 10px;
        width: 236px;
        height: 180px;
        background: url('@/assets/images/emergencyCommandMap/communication/treeBg.png') no-repeat;
        background-size: 100% 100%;
        .user-table {
          padding: 10px 5px;
          display: flex;
          flex-direction: column;
          font-size: 14px;
          color: #fbffff;
          .tr {
            background-color: #102e76;
          }
          .tr,
          .tr2 {
            display: flex;
            padding: 5px 0;
            .td {
              flex: 2;
              display: flex;
              align-items: center;
            }
            .td2 {
              width: 65px;
              display: flex;
              align-items: center;
              justify-content: center;
            }
            .td3 {
              flex: 3;
              display: flex;
              align-items: center;
              justify-content: center;
            }
          }
          .table-content {
            height: 133px;
            overflow-y: auto;
          }
          .tr2 {
            margin-top: 5px;
            background-color: #122868;
          }
          .phone-icon {
            flex-shrink: 0;
            width: 24px;
            height: 25px;
            background: url('@/assets/images/emergencyCommandMap/communication/phone.png') no-repeat;
            background-size: 100%;
            cursor: pointer;
          }
        }
      }
    }
    .select-box2 {
      margin-left: 10px;
      width: 579px;
      height: 421px;
      background: url('@/assets/images/emergencyCommandMap/communication/peopleBg.png') no-repeat;
      background-size: 100% 100%;
      .select-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        color: #fbffff;
        font-size: 32px;
        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: 305px;
        overflow-y: auto;
        .box-item {
          border-bottom: 1px solid #247dff;
          padding: 20px;
          position: relative;
          .line {
            color: #fff;
            font-size: 38px;
            display: flex;
            .text1 {
              margin-right: 35px;
            }
            .text2 {
              color: #a7ccdf;
            }
          }
          .close-btn {
            position: absolute;
            right: 10px;
            top: 50px;
            cursor: pointer;
            width: 29px;
            height: 29px;
            background: url('@/assets/images/emergencyCommandMap/communication/close.png') no-repeat;
          }
        }
      }
    }
    .btn-box {
      width: 100%;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      .btn {
        width: 114px;
        height: 43px;
        background: url('@/assets/images/emergencyCommandMap/communication/btn.png') no-repeat;
        background-size: 100% 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .icon1,
        .icon2,
        .icon3 {
          margin-top: 3px;
        }
        .icon1 {
          width: 19px;
          height: 20px;
          background: url('@/assets/images/emergencyCommandMap/communication/icon1.png') no-repeat;
          background-size: 100% 100%;
        }
        .icon2 {
          width: 20px;
          height: 20px;
          background: url('@/assets/images/emergencyCommandMap/communication/icon2.png') no-repeat;
          background-size: 100% 100%;
        }
        .icon3 {
          width: 20px;
          height: 20px;
          background: url('@/assets/images/emergencyCommandMap/communication/icon3.png') no-repeat;
          background-size: 100% 100%;
        }
        .text {
          color: #fff;
          font-size: 14px;
        }
      }
    }
  }
}

.title {
  position: absolute;
  top: 6px;
  left: 141px;
  font-size: 60px;
}
</style>
