<template>
  <div class="run-modal-style">
    <Form :model="modelForm" ref="editForm" class="ivu-form-no-margin-bottom" :rules="rule" :label-width="90">
      <Form-item prop="ffeaCode" :label="$t('module.flexField.attrCode')">
        <Input placeholder="" size="small" v-model="modelForm.ffeaCode" maxlength="50"></Input>
      </Form-item>
      <Form-item prop="ffeaName" :label="$t('module.flexField.attrName')">
        <Input placeholder="" size="small" v-model="modelForm.ffeaName" maxlength="50"></Input>
      </Form-item>
      <Form-item prop="ffeaUseColumn" :label="$t('module.flexField.columnName')">
        <Input placeholder="" size="small" v-model="modelForm.ffeaUseColumn" maxlength="50"></Input>
      </Form-item>
      <Form-item prop="enabled" :label="$t('common.status')">
        <Select size="small" v-model="modelForm.enabled" style="width:80px">
          <Option :title="$t('common.valid')" :value="1" :key="1">{{$t('common.valid')}}</Option>
          <Option :title="$t('common.invalid')" :value="0" :key="0">{{$t('common.invalid')}}</Option>
        </Select>
      </Form-item>
      <Form-item prop="refValueSet" :label="$t('module.flexField.associatedVs')">
        <Input placeholder="" size="small" v-model="modelForm.refValueSet" maxlength="50"></Input>
      </Form-item>
    </Form>
  </div>
</template>
<script>
export default{
  components:{
  },
  props:{
    ffeId:{
      type: String,
      default(){
        return '';
      }
    },
    ffeaId:{
      type: String,
      default(){
        return '';
      }
    },
    version:{
      type: String,
      default(){
        return '';
      }
    },
  },
  data(){
    return{
      modelForm: {
        refFfeId: this.ffeId,
        ffeaId: this.ffeaId,
        ffeaCode: "",
        ffeaName: "",
        ffeaUseColumn: "",
        enabled: 1,
        refValueSet: "",
      },
      rule: {
        ffeaCode: [{ required: true, message: this.$t('module.flexField.enterAttrCode'), trigger: "blur" }],
        ffeaName: [{ required: true, message: this.$t('module.flexField.enterAttrName'), trigger: "blur" }],
        ffeaUseColumn: [{ required: true, message: this.$t('module.flexField.enterColumnName'), trigger: "blur" }],
      },
    }
  },
  computed:{

  },
  created:function(){
    if(this.ffeaId){
      this.$emit('SetTitle', this.$t('module.flexField.editEleAttr'));
    }else{
      this.$emit('SetTitle',  this.$t('module.flexField.createEleAttr'));
    }
    this.$emit('SetPageSize', 500);
    this.$emit('SetPageActions', [
      {
        text: this.$t('common.confirm'),
        theme: "primary",
        handle: () => {
          this.save();
        }
      },
      {
        text: this.$t('common.close'),
        theme: "primary",
        handle: () => {
          this.$emit('Close');
        }
      }
    ])
  },
  beforeMount:function(){

  },
  mounted:function(){
    this.init();
  },
  beforeDestroy:function(){

  },
  destroyed:function(){

  },
  methods:{
    init(){
      if(this.ffeaId){
        $store.dispatch("GetFlexFieldAttributeDetail", { ffeaId: this.ffeaId }).then(response => {
          this.modelForm = {
            refFfeId: response.refFfeId,
            ffeaId: response.ffeaId,
            ffeaCode: response.ffeaCode,
            ffeaName: response.ffeaName,
            ffeaUseColumn: response.ffeaUseColumn,
            enabled: response.enabled,
            refValueSet: response.refValueSet,
            version: this.version
          };
        });
      }
    },
    save() {
      this.$refs["editForm"].validate(valid => {
        if (valid) {
          if (this.ffeaId) {
            $store.dispatch("UpdateFlexFieldAttribute", this.modelForm).then(response => {
              if (response.statusCode == "0") {
                this.$Message.success({
                  content: this.$t('module.valueSet.warning.editSuccess'),
                  duration: 3
                });
                this.$emit('Close');
              } else {
                this.$Message.warning({
                  content: response.msg,
                  duration: 3
                });
              }
            });
          } else {
            $store.dispatch("AddFlexFieldAttribute", this.modelForm).then(response => {
              if (response.statusCode == "0") {
                this.$Message.success({
                  content: this.$t('module.valueSet.warning.createSuccess'),
                  duration: 3
                });
                this.$emit('Close');
              } else {
                this.$Message.warning({
                  content: response.msg,
                  duration: 3
                });
              }
            });
          }
        } else {
          this.$Message.error({
            content: this.$t('module.valueSet.warning.formValidateFailed'),
            duration: 3
          });
          return 0;
        }
      });
    },
  },
  watch:{

  },
  directives:{

  }
}
</script>
