<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.airwayId" size="mini" popper-class="mmc" style="width:100%">
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in airwayList"></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, Control_API } from "../../../../../../../../../../../../api";

export default {
  name: "TaskAdd",
  inject: ["rootNode"],
  data() {
    return {
      form: {
        date: [],
        time: ["00:00:00", "23:59:59"],
        airwayId: "",
      },
      rules: {
        date: [{ required: true, message: "请选择日期", trigger: "blur" }],
        time: [{ required: true, message: "请选择时间", trigger: "blur" }],
        airwayId: [{ required: true, message: "请选择航线", trigger: "blur" }],
      },
      pickerOptions: {
        disabledDate: (time) => {
          // 日期选择限制
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
      airwayList: [], //航线列表
    };
  },
  computed: {
    ...mapState("MMCFlightControlCenter/hangar", ["hangar"]),
    ...mapState("MMCFlightControlCenter", ["airwayEntities", "useSTLAirway"]),
    /**
     * 选择的日常任务数据
     */
    selectedAirway() {
      let find = this.airwayList.find((item) => {
        return item.id === this.form.airwayId;
      });
      return find;
    },
  },
  watch: {
    selectedAirway: {
      async handler(newVal, oldVal) {
        //清除旧的航线并渲染新航线
        let find = this.airwayEntities.find(
          (item1) => oldVal?.id === item1.airwayId
        );
        if (find) {
          this.clearAirwayEntities({ id: this.selectedAirway.id });
        }

        //渲染新航线
        if (newVal) {
          this.createAirwayEntities({
            polyline: this.selectedAirway.content,
            id: this.selectedAirway.id,
          });
        }
      },
    },
  },
  async mounted() {
    if (this.useSTLAirway) {
      let res = await Control_API.getUavRouteList({
        pageNo: 1,
        pageSize: 100,
        nestId: this.hangar.id,
      });
      if (res?.code === 0) {
        let airwayList = [];
        for (let i = 0; i < res.data.list.length; i++) {
          let item = res.data.list[i];
          let flightCourseJson;
          try {
            flightCourseJson = JSON.parse(item.flightCourseJson);
          } catch (e) {
            console.log(e);
          }
          // 转换成飞控中心能接受的数据协议
          let content = flightCourseJson
            ? await this.$store.dispatch(
                "MMCFlightControlCenter/apiPointsToFKZXPoints",
                {
                  list: flightCourseJson?.linePointSaveReqVOS || [],
                  actionListKey: "pointActionSaveReqVOS",
                }
              )
            : null;
          airwayList.push({
            name: item.flightName,
            id: item.id,
            content: content,
          });
        }
        this.airwayList = airwayList;
      }
    } else {
      this.rootNode.$emit("airwayListGet", {
        pageNo: 1,
        pageSize: 100,
        hangar: this.hangar,
        callback: (data) => {
          this.airwayList = data?.records || [];
        },
      });
    }
  },
  methods: {
    ...mapActions("MMCFlightControlCenter", [
      "createAirwayEntities",
      "clearAirwayEntities",
    ]),
    /**
     * 确认事件
     */
    onConfirm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.rootNode.$emit("taskBatchAdd", {
            type: 3, //1: 日常任务 2.定时任务 3.周期任务
            taskList: [{ ...this.form, airway: this.selectedAirway }], //任务数据
            hangar: this.hangar,
            callback: () => {
              this.$emit("addDone");
              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>