<template>
  <div class="app-container">
    <el-select v-model="NGCyzf" @change="handleChange()" placeholder="请选择常用组分" clearable>
      <el-option v-for="dict in dict.type.ngtools_cyzf" :key="dict.value" :label="dict.label" :value="dict.value" />
    </el-select>
    <el-form :model="ngComponentData" label-position="top" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="甲烷C1" prop="ngC1">
        <el-input v-model="ngComponentData.ngC1" placeholder="请输入甲烷C1" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="氮气N2" prop="ngN2">
        <el-input v-model="ngComponentData.ngN2" placeholder="请输入氮气N2" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="二氧化碳CO2" prop="ngCo2">
        <el-input v-model="ngComponentData.ngCo2" placeholder="请输入二氧化碳CO2" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="乙烷C2" prop="ngC2">
        <el-input v-model="ngComponentData.ngC2" placeholder="请输入乙烷C2" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="丙烷C3" prop="ngC3">
        <el-input v-model="ngComponentData.ngC3" placeholder="请输入丙烷C3" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="水H2O" prop="ngH2o">
        <el-input v-model="ngComponentData.ngH2o" placeholder="请输入水H2O" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="硫化氢H2S" prop="ngH2s">
        <el-input v-model="ngComponentData.ngH2s" placeholder="请输入硫化氢H2S" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="氢气H2" prop="ngH2">
        <el-input v-model="ngComponentData.ngH2" placeholder="请输入氢气H2" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="一氧化碳CO" prop="ngCo">
        <el-input v-model="ngComponentData.ngCo" placeholder="请输入一氧化碳CO" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="氧气O2" prop="ngO2">
        <el-input v-model="ngComponentData.ngO2" placeholder="请输入氧气O2" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="异丁烷iC4" prop="ngIc4">
        <el-input v-model="ngComponentData.ngIc4" placeholder="请输入异丁烷iC4" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="正丁烷nC4" prop="ngNc4">
        <el-input v-model="ngComponentData.ngNc4" placeholder="请输入正丁烷nC4" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="异戊烷iC5" prop="ngIc5">
        <el-input v-model="ngComponentData.ngIc5" placeholder="请输入异戊烷iC5" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="正戊烷nC5" prop="ngNc5">
        <el-input v-model="ngComponentData.ngNc5" placeholder="请输入正戊烷nC5" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="己烷C6" prop="ngC6">
        <el-input v-model="ngComponentData.ngC6" placeholder="请输入己烷C6" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="庚烷C7" prop="ngC7">
        <el-input v-model="ngComponentData.ngC7" placeholder="请输入庚烷C7" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="辛烷C8" prop="ngC8">
        <el-input v-model="ngComponentData.ngC8" placeholder="请输入辛烷C8" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="壬烷C9" prop="ngC9">
        <el-input v-model="ngComponentData.ngC9" placeholder="请输入壬烷C9" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="癸烷C10" prop="ngC10">
        <el-input v-model="ngComponentData.ngC10" placeholder="请输入癸烷C10" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="氦气He" prop="ngHe">
        <el-input v-model="ngComponentData.ngHe" placeholder="请输入氦气He" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="氩气Ar" prop="ngAr">
        <el-input v-model="ngComponentData.ngAr" placeholder="请输入氩气Ar" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="合计" prop="sum">
        <el-input v-model="sumzf" placeholder="请输入合计" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { listComponents, getComponents, delComponents, addComponents, updateComponents } from '@/api/ngtools/components';

export default {
  name: 'NgComponentsForm',
  props: {
    strZf: {
      type: String,
      default: ''
    }
  },
  dicts: ['ngtools_cyzf'],
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      componentsList: [],
      title: '',
      open: false,
      form: {},
      rules: {},
      ngComponentData: {},
      NGCyzf: {},
      sumzf: 0
    };
  },
  watch: {
    ngComponentData: {
      deep: true,
      handler(newVal) {
        const result = this.calculateSum(newVal);
        this.sumzf = result.sum;
        const valuesStr = result.valuesStr;
        console.log(this.sumzf, valuesStr);
        const diff = Math.abs(this.sumzf - 100);
        if (diff < 0.0001) {
          this.$emit('update:strZf', valuesStr);
        } else {
          Message.error('归一化错误：组分总和与 100 的差值超过 0.0001');
        }
      }
    },
    strZf: {
      handler(newVal) {
        const values = newVal.split(',');
        const keys = [
          'ngC1',
          'ngN2',
          'ngCo2',
          'ngC2',
          'ngC3',
          'ngH2o',
          'ngH2s',
          'ngH2',
          'ngCo',
          'ngO2',
          'ngIc4',
          'ngNc4',
          'ngIc5',
          'ngNc5',
          'ngC6',
          'ngC7',
          'ngC8',
          'ngC9',
          'ngC10',
          'ngHe',
          'ngAr'
        ];
        this.ngComponentData = keys.reduce((obj, key, index) => {
          obj[key] = values[index] || '';
          return obj;
        }, {});
      },
      immediate: true
    }
  },
  created() {
    this.strZf && this.$options.watch.strZf.handler.call(this, this.strZf);
  },
  methods: {
    handleChange() {
      console.log(this.NGCyzf);
      try {
        const cleanJSON = this.NGCyzf.replace(/\\'"/g, '"').replace(/'/g, '"');
        const parsedData = JSON.parse(cleanJSON);
        this.ngComponentData = { ...this.ngComponentData, ...parsedData };
      } catch (error) {
        console.error('JSON 解析错误:', error);
      }
    },
    getList() {
      this.loading = true;
      listComponents(this.ngComponentData).then((response) => {
        this.componentsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    calculateSum(obj) {
      const sum = Object.values(obj)
        .map((v) => parseFloat(v) || 0)
        .reduce((acc, cur) => acc + cur, 0)
        .toFixed(4);
      const valuesStr = Object.values(obj).join(',');
      return { sum, valuesStr };
    },
    reset() {
      this.form = {
        id: null,
        createName: null,
        createBy: null,
        createTime: null,
        updateName: null,
        updateBy: null,
        updateTime: null,
        ngC1: null,
        ngN2: null,
        ngCo2: null,
        ngC2: null,
        ngC3: null,
        ngH2o: null,
        ngH2s: null,
        ngH2: null,
        ngCo: null,
        ngO2: null,
        ngIc4: null,
        ngNc4: null,
        ngIc5: null,
        ngNc5: null,
        ngC6: null,
        ngC7: null,
        ngC8: null,
        ngC9: null,
        ngC10: null,
        ngHe: null,
        ngAr: null,
        sum: null,
        sampleno: null,
        meterno: null,
        cyzf: null
      };
      this.resetForm('form');
    }
  }
};
</script>

<style scoped>
/* 可以根据需要添加样式 */
</style>
