<template>
  <a-drawer
    :width="['transfer','datadev'].indexOf(formList.jobType) > -1 ? '65%' : '40%'"
    :visible="visible"
    @close="visible=false"
    :destroy-on-close="true"
  >
    <template slot="title"> 执行一次 </template>
    <div class="execute_once_container">
      <a-form-model
        v-bind="['transfer','datadev'].indexOf(formList.jobType) > -1 ? labelColOne : labelColTwo"
        :model="formList"
        ref="triggerForm"
      >
        <template v-if="['transfer','datadev'].indexOf(formList.jobType) > -1">
          <a-form-model-item label="任务ID" prop="newJobId" v-if="formList.jobType == 'transfer'">
            <a-input
              v-model="formList.newJobId"
              placeholder="请输入任务ID"
              disabled
            />
          </a-form-model-item>
          <a-form-model-item label="选择集群" prop="clusterId">
            <a-select
              class="middle"
              placeholder="请选择"
              v-model="formList.clusterId"
              :options="clusterList"
            >
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="执行机构" prop="executeLocation" v-if="kafkaEnabled=='1'">
            <a-select
              class="middle"
              placeholder="请选择"
              v-model="formList.executeLocation"
              :options="executeLocationList"
            >
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="全量">
            <a-checkbox v-model="formList.totalSelect"></a-checkbox>
          </a-form-model-item>
          <a-form-model-item :label="formList.jobType == 'transfer' ? '转换列表':'节点列表'">
            <conversion-list
              type="trigger"
              :show="visible"
              :info="formList"
            ></conversion-list>
          </a-form-model-item>
        </template>
        <a-form-model-item label="其它任务参数" prop="otherTaskId">
          <a-textarea
            v-model="formList.otherTaskId"
            placeholder="请输入内容"
            :auto-size="{ minRows: 2, maxRows: 4 }"
          />
        </a-form-model-item>
      </a-form-model>
    </div>
    <div class="a_foot_nuttop">
      <a-button @click="visible=false"> 取消 </a-button>
      &nbsp;
      <a-button type="primary" @click="triggerSubmit"> 确定 </a-button>
    </div>
  </a-drawer>
</template>

<script>
const conversionList = require("../components/conversionlist.vue").default;
const $dispatchcenter = require("@/api/dispatchcenter");
const $dataIntegration = require("@/api/dataIntegration");
const $appService = require("w-vue-middle/api/appService");
export default {
  components: {
    conversionList
  },
  props: {
    show: [Boolean],
    info: [Object]
  },
  computed: {
    visible: {
      get() {
        return this.show;
      },
      set(val) {
        this.$emit("update:show", val);
      }
    }
  },
  watch: {
    visible(val) {
      if(val){
        this.formList = this.info;
        this.getKafkaEnabled();
        this.getClusterList();
      }
    }
  },
  data() {
    return {
      clusterList: [],
      kafkaEnabled: "0",
      executeLocationList: [],
      labelColOne: { labelCol: { span: 3 }, wrapperCol: { span: 20 } },
      labelColTwo: { labelCol: { span: 6 }, wrapperCol: { span: 16 } },
      formList: {}
    };
  },
  created() {},
  methods: {
    triggerSubmit() {
      let transforms = this.formList.transforms.map((item) => {
        return {
          ...item,
          lowerBound: item.startDate
            ? moment(item.startDate).format("x")
            : null,
          upperBound: item.endDate ? moment(item.endDate).format("x") : null,
          startDate: undefined,
          endDate: undefined,
        };
      });
      let executorParam = `${this.formList.newJobId},${
        this.formList.operatingMode
      },${this.formList.clusterId || ""},${
        this.formList.otherTaskId
      }`;
      $dispatchcenter
        .jobInfoTrigger({
          id: this.formList.jobInfoId,
          executorParam,
          flinkJobParam: {
            transforms: transforms,
            executeLocation: this.kafkaEnabled == '1' ? this.formList.executeLocation : undefined
          },
        })
        .then((req) => {
          this.$message.success("执行一次成功！");
          this.visible = false;
          this.$emit("callback");
          // this.getFlowList();
          // this.triggerInfo.show = false;
        });
    },
    /**
     * @Author: y_zp
     * @description: 获取系统参数kafka.enabled
     */
    getKafkaEnabled() {
      $appService
        .sysConfigByKey("kafka.enabled")
        .then((req) => {
          this.kafkaEnabled = req.data || "0";
          if (this.kafkaEnabled == "1") {
            this.getExecuteLocation();
          }
        })
    },
    /**
     * @Author: y_zp
     * @description: 获取执行机构列表
     */
    getExecuteLocation() {
      $appService
        .getSysDictData({
          pageNum: 1,
          pageSize: 1000000,
          queryParams: { F_dictType_eq: "execution_location" },
        })
        .then((req) => {
          this.executeLocationList = req.data.map((item) => {
            return {
              label: item.dictLabel,
              value: item.dictValue,
            };
          });
        });
    },
    /**
     * @Author: y_zp
     * @description: 获取集群列表
     */
    getClusterList() {
      $dataIntegration
        .getClusterManager({
          pageNum: 1,
          pageSize: 10000,
          queryParams: {
            F_isDel_eq: 0,
          },
        })
        .then((req) => {
          this.clusterList = req.data.map((item) => {
            return {
              value: item.clusterId,
              label: item.name,
            };
          });
        })
        .finally(() => {});
    },

  }
};
</script>
<style lang="less" scoped>
.execute_once_container {
  height: calc(100vh - 130px);
  overflow: hidden;
  overflow-y: scroll;
}
</style>