<template>
  <div
    class="mainContentBox"
    style="padding: 0; box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1)"
  >
    <div class="header">
      <div class="title">绑定工序</div>
      <el-space>
        <el-button :icon="Back" size="small" @click="back">返回</el-button>
        <el-button
          :type="isChanged ? 'danger' : 'primary'"
          :icon="Document"
          size="small"
          @click="save"
          >保存</el-button
        >
      </el-space>
    </div>
    <div class="types">
      <el-dropdown @command="handleCommand">
        <div class="typeBox addBtn">
          <el-icon class="icon"><CirclePlus /></el-icon>
          <div class="name">添加组件</div>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item
              v-for="(com, index) in comTypes"
              :key="index"
              :command="com"
            >
              {{ com.compentName }}</el-dropdown-item
            >
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <VueDraggable
        v-model="selectProComs"
        :animation="150"
        ghostClass="ghost"
        @update="onUpdate"
        style="display: flex"
      >
        <div
          class="typeContainer"
          v-for="(item, index) in selectProComs"
          :key="index"
          @click="clickToolCom(item, index)"
        >
          <div class="typeBox others" :class="getNameClass(index)">
            <svg-icon :icon-class="item.compentType" size="30" />
            <div class="name">{{ item.compentName }}</div>
          </div>
          <Delete class="delete" @click.stop="clickDelete(index)" />
        </div>
      </VueDraggable>
    </div>
    <div class="binContainer">
      <div v-if="showCSJL"><TestCom /></div>
      <div v-else>
        <div v-if="isChanged || selectIndex === -1 || isNoneedEdit">
          <el-empty :image-size="200" :description="getTipContent()" />
        </div>
        <div v-else-if="tipTitle !== '编辑工序表单'">
          <div class="tipTitle">{{ tipTitle }}</div>
          <el-button
            type="primary"
            @click="creatNewData"
            style="margin-bottom: 15px"
            >新增</el-button
          >
          <el-button
            type="primary"
            @click="saveSortData"
            style="margin-bottom: 15px"
            >保存顺序</el-button
          >
          <BottomTable ref="bottomTableRef" :tableType="tableType" />
        </div>
        <div v-else>
          <div class="tipTitle">{{ tipTitle }}</div>
          <SetExcel />
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { Back, Document, CirclePlus, Delete } from "@element-plus/icons-vue";
import { VueDraggable } from "vue-draggable-plus";
import BottomTable from "@/views/base/craftManagement/route/components/bottomTable.vue";
import { comTypes } from "@/views/base/craftManagement/route/components/configs";
import { saveCompoents, getCompoentsList } from "@/api/craft/process/index";
import TestCom from "./testCom.vue";
import SetExcel from "./setExcel/index.vue";
import { useCommonStoreHook, useDictionaryStoreHook } from "@/store";
import {
  processesByRouteId,
  saveProcessInRoute,
} from "@/api/craft/route/index";

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

//组件是否已经改变，如果改变了需要点击保存，才能展示下面的table
const isChanged = ref(true);
const isNoneedEdit = ref(false); //有些组件是不需要编辑的，所以下面也不展示table
const loadTopList = () => {
  getCompoentsList(route.params.id).then((res) => {
    selectProComs.value = res.data || [];
    selectIndex.value = -1;
    isChanged.value = false;
  });
};

onMounted(async () => {
  operationId.value = route.params.id;
  await loadTopList();
});

// 顶部====================
const back = () => {
  router.back();
};
const operationId = ref("");
provide("operationId", operationId);
const save = async () => {
  for (let i = 0; i < selectProComs.value.length; i++) {
    selectProComs.value[i].operationId = route.params.id;
    selectProComs.value[i].sortNum = i;
  }
  let p = {
    operationId: route.params.id,
    dtos: selectProComs.value,
  };
  let res = await saveCompoents(p);
  ElMessage.success("保存成功");
  loadTopList();
};

// Tools顶部组件 ==================
// 已经存在的工序组件
const selectProComs = ref([]);
const selectIndex = ref(-1);
const currentCom = ref({});
const handleCommand = (item) => {
  let status = true;
  for (let i = 0; i < selectProComs.value.length; i++) {
    if (selectProComs.value[i].compentName == item.compentName) {
      status = false;
    }
  }
  if (status == true) {
    selectProComs.value.push(item);
    isChanged.value = true;
    selectIndex.value = -1;
  } else {
    ElMessage.error("请勿重复添加");
  }
};

// 点击某一个改变下面的table
const showCSJL = ref(false);
const clickToolCom = (com, index) => {
  showCSJL.value = false;
  // 如果上方组件更改了，提示先保存组件再操作
  if (isChanged.value) {
    ElMessage.warning("请先保存组件！！！");
    return;
  }
  tipTitle.value = "编辑" + com.compentName;
  selectIndex.value = index;
  currentCom.value = com;
  if (
    com.compentType === "jiluxiang" ||
    com.compentType === "mingpai" ||
    com.compentType === "duomeiticaiji" ||
    com.compentType === "tiaoshipipei" ||
    com.compentType === "jingu" ||
    com.compentType === "screwdriver"
  ) {
    isNoneedEdit.value = true;
  } else if (com.compentType === "ceshijilu") {
    showCSJL.value = true;
  } else {
    tableType.value = com.compentType;
    isNoneedEdit.value = false;
  }
};

const onUpdate = () => {
  console.log("update");
  isChanged.value = true;
  selectIndex.value = -1;
};

const clickDelete = (index) => {
  selectProComs.value.splice(index, 1);
  selectIndex.value = -1;
  isChanged.value = true;
};

const getNameClass = (index) => {
  return index === selectIndex.value ? "selected" : "normal";
};

// 底部table ===============
let tipTitle = ref({});
const bottomTableRef = ref({});
const tableType = ref("MARTERIAL_BOM");

const creatNewData = () => {
  bottomTableRef.value && bottomTableRef.value.startCreat();
};

const saveSortData = () => {
  bottomTableRef.value && bottomTableRef.value.saveSortData();
};

const getTipContent = (itemValue) => {
  if (isNoneedEdit.value) {
    return "标准组件无需编辑";
  }
  if (selectIndex.value === -1 && isChanged.value) {
    return "请先保存组件，然后再进行操作";
  }

  return "请先选择组件";
};
</script>

<style lang="scss" scoped>
.header {
  height: 50px;
  display: flex;
  background-color: #f5f7f9;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}
.types {
  width: 100%;
  height: 116px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  padding: 12px 20px;
  display: flex;
  .typeContainer {
  }
  .typeBox {
    height: 80px;
    width: 80px;
    border-radius: 4px 4px 4px 4px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .icon {
      height: 32px;
      width: 32px;
    }
    .name {
      width: 56px;
      height: 16px;
      font-weight: 500;
      font-size: 14px;
      line-height: 16px;
      text-align: center;
      font-style: normal;
      text-transform: none;
    }
  }

  .addBtn {
    border: 1px solid #0a59f7;
    color: #0a59f7;
  }
  .others {
    color: rgba(0, 0, 0, 0.9);
    border: 1px solid rgba(0, 0, 0, 0.9);
    margin-left: 20px;
    position: relative;
  }
  .delete {
    //position: absolute;
    //bottom: -20px;
    //left: 0;
    margin-left: 50px;
    width: 1em;
    height: 1em;
    color: red;
    visibility: hidden;
  }
  .typeContainer:hover {
    .delete {
      visibility: visible;
    }
  }
  .normal {
    background: #ffffff;
    border: 1px solid rgba(35, 32, 50, 0.1);
  }
  .selected {
    background: rgba(10, 89, 247, 0.2);
    border: 1px solid #0a59f7;
  }
}

.title {
  line-height: 44px;
  color: #6f7991;
  font-size: 14px;
  font-weight: bold;
  text-align: left;
  margin-left: 20px;
}

.binContainer {
  height: calc(100vh - 280px);
  width: 100%;
  background-color: #f5f7f9;
  overflow-y: auto;
  background-color: white;
  //margin-top: 16px;
  //margin-left: 20px;
  padding: 16px 20px;
}
.tipTitle {
  width: 84px;
  height: 16px;
  font-weight: 500;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.9);
  line-height: 16px;
  text-align: left;
  font-style: normal;
  text-transform: none;
  margin-bottom: 16px;
}
</style>
