<template>
  <div class="task-add" :class="{more: showMore}">
    <div class="task-add-header" v-interact>
      <div class="header__title">定时任务</div>
      <div class="header-right">
        <div class="header-right__add" @click="onTaskAdd">
          <span class="iconfont icon-zengjia"></span>添加任务
        </div>
        <div class="header-right__close" @click="$emit('close')">关闭</div>
      </div>
    </div>
    <div class="task-add-main">
      <div class="main-item" v-for="(item, index) in list" :key="index">
        <el-date-picker
          v-model="item.time"
          size="mini"
          popper-class="mmc"
          type="datetime"
          placeholder="选择时间"
          value-format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>任务名称
        <el-select
          v-model="item.normalTaskId"
          size="mini"
          popper-class="mmc"
          @change="onChangeTask(item)"
        >
          <el-option :label="item1.name" :value="item1.id" v-for="(item1, index) in taskList.normal"></el-option>
        </el-select>
        <el-input class="input" size="mini" :value="item.airway ? item.airway.name : ''" disabled />
        <el-tooltip content="删除" placement="top">
          <span class="icon-shanchu iconfont" @click="onDelTask(index)"></span>
        </el-tooltip>
        <el-tooltip content="查看" placement="top">
          <span
            class="icon-chakan1 iconfont"
            style="font-size: 10px;"
            @click="onSwitchAirway(item)"
          ></span>
        </el-tooltip>
      </div>
    </div>
    <div class="task-add-btn">
      <div class="task-add__btn" @click="onConfirm">确认</div>
    </div>
    <div class="task-add-more" @click="showMore = !showMore">
      <img src="../../../../assets/images/xb.png" width="15" />
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
import { AirLine } from "../../../../../../../../../../../../api";

export default {
  name: "TaskAdd",
  inject: ["rootNode"],
  data() {
    return {
      list: [
        /* {
          time: '',
          normalTaskId: '',
          airwayId: ''
        } */
      ],
      showMore: false,
    };
  },
  computed: {
    ...mapState("MMCFlightControlCenter/hangar", ["taskList"]),
    ...mapState("MMCFlightControlCenter", ["airwayEntities"]),
  },
  methods: {
    ...mapActions("MMCFlightControlCenter", [
      "createAirwayEntities",
      "clearAirwayEntities",
    ]),
    /**
     * 将任务中航线对象补充完整
     */
    async getAirway(item) {
      if (!item.airway) {
        let res = await AirLine.routeDetail({
          id: item?.airwayId,
        });
        if (res.code === 200) {
          this.$set(item, "airway", res.data);
        }
      }
    },
    /**
     * 显示或隐藏航线
     */
    async onSwitchAirway(item) {
      await this.getAirway(item);
      let find = this.airwayEntities.find(
        (item1) => item1.airwayId === item.airwayId
      );
      if (find) {
        this.clearAirwayEntities({ id: find.airwayId });
      } else {
        let airway = JSON.parse(item.airway.content);
        this.createAirwayEntities({
          polyline: airway.content,
          id: item.airwayId,
        });
      }
    },
    /**
     * 添加新任务
     */
    onTaskAdd(item) {
      this.list.push({
        time: "",
        normalTaskId: "",
        airwayId: "",
      });
    },
    /**
     * 任务内容更改
     */
    async onChangeTask(item) {
      let normalTask = this.selectedTask(item.normalTaskId);
      item.airwayId = normalTask.airwayId;
      let find = this.taskList.normal.find(task => task.airwayId === item.airwayId);
      if(find){
        item.airway = find.airway;
      }
      this.getAirway(item);
    },
    /**
     * 删除的任务
     */
    onDelTask(index) {
      this.list = this.list.filter((item, i) => i !== index);
    },
    /**
     * 选择的日常任务数据
     */
    selectedTask(normalTaskId) {
      let find = this.taskList.normal.find((item) => {
        return item.id === normalTaskId;
      });
      return find;
    },
    /**
     * 确认事件
     */
    onConfirm() {
      this.rootNode.$emit("hangarTaskAdd", {
        type: 2, //1: 日常任务 2.定时任务 3.周期任务
        taskList: this.list, //任务数据
      }); // 根节点发送机库任务新增事件
      this.$emit('close');
    },
  },
};
</script>

<style lang="scss" scoped>
.task-add {
  height: 250px;
  background: rgba(9, 32, 87, 0.7);
  border: 1px solid #70daf9;
  position: absolute;
  top: -5px;
  left: 550px;
  width: 700px;
  z-index: 1;
  display: flex;
  flex-direction: column;
  gap: 5px;

  &.more {
    height: 376px;
  }

  .task-add-header {
    flex-shrink: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 32px;
    background-image: linear-gradient(
      180deg,
      #9198ff,
      rgba(45, 81, 153, 0.22) 40%,
      #05091a
    );
    border: 1px solid #70daf9;
    box-shadow: inset 0 0 10px 2px #3f9dff;
    padding: 0 20px;

    .header__title {
      font-family: MicrosoftYaHei-Bold;
      font-size: 16px;
      color: #70daf9;
      letter-spacing: 0;
      font-weight: 700;
    }

    .header-right {
      display: flex;
      gap: 20px;
      font-family: MicrosoftYaHei;
      font-size: 16px;
      color: #70daf9;
      letter-spacing: 0;
      font-weight: 400;

      .header-right__add {
        font-size: 12px;
        display: flex;
        align-items: center;
        cursor: pointer;
      }
      .header-right__close {
        font-size: 16px;
        cursor: pointer;
      }
    }
  }

  .task-add-main {
    padding: 20px 20px 0 10px;
    flex: 1;
    overflow-y: auto;

    .main-item::v-deep {
      margin-bottom: 20px;
      display: flex;
      align-items: center;
      font-size: 15px;
      color: #fff;
      gap: 5px;

      .el-date-editor {
        width: 188px;
      }

      .el-select {
        width: 200px;
      }

      .input {
        width: 150px;
      }

      .iconfont {
        color: rgb(67, 222, 255);
        cursor: pointer;
      }
    }
  }

  .task-add-btn {
    flex-shrink: 0;

    .task-add__btn {
      margin: auto;
      width: 122px;
      height: 32px;
      cursor: pointer;
      text-align: center;
      background-image: linear-gradient(
        180deg,
        #9198ff,
        rgba(45, 81, 153, 0.22) 40%,
        #05091a
      );
      border: 1px solid #70daf9;
      box-shadow: inset 0 0 10px 2px #3f9dff;
      color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }

  .task-add-more {
    background-color: rgba(13, 82, 143, 0.6);
    height: 24px;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>