<script setup>
import moduleDialog from "@/views//modules/project-config/components/test-module-dialog.vue";
import TitleHeader from "@/views/modules/project-config/com/titleHeader.vue";
import { delTestModule } from "@/api/project";
import { useCommonStoreHook } from "@/store";
import { getProjectById, getTestProjectList } from "@/api/project/index";
const { currentTestItemId } = toRefs(useCommonStoreHook());

const emits = defineEmits(["onClickItem"]);

const router = useRouter();
const route = useRoute();

const goGlobalConfig = () => {
  router.push({
    name: "GlobalConfig",
    params: { engineerId: route?.params?.engineerId },
  });
};

// 当前正在配置的项目信息
const currentProject = ref();
onMounted(() => {
  getProjectById(route?.params?.engineerId).then((res) => {
    currentProject.value = res.data;
    toGetTestProjectList();
  });
});

const testProjectList = ref([]);

const toGetTestProjectList = () => {
  // 获取完配置工程信息后，获取测试项目列表
  getTestProjectList({
    engineeringId: currentProject.value.id,
  }).then((res) => {
    testProjectList.value = res.data ?? [];
  });
};

let clickItem = null;
// 当点击了项目
const clickTestProjectItem = (item) => {
  let data = JSON.parse(JSON.stringify(item));
  clickItem = data;
  currentTestItemId.value = item.id;
  emits("onClickItem", data);
};

const moduleDialogRef = ref();
//新增测试项目
const addProject = () => {
  moduleDialogRef.value.openDialog(false, null, route?.params?.engineerId);
};
//修改测试项目
const editProject = () => {
  clickItem &&
    moduleDialogRef.value.openDialog(
      true,
      clickItem,
      route?.params?.engineerId
    );
};
//删除测试项目
const confirmEvent = () => {
  if (currentTestItemId.value) {
    delTestModule({
      id: currentTestItemId.value,
    }).then((res) => {
      currentTestItemId.value = "";
      toGetTestProjectList(); //重新获取
    });
  }
};
</script>

<template>
  <div class="project-message-col">
    <TitleHeader> 工程信息</TitleHeader>

    <div class="project-msg-item">
      <span class="text-l">产品：</span>
      <span class="text-r">{{ currentProject?.engineeringProductName }}</span>
      <!--      <span class="edit">-->
      <!--        <svg-icon icon-class="homeIcon2" />-->
      <!--      </span>-->
    </div>
    <div class="project-msg-item">
      <span class="text-l">工程类型：</span
      ><span class="text-r">{{ currentProject?.engineeringType }}</span>
    </div>
    <div class="project-msg-item">
      <span class="text-l">版本：</span
      ><span class="text-r">{{ currentProject?.engineeringVersion }}</span>
    </div>
    <div class="project-msg-item">
      <!--      <span class="text-l">更新时间：</span>-->
      <span class="text-r">{{ currentProject?.updated }}</span>
    </div>

    <div class="project-all-config" @click="goGlobalConfig">
      <svg-icon icon-class="project-config" />
      全局配置
    </div>

    <div class="project-test-btn">
      <div class="name">测试项目</div>
      <div class="btn">
        <el-popconfirm
          width="220"
          confirm-button-text="确认"
          cancel-button-text="取消"
          title="是否删除"
          @confirm="confirmEvent"
        >
          <template #reference>
            <span class="edit">
              <svg-icon icon-class="jianHao" />
            </span>
          </template>
        </el-popconfirm>
        <span class="edit" @click="addProject">
          <svg-icon icon-class="addHao" />
        </span>
        <span class="edit" @click="editProject">
          <svg-icon icon-class="homeIcon2" />
        </span>
      </div>
    </div>

    <div class="project-list-cont">
      <el-scrollbar>
        <div
          class="project-list-item"
          :class="[
            item.id === currentTestItemId ? 'selected-item' : 'normal-item',
          ]"
          v-for="item in testProjectList"
          :key="item.id"
          @click="clickTestProjectItem(item)"
        >
          {{ item.projectName }}
        </div>
      </el-scrollbar>
    </div>
  </div>

  <moduleDialog ref="moduleDialogRef" @saveFinish="toGetTestProjectList" />
</template>

<style scoped lang="scss">
.project-message-col {
  width: 200px;
  flex-shrink: 0;
  height: calc(100vh - $main-header-height);
  background-color: $hj-black-2;
  border-right: 1px solid $hj-white-4;
  .project-msg-item {
    position: relative;
    padding: 0 11px;
    margin-bottom: 10px;
    .text-l {
      font-size: var(--hj-fs-12);
      color: var(--fc-color-3);
    }
    .text-r {
      font-size: var(--hj-fs-14);
      color: var(--hj-white-1);
    }
    .edit {
      cursor: pointer;
      position: absolute;
      right: 20px;
      color: var(--hj-white-1);
    }
  }
  .project-all-config {
    width: 130px;
    height: 36px;
    line-height: 36px;
    margin: 0 auto;
    background: var(--fc-color-7);
    color: var(--hj-white-1);
    text-align: center;
    border-radius: 4px 4px 4px 4px;
    cursor: pointer;
  }
  .project-test-btn {
    display: flex;
    justify-content: space-between;
    margin: 0 12px;
    margin-top: 12px;
    position: relative;
    padding: 14px 0;
    &::before {
      position: absolute;
      top: 0;
      content: "";
      display: block;
      width: 100%;
      height: 1px;
      background-color: var(--hj-white-4);
    }
    .name {
      font-size: var(--hj-fs-14);
      font-weight: 400;
      color: var(--hj-white-1);
    }
    .btn {
      span {
        cursor: pointer;
        display: inline-block;
        //width: 18px;
        //height: 18px;
        text-align: center;
        line-height: 16px;
        border-radius: 0px 0px 0px 0px;
        //border: 1px solid #ffffff;
        color: var(--hj-white-1);
        //&:nth-of-type(2) {
        margin-left: 8px;
        //}
      }
    }
  }
  .project-list-cont {
    height: calc(100vh - $main-header-height - 310px);
    overflow-y: auto;
    .project-list-item {
      cursor: pointer;

      margin: 0 12px;
      margin-bottom: 12px;
      height: 36px;
      line-height: 36px;
      text-align: center;

      border-radius: 4px 4px 4px 4px;
      border: 1px solid #3b7cff;
    }

    .normal-item {
      background-color: transparent;
      color: $hj-white-3;
      font-weight: normal;
      font-size: var(--hj-fs-12);
    }

    .selected-item {
      background: rgba(59, 124, 255, 0.5);
      color: $hj-white-1;
      font-weight: bold;
      font-size: var(--hj-fs-14);
    }
  }
}
</style>
