<template>

  <!-- 配置文件修改对话框 -->
  <el-dialog
    @close="close"
    @opened="updateConfigInit"
    v-dialogDrag width='550px' :title="$t('ProfileManagement.updateConf')"
    v-if="UpdateConfVisible"
    :visible.sync="UpdateConfVisible">
    <el-form :model="form" inline="true">
      <el-form-item :label="'*'+ $t('ProfileManagement.configFileName')+':'" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-input size='mini' :placeholder="$t('common.placeholder')" v-model="form.configFileName" autocomplete="off"
                  v-on:blur="confNameCheck"
                  style="width: 220px">
          <template slot="append">.conf</template>
        </el-input>

      </el-form-item>

      <el-form-item :label="$t('ProfileManagement.timestamp')+':'" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-input size='mini' :placeholder="$t('common.placeholder')" v-model="form.timestamp" autocomplete="off"
                  style="width: 220px;" disabled="true"></el-input>
      </el-form-item>

      <el-form-item :label="$t('ProfileManagement.source')+':'" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-input size='mini' :placeholder="$t('common.placeholder')" v-model="form.source" autocomplete="off"
                  style="width: 220px;" disabled="true"></el-input>
      </el-form-item>


      <el-form-item :label="$t('ProfileManagement.description')+':'" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;margin-left: 0px;">
        <el-input size="mini" :placeholder="$t('common.placeholder')" v-model="form.description" autocomplete="off"
                  style="width: 220px; margin-top: -10px;"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size='mini' type="primary" @click="updateConfig" v-loading.fullscreen.lock="fullscreenLoading">
        {{$t("common.updateBtn")}}
      </el-button>
      <el-button size='mini' @click="close">{{$t("common.cancel")}}</el-button>
    </div>
  </el-dialog>

</template>

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


  export default {
    props: ['command'],

    components: {},
    data() {
      return {
        UpdateConfVisible: false,
        initList: this.command.target,

        formLabelWidth: '200px',
        extension: null,
        //双向绑定弹出窗口ipput域
        form: {
          configFileKey: '',
          configFileName: '',
          timestamp: '',
          codeByMd5: '',
          path: '',
          delFlag: '',
          description: '',
          source: '',
        },
      }
    },

    methods: {
      close() {
        this.command.done();
      },

      /**
       * @Description  :修改网元信息初始化
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
      updateConfigInit: function () {
        this.form.configFileKey = this.initList.configFileKey
        this.form.configFileName = this.initList.configFileName
        this.form.timestamp = this.initList.timestamp
        this.form.codeByMd5 = this.initList.codeByMd5
        this.form.path = this.initList.path
        this.form.delFlag = this.initList.delFlag
        this.form.description = this.initList.description
        this.form.source = this.initList.source
        this.extension = this.form.configFileName.substring(this.form.configFileName.length - 5, this.form.configFileName.length) == ".conf"
        if (this.extension) {
          this.form.configFileName = this.form.configFileName.substring(0, this.form.configFileName.length - 5)
        }
      },

      /**
       * @Description  :修改配置文件
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
      updateConfig: function () {


        var tempSource = this.form.source

        if (this.form.source == this.$t('ProfileManagement.LocalUpload'))
          tempSource = 0
        if (this.form.source == this.$t('ProfileManagement.DeviceUpload'))
          tempSource = 1

        let configFile = {};
        configFile.configFileKey = this.form.configFileKey
        configFile.configFileName = this.form.configFileName
        configFile.timestamp = this.form.timestamp
        configFile.codeByMd5 = this.form.codeByMd5
        configFile.source = tempSource
        configFile.path = this.form.path
        configFile.deFlag = this.form.deFlag
        configFile.description = this.form.description
        console.log(configFile)
        if (!this.confNameCheck()) {
          return false
        }
        if (this.extension) {
          configFile.configFileName = this.form.configFileName + '.conf'
        }
        let _this = this;
        let loadingInstance = _this.Loading.openLoading();
        //调用函数
        ConfigFileService.modifyConfigFile(configFile).then(result => {
          _this.Loading.closeLoading(loadingInstance)
          //成功
          _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.MODIFY_CODE))
          _this.close();
        }).catch(err => {
          _this.Loading.closeLoading(loadingInstance)
          //失败
          _this.InfoTip.errorTip(_this, err);

        });
      },


//     ------------------------------- 开始合法性校验--------------

      confNameCheck: function () {
        let portCheck = legitimacyCheck()
        if (!portCheck.textNullCheck(this.form.configFileName)) {
          //输入不可为空
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('ProfileManagement.configFileName'))
          return false
        }
        if (!this.textLengthCheck(this.form.configFileName)) {
          return false
        }
        return true
      },

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


    //初始化函数，加载数据
    created: function () {
    },

    mounted() {
      this.UpdateConfVisible = true
    },

  }
</script>

<style scoped>

</style>
