<template>
  <div class="XFpdding" style="height: 400px;">
    <div class="numberinput lefttext">
      <el-form>
        <div>
          <el-form-item label="CST阶数:" :label-width="formLabelWidth200">
            <el-input-number
              v-model="cstn"
              :step="1"
              :min="0"
              :max="20"
              controls-position="right"
              @change="clickblur"
            />
          </el-form-item>
        </div>
        <div style="display: flex">
          <div class="cstleft">
            <el-form-item
              label="上表面类函数参数:"
              :label-width="formLabelWidth200"
            >
              <el-input
                v-model="cstvalue.uppern1"
                :step="100"
                :min="0"
                :max="1000"
                controls-position="right"
              >
                <template #prefix>N1：</template>
              </el-input>
            </el-form-item>
            <el-form-item
              label="下表面类函数参数:"
              :label-width="formLabelWidth200"
            >
              <el-input
                v-model="cstvalue.lowern1"
                :step="100"
                :min="0"
                :max="1000"
                controls-position="right"
              >
                <template #prefix>N1：</template>
              </el-input>
            </el-form-item>
          </div>
          <div class="cstleft" style="margin-left: 20px">
            <el-form-item
              label="上表面类函数参数:"
              :label-width="formLabelWidth200"
            >
              <el-input
                v-model="cstvalue.uppern2"
                :step="100"
                :min="0"
                :max="1000"
                controls-position="right"
              >
                <template #prefix>N2：</template>
              </el-input>
            </el-form-item>
            <el-form-item
              label="下表面类函数参数:"
              :label-width="formLabelWidth200"
            >
              <el-input
                v-model="cstvalue.lowern2"
                :step="100"
                :min="0"
                :max="1000"
                controls-position="right"
              >
                <template #prefix>N2：</template>
              </el-input>
            </el-form-item>
          </div>
        </div>
        <div class="classtable">
          <el-table :data="tableDatacst" style="width: 100%" border="true" :header-cell-class-name="headerCellClassName">
            <el-table-column prop="date" label="" width="150" />
            <el-table-column
              v-for="(item, index) in tablecstHeaders"
              :key="index"
              :prop="item.prop"
              :label="item.label"
            >
              <template #default="{ row }">
                <el-input v-model="row[item.prop]" />
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script  setup>
import { ref, onMounted, reactive, provide, nextTick } from "vue"
import { RouterView, RouterLink, useRouter, useRoute } from "vue-router"
import myheader from "@/components/header.vue"
import { request, uploadFile } from "@/utils/request"
import { ElMessage, ElButton, ElDialog, ElSelect } from "element-plus"
import { Edit, CaretBottom } from "@element-plus/icons-vue"
import { cs, ta } from "element-plus/es/locale/index.mjs"
let formLabelWidth3 = ref(100)
let formLabelWidth200 = ref(180)
let cstn = ref("8")
let pid = ref("")
let wid = ref()
let cstid = ref("")
let cstvalue = ref({
  uppern1: "0.5",
  lowern1: "1",
  uppern2: "0.5",
  lowern2: "1",
})
let upperb = ref("")
let lowerb = ref("")
let tableDatacst = ref([
  { date: "上表面CST参数", X1: "0.173482", X2: "0.151492", X3: "0.180515", X4: "0.121639", X5: "0.182491", X6: "0.118110", X7: "0.157365", X8: "0.136958", X9: "0.146738" },
  { date: "下表面CST参数", X1: "0.173482", X2: "0.151492", X3: "0.180515", X4: "0.121639", X5: "0.182491", X6: "0.118110", X7: "0.157365", X8: "0.136958", X9: "0.146738" }
])
let tablecstHeaders = ref([
  {prop:"X1",label:"X1"},
  {prop:"X2",label:"X2"},
  {prop:"X3",label:"X3"},
  {prop:"X4",label:"X4"},
  {prop:"X5",label:"X5"},
  {prop:"X6",label:"X6"},
  {prop:"X7",label:"X7"},
  {prop:"X8",label:"X8"},
  {prop:"X9",label:"X9"},
])

const headerCellClassName = ({columnIndex}) => {
  if( columnIndex !== 0 ) {
    return 'header-blue';
  }

  return '';
}




// cst阶数改变
const clickblur = () => {
  tablecstHeaders.value = []
  let num = Number(cstn.value)
  if (!num) return; // 直接返回，避免冗余的 `if`

  // 生成表头
  tablecstHeaders.value = Array.from({ length: num + 1 }, (_, i) => ({
    label: `X${i + 1}`,
    prop: `X${i + 1}`
  }))

  // 更新表格数据
  tableDatacst.value = tableDatacst.value.map(item => {
    let newItem = { ...item }
    tablecstHeaders.value.forEach(col => {
      newItem[col.prop] = null
    })
    return newItem
  })
}

// 获取upperb和lowerb
const getupperlower = () => {
  const upperRow = tableDatacst.value[0];
  const lowerRow = tableDatacst.value[1];
  let upperbValues = [];
  let lowerbValues = [];

  // 处理第一行数据
  for (let key in upperRow) {
      if (key !== "date") {
        upperbValues.push(upperRow[key]);
      }
  }
  // 将拼接后的值存储在 upperb 中
  upperb.value = upperbValues.join(",");

  // 处理第二行数据
  for (let key in lowerRow) {
      if (key !== "date") {
        lowerbValues.push(lowerRow[key]);
      }
    }
  // 将拼接后的值存储在 lowerb 中
  lowerb.value = lowerbValues.join(",");
}



//查询cst 
const getcst = (id,nowid) => {
  pid.value=id;
  wid.value = nowid;
  console.log('getcst:',pid.value)
  const params = {
    transCode: 'MDO0040',
    pid: pid.value,
    wid: wid.value
  }
  request(params)
    .then((res) => {

      if (res.hasOwnProperty('cstid')) {

        getcstAssign(res);
        

      } else {
        
      }

    })
    .catch((err) => {
      ElMessage.error("CST初始化失败")
    })
}

const getcstAssign = (data) => {
  pid.value = data.pid
  cstid.value = data.cstid;
  cstn.value = data.cstn;
  cstvalue.value.uppern1 = data.uppern1;
  cstvalue.value.lowern1 = data.lowern1;
  cstvalue.value.uppern2 = data.uppern2;
  cstvalue.value.lowern2 = data.lowern2;
  // upperb.value = data.upperb;
  // lowerb.value = data.lowerb;

  clickblur(); 

  // 解析 upperb 和 lowerb
  let upperbValues = data.upperb ? data.upperb.split(",") : [];
  let lowerbValues = data.lowerb ? data.lowerb.split(",") : [];
  
  tableDatacst.value.forEach((row, rowIndex) => {
    if (rowIndex === 0) {
      // 第一行赋值 upperb
      tablecstHeaders.value.forEach((header, index) => {
        if (index < upperbValues.length) {
          row[header.prop] = upperbValues[index]; // 赋值对应列
        }
      });
    } else if (rowIndex === 1) {
      // 第二行赋值 lowerb
      tablecstHeaders.value.forEach((header, index) => {
        if (index < lowerbValues.length) {
          row[header.prop] = lowerbValues[index]; // 赋值对应列
        }
      });
    }
  });
}

//cst 保存
const getcstsave = (nowid) => {
  if(nowid){
    wid.value = nowid
  }
  getupperlower();
  const params = {
    transCode: 'MDO0041',
    pid: pid.value,
    wid: wid.value,
    cstn: cstn.value,
    uppern1: cstvalue.value.uppern1,
    lowern1: cstvalue.value.lowern1,
    uppern2: cstvalue.value.uppern2,
    lowern2: cstvalue.value.lowern2,
    upperb: upperb.value,
    lowerb: lowerb.value,
    checked:1
  }
  request(params)
    .then((res) => {
      ElMessage({
        message: '保存成功',
        type: 'success',
      })
    })
    .catch((err) => {
      ElMessage.error('保存失败')
    })
}

defineExpose({getcst,getcstAssign,getcstsave});

</script>


