<template>
    <div style="padding:10px;">
        <div>
            <Form :model="formSearch" :rules="rules" ref="addVsForm" :label-width="85">
            <Row :gutter="20" class="code-row-bg">
                <Col span="12">
                <Form-item prop="vsCode" :label="$t('module.valueSet.vsCode')">
                <Input  v-model="formSearch.vsCode" :readonly="canEdit" size="small"/>
                </Form-item>
                </Col>
                <Col span="12">
                <Form-item prop="vsName" :label="$t('module.valueSet.vsName')">
                <Input v-model="formSearch.vsName" size="small"></Input>
                </Form-item>
                </Col>           
            </Row>
            <Row :gutter="20" class="code-row-bg">
                <Col span="12">
                <Form-item prop="vsType" :label="$t('module.valueSet.vsType')">
                    <Select v-model="formSearch.vsType" size="small" :disabled="canEdit" @on-change="onVsTypeChange">
                        <Option value="1" selected = "selected">{{$t('module.valueSet.independent')}}</Option>
                        <Option value="2">{{$t('module.valueSet.subordinate')}}</Option>
                    </Select>
                </Form-item>
                </Col>
                <Col span="12">
                <Form-item prop="vsParentCode" :label="$t('module.valueSet.parentVs')">
                    <Input v-model="formSearch.vsParentName" @click.native="getParentVs()" size="small" readonly></Input>
                    <Icon class="getList-icon"  @click.native="getParentVs()"></Icon> 
                </Form-item>
                </Col>
            </Row>
            <Row :gutter="20" class="code-row-bg">
                <Col span="12">
                <Form-item prop="vsValueSource" :label="$t('module.valueSet.vsValueSource')">
                   <Select v-model="formSearch.vsValueSource" @on-change="onVsValueSourceChange" :disabled="yesOrNo" size="small">
                        <Option value="1">{{$t('module.valueSet.userDefine')}}</Option>
                        <Option value="2">{{$t('module.valueSet.dynamicSql')}}</Option>
                    </Select> 
                </Form-item>
                </Col>
                <Col span="12">
                <Form-item prop="orgName" :label="$t('module.userManage.affiliation')">
                    <Input v-model="formSearch.orgName" @click.native="getOrgTree()" size="small" readonly></Input>
                    <Icon class="getList-icon"  @click.native="getOrgTree()"></Icon>              
                </Form-item>
                </Col>
            </Row>
            <Row :gutter="20" class="code-row-bg">
                <Col span="12">
                <Form-item prop="enabled" :label="$t('common.status')">
                <Select v-model="formSearch.enabled" size="small" clearable>
                    <Option value="1">{{$t("common.valid")}}</Option>
                    <Option value="0">{{$t("common.invalid")}}</Option>
                </Select>
                </Form-item>
                </Col>
                <Col span="12">
                <Form-item :label="$t('module.valueSet.vsDesc')">
                <Input v-model="formSearch.vsDescribe" size="small"></Input>
                </Form-item>
                </Col>            
            </Row>
            </Form>
        </div>
    </div>
</template>
<script>
import GetParentVs from './GetParentVs.vue';
import GetOrgTree from './GetOrgTree.vue';
export default {
  components: {},
  props: {
      vsId: {
          type: String,
          default() {
              return "";
          }
      },
      version: {
          type: String,
          default() {
              return "";
          }
      }
  },
  data() {
    return {
        yesOrNo: false,
        canEdit: false,
        formSearch: {
            vsCode: "",
            vsName: "",
            vsType: "1",
            vsParentCode: "",
            vsParentName: "",
            vsOrgId: "",
            orgName: "",
            enabled: "1",
            vsDescribe: "",
            vsValueSource: "1"
        },
        rules: {
            vsCode: [{ required: true, message: this.$t("module.valueSet.warning.enterVsCode"), trigger: "blur" }],
            vsName: [{ required: true, message: this.$t("module.valueSet.warning.enterVsName"), trigger: "blur" }],
            vsType: [{ required: true, message: this.$t("module.valueSet.warning.enterVsType"), trigger: "blur" }],
            orgName: [{ required: true, message: this.$t("module.valueSet.warning.enterAffiliation"), trigger: "blur" }],
            enabled: [{ required: true, message: this.$t("module.valueSet.warning.enterStatus"), trigger: "blur" }],
            vsValueSource: [{ required: true, message: this.$t("module.valueSet.warning.enterVsSource"), trigger: "blur" }],
        },
    };
  },
  computed: {},
  created: function() {
      if(this.formSearch.vsType == "2" || this.vsId){
          this.yesOrNo = true;
      }
      if(this.vsId){
          $store.dispatch("GetValueSetInfo", this.vsId).then((response) => {
          this.formSearch.vsCode = response.dataResult.vsCode;
          this.formSearch.vsName = response.dataResult.vsName;
          this.formSearch.vsType = String(response.dataResult.vsType);
          this.formSearch.vsParentName = response.dataResult.vsParentName;
          this.formSearch.vsParentCode = response.dataResult.vsParentCode;
          this.formSearch.orgName = response.dataResult.orgName;
          this.formSearch.enabled = String(response.dataResult.enabled);
          this.formSearch.vsDescribe = response.dataResult.vsDescribe;
          this.formSearch.vsValueSource = String(response.dataResult.vsValueSource);
          this.formSearch.vsId = this.vsId;
          this.formSearch.version = this.version;
          this.$emit("SetTitle", this.$t("module.valueSet.editVs"));
          this.canEdit = true;
          this.yesOrNo = true;
          })
      }else{
          this.$emit("SetTitle", this.$t("module.valueSet.createVs"));
      }
      this.$emit("SetPageSize", 700);
      this.$emit('SetPageActions', [
        {
            text: this.$t("common.save"),
            theme: "success",
            icon: "checkmark-round",
            handle: this.saveAddOrUpdate,
        }
    ]);
  },
  beforeMount: function() {},
  mounted: function() {},
  beforeDestroy: function() {},
  destroyed: function() {},
  methods: {
      saveAddOrUpdate(){
        //值集类型为从属时，校验"值来源"是否为空
        if(this.formSearch.vsParentCode == "" && this.formSearch.vsType == "2" && !this.vsId){
            this.$Message.error({
                content: this.$t("module.valueSet.warning.enterParentVs"),
                duration: 3
            });
            return ;
        }
        this.$refs["addVsForm"].validate(valid => {
          if(valid){
            if(this.vsId){
                  $store.dispatch("UpdateValueSet", {
                        "vsCode": this.formSearch.vsCode,
                        "vsDescribe": this.formSearch.vsDescribe,
                        "vsName": this.formSearch.vsName,
                        "vsOrgId": this.formSearch.vsOrgId,
                        "vsParentCode": this.formSearch.vsParentCode,
                        "vsType": this.formSearch.vsType,
                        "vsValueSource": this.formSearch.vsValueSource,
                        "enabled": this.formSearch.enabled,
                        "vsId": this.vsId,
                        "version": this.version
                    }).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("AddValueSet", {
                    "vsCode": this.formSearch.vsCode,
                    "vsDescribe": this.formSearch.vsDescribe,
                    "vsName": this.formSearch.vsName,
                    "vsOrgId": this.formSearch.vsOrgId,
                    "vsParentCode": this.formSearch.vsParentCode,
                    "vsType": this.formSearch.vsType,
                    "vsValueSource": this.formSearch.vsValueSource,
                    "enabled": this.formSearch.enabled
                  }).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
            })
        }})
      },
      getParentVs(){
          if(this.formSearch.vsType == "1"){
              this.$Message.warning({
                  content: this.$t("module.valueSet.warning.indVsNoParentVs"),
                  duration: 3
              });
              return ;
          }
          if(this.vsId){
              this.$Message.warning({
                  content: this.$t("module.valueSet.warning.parentVsNoEdit"),
                  duration: 3
              });
              return ;
          }
          new this.$pageModal(
              GetParentVs,
              {

              },(data) => {
                  if(!!data){
                  this.formSearch.vsParentCode = data.vsCode;
                  this.formSearch.vsParentName = data.vsName;
                  return ;
                  }
              })
      },
      getOrgTree(){
          if(this.vsId){
              this.$Message.warning({
                  content: this.$t("module.valueSet.warning.affiliationNoEdit"),
                  duration: 3
              });
              return ;
          }
          new this.$pageModal(
              GetOrgTree,
              {

              },(data) => {
                  if(!!data){
                      this.formSearch.orgName = data.orgName;
                      this.formSearch.vsOrgId = data.orgId;
                  }else{
                      this.formSearch.orgName = "";
                      this.formSearch.vsOrgId = "";
                  }
              }
          )
      },
      cancel(){
          this.$emit("Close")
      },
      clearInput(){
          this.formSearch.vsParentCode = "";
        //   this.formSearch.vsName = "";
      },
      onVsTypeChange(id){
          if(!this.vsId){
            if(id == "2"){
                this.formSearch.vsValueSource = "1";
                this.yesOrNo = true;
            }else{
                this.yesOrNo = false;
                this.clearInput();
            }
          }
      },
  },
  watch: {},
  directives: {}
};
</script>
<style>
    .ivu-select-selection{
        min-height: 34px;
        border-radius: 3px
    }
    .ivu-select-selected-value{
        height: 32px;
        line-height: 32px;
    }
</style>

