<template>
  <el-card class="box-card" shadow="always">
    <div slot="header" class="clearfix">
      <span>{{ title }}</span>
      <el-button-group style="float: right;">
        <el-button v-if="checkScene != 0" type="primary" size="mini" icon="el-icon-finished" :loading="loadingOptions.loading" :disabled="loadingOptions.isDisabled" @click="submitForm">{{ loadingOptions.loadingText }}</el-button>
        <el-button v-if="checkScene != 0" size="mini" type="primary" @click="checkSceneStart('0')">上一步</el-button>
        <el-button size="mini" icon="el-icon-back" @click="showCard">返回</el-button>
      </el-button-group>
    </div>
    <div v-if="checkScene == 0" style="padding-top: 40px; padding-bottom: 40px; text-align: center;"><h2 class="app-pts-web-h2">选择检测场景</h2><p style="margin-top: 10px; color: rgb(124, 127, 160);font-size: 14px">请选择下面合适的类型创建</p></div>
    <div v-if="checkScene == 0" class="style-module__grid-container--29AQk " style="margin: 0px 15%; --gap:20px; --template-cols:repeat(auto-fill, minmax(420px, 1fr));">
      <div class="app-pts-web-card style-module__grid-item--2nHmp" style="height: 200px; margin: 10px;">
        <div class="app-pts-web-card__body">
          <div class="app-pts-web-card__title">
            <div class="app-pts-web-justify-grid">
              <div class="app-pts-web-justify-grid__col app-pts-web-justify-grid__col--left">
                <h3 class="app-pts-web-h3"><span class="" style="color: rgb(0, 0, 0);">简单模式</span></h3>
              </div>
            </div>
          </div>
          <div class="app-pts-web-card__content">
            <div class="app-pts-web-justify-grid">
              <div class="app-pts-web-justify-grid__col app-pts-web-justify-grid__col--left">
                <p class="" style="min-height: 50px; font-size: 12px; line-height: 20px;">使用我们的交互式 UI 组合接入数据源、定义数据模型、创建检测用例、执行检测任务、查看检测结果与报告生成。</p>
                <el-button size="mini" type="primary" @click="checkSceneStart('1')">开始</el-button>
              </div>
              <div class="app-pts-web-justify-grid__col app-pts-web-justify-grid__col--right">
                <img src="data:image/svg+xml;base64,[encoded data, 5428 characters]" style="width: 120px;">
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="app-pts-web-card style-module__grid-item--2nHmp" style="height: 200px; margin: 10px;">
        <div class="app-pts-web-card__body">
          <div class="app-pts-web-card__title">
            <div class="app-pts-web-justify-grid">
              <div class="app-pts-web-justify-grid__col app-pts-web-justify-grid__col--left">
                <h3 class="app-pts-web-h3"><span class="" style="color: rgb(0, 0, 0);">脚本模式</span></h3>
              </div>
            </div>
          </div>
          <div class="app-pts-web-card__content">
            <div class="app-pts-web-justify-grid">
              <div class="app-pts-web-justify-grid__col app-pts-web-justify-grid__col--left">
                <p class="" style="min-height: 50px; font-size: 12px; line-height: 20px;">使用我们的代码示例作为脚本基础，或从头开始。通过脚本进行自动化数据标准符合性检测。</p>
                <el-button size="mini" type="primary"  @click="checkSceneStart('2')">开始</el-button>
              </div>
              <div class="app-pts-web-justify-grid__col app-pts-web-justify-grid__col--right">
                <img src="data:image/svg+xml;base64,[encoded data, 4708 characters]" style="width: 120px;">
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="body-wrapper" style="padding-left: 25%">
      <el-form ref="form" v-if="checkScene == 1" :model="form" :rules="rules" label-width="100px" style="width: 700px">
        <el-form-item label="任务名称" prop="jobName">
          <el-input v-model="form.jobName" placeholder="任务名称" />
        </el-form-item>
        <el-form-item label="任务类型" prop="jobType">
          <el-select v-model="form.jobType" placeholder="请选择任务类型" style="width: 100%" @change="ruleItemSelectChanged">
            <el-option
              v-for="item in ruleItemOptions"
              :key="item.typeId"
              :label="item.typeName"
              :value="item.typeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="检测用例" prop="checkCase">
          <el-select v-model="form.checkCase" multiple placeholder="检测用例" style="width: 100%" v-if="showCheckCase">
            <el-option
              v-for="item in checkCaseOptions"
              :key="item.id"
              :label="item.ruleName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="cron表达式" prop="cronExpression">
          <el-input v-model="form.cronExpression" placeholder="cron表达式" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.id"
              :label="dict.itemText"
            >{{ dict.itemValue }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="任务概述" prop="remark">
          <el-input v-model="form.remark" :rows="8" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
import { saveCheckJob } from '@/api/quality/checkjob'
import { pageCheckRule } from '@/api/quality/checkrule'

export default {
  name: 'CheckJobAdd',
  props: {
    data: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data() {
    return {
      title: '检测任务新增',
      checkScene: '0',
      // 展示切换
      showOptions: {
        data: {},
        showList: true,
        showAdd: false,
        showEdit: false,
        showDetail: false
      },
      showCheckCase: true,
      // 保存按钮
      loadingOptions: {
        loading: false,
        loadingText: '保存',
        isDisabled: false
      },
      // 表单参数
      form: {
        jobName: undefined,
        jobType: undefined,
        checkCase: '',
        cronExpression: undefined,
        beanName: 'qualityTask',
        methodName: 'task',
        status: '0'
      },
      // 表单校验
      rules: {
        jobName: [
          { required: true, message: '任务名称不能为空', trigger: 'blur' }
        ],
        jobType: [
          { required: true, message: '任务类型不能为空', trigger: 'change' }
        ],
        cronExpression: [
          { required: true, message: 'cron表达式不能为空', trigger: 'blur' }
        ]
      },
      checkCaseOptions:[],
      // 状态数据字典
      statusOptions: [],
      // 规则级别数据字典
      ruleLevelOptions: [],
      // 核查类型数据字典
      ruleItemOptions: [
        { typeId: 'structure', typeName: '结构符合性' },
        { typeId: 'content', typeName: '内容符合性' },
        { typeId: 'relevance', typeName: '关联符合性' }
      ],
      sourceOptions: [],
      tableOptions: [],
      columnOptions: [],
      dictTypeOptions: [],
      relatedColumnOptions: []
    }
  },
  created() {
    this.form.jobType = this.data.jobType
    this.getDicts('sys_common_status').then(response => {
      if (response.success) {
        this.statusOptions = response.data
      }
    })
    this.getCheckRule()
  },
  methods: {
    showCard() {
      this.$emit('showCard', this.showOptions)
    },
    ruleItemSelectChanged(val) {
      const item = this.ruleItemOptions.find(function (item) {
        return item.typeId === val
      })
      this.form.jobType = item.typeId
      this.getCheckRule()
    },
    checkSceneStart(scene){
      this.checkScene = scene
    },
    getCheckRule() {
      let ruleType = 'nr'
      if(this.form.jobType == 'structure'){
        ruleType = 'jg'
      } else if(this.form.jobType == 'relevance'){
        ruleType = 'gl'
      }
      let params = {
        pageNum: 1,
        pageSize: 20,
        projectId: '1111',
        ruleTypeId: '',
        ruleName: '',
        ruleType: ruleType
      }
      this.checkCaseOptions = []
      pageCheckRule(params).then(response => {
        if (response.success) {
          const { data } = response
          this.checkCaseOptions = data.data
          this.showCheckCase = false
          this.$nextTick(() => {
            this.showCheckCase = true
          })
          console.log('datadddd',data)
        }
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loadingOptions.loading = true
          this.loadingOptions.loadingText = '保存中...'
          this.loadingOptions.isDisabled = true
          saveCheckJob(this.form).then(response => {
            if (response.success) {
              this.$message.success('保存成功')
              setTimeout(() => {
                // 2秒后跳转列表页
                this.$emit('showCard', this.showOptions)
              }, 2000)
            } else {
              this.$message.error('保存失败')
              this.loadingOptions.loading = false
              this.loadingOptions.loadingText = '保存'
              this.loadingOptions.isDisabled = false
            }
          }).catch(() => {
            this.loadingOptions.loading = false
            this.loadingOptions.loadingText = '保存'
            this.loadingOptions.isDisabled = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card ::v-deep .el-card__body {
    height: calc(100vh - 230px);
    overflow-y: auto;
  }
  .style-module__grid-container--29AQk{
    display: grid;
    grid-template-rows: var(--template-rows);
    grid-template-columns: var(--template-cols);
    grid-gap: var(--gap,0);
  }
  .style-module__grid-item--2nHmp{
    grid-row: span var(--row-span,1);
    grid-column: span var(--col-span,1);
    margin: 0!important;
  }
  .app-pts-web-card{
    background-color: #fff;
    box-shadow: 0 2px 4px 0 rgba(54,58,80,.32);
    margin-left: auto;
    margin-right: auto;
    box-sizing: border-box;
    border-radius: 0;
    color: rgba(0,0,0,.9);
  }
  .app-pts-web-justify-grid{
    display: flex;
    width: 100%;
    align-items: center;
    color: rgba(0,0,0,.9);
    box-sizing: border-box;
  }
  .app-pts-web-card__body{
    font-size: 12px;
    padding: 20px;
  }
  .app-pts-web-card{
    background-color: #fff;
    box-shadow: 0 2px 4px 0 rgba(54,58,80,.32);
    margin-left: auto;
    margin-right: auto;
    box-sizing: border-box;
    border-radius: 0;
    color: rgba(0,0,0,.9);
  }
</style>
