<template>
  <div class="XFpdding">
    <ul>
      <li
        class="item"
        v-for="(tab, index) in tabslist1"
        :key="index"
        :class="{ active: currentTab1 === index }"
        @click="selectTab1(index)"
      >
        <img :src="tab.imgSrc"/>
        {{ tab.name }}
      </li>
    </ul>
    <!-- 网格文件 -->
    <!-- 执行 -->
    <div
      class="eldesign classtable"
      style="margin-top: 10px;height: 400px;"
      v-if="currentTab1 == '0'"
    >
      <div style="display: flex;flex-direction: column;">
        <el-form-item label="网格文件：" :label-width="formLabelWidth3" style="width: 100%">
          <el-row style="width: 100%">
            <el-col :span="23">
              <el-input
                v-model="adflowvalue.gridfile"
                readonly
                :step="100"
                :min="0"
                :max="1000"
                controls-position="right"
              />
            </el-col>
            <!-- 文件上传按钮部分 -->
            <el-col :span="1" style="display: flex; align-items: center; margin-left: -35px">
              <fileUploads
                :projectId="124" 
                solverType="exampleSolver" 
                accept=".cgns" 
                upId="uniqueId1" 
                name="点击选择文件"
                :imgSrc="meshFileImgSrc"
                @upload-success="handleFileUploadSuccess"
                @update-fileName="updateFileName"
                @update-percentage="updatePercentage"
                @upload-status="getUploadStatus"
              />
            </el-col>
          </el-row>
          <!-- 进度条 -->
          <el-row v-if="showProgress" style="width: 100%; margin-top: 10px;">
            <el-col :span="20">
              <el-progress :percentage="percentage"></el-progress>
            </el-col>
            <el-col :span="4">
              <div style="line-height: 15px">{{uploadStatus}}</div>
            </el-col>
            <!-- <el-col :span="2">
              <el-button type="text" @click="fileDel" :icon="Delete" circle style="color: red; padding-bottom:25px;" />
            </el-col> -->
          </el-row>
        </el-form-item>
      <div style="flex-grow: 1; ">

      </div>
      </div>
    </div>
    <!-- 来流参数 v-show="currentTab=='0'"-->
    <div v-show="currentTab1 == '1'">
      <div class="eldesign classtable" style="margin-top: 10px">
        <el-table :data="inParams" border style="width: 100%; height: 400px">
          <el-table-column type="index" width="70" label="编号" />
          <el-table-column prop="name" label="参数名称">
            <!-- <template #default="{ row }">
              <el-input v-model="row.name" @change="handleEdit(row)" />
            </template> -->
          </el-table-column>
          <el-table-column prop="value" label="参数值">
            <template #default="{ row }">
              <el-input v-model="row.value" type="number" />
            </template>
          </el-table-column>
          <el-table-column prop="flag" label="启用" width="100">
            <template v-slot="scope">
              <el-checkbox
                :false-label="0"
                :true-label="1"
                :label="false"
                v-model="scope.row.flag"
              />
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <!-- 设置参数 -->

    <div
      class="eldesign classtable dlheight"
      style="margin-top: 10px"
      v-show="currentTab1 == '2'"
    >
      <el-form-item label="工况条件：" :label-width="formLabelWidth1">
        <el-select
          v-model="adflowvalue.proname"
          :suffix-icon="CaretBottom"
          placeholder="请选择"
        >
          <el-option
            v-for="item in wclist"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="计算维数：" :label-width="formLabelWidth1">
        <el-select
          v-model="isairfoil"
          :suffix-icon="CaretBottom"
          placeholder="请选择"
        >
          <el-option
            v-for="item in isairfoilList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="控制方程：" :label-width="formLabelWidth1">
        <el-select
          v-model="equationtype"
          :suffix-icon="CaretBottom"
          placeholder="请选择"
        >
          <el-option
            v-for="item in equationtypelist"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="升力定义方向：" :label-width="formLabelWidth1">
        <el-select
          v-model="liftindex"
          :suffix-icon="CaretBottom"
          placeholder="请选择"
        >
          <el-option
            v-for="item in liftindexlist"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="特征长度：" :label-width="formLabelWidth1">
        <el-input
          v-model="adflowvalue.length"
          :step="100"
          :min="0"
          :max="1000"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item label="参考温度：" :label-width="formLabelWidth1">
        <el-input
          v-model="adflowvalue.temperature"
          :step="1"
          :min="0"
          :max="1000"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item label="参考面积：" :label-width="formLabelWidth1">
        <el-input
          v-model="adflowvalue.area"
          :step="100"
          :min="0"
          :max="1000"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item label="力矩中心：" :label-width="formLabelWidth1">
        <div style="display: flex">
          <el-input
            style="margin-right: 10px"
            v-model="adflowvalue.momx"
            :step="100"
            :min="0"
            :max="1000"
            controls-position="right"
          />
          <el-input
            style="margin-right: 10px"
            v-model="adflowvalue.momy"
            :step="100"
            :min="0"
            :max="1000"
            controls-position="right"
          />
          <el-input
            v-model="adflowvalue.momz"
            :step="100"
            :min="0"
            :max="1000"
            controls-position="right"
          />
        </div>
      </el-form-item>

      <el-form-item label="多重网格：" :label-width="formLabelWidth1">
        <el-input
          v-model="adflowvalue.mgcycle"
          :step="100"
          :min="0"
          :max="1000"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item label="计算步数：" :label-width="formLabelWidth1">
        <el-input
          v-model="adflowvalue.ncycles"
          :step="100"
          :min="0"
          :max="1000"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item label="监控变量：" :label-width="formLabelWidth1">
        <el-row>
        <el-col :span="12">
          <el-checkbox v-model="cl" :false-label="0"
          :true-label="1" label="升力系数Cl" size="large" />
          <el-checkbox v-model="resrho" :false-label="0"
          :true-label="1" label="密度残差Resrho" size="large" />
      
        </el-col>
        <el-col :span="12">
          <el-checkbox v-model="cd" :false-label="0"
          :true-label="1" label="阻力系数Cd" size="large" />
          <el-checkbox v-model="cmz" :false-label="0"
          :true-label="1" label="力矩系数Cm" size="large" />
        </el-col>
      </el-row>
      </el-form-item>
      <el-form-item label="细网格收敛残差：" :label-width="formLabelWidth1">
        <el-input
          v-model="adflowvalue.l2convergence"
          :step="100"
          :min="0"
          :max="1000"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item label="粗网格收敛残差：" :label-width="formLabelWidth1">
        <el-input
          v-model="adflowvalue.l2convergencecoarse"
          :step="100"
          :min="0"
          :max="1000"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item label="输出物面信息：" :label-width="formLabelWidth1">
          <el-radio-group v-model="writetecplotsurfacesolution">
            <el-radio :value="1" :label="1">是</el-radio>
            <el-radio :value="0" :label="0">否</el-radio>
          </el-radio-group>  
      </el-form-item>
      <el-form-item label="输出切面信息：" :label-width="formLabelWidth1">
          <el-radio-group v-model="radioq">
            <el-radio :value="1" :label="1">是</el-radio>
            <el-radio :value="0" :label="0">否</el-radio>
          </el-radio-group>  
      </el-form-item>
    </div>
    <!-- 分析参数 v-show="currentTab=='0'"-->
    <div v-show="currentTab1 == '3'">
      <div class="eldesign classtable" style="margin-top: 10px">
        <el-table :data="outParams" border style="width: 100%; height: 400px">
          <el-table-column type="index" width="70" label="编号" />
          <el-table-column prop="name" label="参数名称">
            <!-- <template #default="{ row }">
              <el-input v-model="row.name" @change="handleEdit(row)" />
            </template> -->
          </el-table-column>
          <el-table-column prop="value" label="参数值">
            <template #default="{ row }">
              <el-input v-model="row.value" type="number" />
            </template>
          </el-table-column>
          <el-table-column prop="flag" label="启用" width="100">
            <template v-slot="scope">
              <el-checkbox
                :false-label="0"
                :true-label="1"
                :label="false"
                v-model="scope.row.flag"
              />
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>
<script  setup>
import { ref, onMounted, reactive, provide, nextTick } from "vue"
import { ElMessage, ElButton, ElDialog, ElSelect } from "element-plus"
import { Edit, CaretBottom, Delete } from "@element-plus/icons-vue"
import { request, uploadFile } from "@/utils/request"
import fileUploads from "../components/fileuploads.vue";
import meshFile from "@/assets/img/meshFile.png";
import inputParams from "@/assets/img/inputParams.png";
import configParams from "@/assets/img/configParams.png";
import analysisParams from "@/assets/img/analysisParams.png";
import { da } from "element-plus/es/locale/index.mjs";
const meshFileImgSrc = new URL("@/assets/img/open.png", import.meta.url).href;
let meshFileName = ref('');
let percentage = ref(0);
let uploadStatus = ref('');
let isUploadSuccess = ref(true);
let formLabelWidth3 = ref(120)
let formLabelWidth1 = ref(170)
let formLabelWidth200 = ref(200)
let currentTab1 = ref(0)
let pid = ref()
let adid = ref()
let resrho = ref(1)
let cl = ref(1)
let cd = ref(1)
let cmz = ref(1)
let writetecplotsurfacesolution = ref(1)
let radioq = ref(1)
let equationtype = ref("RANS")
let equationtypelist = ref([
  { label: "RANS", value: "RANS" },
  { label: "Euler", value: "Euler" }
])
let isairfoil = ref(1)
let isairfoilList = ref([
  { label: "二维", value: 1 },
  { label: "三维", value: 0 }
])
let liftindex = ref("x")
let liftindexlist = ref([
  { label: "x", value: 1 },
  { label: "y", value: 2 },
  { label: "z", value: 3 }
])
let inParams = ref([
  {
    code: "alpha",
    value: 2.66482,
    name: "攻角",
    type: 1,
    flag: 1
  },
  {
    code: "mach",
    value: 0.734,
    name: "马赫数",
    type: 1,
    flag: 1
  },
  {
    code: "reynolds",
    value: 6.5e6,
    name: "雷诺数",
    type: 1,
    flag: 1
  }
])

let outParams = ref([
  {
    code: "cl",
    value: "0.0",
    name: "升力系数Cl",
    type: 2,
    flag: 1
  },
  {
    code: "cd",
    value: "0.0",
    name: "阻力系数Cd",
    type: 2,
    flag: 1
  },
  {
    code: "cm",
    value: "0.0",
    name: "力矩系数Cm",
    type: 2,
    flag: 1
  },
  {
    code: " ",
    value: "0.0",
    name: "体积",
    type: 2,
    flag: 1
  }
])

let adflowvalue = ref({
  gridfile: "",
  fid:'',
  proname: "巡航工况",
  temperature: 288.15,
  length: 1,
  area: 1,
  momx: 0.25,
  momy: 0,
  momz: 0,
  mgcycle: "sg",
  ncycles: 500,
  l2convergence: 1.0e-5,
  l2convergencecoarse: 1.0e-4
})

let wclist = ref([{ label: "巡航工况", value: "cruise" }])
let tabslist1 = ref([
  { id: "0", name: "网格文件", imgSrc: meshFile },
  { id: "1", name: "来流参数", imgSrc: inputParams },
  { id: "2", name: "设置参数", imgSrc: configParams },
  { id: "3", name: "分析参数", imgSrc: analysisParams }
])
const selectTab1 = (index) => {
  currentTab1.value = index
}

const convertToStringArray = (result, Data) => {
  console.log("Data:", Data)

  // 安全检查 Data，确保它是一个数组
  if (!Array.isArray(Data)) {
    console.error("Data should be an array")
    return result // 返回原 result 或者根据需要返回其他默认值
  }

  result = Data.map((row) => {
    // 获取每一行的 `code`, `name`, `value` 和 `flag`
    const code = row.code || " "
    const name = row.name || " "
    const value =
      row.value === null || row.value === undefined || row.value === ""
        ? " "
        : row.value
    const flag = row.flag || " "

    // 将字段连接为一个以逗号分隔的字符串
    return `${code},${name},${value},${flag}`
  }).join(";") // 每行之间用分号分隔

  return result
}

const getadflow = (id) => {
  pid.value = id
  const params = {
    transCode: "MDO0049",
    pid: pid.value
  }
  request(params)
    .then((res) => {
      if (res.hasOwnProperty("adid")) {
        getadflowAssign(res)
      }
    })
    .catch((err) => {
      ElMessage.error(err.returnMsg)
    })
}

const getadflowAssign = (data) => {
  pid.value = data.pid
  adflowvalue.value.gridfile = data.gridfile
  adflowvalue.value.fid = data.fid
  adflowvalue.value.proname = data.proname
  isairfoil.value = data.isairfoil
  writetecplotsurfacesolution.value = data.writetecplotsurfacesolution
  equationtype.value = data.equationtype
  liftindex.value = data.liftindex
  adflowvalue.value.temperature = data.temperature
  adflowvalue.value.cl = data.cl
  adflowvalue.value.cd = data.cd
  adflowvalue.value.cmz = data.cmz
  adflowvalue.value.resrho = data.resrho
  adflowvalue.value.length = data.length
  adflowvalue.value.area = data.area
  adflowvalue.value.momx = data.momx
  adflowvalue.value.momy = data.momy
  adflowvalue.value.momz = data.momz
  adflowvalue.value.mgcycle = data.mgcycle
  adflowvalue.value.ncycles = data.ncycles
  adflowvalue.value.l2convergence = data.l2convergence
  adflowvalue.value.l2convergencecoarse = data.l2convergencecoarse
  inParams.value = data.inParams
  outParams.value = data.outParams
}

const getadflowsave = () => {
  const params = {
    transCode: "MDO0050",
    pid: pid.value,
    gridfile: adflowvalue.value.gridfile,
    fid: adflowvalue.value.fid,
    proname: adflowvalue.value.proname,
    isairfoil: isairfoil.value,
    writetecplotsurfacesolution: writetecplotsurfacesolution.value,
    equationtype: equationtype.value,
    liftindex: liftindex.value,
    temperature: adflowvalue.value.temperature,
    cl:cl.value,
    resrho:resrho.value,
    cd:cd.value,
    cmz:cmz.value,
    length: adflowvalue.value.length,
    area: adflowvalue.value.area,
    momx: adflowvalue.value.momx,
    momy: adflowvalue.value.momy,
    momz: adflowvalue.value.momz,
    mgcycle: adflowvalue.value.mgcycle,
    ncycles: adflowvalue.value.ncycles,
    l2convergence: adflowvalue.value.l2convergence,
    l2convergencecoarse: adflowvalue.value.l2convergencecoarse,
    checked: 1,
    inParams: convertToStringArray([], inParams.value),
    outParams: convertToStringArray([], outParams.value)
  }
  request(params)
    .then((res) => {
      ElMessage({
        message: res.returnMsg,
        type: "success"
      })
    })
    .catch((err) => {
      ElMessage.error(err.returnMsg)
    })
}
// 更新文件名
const updateFileName = (newValue) => {
  meshFileName.value = newValue
}

// 更新进度条
const updatePercentage = (newValue) => {
  percentage.value = newValue
}

// 控制进度条显隐
const showProgress = computed(() => percentage.value > 0 && percentage.value <= 100);

// 处理上传成功后的逻辑
const handleFileUploadSuccess = (newValue) => {
  //隐藏进度条
  setTimeout(() => {
    percentage.value = 0;
  }, 1000);

  adflowvalue.value.gridfile = newValue.fname;
  adflowvalue.value.fid = newValue.bfid;
}

// 更新上传状态
const getUploadStatus = (newValue) => {
  uploadStatus.value = newValue
}

const fileDel = () => {
}

defineExpose({
  getadflow,
  getadflowAssign,
  getadflowsave
})
</script>


