<template>
  <audit-form-top-banner :handel-save-form="saveForm" :form-data="mode" @handelSaveForm="saveForm">
    <sd-detail-form
      ref="docform"
      form-id="kpiJobInfo"
      page-id="kpi/job/kpiJobInfo"
      :record-id="this.$route.query.record ? parseInt(this.$route.query.record) : null"
      :class="$style.form"
      :read-only="!saveFlag"
      @close="close(true)"
      @saved="saved"
      @sdFormReady="sdFormReady"
    >
      <template v-slot="{ model, fields }">
        <table>
          <tr>
            <td style="padding-left:5px;border:none">
              <audit-advanced-group
                :expand="basicExpand"
                :expand-str="'basicExpand'"
                :group-label="'基本信息'"
                tablestyle="''"
                @changedClick="changedClick"
              ></audit-advanced-group>
            </td>
          </tr>
        </table>

        <table v-show="basicExpand">
          <tr>
            <td class="ant-form-item-label ant-form-item-label" :class="$style.firstTd"></td>
            <td class="ant-form-item-control-wrapper" :class="$style.firstTd"></td>
            <td class="ant-form-item-label ant-form-item-label" :class="$style.firstTd"></td>
            <td class="ant-form-item-control-wrapper" :class="$style.firstTd"></td>
            <sd-form-item-td name="id" label="ID" :colspan="3" :hidden="true"> </sd-form-item-td>
          </tr>
          <tr>
            <!-- 时间维度 -->
            <sd-form-item-td name="jobFrequency" label="时间维度" :colspan="3"> </sd-form-item-td>
          </tr>
          <tr>
            <!-- 执行规则 -->
            <sd-form-item-td name="jobCron" :colspan="3">
              <template v-slot:read-and-edit>
                <a-input v-model="model.jobCron" @click="fnShowCron"></a-input>
              </template>
            </sd-form-item-td>
          </tr>
          <tr>
            <!-- 是否启用 -->
            <sd-form-item-td name="jobStatus" :colspan="3" label="是否启用"> </sd-form-item-td>
          </tr>
          <tr>
            <!-- 备注 -->
            <sd-form-item-td name="remark" :colspan="3">
              <a-textarea v-model="model.remark" :rows="3" />
            </sd-form-item-td>
          </tr>
        </table>

        <!-- cron选择组件 -->
        <a-modal
          width="600px"
          title="表达式"
          :footer="null"
          :visible="showCron"
          @cancel="handleCancel"
        >
          <vcrontab
            v-model="croninput"
            :expression="croninput"
            @hide="showCron = false"
            @fill="crontabFill"
          ></vcrontab>
        </a-modal>
      </template>
    </sd-detail-form>
  </audit-form-top-banner>
</template>

<script>
import vcrontab from 'vcrontab'
import axios from '@/common/services/axios-instance'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import { message } from '@/common/one-ui'
import auditFormTopBanner from '@product/iam/components/audit-form-top-banner'
import auditAdvancedGroup from '@product/iam/components/audit-advanced-group'
import auditAdvancedGroupMixins from '@product/iam/components/audit-advanced-group-mixins'
import KpiService from '../kpi-service'
import components from './_import-components/kpi-job-form-import'
import 'element-ui/lib/theme-chalk/index.css'

export default {
  name: 'KpiJobForm',
  metaInfo: {
    title: '指标定时任务表单',
  },

  components: {
    ...components,
    auditAdvancedGroup,
    auditFormTopBanner,
    vcrontab,
  },
  mixins: [auditAdvancedGroupMixins],
  data() {
    return {
      configOptions: [], // 转换配置列表
      croninput: '',
      showCron: false,
      mode: null,
      saveFlag: true,
      basicExpand: true,
      etlConfigOptions: [],
    }
  },
  mounted() {
    const ini = setInterval(() => {
      if (this.$refs.docform) {
        this.croninput = this.$refs.docform.getFieldValue('jobCron')
        clearInterval(ini)
      }
    }, 200)
  },
  methods: {
    fnShowCron() {
      this.showCron = true
    },
    handleCancel(e) {
      this.showCron = !this.showCron
    },
    crontabFill(value) {
      // 确定后回传的值
      this.croninput = value
      this.$refs.docform.setFieldValue('jobCron', value)
    },
    configChange(val, option) {
      this.$refs.docform.setFieldValue('configId', Number(option.data.key))
    },
    // 页面加载完成后
    sdFormReady() {
      this.mode = this.$refs.docform.formData.mode
      // this.croninput = this.$refs.docform.getFieldValue('jobCron')
    },
    // 保存
    saveForm() {
      var jobFrequency = this.$refs.docform.getFieldValue('jobFrequency')
      var jobCron = this.$refs.docform.getFieldValue('jobCron')
      var id = this.$refs.docform.getFieldValue('id')
      const params = {
        id: id,
        jobFrequency: jobFrequency,
        jobCron: jobCron,
      }
      KpiService.hasJobFrequency(params).then((res) => {
        if (res) {
          message.error('该时间维度已存在，请重新选择！')
        } else {
          this.$refs.docform.validateFields().then(() => {
            message.success('保存成功').then(() => {
              this.$refs.docform.saveBtnClick()
            })
          })
        }
      })
    },
    saved() {
      this.close(true)
    },
    close(flag) {
      crossWindowWatcher.notifyChange(this.$route.fullPath, flag)
      window.close()
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
@import '@/webflow/sd-flow-form.scss';
.connect-btn {
  float: right;
  font-weight: 400;
  font-size: 16px;
  top: 8px;
  cursor: pointer;
  margin-right: 10px;
}
.first-td {
  border: none !important;
  padding: 0;
}
</style>
