<template>
  <div class="task-add dialog1027" :class="{more: showMore}" v-interact>
    <div class="task-add-header dialog-header">
      <img class="dialog-header__icon" src="../../../../../../assets/images/mount_head.png" />
      <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.taskId"
          clearable
          size="mini"
          popper-class="mmc"
          @change="onTaskChange(item)"
          placeholder="请选择任务"
          v-if="useTask"
        >
          <el-option v-for="(item , i) in taskList" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <el-select
          v-model="item.airwayId"
          size="mini"
          popper-class="mmc"
          placeholder="请选择航线"
          :disabled="!!item.taskId"
        >
          <el-option :label="item1.name" :value="item1.id" v-for="(item1, index) in airwayList"></el-option>
        </el-select>
        <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">
      <el-button type="primary" size="medium" :loading="confirmLoading" @click="onConfirm">确认</el-button>
    </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: '',
          airwayId: '',
          taskId: '',
        } */
      ],
      showMore: false,
      pickerOptions: {
        disabledDate: (time) => {
          // 日期选择限制
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
      airwayList: [], //航线列表
      confirmLoading: false,
      taskList: [], //任务列表
    };
  },
  computed: {
    ...mapState("MMCFlightControlCenter/hangar", ["hangar"]),
    ...mapState("MMCFlightControlCenter", ["airwayEntities", "useTask"]),
  },
  mounted() {
    this.rootNode.$emit("airwayListGet", {
      pageNo: 1,
      pageSize: 100,
      hangar: this.hangar,
      callback: (data) => {
        this.airwayList = data?.records || [];
      },
    });

    this.rootNode.$emit("taskListGet", {
      pageNo: 1,
      pageSize: 100,
      type: 1,
      isHangar: true,
      hangar: null,
      callback: (res) => {
        this.taskList = res?.records || [];
      },
    });
  },
  methods: {
    ...mapActions("MMCFlightControlCenter", [
      "createAirwayEntities",
      "clearAirwayEntities",
    ]),
    /**
     * 显示或隐藏航线
     */
    async onSwitchAirway(item) {
      this.rootNode.$emit("airwayGet", {
        airwayId: item.airwayId,
        callback: (airway) => {
          let find = this.airwayEntities.find(
            (item1) => item1.airwayId === airway.id
          );
          if (find) {
            this.clearAirwayEntities({ id: airway.id });
          } else {
            this.createAirwayEntities({
              polyline: airway.content,
              id: airway.id,
            });
          }
        },
      });
    },
    /**
     * 添加新任务
     */
    onTaskAdd(item) {
      this.list.push({
        time: "",
        airwayId: "",
      });
    },
    /**
     * 删除的任务
     */
    onDelTask(index) {
      this.list = this.list.filter((item, i) => i !== index);
    },
    /**
     * 确认事件
     */
    onConfirm() {
      let isOk = true;
      this.list.some((item) => {
        if (!item.time) {
          this.$message.warning("请选择时间");
          isOk = false;
          return true;
        }
        if (!item.airwayId) {
          this.$message.warning("请选择航线");
          isOk = false;
          return true;
        }
      });
      if (isOk) {
        let list = this.list.map((item) => {
          let find = this.airwayList.find(
            (airway) => airway.id === item.airwayId
          );
          return {
            ...item,
            airway: find,
            time: [item.time, item.time]
          };
        });
        this.confirmLoading = true;
        this.rootNode.$emit("taskBatchAdd", {
          type: 1, //0: 日常任务 1.定时任务 2.周期任务
          taskList: list, //任务数据
          hangar: this.hangar,
          callback: ({ code }) => {
            this.confirmLoading = false;
            if (code === 0) {
              this.$emit("addDone");
              this.$emit("close");
            }
          },
        }); // 根节点发送机库任务新增事件
      }
    },
    /**
     * 任务更改事件
     */
    onTaskChange(item) {
      let find = this.taskList.find((item1) => {
        return item1.id == item.taskId;
      });
      if (find) {
        let findAirway = this.airwayList.find((item1) => {
          return item1.id === find.airwayId;
        });

        if (findAirway) {
          item.airwayId = find.airwayId;
          return;
        }
      }

      // 找不到航线
      item.airwayId = "";
      item.taskId = "";
      this.$message.warning("未找到相应的航线");
    },
  },
};
</script>

<style lang="scss" scoped>
.task-add {
  height: 250px;
  position: absolute;
  top: -5px;
  left: 550px;
  min-width: 520px;
  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;
    text-align: center;

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