<template>
  <ych-display-text-tag v-if="displayByText && disabled" v-model="text"/>
  <a-input
    v-else
    :disabled="disabled"
    :placeholder="placeholder || LocleUtil.getLocale('m.eform.formDesign.label.placeholder_input')"
    :readOnly="readOnly"
    v-model="text"
  />
</template>

<script>

  import YchConstant from '../../utils/YchConstant'
  import { putAction } from '../../utils/manage'
  import YchDisplayTextTag from "../../KFormItem/YchDisplayTextTag.vue";
  import LocleUtil from "../../utils/LocleUtil";

  export default {
    name: 'YchSerialno',
    computed: {
      LocleUtil() {
        return LocleUtil
      }
    },
    components:{
      YchDisplayTextTag,
    },
    props: {
      value: String,
      disabled: Boolean,
      readOnly: Boolean,
      displayByText: Boolean,
      placeholder: {
        type: String,
      },
      ruleCode: String,
      form: {
        type: Object,
        default: () => {
        }
      },
      record: Object,
      property: String,
    },
    data() {
      return {
        text: this.value,
        url: {
          executeRuleByCode: YchConstant.PREFIX_SYSTEM + '/fillRule/executeRuleByCode/'
        }
      }
    },
    created() {
      if (!this.value && this.ruleCode && this.form) {
        const formData = this.form.getFieldsValue()
        putAction(this.url.executeRuleByCode + this.ruleCode, formData).then((res) => {
          if (res.success) {
            this.text = res.result
          } else {
            this.$message.warning(res.message)
          }
        })
      }
    },
    methods: {},
    watch: {
      value(value) {
        this.text = value
      },
      text() {
        this.$emit('change', this.text)
        this.$emit('setFieldValue', this.property, this.text, this.record)
      }
    },
    model: {
      prop: 'value',
      event: 'change'
    }
  }
</script>
