<template>
  <div class="task-add dialog1027" :class="{more: showMore}" v-interact>
    <div class="task-add-header dialog-header">
      <div class="header__title dialog-header__title">定时任务</div>
      <div class="header-right dialog-header__close">
        <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 dialog-content">
      <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"
          :picker-options="pickerOptions"
        ></el-date-picker>
        <el-select
          v-model="item.normalTaskId"
          size="mini"
          popper-class="mmc"
          @change="onChangeTask(item, index)"
          placeholder="请选择任务"
        >
          <el-option
            :label="item1.name"
            :value="item1.id"
            v-for="(item1, index) in taskList.normal"
          ></el-option>
        </el-select>
        <el-input
          placeholder="航线名称"
          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,
      pickerOptions: {
        disabledDate: (time) => {
          // 日期选择限制
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
    };
  },
  computed: {
    ...mapState("MMCFlightControlCenter/hangar", ["taskList"]),
    ...mapState("MMCFlightControlCenter", ["airwayEntities"]),
  },
  methods: {
    ...mapActions("MMCFlightControlCenter", [
      "createAirwayEntities",
      "clearAirwayEntities",
    ]),
    /**
     * 将任务中航线对象补充完整
     */
    async getAirway(id) {
      let res = await AirLine.routeDetail({
        id: id,
      });
      if (res.code === 0) {
        return res.data;
      }
    },
    /**
     * 显示或隐藏航线
     */
    async onSwitchAirway(item) {
      let find = this.airwayEntities.find(
        (item1) => item1.airwayId === item.airwayId
      );
      if (find) {
        this.clearAirwayEntities({ id: find.airwayId });
      } else {
        this.createAirwayEntities({
          polyline: item.airway.content,
          id: item.airwayId,
        });
      }
    },
    /**
     * 添加新任务
     */
    onTaskAdd(item) {
      this.list.push({
        time: "",
        normalTaskId: "",
        airwayId: "",
      });
    },
    /**
     * 任务内容更改
     */
    async onChangeTask(item, i) {
      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;
      }
      if (!find.airway) {
        item.airway = await this.getAirway(item.airwayId);
      }
      this.$set(this.list, i, 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() {
      let isOk = true;
      this.list.some((item) => {
        if (!item.time) {
          this.$message.warning("请选择时间")
          isOk = false;
          return true;
        }
        if (!item.normalTaskId) {
          this.$message.warning("请选择任务")
          isOk = false;
          return true;
        }
      });
      if (isOk) {
        this.rootNode.$emit("hangarTaskAdd", {
          type: 2, //1: 日常任务 2.定时任务 3.周期任务
          taskList: this.list, //任务数据
        }); // 根节点发送机库任务新增事件
        this.$emit("close");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.task-add {
  height: 250px;
  position: absolute;
  top: -5px;
  left: 550px;
  width: 620px;
  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;
    padding: 0 20px 0 0;

    .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;
      height: fit-content;

      .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 {
      width: 92px;
      height: 36px;
      background: #3388ff;
      border-radius: 2px;
      text-align: center;
      line-height: 32px;
      margin: 0 auto;
      margin-bottom: 10px;
      cursor: pointer;
      color: #fff;
    }
  }

  .task-add-more {
    background-color: #191919;
    height: 24px;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>