<template>
  <div class="XFpdding">
    <ul>
      <li
        class="item"
        v-for="(tab, index) in tabslist"
        :key="index"
        :class="{ active: currentTab === index }"
        @click="selectTab(index)"
      >
        {{ tab.name }}
      </li>
    </ul>
    <!-- 来流参数 -->
    <div v-show="currentTab == '0'">
      <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="code" width="70" label="编码" /> -->
          <el-table-column prop="name" label="参数名称">
            <!-- <template #default="{ row }">
              <el-input v-model="row.name" />
            </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="-1"
                :true-label="1"
                :label="false"
                v-model="scope.row.flag"
              />
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 分析参数 -->
    <div v-show="currentTab == '2'">
      <div class="eldesign classtable" style="margin-top: 10px">
        <el-table :data="outParams" border style="width: 100%">
          <el-table-column type="index" width="70" label="编号" />
          <!-- <el-table-column prop="code" 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="-1"
                :true-label="1"
                :label="false"
                v-model="scope.row.flag"
              />
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 设置参数 -->
    <div v-show="currentTab == '1'">
      <div class="eldesign classtable" style="margin-top: 10px">
        <el-form-item label="计算完成：" :label-width="formLabelWidth1">
          <el-radio-group v-model="analyzed">
            <el-radio label="1" value=1>是</el-radio>
            <el-radio label="0" value=0>否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="迭代次数：" :label-width="formLabelWidth1">
          <el-input
            v-model="xfoilvalue.iter"
            :step="100"
            :min="0"
            :max="1000"
            controls-position="right"
          />
        </el-form-item>
        <el-form-item label="表面求解点个数：" :label-width="formLabelWidth1">
          <el-input
            v-model="xfoilvalue.ppar"
            :step="100"
            :min="0"
            :max="1000"
            controls-position="right"
          />
        </el-form-item>
        <!-- <el-form-item label="结果文件：" :label-width="formLabelWidth1"></el-form-item> -->
        <div class="cheinput">
          <el-form-item label="压力分布：" :label-width="formLabelWidth1">
            <el-input
              v-model="xfoilvalue.cpwr"
              :step="100"
              :min="0"
              :max="1000"
              controls-position="right"
            />
          </el-form-item>
          <el-checkbox v-model="checked1"  style="width:40px;height:40px" />
        </div>
        <div class="cheinput">
          <el-form-item label="翼型坐标：" :label-width="formLabelWidth1">
            <el-input
              v-model="xfoilvalue.yxzb"
              :step="100"
              :min="0"
              :max="1000"
              controls-position="right"
            />
          </el-form-item>
          <el-checkbox v-model="checked2" style="width:40px;height:40px" />
        </div>
        <div class="cheinput">
          <el-form-item label="力和力矩：" :label-width="formLabelWidth1">
            <el-input
              v-model="xfoilvalue.pacc"
              :step="100"
              :min="0"
              :max="1000"
              controls-position="right"
            />
          </el-form-item>
          <el-checkbox v-model="checked3"  style="width:40px;height:40px" />
        </div>
      </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 } from "@element-plus/icons-vue"
import { tr } from "element-plus/es/locale/index.mjs"
import { request, uploadFile } from "@/utils/request"
let formLabelWidth3 = ref(100)
let formLabelWidth1 = ref(170)
let formLabelWidth2 = ref(250)
let currentTab = ref(0)
let checked1 = ref(true)
let checked2 = ref(true)
let checked3 = ref(true)
let analyzed = ref('1')

let pid = ref()
let xfid = ref()

let xfoilvalue = ref({
  iter: 100,
  ppar: 200,
  cpwr:"op.dat",
  yxzb:"airfoil.dat",
  pacc:"output.dat"
})

let tabslist = ref([
  { id: "0", name: "来流参数" },
  { id: "1", name: "设置参数" },
  { id: "2", name: "分析参数" }
])


let inParams = ref([
  {
    code: "alpha",
    value: 0.1,
    name: "AoA",
    flag: 1,
    
  },
  {
    code: "mach",
    value: 0.1,
    name: "MA",
    flag: 1,
    
  },
  {
    code: "reynolds",
    value: 6.5e6,
    name: "Re",
    flag: 1,
    
  }
])
let outParams = ref([
{
    code: "cl",
    value: "0.0",
    name: "升力系数Cl",
    flag: 1,
    
  },
  {
    code: "cd",
    value: "0.00436",
    name: "阻力系数Cd",
    flag: 1,
    
  },
  {
    code: "cdp",
    value: "0.1",
    name: "压阻力系数Cdp",
    flag: 1,
    
  },
  {
    code: "cm",
    value: "0.1",
    name: "力矩系数Cm",
    flag: 1,
    
  },
  {
    code: "xtr_upper",
    value: "0.1",
    name: "上表面转换点位置xtr-upper",
    flag: 1,
    
  },
  {
    code: "xtr_lower",
    value: "0.1",
    name: "下表面转换点位置xtr-lower",
    flag: 1,
    
  }
])
// 选项卡
const selectTab = (index) => {
  currentTab.value = index
}

// 新建表格行
const addRow = () => {
  lailiutable.value.push({
    data: 0,
    name: "",
    enable: -1
  })
}

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 getxfoil = (id) => {
  pid.value = id;
  console.log('pid-1:',pid.value)
  const params = {
    transCode: "MDO0046",
    pid: pid.value,
  };
  request(params).then((res) => {
    if (res.hasOwnProperty("xfid")) {
      getxfoilAssign(res);
    }
  })
  .catch((err) => {
      ElMessage.error(err.returnMsg)
    })
}

const getxfoilAssign = (data) => {
  // console.log('getxfoilAssign-data:', data);
  pid.value = data.pid;
  xfid.value = data.xfid;
  xfoilvalue.value.iter = data.iter;
  xfoilvalue.value.ppar = data.ppar;
  xfoilvalue.value.cpwr = data.cpwr;
  xfoilvalue.value.yxzb = data.yxzb;
  xfoilvalue.value.pacc = data.pacc;
  analyzed.value = data.analyzed;
  inParams.value = data.inParams;
  outParams.value = data.outParams;
}

const getxfoilsave = () => {
  console.log("pid:",pid.value);
  const params = {
    transCode: "MDO0047",
    pid: pid.value,
    iter: xfoilvalue.value.iter,
    ppar: xfoilvalue.value.ppar,
    cpwr: xfoilvalue.value.cpwr,
    yxzb: xfoilvalue.value.yxzb,
    pacc: xfoilvalue.value.pacc,
    analyzed: analyzed.value,
    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)
    })
}

defineExpose({
  getxfoil,
  getxfoilAssign,
  getxfoilsave
})

</script>



