<template>
  <!-- 采集任务弹框 -->
  <div>
    <el-dialog
      :title="title + '任务'"
      :visible.sync="showFormDia"
      :before-close="handleClose"
      width="65%"
    >
      <el-form
        ref="formInfo"
        :rules="rules"
        :model="params"
        label-width="150px"
      >
        <el-row>
          <el-col :span="11">
            <el-form-item label="任务名称：" prop="taskName">
              <el-input
                size="mini"
                clearable
                placeholder="请输入任务名称"
                v-model="params.taskName"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="任务类型：" prop="taskType">
              <el-select
                size="mini"
                style="width:100%"
                placeholder="请选择任务类型"
                clearable
                v-model="params.taskType"
              >
                <el-option
                  v-for="item in taskTypes"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="19">
            <el-form-item label="任务执行类：" prop="taskClass">
              <el-input
                size="mini"
                clearable
                style="width: 100%"
                placeholder="请输入任务执行类"
                v-model="params.taskClass"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button @click="verification">验证任务执行类</el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item inline="true" label="任务频率：" prop="taskRate">
              <el-input
                size="mini"
                clearable
                placeholder="请输入调用频率"
                v-model="params.taskRate"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6" style="margin-left:-150px">
            <el-form-item>
              <el-select size="mini" clearable v-model="params.taskRateUnit">
                <el-option
                  v-for="item in taskRateUnits"
                  :key="item.id"
                  :label="item.label"
                  :value="item.label"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="数据源：" prop="sourceCode">
              <el-select
                size="mini"
                style="width:100%"
                placeholder="请选择数据源"
                clearable
                v-model="params.sourceCode"
                @change="changCode"
              >
                <el-option
                  v-for="item in dataSources"
                  :key="item.id"
                  :label="item.alias"
                  :value="item.alias"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="源表名：" prop="sourceTable">
              <el-select
                size="mini"
                style="width:100%"
                placeholder="请输入关键字搜索"
                clearable
                filterable
                remote
                v-model="params.sourceTable"
                :remote-method="remoteMethod"
              >
                <el-option
                  v-for="(item, index) in sourceTables"
                  :key="index"
                  :label="item.name"
                  :value="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="任务说明：" prop="taskMemo">
              <el-input
                type="textarea"
                :autosize="{minRows: 4}"
                placeholder="请输入内容"
                v-model="params.taskMemo"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-tooltip
          content="请暂停任务后修改"
          :disabled="(this.title === '新增'||this.params.taskStatus==='NONE')? true : this.params.taskStatus==='PAUSED'?true:false"
          placement="top"
        >
          <span>
            <el-button
              type="primary"
              @click="goInfo"
              :disabled="this.params.taskStatus === 'NORMAL' ? true : false"
              >保 存</el-button
            >
          </span>
        </el-tooltip>
        <el-button @click="handleClose" style="margin-left:10px"
          >取 消</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import req from '@/request.js'
import collocation from '@/api/collocation.js'
export default {
  name: 'FormDialog',
  props: {
    showFormDia: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    pId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      params: {
        taskName: '', //任务名称
        taskType: undefined, //任务类别
        taskClass: '', //任务执行类
        taskRate: null, //任务调用频率
        taskRateUnit: 'm', //任务调用频率单位(m:分钟，h:小时，d:天，w:周，M:月，Y：年)
        sourceCode: '', //数据源
        sourceTable: '', //源表名
        taskMemo: '', //任务说明
        id: null,
        taskStatus: ''
      },
      dataSources: [], //数据源列表
      sourceTables: [], //源表名列表
      show: true,
      tooltip: '',
      taskTypes: [
        {value: 1, label: '定时任务'},
        {value: 2, label: '非定时任务'}
      ], //任务类别列表
      taskRateUnits: [
        {value: 0, label: 'm'},
        {value: 1, label: 'h'},
        {value: 2, label: 'd'},
        {value: 3, label: 'w'},
        {value: 4, label: 'M'},
        {value: 5, label: 'Y'}
      ], //任务调用频率单位列表
      rules: {
        taskName: [
          {
            required: true,
            message: '请输入任务名称',
            trigger: 'blur'
          }
        ],
        taskType: [
          {
            required: true,
            message: '请选择任务类别',
            trigger: 'blur'
          }
        ],
        taskClass: [
          {
            required: true,
            message: '请输入任务执行类',
            trigger: 'blur'
          }
        ],
        taskRate: [
          {
            required: true,
            message: '请输入任务调用频率',
            trigger: 'blur'
          }
        ],
        sourceCode: [
          {
            required: true,
            message: '请输入数据源',
            trigger: 'blur'
          }
        ],
        sourceTable: [
          {
            required: true,
            message: '请输入源表名',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getDataSources()
    this.getDetailInfo()
  },
  methods: {
    // 关闭新增窗口
    handleClose() {
      this.params = {
        taskName: null, //任务名称
        taskType: null, //任务类别
        taskClass: '', //任务执行类
        taskRate: null, //任务调用频率
        taskRateUnit: '', //任务调用频率单位(m:分钟，h:小时，d:天，w:周，M:月，Y：年)
        sourceCode: '', //数据源
        sourceTable: '', //源表名
        taskMemo: '', //任务说明
        id: null
      }
      this.$emit('closeFormDiao')
    },
    // 获取数据源
    getDataSources() {
      let portal = window.context.portal
      req.get(portal + '/sys/sysDataSource/v1/getDataSources').then(res => {
        this.dataSources = res.data
      })
    },
    changCode() {
      console.log(this.sourceTables)
    },
    //获取源表名
    remoteMethod(query) {
      let obj = {
        dsalias: this.params.sourceCode,
        isTable: '1',
        objName: query
      }
      this.sourceTables = []
      collocation.sourceTables(obj, res => {
        this.sourceTables = res
        console.log(res)
      })
    },
    // 根据ID查询
    getDetailInfo() {
      if (this.title === '修改') {
        collocation.detailId(this.pId, res => {
          this.params.taskName = res.taskName
          this.params.taskType = res.taskType
          this.params.taskClass = res.taskClass
          this.params.taskRate = res.taskRate
          this.params.taskRateUnit = res.taskRateUnit
          this.params.sourceCode = res.sourceCode
          this.params.sourceTable = res.sourceTable
          this.params.taskMemo = res.taskMemo
          this.params.taskStatus = res.taskStatus
          this.params.id = res.id
        })
      } else {
      }
    },

    // 保存新增任务
    goInfo(e) {
      this.$refs.formInfo.validate(valid => {
        console.log(valid)
        if (valid) {
          if (this.title === '新增') {
            let arr = JSON.parse(JSON.stringify(this.params))
            collocation.addSave(this.params, response => {
              if (response.state) {
                this.$notify({
                  type: 'success',
                  message: '新增成功',
                  duration: 2000
                  // offset: 80
                })
                this.$emit('closeFormDiao')
                this.$emit('refList')
              } else {
              }
            })
          }
          if (this.title === '修改') {
            collocation.upData(this.params, response => {
              if (response.state) {
                this.$notify({
                  type: 'success',
                  message: '修改成功',
                  duration: 2000
                })
                this.$emit('closeFormDiao')
                this.$emit('refList')
              } else {
              }
            })
          }
        } else {
          return false
        }
      })
    },
    // 验证任务执行
    verification() {
      let className = this.params.taskClass
      collocation.verification(className, res => {
        console.log(res)
        if (res.state) {
          this.$message({
            message: '验证成功',
            type: 'success',
            duration: 2000
          })
        }
      })
    },
    getRowKey(row) {
      return row.id
    }
  }
}
</script>

<style lang="scss" scoped></style>
