<template>
  <sd-form ref="form" :init-values="fields">
    <template v-slot="{ model }">
      <sd-form-item
        v-for="field in fields"
        :key="field.name"
        :name="field.name"
        :label="field.dataType === 'collection' ? null : undefined"
        :hidden="!!field.sdHidden"
        @change="(value) => valueChange(value, field.name)"
      >
      </sd-form-item>
    </template>
  </sd-form>
</template>

<script>
import components from './_import-components/xm-linkopen-import'

export const metaInfo = {
  name: '链接',
  order: 100,
}

export default {
  name: 'XmLinkopen',
  components,
  props: {
    dataSource: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      fields: [
        {
          name: 'targetLink',
          caption: '链接地址',
          dataType: 'string',
          required: true,
        },
        {
          name: 'appModuleId',
          caption: '应用ModuleId',
          dataType: 'string',
        },
        {
          name: 'formListId',
          caption: '表单ListId',
          dataType: 'string',
        },
        {
          name: 'newWindow',
          caption: '新窗口打开',
          dataType: 'selectlist',
          attr: {
            defaultValue: '1',
            selectListItem: [
              { label: '是', value: '1' },
              { label: '否', value: '0' },
            ],
          },
        },
      ],
      businessTypeOptions: [],
    }
  },
  computed: {},
  created() {
    // 编辑时
    if (Object.keys(this.dataSource).length > 0) {
      this.fields.forEach((f) => {
        const value = this.dataSource[f.name]
        if (value && typeof value === 'object') {
          f.value = JSON.stringify(value)
        } else {
          f.value = value
        }
      })
    } else {
      // 新建，赋默认值
      this.dataSource.newWindow = '1' // 新窗口
    }
  },
  methods: {
    validate() {
      return this.$refs.form.validateFields()
    },
    valueChange(value, field) {
      this.dataSource[field] = value.target.value
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
