<template>
  <div class="shiyan-page BaseApp-page">
    <header class="BaseApp-page-header">
      <div class="debug-header-title">仪器校准</div>
    </header>
    <main class="BaseApp-page-main">
      <div class="shiyan-inner">
        <div class="shiyan-article-title">
          <div class="shiyan-article-title__icon"></div>
          A标准
        </div>
        <article class="shiyan-article">
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.a_1_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.a_1_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.a_2_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.a_2_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.a_3_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.a_3_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.a_4_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.a_4_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.a_5_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.a_5_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
        </article>
      </div>
      <div class="shiyan-inner">
        <div class="shiyan-article-title">
          <div class="shiyan-article-title__icon"></div>
          B标准
        </div>
        <article class="shiyan-article">
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.b_1_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.b_1_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.b_2_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.b_2_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.b_3_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.b_3_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.b_4_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.b_4_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.b_5_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.b_5_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
        </article>
      </div>
      <div class="shiyan-inner">
        <div class="shiyan-article-title">
          <div class="shiyan-article-title__icon"></div>
          C标准
        </div>
        <article class="shiyan-article">
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.c_1_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.c_1_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.c_2_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.c_2_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.c_3_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.c_3_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.c_4_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.c_4_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">实测值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.c_5_old"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
          <div class="shiyan-article-item">
            <div class="shiyan-article-item__label">参照值</div>
            <div class="shiyan-article-item__value">
              <CommonInput
                v-model="formData.c_5_new"
                inputType="number"
                size="small"
                inputUnit="℃"></CommonInput>
            </div>
          </div>
        </article>
      </div>
    </main>
    <footer class="BaseApp-page-footer">
      <div class="BaseApp-btn" @click="goBack">返回</div>
      <div class="BaseApp-btn BaseApp-btn--primary" @click="submit">保存</div>
    </footer>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { notify } from '../../utils/message.js';
import { setCoefficientCalculationApi } from '../../api/equipment.js';
const router = useRouter();
let formData = reactive({
  a_1_old: '',
  a_1_new: '',
  a_2_old: '',
  a_2_new: '',
  a_3_old: '',
  a_3_new: '',
  a_4_old: '',
  a_4_new: '',
  a_5_old: '',
  a_5_new: '',
  b_1_old: '',
  b_1_new: '',
  b_2_old: '',
  b_2_new: '',
  b_3_old: '',
  b_3_new: '',
  b_4_old: '',
  b_4_new: '',
  b_5_old: '',
  b_5_new: '',
  c_1_old: '',
  c_1_new: '',
  c_2_old: '',
  c_2_new: '',
  c_3_old: '',
  c_3_new: '',
  c_4_old: '',
  c_4_new: '',
  c_5_old: '',
  c_5_new: ''
});

const goBack = () => {
  router.go(-1);
};

const submit = async () => {
  let aPrams = {
    reference_value: [],
    real_value: [],
    tag: '1'
  };
  let bPrams = {
    reference_value: [],
    real_value: [],
    tag: '2'
  };
  let cPrams = {
    reference_value: [],
    real_value: [],
    tag: '3'
  };

  for (let key in formData) {
    if (Number.isNaN(Number.parseFloat(formData[key]))) {
      continue;
    }

    if (key.includes('a_')) {
      if (key.includes('_old')) {
        aPrams.real_value.push(Number.parseFloat(formData[key]));
      }
      if (key.includes('_new')) {
        aPrams.reference_value.push(Number.parseFloat(formData[key]));
      }
    }
    if (key.includes('b_')) {
      if (key.includes('_old')) {
        bPrams.real_value.push(Number.parseFloat(formData[key]));
      }
      if (key.includes('_new')) {
        bPrams.reference_value.push(Number.parseFloat(formData[key]));
      }
    }
    if (key.includes('c_')) {
      if (key.includes('_old')) {
        cPrams.real_value.push(Number.parseFloat(formData[key]));
      }
      if (key.includes('_new')) {
        cPrams.reference_value.push(Number.parseFloat(formData[key]));
      }
    }
  }

  if (
    aPrams.reference_value.length != 5 &&
    aPrams.real_value.length != 5 &&
    bPrams.reference_value.length != 5 &&
    bPrams.real_value.length != 5 &&
    cPrams.reference_value.length != 5 &&
    cPrams.real_value.length != 5
  ) {
    notify('请填写完整5组数据', 'danger');
    return;
  }

  // a 标准
  if (aPrams.reference_value.length == 5 && aPrams.real_value.length == 5) {
    let res = await setCoefficientCalculationApi(aPrams);
    let { data, desc } = res.data;
    if (res?.data?.code !== 200) {
      notify(desc, 'danger');
      return;
    } else {
      notify(`A标准保存成功：${desc}`, 'success');
    }
  }
  // b 标准
  if (bPrams.reference_value.length == 5 && bPrams.real_value.length == 5) {
    let res = await setCoefficientCalculationApi(bPrams);
    let { data, desc } = res.data;
    if (res?.data?.code !== 200) {
      notify(desc, 'danger');
      return;
    } else {
      notify(`B标准保存成功：${desc}`, 'success');
    }
  }
  //c 标准
  if (cPrams.reference_value.length == 5 && cPrams.real_value.length == 5) {
    let res = await setCoefficientCalculationApi(cPrams);
    let { data, desc } = res.data;
    if (res?.data?.code !== 200) {
      notify(desc, 'danger');
      return;
    } else {
      notify(`C标准保存成功：${desc}`, 'success');
    }
  }
};

onMounted(() => {});
</script>

<style lang="less" scoped>
.shiyan-page {
  font-size: 20px;
  color: #666666;
  .BaseApp-page-header {
    margin: 40px 0 0 40px;
    .debug-header-title {
      font-weight: bold;
      font-size: 28px;
      color: #3a3a3a;
    }
  }
  .BaseApp-page-main {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-template-rows: repeat(2, 1fr);
    gap: 15px;
    overflow-y: auto;
  }
  .shiyan-inner {
    padding: 20px;
    background: #f6f6f6;
    border: 1px solid #cccccc;
    border-radius: 12px;
    .shiyan-article-title {
      display: flex;
      flex-direction: row;
      align-items: center;
      font-weight: bold;
      font-size: 24px;
      color: #3a3a3a;
      .shiyan-article-title__icon {
        width: 6px;
        height: 28px;
        margin-right: 25px;
        background: linear-gradient(180deg, #ff7b83 0%, #850006 100%);
        box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.4);
        border-radius: 8px 8px 8px 8px;
      }
    }
    .shiyan-article {
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      padding: 0 15px;
      .shiyan-article-item {
        display: flex;
        flex-direction: row;
        align-items: center;
        width: 50%;
        margin-top: 25px;
        &:nth-child(2n) {
          justify-content: flex-end;
        }
        .shiyan-article-item__value {
          display: flex;
          flex-direction: row;
          align-items: center;
          margin-left: 20px;
        }
      }
    }
  }
  :deep(.out-container .input-container.small) {
    width: 150px;
  }
}
</style>
