<template>
  <div class="run-modal-style">
    <Form :model="modelForm" ref="editForm" class="ivu-form-no-margin-bottom" :rules="rule" :label-width="100">
      <Row :gutter="20" class="code-row-bg">
        <Col span="12" style="border:none;">
          <Form-item prop="ffCode" :label="$t('module.flexField.code')">
            <Input placeholder="" size="small" v-model="modelForm.ffCode" maxlength="50"></Input>
          </Form-item>
        </Col>
        <Col span="12" style="border:none;">
          <Form-item prop="ffName" :label="$t('module.flexField.name')">
            <Input placeholder="" size="small" v-model="modelForm.ffName" maxlength="50"></Input>
          </Form-item>
        </Col>
      </Row>
      <Form-item :label="$t('module.flexField.desc')">
        <Input placeholder="" size="small" v-model="modelForm.ffDesc" maxlength="100"></Input>
      </Form-item>
      <Row :gutter="20" class="code-row-bg">
        <Col span="12" style="border:none;">
          <Form-item prop="ffRefOrgAttribute" :label="$t('module.flexField.orgAlias')">
            <Input placeholder="" size="small" v-model="modelForm.ffRefOrgAttribute" maxlength="50"></Input>
          </Form-item>
        </Col>
        <Col span="12" style="border:none;">
          <Form-item prop="ffRefBeanAttribute" :label="$t('module.flexField.refAttAlias')">
            <Input placeholder="" size="small" v-model="modelForm.ffRefBeanAttribute" maxlength="50"></Input>
          </Form-item>
        </Col>
      </Row>
      <Row :gutter="20" class="code-row-bg">
        <Col span="12" style="border:none;">
          <Form-item prop="enabled" :label="$t('common.status')">
            <Select size="small" v-model="modelForm.enabled">
              <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>
        </Col>
      </Row>
    </Form>
  </div>
</template>
<script>
export default{
  components:{
  },
  props:{
    ffId:{
      type: String,
      default(){
        return '';
      }
    },
    version:{
      type: String,
      default(){
        return '';
      }
    },
  },
  data(){
    return{
      modelForm: {
        ffId: this.ffId,
        ffCode: "",
        ffName: "",
        ffDesc: "",
        ffRefOrgAttribute: "",
        ffRefBeanAttribute: "",
        enabled: 1,
      },
      rule: {
        ffCode: [{ required: true, message: this.$t('module.flexField.enterCode'), trigger: "blur" }],
        ffName: [{ required: true, message: this.$t('module.flexField.enterName'), trigger: "blur" }],
        ffRefBeanAttribute: [{ required: true, message: this.$t('module.flexField.enterRefAttAlias'), trigger: "blur" }],
      },
    }
  },
  computed:{

  },
  created:function(){
    if(this.ffId){
      this.$emit('SetTitle', this.$t('module.flexField.editFlexField'));
    }else{
      this.$emit('SetTitle', this.$t('module.flexField.createFlexField'));
    }
    this.$emit('SetPageSize', 700);
    this.$emit('SetPageActions', [
      {
        text: this.$t('common.save'),
        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.ffId){
        $store.dispatch("GetFlexFieldDetail", { ffId: this.ffId }).then(response => {
          this.modelForm = {
            ffId: response.ffId,
            ffCode: response.ffCode,
            ffName: response.ffName,
            ffDesc: response.ffDesc,
            enabled: response.enabled,
            orgId: response.orgId,
            ffRefOrgAttribute: response.ffRefOrgAttribute,
            ffRefBeanAttribute: response.ffRefBeanAttribute,
            version: this.version
          };
        });
      }
    },
    save() {
      this.$refs["editForm"].validate(valid => {
        if (valid) {
          if (this.ffId) {
            $store.dispatch("UpdateFlexField", 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("AddFlexField", 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>
