<template>
  <div class="layout-no-cols">
    <el-toolbar>
      <el-button @click="$router.back()" type="primary" size="medium" round class="shadow-primary"><i class="fa fa-chevron-left"></i> 返回上页</el-button>
      <span class="button-separator"></span>
      <el-button type="button" class="el-button el-button--default save" :priv="`Search.WeightsConfig.Edit${tabName}`" :loading="btnLoading" @click="saveWeightsClickHandler"><span><i class="fa fa-save"></i>保存</span></el-button>
    </el-toolbar>
    <div class="detail-panel form-panel">
      <el-form :model="formData" ref="editWeightsForm" label-width="128px">
        <el-form-item label="名称：" prop="name" :verify="['NotNull']">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="字段代码：" prop="subType" :verify="['NotNull']">
          <el-input v-model="formData.subType" disabled></el-input>
        </el-form-item>
        <el-form-item label="字段权重：" prop="weights" :verify="['NotNull','Number']">
          <el-input v-model="formData.weights"></el-input>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import util from '@src/common/util.js'

export default {
  props: {
    tabName: {
      type: String,
      default: 'FieldWeights'
    },
    ID: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      formData: {},
      btnLoading: false
    }
  },
  created() {
    if (this.ID !== 'new') {
      this.loadData()
    } else {
      this.formData = Object.assign({}, this.$route.query || {})
    }
  },
  methods: {
    async loadData() {
      let res = await axios.get(`/ui/fieldweightsconfig/${this.ID}`)
      if (res.data.status === 1) {
        this.formData = res.data.data
      } else {
        this.formData = {}
      }
    },
    validateFormData() {
      let name = this.formData.name
      let weights = this.formData.weights
      if (!name) {
        this.Dialog('请输入名称 ！')
        return false
      }
      if (weights === '') {
        this.Dialog('请输入权重 ！')
        return false
      }

      if (!/(^-?\d+\.?\d+$)|(^-?\d+$)/.test(weights)) {
        this.Dialog('权重值必须为数值 ！')
        return false
      }

      weights = weights - 0
      if (weights < Number.MIN_SAFE_INTEGER || weights > Number.MAX_SAFE_INTEGER) {
        this.Dialog(`权重值超出安全数值范围(${Number.MIN_SAFE_INTEGER} - ${Number.MAX_SAFE_INTEGER})`)
        return false
      }
      return true
    },

    // 保存权重处理事件（字段，均衡，距离）
    async saveWeightsClickHandler() {
      if (this.validateFormData() === false) {
        return
      }
      if (this.ID === 'new') {
        this.formData.ID = 0
      }
      this.btnLoading = true
      let res = null
      if (this.tabName === 'FieldWeights') {
        res = await axios.put(`/ui/fieldweightsconfig/${this.formData.ID}`, this.formData)
      } else if (this.tabName === 'BlanceWeights') {
        res = await axios.put(`/ui/blanceweightsconfig/${this.formData.ID}`, this.formData)
      } else if (this.tabName === 'DistanceWeights') {
        res = await axios.put(`/ui/distanceweightsconfig/${this.formData.ID}`, this.formData)
      }

      this.btnLoading = false

      if (!res) {
        this.Dialog('请求失败 ！')
        return
      }

      let data = res.data
      if (data.status === 1) {
        this.$router.back()
        util.showResponseMessage(data)
      } else {
        this.Dialog(data.message, 'error')
      }
    },

    // 消息Dialog封装
    Dialog(msg) {
      this.$confirm(msg, '系统提示', {
        confirmButtonText: '确定',
        showCancelButton: false,
        type: 'warning'
      })
    },

    // 消息显示封装
    MsgPop(msg, type) {
      if (!type) {
        this.$message(msg)
        return
      }
      this.$message({
        message: msg,
        type: type
      })
    }
  }
}
</script>
