<template>
  <!-- 子系统点位（数据来源） -->
  <div id="dataSource" v-loading="loading">
    <div class="data-item" v-for="form in forms" :key="form.id">
      <el-form :model="form" label-width="150px">
        <el-form-item label="数据源名称:">
          {{form.Name || '--'}}
        </el-form-item>
        <el-form-item label="值单位说明:">
          {{form.ValueDescription || '--'}}
        </el-form-item>
        <el-form-item label="原始点位值:">
          {{form.Data || '--'}}
        </el-form-item>
        <el-form-item label="原始点位描述:">
          {{form.Description || '--'}}
        </el-form-item>
        <el-form-item label="位置标签:">
          {{form.LocationFlag.toString() || '--'}}
        </el-form-item>
        <el-form-item label="原始值处理方式:">
          {{form.DataRuleType  || '--'}}
        </el-form-item>
        <el-form-item label="原始值处理规则详情:">
          {{form.des  || '--'}}
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  getTabFunNumSourceData
} from '@/api/scan/request'
export default {
  data() {
    return {
      forms: [
      ],
      loading: true,
      toMap: {
        plus: '+',
        minus: '-',
        time: '*',
        divide: '/',
      }
    }
  },
  methods: {
    //加载数据
    loadDataSource() {
      this.loading = true;
      let pa = {
        Filters: "Infos='" + this.tabFunNum + "'"
      };
      getTabFunNumSourceData(pa, res => {
        this.forms = res.Content.map(t => {
          if (t.DataRuleContent) {
            t.des = this.formatContent(t.DataRuleType, t.DataRuleContent, t.EquipmentMark);
          }
          return t;
        });
        this.loading = false;
      });
    },
    formatContent(type, data, EquipmentMark) {
      let str = '', json = JSON.parse(data);
      if (type == '需按设置枚举转换') {
        try {
          json[0].content.forEach(t => {
            str += `如果原始值为${t.from}时,则转换为标准值${t.to};`
          })
        } catch (err) {
          console.log(err)
        }
      } else if (type == '需自动单位转换') {
        try {
          json[0].content.forEach(t => {
            str = `原始值单位转换为${t.from};`
          })
        } catch (err) {
          console.log(err)
        }
      } else if (type == '需按公式转换') {
        try {
          json.forEach(t => {
            if (t.ruletype == "type4") {
              str += `截取原始值第${t.content[0].from}位-第${t.content[0].to}位;`
            } else if (t.ruletype == "type5") {
              str += `仅提取其中的数值;`
            } else if (t.ruletype == "type6") {
              str += `数值用公式计算:(公式为:数值${this.toMap[t.content[0].calculationtype]}${t.content[0].value}公式计算);`
            }
          })
        } catch (err) {
          console.log(err)
        }
      } else if (type == '需拆分处理') {
        try {
          json.forEach(t => {
            if (t.ruletype == "type4") {
              str += `截取原始值第${t.content[0].from}位-第${t.content[0].to}位，为对象标识${EquipmentMark};`
            } else if (t.ruletype == "type2") {
              str += `如果截取部分的原始值为${t.content[0].from}时，则转换为标准值${t.content[0].to};`
            }
          })
        } catch (err) {
          console.log(err)
        }
      }
      return str;
    }
  },
  props: {
    tabFunNum: String
  },
  mounted() {
    this.loadDataSource();
  },
  watch: {
    tabFunNum() {
      this.loadDataSource();
    }
  }
}
</script>

<style lang="less" scoped>
#dataSource {
  height: 100%;
  overflow: auto;
}
.data-item {
  margin: 15px 10px;
  box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.2);
  background-color: white;
  padding: 10px 10px;
}
</style>