<template>
  <div>
    <m-form ref="form" :items="formItems" v-model="formValues">
      <template #childrenId="{ item }">
        <search-nodes v-model="item.value" v-bind="item.options" :search-value="item.search"></search-nodes>
      </template>
    </m-form>
  </div>
</template>

<script>

import MForm from '@/components/MForm'
// import { combinationTagsPage } from '@/api/dataBook'
import {
  metadata,
  frequency
  // sensitivity
} from '@/utils/dataGovernance'
import SearchNodes from '../../components/searchNodes.vue'
import { api } from '@/api/dataGovernance'
export default {
  name: 'edit-base',
  props: {
    itemData: {
      type: Object,
      default: () => ({})
    }
  },
  components: { MForm, SearchNodes },
  data () {
    return {
      formValues: {
        name: null,
        name_en: null,
        category: '应用类',
        leader: 'system',
        organization: null,
        script_type: 'sql',
        update_mode: 'append',
        frequency: '月',
        // data_sensitivity: '低',
        tag: null,
        describe: null,
        status: 'active'
      },
      tagItems: [],
      pageInfo: {
        size: 10,
        current: 1
      },
      total: 0,
      loading: false
    }
  },
  computed: {
    formItems () {
      return [
        { type: 'text', key: 'name', label: '名称 *', rules: [v => !!v || '请输入名称'] },
        { type: 'text', key: 'name_en', label: '英文名称 *', rules: [v => !!v || '请输入英文名称'] },
        { type: 'autocomplete', key: 'category', label: '请选择分类 *', rules: [v => !!v || '请选择分类'], items: [...metadata] },
        { type: 'text', key: 'leader', label: '创建者 *', rules: [v => !!v || '请输入负责人'] },
        { type: 'text', key: 'organization', label: '请输入所属机构 *', rules: [v => !!v || '请输入所属机构'] },
        {
          type: 'autocomplete',
          key: 'script_type',
          label: '脚本类型 *',
          rules: [v => !!v || '请选择脚本类型'],
          items: [
            { label: 'SQL', value: 'sql' },
            { label: 'Python', value: 'python' },
            { label: 'PythonScript', value: 'python_script' },
            { label: 'SQLScript', value: 'sql_script' }
          ]
        },
        {
          type: 'autocomplete',
          key: 'update_mode',
          label: '更新模式 *',
          rules: [v => !!v || '请选择更新模式'],
          items: [
            { label: '追加', value: 'append' },
            { label: '全量更新', value: 'full_refresh' }
          ]
        },
        { type: 'autocomplete', key: 'frequency', label: '更新频率 *', rules: [v => !!v || '请选择更新频率'], items: [...frequency] },
        {
          type: 'autocomplete',
          key: 'tag',
          label: '标签',
          itemText: 'name',
          itemValue: 'id',
          items: this.tagItems
        },
        { type: 'text', key: 'describe', label: '描述' },
        {
          type: 'ifRadio',
          key: 'status',
          label: '启用',
          width: 120,
          items: [{ label: '是', value: 'active' }, { label: '否', value: 'inactive' }]
        }
      ]
    }
  },
  created () {
    this.init()
    // if (!Object.keys(this.itemData).length) {
    //   return
    // }
    // Object.keys(this.formValues).forEach(key => {
    //   if (!Object.prototype.hasOwnProperty.call(this.itemData, key)) {
    //     return
    //   }
    //   if (key === 'tag') {
    //     this.formValues[key] = this.itemData.tag
    //     return
    //   }
    //   this.formValues[key] = this.itemData[key]
    // })
  },
  methods: {
    setValues (item) {
      this.formValues = {
        name: item.name_zh,
        name_en: item.name_en,
        category: item.category,
        leader: item.leader,
        organization: null,
        script_type: item.script_type,
        update_mode: item.update_mode,
        frequency: item.frequency,
        // data_sensitivity: '低',
        tag: item.tag,
        describe: item.describe,
        status: item.status

      }
    },
    async init () {
      try {
        this.loading = true
        try {
          const { data } = await api.dataLabelList({
            ...this.pageInfo
          })
          this.tagItems = data.records
          this.total = data.total
        } catch (error) {
          this.$snackbar.error(error)
        } finally {
          this.loading = false
        }
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    getValue () {
      if (!this.$refs.form.validate()) {
        return
      }
      return this.formValues
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
