<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-iam-modify-cpmqrzt-import'

export const metaInfo = {
  name: '设置确认状态',
  order: 120,
}

export default {
  name: 'XmIamModifyCpmqrzt',
  components,
  data() {
    return {
      fields: [
        {
          name: 'fieldName',
          caption: '字段名称',
          dataType: 'string',
          required: true,
          attr: {
            defaultValue: 'FIND_STATUS',
          },
        },
        {
          name: 'fieldValue',
          caption: '字段值',
          dataType: 'selectlist',
          attr: {
            defaultValue: '1',
            selectListItem: [
              { label: '已确认', value: '1' },
              { label: '未确认', value: '0' },
            ],
          },
        },
      ],
    }
  },
  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.fieldValue = '1' // 新窗口
      this.dataSource.fieldName = 'FIND_STATUS'
    }
  },
  methods: {
    validate() {
      return this.$refs.form.validateFields()
    },
    valueChange(value, field) {
      this.dataSource[field] = value.target ? value.target.value : value
    },
  },
}
</script>

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