<template>
  <div class="task-add dialog1027" v-interact>
    <div class="dialog-header">
      <div class="dialog-header__title">定时任务</div>
      <div class="dialog-header__close" @click="$emit('close')">关闭</div>
    </div>
    <div class="dialog-content">
      <el-form ref="form" :model="form" :rules="rules" 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", "23:59:59"],
        normalTaskId: "",
      },
      rules: {
        date: [{ required: true, message: "请选择日期", trigger: "blur" }],
        time: [{ required: true, message: "请选择时间", trigger: "blur" }],
        normalTaskId: [
          { required: true, message: "请选择任务", trigger: "blur" },
        ],
      },
      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);
          this.createAirwayEntities({
            polyline: newVal.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 === 0) {
          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, airway: this.selectedTask.airway }], //任务数据
          }); // 根节点发送机库任务新增事件
          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;
  }

  .dialog-header {
    padding-left: 16px !important;
  }

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

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

    .task-add__btn {
      margin: auto;
      width: 92px;
      height: 36px;
      background: #3388ff;
      border-radius: 2px;
      cursor: pointer;
      text-align: center;
      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>