/* eslint-disable semi-spacing */
<template>
  <div class="add-target commonFont">
    <el-form
      ref="formTarget"
      :model="formData"
      size="small"
      label-width="100px"
      :rules="formRules"
      label-position="left"
      style="width: 600px;"
    >
      <el-form-item
        label="指标分类："
        prop="indicatorType"
      >
        <el-select
          v-model="formData.indicatorType"
          placeholder="请选择指标分类"
        >
          <el-option
            v-for="item in calsseDataList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="type">
        <div
          slot="label"
          style="margin-left:10px;"
        >指标类型：</div>
        <el-select
          v-model="formData.type"
          placeholder="请选择指标分类"
        >
          <el-option
            v-for="item in targetTypeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>

      </el-form-item>
      <el-form-item
        prop="name"
        label="指标名称："
      >
        <el-input
          v-model="formData.name"
          placeholder="请输入指标名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="keyResult">
        <div
          slot="label"
          style="margin-left:10px;"
        >考核标准：</div>
        <el-input
          type="textarea"
          v-model="formData.keyResult"
          placeholder="请输入指标名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="weight">
        <div
          slot="label"
          style="margin-left:10px;"
        >权重：</div>

        <el-input
          v-model="formData.weight"
          placeholder="请输入指标名称"
          clearable
          @blur="$handleBlur('formData.weight')"
          @input.native="$handleInputInt('formData.weight')"
        >
          <el-button slot="append">%</el-button>
        </el-input>
        <div class="comonPromptFont">权重设置为0时，此指标将不参与运算</div>

      </el-form-item>
      <el-form-item label=" ">
        <el-button
          type="primary"
          @click="handleSaveSet"
        >保存设置</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>
<script>
import { apiGetIndicatorType, apiGetIndicatorLibraryEdit } from '@/api/target'
import { getDimensions } from '@/api/data'

export default {
  methods: {
    // handleWeightChange (val) {
    //   if (val) {
    //     if (val.length > 0) {
    //       let weight = parseFloat(this.formData.weight)
    //       this.formData.weight = weight
    //     }
    //     console.log(this.formData.weight)
    //     // if (val.length > 0) {
    //     //   let weightTwo = weight.toFixed(2).toString()
    //     //   let arr = weightTwo.split('.')
    //     //   if (arr.length === 2) {
    //     //     /* eslint-disable */
    //     //     for (let index = weightTwo.length - 1;index < weightTwo.length;index--) {
    //     //       let sub = weightTwo.substring(index, 1)
    //     //       if (sub !== '0' && sub !== '.') {
    //     //         weightTwo = weightTwo.substring(0, index)
    //     //         break
    //     //       }

    //     //     }
    //     //     console.log(weightTwo)
    //     //   }
    //     // }
    //   }
    // },
    handleSaveSet () {
      // 修改 新增 指标
      this.$refs.formTarget.validate((valid) => {
        if (valid) {
          let weight = this.formData.weight
          if (weight) {
            weight = this.formData.weight / 100 > 1 ? 1 : this.formData.weight / 100
          }
          let para = {
            indicatorType: this.formData.indicatorType,
            type: this.formData.type,
            name: this.formData.name,
            keyResult: this.formData.keyResult,
            weight: weight
          }
          if (this.formData.id) {
            para['id'] = this.formData.id
          }
          apiGetIndicatorLibraryEdit(para).then(res => {
            if (res.code === 200) {
              this.$message.success(res.msg)
              this.$router.go(-1)
            }
          })
        } else {
          return false
        }
      })
    },
    handleGetIndicatorType () {
      let para = {
        currPage: 1,
        type: 0,
        pageSize: 40
      }
      apiGetIndicatorType(para).then(res => {
        if (res.code === 200) {
          let data = res.data
          // 指标分类
          this.calsseDataList = data.list
          this.calsseDataList.push({ id: 0, name: '未分类指标' })
          if (this.calsseDataList.length > 0 && this.changeTargetId === -1) {
            let item = data.list[0]
            this.formData.indicatorType = item.id
          }
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleGetDimensionsList () {
      // 获取类型
      getDimensions().then(res => {
        if (res.code !== 200) {
          this.targetTypeList = []
          this.$message.error(res.msg)
          return
        }
        this.targetTypeList = res.data
        if (this.targetTypeList.length > 0 && this.changeTargetId === -1) {
          let item = res.data[0]
          this.formData.type = item.id
        }
      })
    }
  },
  created () {
    if (this.$route.query.id) {
      this.changeTargetId = this.$route.query.id
      this.formData = this.$route.query
    }
    // 获取类型  和 分类列表
    this.handleGetIndicatorType()
    this.handleGetDimensionsList()
  },
  data () {
    return {
      changeTargetId: -1, // 要修改的 指标id
      // indicatorType: 0, type: 0, name: '', keyResult: '', weight: ''
      formData: { indicatorType: 0, type: 0, name: '', keyResult: '', weight: '' },
      formRules: {
        indicatorType: [{ required: true, message: '请选择指标分类', trigger: 'blur' }],
        name: [{ required: true, message: '请选择指标名称', trigger: 'blur' }]
      },
      calsseDataList: [{ id: 0, name: '未分类指标' }], // {id:2,name:'',type:1,orderBy:''}
      targetTypeList: [] // {id:2,name:'',type:1,orderBy:''}
    }
  }
}
</script>
<style lang='less' scoped>
.add-target {
  padding: 28px;
  background-color: #fff;
}
</style>
