<template>
  <div class="container">
    <div class="container-hearder">
      <div class="container-back" @click="back">
        <el-icon><ArrowLeftBold /></el-icon>
        <div class="back-text">返回</div>
      </div>
    </div>
    <div class="container-body" v-if="from != 'company'">
      <div class="container-body-avatar"></div>
      <div class="container-body-right">
        <span class="container-body-content-title">{{ groupInfo.name }}</span>
        <div class="container-body-creator">
          <span class="creator-title">创建者:</span>
          <el-image :src="groupInfo.creator && groupInfo.creator.avatar" style="width: 18px; height: 18px; border-radius: 18px" />
          <span class="creator-name">{{ groupInfo.creator && groupInfo.creator.name }}</span>
        </div>
        <div class="container-body-creator">
          <span class="creator-title">简介:</span>
          <span>暂无简介~</span>
        </div>
      </div>
    </div>
    <div class="container-footer">
      <div style="display: flex">
        <div class="change-btn">
          <div class="change-btn-item" :class="type == 'category' ? 'change-btn-select' : ''" @click="changeSelect('category')">分类管理</div>
          <div class="change-btn-item" :class="type == 'label' ? 'change-btn-select' : ''" @click="changeSelect('label')">标签管理</div>
        </div>
      </div>
      <div class="cla-container" v-if="type == 'category'">
        <div class="cla-container-parents">
          <div class="cla-container-item-header">
            <div>父类树</div>
            <el-icon class="tree-item-add" @click="createClaBtn(false, 0)"><Plus /></el-icon>
          </div>
          <div class="cla-container-item-body">
            <template v-for="item in cla_list">
              <div class="cla-container-item" :class="parent_cla_id.value == item.value ? 'cla-container-item-select' : ''" @click="handleChild(item)">
                <div class="cla-container-item-name">{{ item.title }}</div>
                <div class="item-container" style="display: flex; align-items: center">
                  <div style="position: relative">
                    <span style="font-size: 12px">{{ item.children && item.children.length > 0 ? item.children.length : 0 }} 个分类</span>
                    <el-icon @click.stop="item.visible = true" style="margin-left: 5px"><More style="transform: rotate(90deg); margin-right: 5px" /></el-icon>
                    <div class="popup-container" v-if="item.visible">
                      <span class="popup-item" @click.stop="hanldeChangeName(item)">重命名</span>
                      <span class="popup-item" @click.stop="hanldeDeleteCla(item)" v-if="Object.keys(cla_list).length > 1">删除</span>
                    </div>
                  </div>
                </div>
              </div>
            </template>
          </div>
        </div>
        <div class="cla-container-children">
          <div class="cla-container-item-header">
            <div style="margin-right: 20px">子类树</div>
            <el-icon class="tree-item-add" @click="createClaBtn(true, 0)"><Plus /></el-icon>
          </div>
          <div class="cla-container-item-body" v-if="parent_cla_id.children && parent_cla_id.children.length > 0">
            <template v-for="item in parent_cla_id.children">
              <div class="cla-container-item">
                <div class="cla-container-item-name">{{ item.title }}</div>
                <div class="item-container">
                  <div style="position: relative">
                    <el-icon @click.stop="item.visible = true"><More style="transform: rotate(90deg); margin-right: 5px" /></el-icon>
                    <div class="popup-container" v-if="item.visible">
                      <span class="popup-item" @click.stop="hanldeChangeName(item)">重命名</span>
                      <!-- <span class="popup-item" @click.stop="hanldeChangeName(item)">移动到...</span> -->
                      <span class="popup-item" @click.stop="hanldeDeleteCla(item)" v-if="Object.keys(cla_list).length > 1">删除</span>
                    </div>
                  </div>
                </div>
              </div>
            </template>
          </div>
          <div v-else>
            <el-empty description="没有分类"></el-empty>
            <div class="empty-container">
              <span>
                您还没有分类，请先
                <span class="create-cla-btn" @click="createClaBtn(false, 0)">创建分类</span>
              </span>
            </div>
          </div>
        </div>
      </div>
      <div class="cla-container" v-if="type == 'label'">
        <div class="cla-container-parents">
          <div class="cla-container-item-header">
            <div style="margin-right: 20px">标签组</div>
            <el-icon class="tree-item-add" @click="createClaBtn(true, 0)"><Plus /></el-icon>
          </div>
          <div class="cla-container-item-body" v-if="parent_cla_id.children && parent_cla_id.children.length > 0">
            <template v-for="item in parent_cla_id.children">
              <div class="cla-container-item">
                <div class="cla-container-item-name">{{ item.title }}</div>
                <div class="item-container">
                  <div style="position: relative">
                    <el-icon @click.stop="item.visible = true"><More style="transform: rotate(90deg); margin-right: 5px" /></el-icon>
                    <div class="popup-container" v-if="item.visible">
                      <span class="popup-item" @click.stop="hanldeChangeName(item)">重命名</span>
                      <!-- <span class="popup-item" @click.stop="hanldeChangeName(item)">移动到...</span> -->
                      <span class="popup-item" @click.stop="hanldeDeleteCla(item)" v-if="Object.keys(cla_list).length > 1">删除</span>
                    </div>
                  </div>
                </div>
              </div>
            </template>
          </div>
        </div>
        <div class="cla-container-children"></div>
      </div>
    </div>
    <el-dialog v-model="createClaDialog" :title="type == 'category' ? '创建分类' : '创建标签'" width="20%">
      <div>
        <span style="margin-bottom: 10px; display: block">{{ type == 'category' ? '分类' : '标签' }}名称</span>
        <el-input v-model="cla_option.name" placeholder="请输入名称" clearable></el-input>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleCreateCategory">确 定</el-button>
          <el-button @click="createClaDialog = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog v-model="moveClaDialog" title="移动分类" width="20%">
      <div></div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleMoveCategory">确 定</el-button>
          <el-button @click="moveClaDialog = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { listApolloByTeam, getGroupLabel, updateCategory, createCategory, getGroupInfoById, deleteCategory, createGroupLabel } from '@/api/zhihui/index.js';

const { proxy } = getCurrentInstance();
const route = useRoute();
const groupId = ref(route.params?.groupId);
const from = ref(route.query?.from);
const type = ref(route.query?.type);
const data_list = ref([]); //原始数据
const cla_list = ref([]);
const groupInfo = ref({});
const createClaDialog = ref(false); //创建分类
const parent_cla_id = ref({});
const cla_option = ref({});
const cla_select_list = ref([]);
const moveClaDialog = ref(false);

const changeSelect = item => {
  type.value = item;
  parent_cla_id.value = {};
  data_list.value = {};
  cla_list.value = {};
  getList();
};

const back = () => {
  proxy.$router.back();
};
//创建
async function handleCreateCategory() {
  let data = {};
  if (type.value == 'category') {
    if (cla_option.value.value) {
      data = await updateCategory({ name: cla_option.value.name }, cla_option.value.value);
    } else {
      if (parent_cla_id.value.value && cla_option.value.level == 1) {
        cla_option.value.parentId = parent_cla_id.value.value;
      }
      data = await createCategory(cla_option.value);
    }
  }
  if (type.value == 'label') {
    cla_option.value.type = 'content';
    data = await createGroupLabel({ ...cla_option.value, isGroup: false });
  }
  if (data.statusCode == 201) {
    createClaDialog.value = false;
  }
  getList();
}
function handleChange(value) {
  if (cla_select_list.value.indexOf(value) == -1) {
    cla_select_list.value.push(value);
  } else {
    cla_select_list.value = cla_select_list.value.filter(item => item !== value);
  }
  proxy.$forceUpdate();
}
//点击按钮创建参数
function createClaBtn(item, level) {
  createClaDialog.value = true;
  cla_option.value = {
    level: level,
    name: '',
    teamId: groupId.value,
    type: '',
  };
  if (item) {
    cla_option.value.id = item.value;
  }
}
//转到子类
function handleChild(item) {
  parent_cla_id.value = item;
  if (cla_select_list.value.length > 0) {
    parent_cla_id.value.children.map(i => {
      let list = cla_select_list.value.filter(k => k.value === i.value);
      if (list.length > 0) {
        i.checked = true;
      }
    });
  }
}
//重命名
function hanldeChangeName(item) {
  cla_option.value = item;
  cla_option.value.name = item.title;
  createClaDialog.value = true;
}
//删除
function hanldeDeleteCla(item) {
  ElMessageBox.confirm('是否删除该分类?')
    .then(async () => {
      let data = await deleteCategory({}, item.value);
      if (data.statusCode == 200) {
        delSelectData(item, 'delete');
        if (item.children.length > 0) {
          item.children.map(i => {
            delSelectData(i, 'delete');
          });
        }
      }
    })
    .catch(() => {})
    .finally(() => {
      hidePopup();
      getList();
    });
}

function delSelectData(option, type = 'update') {
  if (type == 'update') {
    cla_select_list.value.map(item => {
      if (item.value == option.value) {
        item.title = option.name;
      }
    });
  }
  if (type == 'delete') {
    cla_select_list.value = cla_select_list.value.filter(item => item.value !== option.value);
  }
}

document.addEventListener('click', hidePopup);
function hidePopup() {
  Object.keys(cla_list.value).map(item => {
    cla_list.value[item].visible = false;
    cla_list.value[item]?.children?.map(i => {
      i.visible = false;
    });
  });
}
const getInfo = async () => {
  let data = await getGroupInfoById({ id: groupId.value });
  if (data.statusCode != 200) {
    return proxy.$modal.msgError(data.message);
  }
  groupInfo.value = data.data;
  getList();
};

const getList = async () => {
  let data = {};
  if (type.value == 'category') {
    data = await listApolloByTeam({
      teamId: groupId.value,
    });
  }
  if (type.value == 'label') {
    data = await getGroupLabel({
      teamId: groupId.value,
    });
  }
  if (data.statusCode == 200) {
    data_list.value = data.data;
    cla_list.value = {};
    let list = {};
    data.data?.map(item => {
      item.checked = false;
      if (!item.parentId) {
        list[item.key] = item;
      } else {
        if (!list[item.parentId]) {
          list[item.parentId] = {};
        }
        if (!list[item.parentId]?.children) {
          list[item.parentId].children = [];
        }
        list[item.parentId].children.push(item);
      }
      Object.keys(list).map(k => {
        if (list[k].value) {
          cla_list.value[k] = list[k];
          if (Object.keys(parent_cla_id.value).length == 0) {
            parent_cla_id.value = list[k];
          }
        }
      });
    });
    if (parent_cla_id?.value?.value) {
      Object.keys(cla_list.value).map(item => {
        if (item == parent_cla_id.value.value) {
          parent_cla_id.value = cla_list.value[item];
        }
      });
    }
  }
};
getInfo();
</script>
<style lang="scss" scoped>
.container {
  padding: 30px;
  .container-back {
    display: flex;
    align-items: center;
    .back-text {
      position: relative;
      font-size: 24px;
      font-weight: 700;
      line-height: 29px;
      letter-spacing: 0;
      cursor: pointer;
      ::after {
        content: '';
        position: absolute;
        left: 0;
        bottom: 0;
        width: 100%;
        height: 6px;
        border-radius: 3px;
        background: rgba(170, 163, 245, 0.4);
      }
    }
    &:hover {
      cursor: pointer;
      color: #3a2be0;
    }
  }
  .container-body {
    border-radius: 18px;
    padding: 20px 44px;
    display: flex;
    align-items: center;
    .container-body-avatar {
      width: 168px;
      height: 168px;
      border-radius: 20px;
      background-color: rgb(58, 43, 224);
      margin-right: 5px;
    }
    .container-body-right {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      height: 168px;
      .container-body-content-title {
        font-size: 26px;
        font-weight: 800;
        line-height: 31px;
        letter-spacing: 0;
        color: #333;
        margin-bottom: 16px;
      }
      .container-body-creator {
        display: flex;
        align-items: center;
        margin-bottom: 10px;
        .creator-title {
          flex-shrink: 1;
          margin-right: 4px;
          line-height: 22px;
          color: #b2b7be;
          font-size: 16px;
        }
        .creator-name {
          color: #1b2337;
          font-size: 16px;
          margin-left: 6px;
        }
      }
    }
  }
  .container-footer {
    display: flex;
    flex-direction: column;
    margin-top: 50px;
    .change-btn {
      height: 42px;
      border-radius: 18px;
      border: 1px solid #eee;
      margin-bottom: 18px;
      display: flex;
      .change-btn-item {
        width: 112px;
        height: 40px;
        border-radius: 18px;
        margin: 0;
        display: flex;
        justify-content: center;
        color: #999;
        line-height: 40px;
        border: 1px solid transparent;
        &:hover {
          cursor: pointer;
        }
      }
      .change-btn-select {
        border: 1px solid #3a2be0;
        color: #3a2be0;
      }
    }
  }
  .cla-container {
    min-height: 300px;
    display: flex;
    font-size: 16px;
    .cla-container-parents,
    .cla-container-children {
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      position: relative;
      width: 320px;
      flex-basis: 320px;
      font-size: 14px;
      padding: 0 8px;
      border-right: 1px solid #e8eaec;
      border-left: 1px solid #e8eaec;
      .cla-container-item-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 16px;
        font-weight: 700;
        .tree-item-add {
          cursor: pointer;
        }
      }
      .cla-container-item-body {
        margin-top: 10px;
        .cla-container-item {
          display: flex;
          align-items: center;
          justify-content: space-between;
          position: relative;
          width: 100%;
          padding: 0 12px;
          font-weight: 500;
          color: #33383e;
          background-color: transparent;
          line-height: 36px;
          cursor: pointer;
          &:hover {
            background-color: #f6f7f9;
            border-radius: 4px;
          }
        }
        .cla-container-item-select {
          background-color: #f6f7f9;
          border-radius: 4px;
        }
      }
    }
    .cla-container-children {
      padding: 0 20px;
      flex: 1 1;
      .cla-container-item-header {
        display: flex;
        align-items: center;
        justify-content: flex-start;
        font-size: 16px;
        font-weight: 700;
      }
      .cla-container-item-body {
        width: 264px;
      }
    }
  }
}
.popup-container {
  z-index: 2;
  position: absolute;
  left: -40px;
  width: 65px;
  background-color: #fff;
  padding: 10px;
  display: flex;
  flex-direction: column;
  box-shadow:
    0 3px 6px -4px rgba(0, 0, 0, 0.12),
    0 6px 16px 0 rgba(0, 0, 0, 0.08),
    0 9px 28px 8px rgba(0, 0, 0, 0.05);
  .popup-item {
    &:hover {
      color: #409eff;
      cursor: pointer;
    }
  }
}
.empty-container {
  display: flex;
  justify-content: center;
  align-items: center;
  span {
    font-size: 14px;
    color: #999;
  }
  .create-cla-btn {
    color: #409eff;
    cursor: pointer;
  }
}
</style>
