<template>
  <!-- 机房修改对话框 -->
  <el-dialog v-dialogDrag style="font-size: 10px;" width='450px'
             :title="$t('LocationConfigurationManagement.dialogLocationUpdate')"
             :visible.sync="dialog_locationUpdate"
             @close="close"
             @opened="updateLocationInit">
    <el-form :model="form" inline="true">
      <el-form-item :label="$t('LocationConfigurationManagement.dialogLocationId')" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-input size='mini' :placeholder="$t('common.placeholder')" v-model="form.locationID"
                  v-on:blur="legalCheck(form.locationID)&&locationIDCheck()" autocomplete="off" style="width: 200px"
                  :disabled="LocationIDShow" ></el-input>
      </el-form-item>
      <el-form-item :label="$t('LocationConfigurationManagement.dialogLocationName')" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-input size='mini' :placeholder="$t('common.placeholder')" v-model="form.locationName"
                  v-on:blur="legalCheck(form.locationName)&&locationNameCheck()" autocomplete="off" style="width: 200px"
                  ></el-input>
      </el-form-item>
      <el-form-item :label="$t('LocationConfigurationManagement.dialogLine')" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-input size='mini' :placeholder="$t('common.placeholder')" v-model="form.line"
                  v-on:blur="legalCheck(form.line)" autocomplete="off" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item :label="$t('LocationConfigurationManagement.dialogSite')" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-input size='mini' :placeholder="$t('common.placeholder')" v-model="form.site"
                  v-on:blur="legalCheck(form.site)" autocomplete="off" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item :label="$t('LocationConfigurationManagement.dialogKm')" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-input size='mini' :placeholder="$t('common.placeholder')" v-model="form.km" v-on:blur="legalCheck(form.km)"
                  autocomplete="off" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item :label="$t('LocationConfigurationManagement.dialogLongitude')" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-input size='mini' :placeholder="$t('common.placeholder')" v-model="form.longitude"
                  v-on:blur="legalCheck(form.longitude)" autocomplete="off" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item :label="$t('LocationConfigurationManagement.dialogLatitude')" :label-width="formLabelWidth"
                    style="margin-bottom: -10px;">
        <el-input size='mini' :placeholder="$t('common.placeholder')" v-model="form.latitude"
                  v-on:blur="legalCheck(form.latitude)" autocomplete="off" style="width: 200px"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size='mini' type="primary" @click="updateLocation" 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 LocationService from '@/domain/services/LocationService.js'
  import legitimacyCheck from "../../../utils/legitimacyCheck";
  import HelperUtil from '@/utils/HelperUtil'

  export default {
    name: "UpdateLocationConfig",
    props: ['command'],
    data: function () {
      return {
        dialog_locationUpdate: false,
        LocationIDShow: true,
        fullscreenLoading: false,
        initList: this.command.target,
        form: {
          locationID: null,
          locationName: null,
          line: null,
          site: null,
          km: null,
          longitude: null,
          latitude: null,
        },
        formLabelWidth: '120px',
      }
    },
    methods: {

      //关闭
      close() {
        this.command.done();
      },

      //更新机房
      updateLocationInit: function () {
        this.form.locationKey = this.initList.locationKey;
        this.form.locationID = this.initList.locationID;
        this.form.locationName = this.initList.locationName;
        this.form.line = this.initList.line;
        this.form.site = this.initList.site;
        this.form.km = this.initList.km;
        this.form.longitude = this.initList.longitude;
        this.form.latitude = this.initList.latitude;
        this.form.delFlag = this.initList.delFlag;
        this.form.modifyTime = this.initList.modifyTime;
      },

      //合法性检查
      legalCheck: function (attr) {
        let legal = legitimacyCheck();
        if (legal.textLengthCheck(attr)) {
          return true;
        } else {
          //所输文本长度必须控制在20之内
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.LENGTH_CODE))
          return false
        }
      },

      updateLocation: function () {
        let _this = this
        if (!_this.locationNameCheck()) {
          return
        }
        else {
          let loc = {};
          loc.locationKey = _this.form.locationKey;
          //*******************************************************************************
          //特别注意！特别注意！特别注意！后台的locationId的d是小写！！而前端定义的变量的ID是大写！！
          //*********************************************
          loc.locationId = _this.form.locationID;
          loc.locationName = _this.form.locationName;
          loc.line = _this.form.line;
          loc.site = _this.form.site;
          loc.km = _this.form.km;
          loc.longitude = _this.form.longitude;
          loc.latitude = _this.form.latitude;
          loc.delFlag = _this.form.delFlag;
          loc.modifyTime = _this.form.modifyTime;

          if (!(_this.locationNameCheck() && _this.locationIDCheck())) {
            return;
          }
          let loadingInstance = _this.Loading.openLoading();
          LocationService.modifyLocation(loc).then(result => {
            //成功
            _this.Loading.closeLoading(loadingInstance);
            _this.close()
            _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.MODIFY_CODE))
          }).catch(err => {
            //失败
            _this.Loading.closeLoading(loadingInstance);
            _this.InfoTip.errorTip(_this, err);
          });
//              this.command.done();
        }
      },

      locationNameCheck: function () {
        let locationCheck = legitimacyCheck()
        if (!locationCheck.textNullCheck(this.form.locationName)) {
          //输入不可为空
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE),this.$t('LocationConfigurationManagement.locationName'))
          return false
        }
        return true
      },

      locationIDCheck: function () {
        let locationCheck = legitimacyCheck()
        if (!locationCheck.textNullCheck(this.form.locationID)) {
          //输入不可为空
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE),this.$t('LocationConfigurationManagement.locationId'))
          return false
        }
        return true
      },
    },
    mounted() {
      this.dialog_locationUpdate = true;
    }
  }
</script>

