<template>
  <!-- 子网创建对话框 -->
  <el-dialog
    @close="close"
    @opened="createSubnetInit"
    v-dialogDrag
    :title=title
    style="font-size: 10px;" width='450px'
    :visible.sync="createAndCopySubnetVisible">
    <el-form :model="form" inline="true">
      <el-form-item :label="'*'+ $t('SubnetConfigurationManagement.subnetName')+':'" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-input size='mini' :placeholder="$t('common.placeholder')" v-model="form.subnetName"
                  v-on:blur="subnetNameCheck" autocomplete="off" style="width: 152px;"></el-input>
      </el-form-item>
      <el-form-item :label="$t('SubnetConfigurationManagement.domain')+':'" :label-width="formLabelWidth"
                    style="margin-bottom: -10px; margin-left: 0px;">
        <el-input type="textarea" :rows="4" :placeholder="$t('common.placeholder')" v-model="form.domain"
                  autocomplete="off" style="width: 152px; margin-top: 10px;"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size='mini' type="primary" @click="createSubnet">
        {{$t("common.createBtn")}}
      </el-button>
      <el-button size='mini' @click="close">{{$t("common.cancel")}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import SubnetService from '@/domain/services/SubnetService.js'
  import legitimacyCheck from "../../../utils/legitimacyCheck";
  import HelperUtil from"../../../utils/HelperUtil";

  export default {
    props: ['command'],
    data: function () {
      return {
        title: this.command.title,
        initList: this.command.target,
        createAndCopySubnetVisible: false,
        formLabelWidth: '120px',
        form: {
          subnetName: null,
          domain: null,
        },
      }
    },
    methods: {
      close() {
        this.command.done();
      },

      //------------------------------------创建开始-------------------------------------------------------------------
      createSubnetInit(){
        this.form.subnetName = this.initList.subnetName;
        this.form.domain = this.initList.domain
      },

      createSubnet() {
        if (!this.subnetNameCheck()) {
          return

        }
        else {
          let subnet = {};
          subnet.subnetKey = '';
          subnet.subnetName = this.form.subnetName;
          subnet.domain = this.form.domain;
          subnet.delFlag = 0;
          subnet.modifyTime = '';
          SubnetService.addSubent(subnet).then(result => {
            //成功
            this.InfoTip.successTip(this,HelperUtil.getStatusCodeObjectByCode(this.successCode.ADD_CODE));
            this.command.done();
          }).catch(err => {
            this.InfoTip.errorTip(this,err);

          });
        }
      },


      //------------------------------------创建结束-------------------------------------------------------------------

      //------------------------------------验证开始-------------------------------------------------------------------
      /**
       * @Description  :子网名称校验: 不能为空
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
      subnetNameCheck: function () {
        let subnetCheck = legitimacyCheck()
        if (!subnetCheck.textNullCheck(this.form.subnetName)) {
          //输入不可为空
          this.InfoTip.warningTip_alarm(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE),this.$t('SubnetConfigurationManagement.subnetName'))
          return false
        }
        if(!this.textLengthCheck(this.form.subnetName)) return false
        return true
      },

      /**
       * @Description  :文本框所输入的字符长度校验: 控制在20之内
       * @author       : bjh
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2019/1/15 16:09
       */
      textLengthCheck: function (checkString) {
        let subnetCheck = legitimacyCheck()
        if (!subnetCheck.textLengthCheck(checkString)) {
          //所输文本长度必须控制在20之内
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.LENGTH_CODE))
          return false
        }
        return true
      },


      //------------------------------------验证结束-------------------------------------------------------------------

    },

    created() {
      console.log('子网创建初始化加载数据')
    },

    mounted(){
      this.createAndCopySubnetVisible = true;
    }
  }
</script>