<template>
<div class="conversion_list_container">
  <div class="set_time">
    <a-date-picker
      v-model="startValue"
      :locale="locale"
      show-time
      format="YYYY-MM-DD HH:mm:ss"
      placeholder="开始时间"
    />
    &nbsp; ~ &nbsp;
    <a-date-picker
      v-model="endValue"
      :locale="locale"
      show-time
      format="YYYY-MM-DD HH:mm:ss"
      placeholder="结束时间"
    />
    &nbsp; <a-button type="primary" @click="toSetTime">批量设置时间</a-button>
  </div>
  <a-spin :spinning="loading">
    <vxe-table
      class="table_list"
      ref="conversionTable"
      show-overflow
      show-header-overflow
      border
      resizable
      :row-config="{ isHover: true }"
      :data="info.transforms"
      height="auto"
      @checkbox-all="selectChange"
      @checkbox-change="selectChange"
    >
      <vxe-column type="checkbox" width="50"></vxe-column>
      <vxe-column
        type="seq"
        title="序号"
        width="60"
        align="center"
      ></vxe-column>
      <vxe-column field="name" title="名称"></vxe-column>
      <vxe-column field="nodeType" title="节点类型" width="90" v-if="info.jobType=='datadev'">
        <template #default="{ row }">
          <span v-if="row.nodeType == 1">模型</span>
          <span v-else-if="row.nodeType == 2">指标</span>
          <span v-else>--</span>
        </template>
      </vxe-column>
      <vxe-column field="startTime" title="开始时间" width="210">
        <template #default="{ row }">
          <a-date-picker
            :locale="locale"
            :allowClear="false"
            placeholder="请选择"
            :show-time="{ format: 'HH:mm:ss' }"
            format="YYYY-MM-DD HH:mm:ss"
            v-model="row.startDate"
            :disabled="info.totalSelect"
          />
        </template>
      </vxe-column>
      <vxe-column field="endTime" title="结束时间" width="210">
        <template #default="{ row }">
          <a-date-picker
            :locale="locale"
            :allowClear="false"
            placeholder="请选择"
            :show-time="{ format: 'HH:mm:ss' }"
            format="YYYY-MM-DD HH:mm:ss"
            v-model="row.endDate"
            :disabled="$versionCompare('v6.0.2302') && (type=='realStart' || type=='cronStart') ? true:info.totalSelect"
          />
        </template>
      </vxe-column>
      
      <template #empty>
        <a-empty
          :image-style="{
            height: '80px',
          }"
        >
          <span slot="description"> 暂无数据 </span>
        </a-empty>
      </template>
    </vxe-table>
  </a-spin>
</div>
</template>

<script>
const $dispatchcenter = require("@/api/dispatchcenter");
export default {
  name: "conversionList",
  props: {
    type: [String],
    info: [Object],
    show: [Boolean]
  },
  data() {
    return {
      loading: false,
      initData: [],
      checkedList: [],
      startValue: moment("1900-01-01 00:00:00"),
      endValue: moment("2099-01-01 00:00:00")
    };
  },
  watch: {
    show: {
      handler(val) {
        if(val) {
          this.startValue = moment("1900-01-01 00:00:00");
          this.endValue = moment("2099-01-01 00:00:00");
          this.checkedList = [];
          this.getList();
        }
      },
      immediate: true
    },
    'info.totalSelect'(val){
      this.info.transforms = this.initData.map(item => {
        return {
          ...item,
          startDate: val ? moment("1900-01-01 00:00:00") : item.startDate,
          endDate: val ? moment("2099-01-01 00:00:00") : item.endDate,
        }
      })
      this.info.transforms.forEach(item => {
        let idx = this.checkedList.findIndex(v => { return v.id == item.id });
        if (idx > -1) {
          this.$refs.conversionTable.setCheckboxRow([this.info.transforms[idx]], true);
          item.run = true;
        } else {
          item.run = false;
        }
      })
    }
  },
  created() {
    // this.getList()
  },
  methods: {
    // 启动时不设置停止时间，将停止时间置灰，默认2099-01-01
    getList() {
      this.loading = true;
      $dispatchcenter
        .getTransformsList(this.info.jobInfoId)
        .then((req) => {
          let info = req.data;
          let list = [];
          if (info && info.subJobs && info.subJobs.length > 0) {
            if ($versionCompare('v6.0.2302')) {
              list = info.subJobs.map(item => {
                return {
                  ...item,
                  startDate: item.lowerBound ? moment(Number(item.lowerBound)).format("YYYY-MM-DD HH:mm:ss") :  moment().subtract(1,"day").format("YYYY-MM-DD 00:00:00"),
                  endDate: (this.type=="realStart" || this.type=="cronStart") ? 
                    moment("2099-01-01 00:00:00") : (item.upperBound ? moment(Number(item.upperBound)).format("YYYY-MM-DD HH:mm:ss") : moment().format("YYYY-MM-DD 23:59:59")),
                  run: true,
                }
              });
            } else {
              list = info.subJobs.map(item => {
                return {
                  ...item,
                  startDate: item.lowerBound ? moment(Number(item.lowerBound)).format("YYYY-MM-DD HH:mm:ss") :  moment().subtract(1,"day").format("YYYY-MM-DD 00:00:00"),
                  endDate: this.type == "trigger" ? moment().format("YYYY-MM-DD 23:59:59") : 
                  (item.upperBound ? moment(Number(item.upperBound)).format("YYYY-MM-DD HH:mm:ss") : moment().format("YYYY-MM-DD 23:59:59")),
                  run: true,
                }
              });
            }
          }
          this.initData = list;
          this.info.transforms = JSON.parse(JSON.stringify(list));
          this.$nextTick(() => {
            this.$refs.conversionTable.setAllCheckboxRow(true);
            this.checkedList = this.$refs.conversionTable.getCheckboxRecords();
          })
        })
        .finally(() => {
          this.loading = false;
        })
    },
    checkStartTime() {
      let curTime = moment();
      for(let i = 0; i < this.info.transforms.length; i++) {
        let row = this.info.transforms[i];
        let diff = moment(row.startDate).diff(curTime);
        if(Number(diff) > 1000*60*60) {
          this.$message.warn(`${row.name}的开始时间超过当前时间1小时，请先执行手动任务！`);
          return false;
        }
        return true;
      }
    },
    selectChange() {
      this.checkedList = this.$refs.conversionTable.getCheckboxRecords();
      this.info.transforms.forEach((item, index) => {
        let idx = this.checkedList.findIndex(v => { return v.id == item.id });
        if(idx > -1) {
          item.run = true;
        } else {
          item.run = false;
        }
      })
    },

    toSetTime() {
      this.info.transforms.forEach(item => {
        if (item.run) {
          item.startDate = this.startValue || item.startDate;
          item.endDate = this.endValue || item.endDate;
        }
        
      })
    }


  }
};
</script>
<style lang="less" scoped>
.conversion_list_container {
  .set_time {
    text-align: right;
    margin-bottom: 6px;
  }
  .table_list{
    height: 280px;
    .ant-calendar-picker {
      width: 176px;
      min-width: auto !important;
    }
  }
}
 
</style>