<template>
  <div>
    <div class="content_box" v-if="list.length > 0">
      <div class="content_box_list" v-for="(item, idx) in list" :key="idx">
        <div class="content_ll">
          <div class="name">质检类型</div>
          <div>
            {{ item.categoryLevelName }}
          </div>
        </div>

        <div class="content_ll">
          <div class="name">标准类型</div>
          <div>
            {{
              getDictValue(
                '质检标准类型',
                item.qualityStandard && item.qualityStandard.type
              )
            }}
          </div>
        </div>

        <div class="content_ll">
          <div class="name">标准编码</div>
          <div class="">{{
            item.qualityStandard && item.qualityStandard.code
          }}</div>
        </div>

        <div class="content_ll">
          <div class="name">标准名称</div>
          <div class="">{{
            item.qualityStandard && item.qualityStandard.name
          }}</div>
        </div>

        <el-form ref="form" style="width: 100%">
          <el-table
            style="margin-top: 15px"
            :data="
              item.qualityStandard && item.qualityStandard.parameterStandards
            "
            border
          >
            <el-table-column
              :label="
                item.qualityStandard &&
                item.qualityStandard.singleWeightDivision == 'undefined undefined'? '' :  item.qualityStandard.singleWeightDivision 
              "
              align="center"
            >
              <el-table-column
                label="参数上限"
                align="center"
                v-if="
                  item.qualityStandard &&
                  item.qualityStandard.parameterType == 3
                "
              >
                <template slot-scope="scope">
                  <el-form-item label-width="0" prop="finalValue">
                    <el-input
                      clearable
                      disabled
                      v-model="scope.row.finalValue"
                      placeholder="请输入"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="参数下限"
                align="center"
                v-if="
                  item.qualityStandard &&
                  item.qualityStandard.parameterType == 3
                "
              >
                <template slot-scope="scope">
                  <el-form-item label-width="0" prop="initialValue">
                    <el-input
                      clearable
                      disabled
                      v-model="scope.row.initialValue"
                      placeholder="请输入"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="默认值"
                align="center"
                v-if="
                  item.qualityStandard &&
                  item.qualityStandard.parameterType != 3
                "
              >
                <template slot-scope="scope">
                  <el-form-item label-width="0" prop="defaultValue">
                    {{ scope.row.defaultValue }}
                  </el-form-item>
                </template>
              </el-table-column>
            </el-table-column>

            <el-table-column
              :label="item.qualityStandard && item.qualityStandard.tolerance == 'undefined undefined' ? '' :  item.qualityStandard.tolerance "
              align="center"
            >
              <el-table-column label="质检标准" align="center">
                <template slot-scope="scope">
                  <el-form-item label-width="0" prop="inspectionStandard">
                    {{ scope.row.symbol }}
                    {{ scope.row.toleranceValue }}
                  </el-form-item>
                </template>
              </el-table-column>
            </el-table-column>

            <el-table-column label="质检结果" align="center">
              <template slot-scope="scope">
                <el-input
                  clearable
                  v-model="scope.row.qualityResults"
                  placeholder="请输入质检结果"
                >
                </el-input>
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
  import dictMixins from '@/mixins/dictMixins';
  import { getByCode } from '@/api/system/dictionary-data';

  export default {
    components: {},
    mixins: [dictMixins],
    props: {
      qualityParam: {
        type: Array
      }
    },

    watch: {
      qualityParam: {
        handler(val) {
          this.list = val;
          this.$forceUpdate();
        },
        deep: true,
        immediate: true
      }
    },

    data() {
      return {
        list: [],
        dictList: []
      };
    },

    created() {
      this.requestDict('质检标准类型');
      this.getDictList('mathematical_symbol');
    },

    methods: {
      updateOrCreateObjectInArray(array, newObj, idKey = 'id') {
        // 用来检查是否已存在具有特定 id 的对象
        const exists = array.some((obj) => obj[idKey] === newObj[idKey]);

        if (exists) {
          // 如果存在，使用 map 来替换找到的对象
          return array.map((obj) =>
            obj[idKey] === newObj[idKey] ? newObj : obj
          );
        } else {
          // 如果不存在，将新对象添加到数组中
          return [...array, newObj];
        }
      },

      async getDictList(code) {
        let { data: res } = await getByCode(code);
        this.dictList = res.map((item) => {
          let values = Object.keys(item);
          return {
            value: Number(values[0]),
            label: item[values[0]]
          };
        });
      },

      getDate() {
        return this.list;
      }
    }
  };
</script>

<style lang="scss" scoped>
  .content_box {
    width: 100%;
    margin-top: 12px;
    max-height: 42vh;
    overflow-y: scroll;
  }

  .content_box_list {
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 30px;
  }

  .content_ll {
    width: 23%;
    display: flex;
    flex-direction: row;

    margin: auto;
    margin-top: 10px;

    .name {
      width: 130px;
      color: #000;
      font-weight: 500;
    }
  }
</style>
