<template>
  <el-dialog
    title='修改'
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="数据集名称" prop="datasetName">
        <el-input v-model="dataForm.datasetName" placeholder="数据集名称, 如: testTask"></el-input>
      </el-form-item>

      <el-form-item label="分类" prop="params">
<!--        <el-input v-model="dataForm.params" placeholder="参数"></el-input>-->
        <el-select v-model="dataForm.params" placeholder="类别" >
          <el-option v-for="data in dataForm.classification" :key="data.categoryId" :label="data.categoryName" :value="data.categoryId">
          </el-option>

        </el-select>
      </el-form-item>
      <el-form-item label="cron表达式" prop="cronExpression">
        <el-input v-model="dataForm.cronExpression" placeholder="如: 0 0 12 * * ?"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { Message } from "element-ui"
import { MessageBox } from 'element-ui'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        datasetName: '',
        params: '',
        classification: '',
        cronExpression: '',
        remark: '',
        status: 0
      },
      dataRule: {
        datasetName: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        cronExpression: [
          { required: true, message: 'cron表达式不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (info) {
      // console.log(info)

      this.dataForm.datasetName = info.datasetName
      this.dataForm.params = info.categoryId
      this.dataForm.cronExpression = info.cronExpression
      this.dataForm.remark = info.remark
      this.dataForm.status = info.status
      this.dataForm.id = info.datasetId
      this.dataForm.classification = info.classification
      this.visible = true
      // this.dataForm.id = id || 0
      // this.visible = true
      // this.$nextTick(() => {
      //   this.$refs['dataForm'].resetFields()
      //   if (this.dataForm.id) {
      //     this.$http({
      //       url: this.$http.adornUrl(`/sys/schedule/info/${this.dataForm.id}`),
      //       method: 'get',
      //       params: this.$http.adornParams()
      //     }).then(({data}) => {
      //       if (data && data.code === 0) {
      //         this.dataForm.beanName = data.schedule.beanName
      //         this.dataForm.params = data.schedule.params
      //         this.dataForm.cronExpression = data.schedule.cronExpression
      //         this.dataForm.remark = data.schedule.remark
      //         this.dataForm.status = data.schedule.status
      //       }
      //     })
      //   }
      // })
    },
    // 表单提交
    dataFormSubmit () {
      // console.log(this.dataForm.params)
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.visible = false
          MessageBox.confirm(`确定保存修改?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
              this.$http({
              url: this.$http.adornUrl(`/sys/schedule/update`),
              method: 'post',
              data: this.$http.adornData({
                'datasetId': this.dataForm.id || undefined,
                'datasetName': this.dataForm.datasetName,
                'categoryId': this.dataForm.params, // 分类对应的ID
                'cronExpression': this.dataForm.cronExpression,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                Message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                Message.error(data.msg)
              }
            })
          }).catch(() => {
            this.visible=true
          })

          

        }
      })
    }
  }
}
</script>
