<script lang="ts">
export default {
  name: "x6查看结果数据",
};
</script>

<script setup lang="ts">
import { onMounted, ref, nextTick } from "vue";
import { ElForm, ElMessage, ElMessageBox, selectEmits } from "element-plus";
import Page from '@/components/Pagination/page.vue'
import { getByScenario,exportAllExports } from "@/api/business/scenario";
const apiUrl = import.meta.env.VITE_APP_BASE_API; // 基础路径
const emit = defineEmits([ 'closeEditdevice']);

const props = defineProps({
  deviceId: {
    required: false,
    type: String,
    default: ''
  },
  scenarioId: {
    required: false,
    type: String,
    default: ''
  },

})
const queryParams = ref({
  current: 1,
  size: 10,
})
const total = ref(0)
const scenarioResultData:any = ref([]) 

const selectData = ref<any>([])
function  getIf(list:any,code:any){
  for(let i = 0;i<list.length;i++){
    if(list[i] == code){
      return true
    }
  }

}
function getScenarioResults(){
  scenarioResultData.value = []
  getByScenario({
    scenarioId: props.scenarioId,
    deviceId: props.deviceId,
    pageNum:queryParams.value.current,
    pageSize:queryParams.value.size
  }).then((res:any) => {
   for(let i = 0;i<res.data.records.length;i++){
    if(res.data.records[i].attrState != null){
      let attrState = JSON.parse(res.data.records[i].attrState)
      for (const key in attrState) {
        if (!Object.hasOwn(attrState, key)) continue;
        const element = attrState[key];
        res.data.records[i][key] = element
        if(i == 0){
          selectData.value.push(key)
        }
       
      }
      
    }
   }
    scenarioResultData.value = res.data.records
    total.value = res.data.total
  })
}
onMounted(() => {
  getScenarioResults()

});
function downloadFile(obj :any, name :any, suffix :any) {
  const url = window.URL.createObjectURL(new Blob([obj]))
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  const fileName = name.trim() + '.' + suffix
  link.setAttribute('download', fileName)
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}

 function exportExportsClick(){
  exportAllExports(props.scenarioId,props.deviceId).then((response:any) => {
    debugger
     downloadFile(response, '结果数据' , 'xlsx')
  });
}
</script>

<template>
  <div class="editdevice-box">
    <div style="margin-bottom: 10px;display: flex;justify-content: flex-end;">
        <el-button  type="primary" @click="exportExportsClick">导出</el-button>   
    </div>

    <el-table  :data="scenarioResultData" style="width: 100%; height: calc(100vh - 315px);margin-bottom: 10px;" border
      :header-cell-style="{ background: 'rgb(250 250 250)', color: '#383838', height: '50px' }">
      <el-table-column type="selection" width="50" align="center"></el-table-column>
      <el-table-column prop="step" label="时间" width="100"></el-table-column>
      <el-table-column prop="keffValue" label="keff" min-width="100"></el-table-column>
      <el-table-column v-if="getIf(selectData,'diameter')" prop="diameter" label="直径cm" min-width="100"></el-table-column>
      <el-table-column v-if="getIf(selectData,'height')" prop="height" label="高度cm" min-width="100"></el-table-column>
      <el-table-column v-if="getIf(selectData,'width')" prop="width" label="宽度cm" min-width="100"></el-table-column>
      <el-table-column v-if="getIf(selectData,'length')" prop="length" label="长度cm" min-width="100"></el-table-column>
      <el-table-column v-if="getIf(selectData,'volume')" prop="volume" label="体积（单位：L）" min-width="160"></el-table-column>
      <el-table-column v-if="getIf(selectData,'flow_rate')" prop="flow_rate" label="流量（单位：m3/h）" min-width="160"></el-table-column>
      <el-table-column v-if="getIf(selectData,'pulse_velocity')" prop="pulse_velocity" label="脉冲速度（单位：Hz）" min-width="160"></el-table-column>
      <el-table-column v-if="getIf(selectData,'u_concentration')" prop="u_concentration" label="铀浓度（g/L）" min-width="160"></el-table-column>
      <el-table-column v-if="getIf(selectData,'uo2_density')" prop="uo2_density" label="氧化铀密度（g/cm3）" min-width="160"></el-table-column>
      <el-table-column v-if="getIf(selectData,'u_enrichment')" prop="u_enrichment" label="铀富集度（%）" min-width="160"></el-table-column>
      <el-table-column v-if="getIf(selectData,'pu_concentration')" prop="pu_concentration" label="钚浓度（g/L）" min-width="160"></el-table-column>
      <el-table-column v-if="getIf(selectData,'puo2_density')" prop="puo2_density" label="氧化钚密度（g/cm3）" min-width="160"></el-table-column>
      <el-table-column v-if="getIf(selectData,'pu_isotope')" prop="pu_isotope" label="钚同位素比例（PU-240占比）%" min-width="160"></el-table-column>
      <el-table-column v-if="getIf(selectData,'hno3_acidity')" prop="hno3_acidity" label="硝酸酸度（mol/L）" min-width="160"></el-table-column>
      <el-table-column v-if="getIf(selectData,'h2c2o4_concentration')" prop="h2c2o4_concentration" label="草酸浓度（mol/L）" min-width="160"></el-table-column>
      <el-table-column v-if="getIf(selectData,'organic_ratio')" prop="organic_ratio" label="有机相比例%" min-width="160"></el-table-column>
      <el-table-column v-if="getIf(selectData,'moisture_content')" prop="moisture_content" label="含水率%" min-width="160"></el-table-column>
    </el-table>
    <div  style="display: flex; justify-content: flex-end;">
    <Page :total="total" v-model:size="queryParams.size" v-model:current="queryParams.current" @pagination="getScenarioResults" ></Page>

    </div>
    
  </div>

</template>

<style scoped>

</style>
