<template>
  <form-list ref="form" class="pt-3" :items="formItems"></form-list>
</template>

<script>
import FormList from '@/components/Form/list.vue'
import { dataFactoryDictList } from '@/api/dataFactory'
export default {
  name: 'edit-page',
  components: { FormList },
  props: {
    itemData: {
      type: Object,
      default: () => {}
    },
    typeDict: {
      type: Array,
      default: () => []
    },
    statusDict: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      formItems: {
        options: [
          {
            type: 'text',
            key: 'name',
            value: null,
            label: '请输入名称',
            outlined: true,
            dense: true,
            rules: [v => !!v || '请输入名称']
          },
          {
            type: 'autocomplete',
            key: 'tag',
            value: null,
            label: '请选择分组',
            outlined: true,
            dense: true,
            rules: [v => !!v || '请选择分组'],
            items: []
          },
          {
            type: 'autocomplete',
            key: 'type',
            value: null,
            label: '请选择类型',
            outlined: true,
            dense: true,
            rules: [v => !!v || '请选择类型'],
            items: this.typeDict
          },
          {
            type: 'text',
            key: 'mark',
            value: null,
            label: '请输入标识',
            outlined: true,
            dense: true,
            rules: [v => !!v || '请输入标识']
          },
          {
            type: 'ifRadio',
            key: 'status',
            value: '1',
            label: '状态',
            items: this.statusDict
          },
          {
            type: 'textarea',
            key: 'remark',
            value: null,
            label: '详细说明',
            outlined: true,
            dense: true
          }
        ]
      }
    }
  },
  created () {
    this.initTagDict()
    if (!Object.keys(this.itemData).length) return
    this.formItems.options.forEach(ele => {
      ele.value = this.itemData[ele.key]
    })
  },
  methods: {
    async initTagDict () {
      try {
        const { data } = await dataFactoryDictList({ type: 'production_line_tag' })
        this.formItems.options.find(e => e.key === 'tag').items = data[0].itemList
      } catch (error) {
        this.$snackbar.error(error)
      }
    },
    getValue () {
      if (!this.$refs.form.validate()) return
      return this.formItems.options.reduce((res, item) => {
        res[item.key] = item.value
        return res
      }, {})
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
