<template>
  <div class="task-add">
    <div class="task-add-header" v-interact>
      <div class="header__title">定时任务</div>
      <div class="header-right">
        <div class="header-right__close" @click="$emit('close')">关闭</div>
      </div>
    </div>
    <div class="task-add-main">
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="执行日期" prop="date" required>
          <el-date-picker
            v-model="form.date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="mini"
            popper-class="mmc"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="执行时间" prop="time" required>
          <el-time-picker
            is-range
            v-model="form.time"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            placeholder="选择时间范围"
            size="mini"
            popper-class="mmc"
            value-format="HH:mm:ss"
          ></el-time-picker>
        </el-form-item>
        <el-form-item label="任务名称" required>
          <el-select v-model="form.normalTaskId" size="mini" popper-class="mmc" style="width:100%">
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in taskList.normal"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="task-add-btn">
      <div class="task-add__btn" @click="onConfirm">确认</div>
    </div>
  </div>
</template>

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

export default {
  name: "TaskAdd",
  inject: ["rootNode"],
  data() {
    return {
      form: {
        date: [],
        time: ['00:00:00', '00:00:00'],
        normalTaskId: "",
      },
      pickerOptions: {
        disabledDate: (time) => {
          // 日期选择限制
          return time.getTime() < Date.now() - 8.64e7;
        },
      }
    };
  },
  computed: {
    ...mapState("MMCFlightControlCenter/hangar", ["taskList"]),
    ...mapState("MMCFlightControlCenter", ["airwayEntities"]),
    /**
     * 选择的日常任务数据
     */
    selectedTask() {
      let find = this.taskList.normal.find((item) => {
        return item.id === this.form.normalTaskId;
      });
      return find;
    },
  },
  watch: {
    selectedTask: {
      async handler(newVal, oldVal) {
        //清除旧的航线并渲染新航线
        let find = this.airwayEntities.find(
          (item1) => oldVal?.airwayId === item1.airwayId
        );
        if (find) {
          this.clearAirwayEntities({ id: find.airwayId });
        }

        //渲染新航线
        if (newVal) {
          await this.getAirway(newVal);
          let airway = JSON.parse(newVal.airway.content);
          this.createAirwayEntities({
            polyline: airway.content,
            id: newVal.airwayId,
          });
        }
      },
    },
  },
  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);
        }
      }
    },

    /**
     * 确认事件
     */
    onConfirm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.rootNode.$emit("hangarTaskAdd", {
            type: 3, //1: 日常任务 2.定时任务 3.周期任务
            taskList: [this.form], //任务数据
          }); // 根节点发送机库任务新增事件
          this.$emit("close");
        } else {
          return false;
        }
      });
    },
  },
};
</script>

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

  &.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__close {
        font-size: 16px;
        cursor: pointer;
      }
    }
  }

  .task-add-main {
    padding: 20px 20px 0 10px;
  }

  .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>