<template>
  <a-layout style="height: 100%; padding: 5px; padding-left: 0px; min-width: 300px">
    <a-layout-content style="padding: 5px; height: 100%; border: 1px solid #d1dbe5">
      <a-card class="box-card" style="padding: 0px; height: 99.8%" :body-style='{ padding: "5px" }'>
        <lt-a-form contendPadding="5px" :fill="true" :fit="false" style="border-width: 0px"
          @updateData="handleUpdateForm" :initial-values="form">
          <a-form ref="refFormComFunctQC" id="refFormComFunctQC" :inline="false" :model="form" :inline-message="true"
            abel-width="100px" style="width: 100%;" :labelCol="{
              span: 6
            }" :rules="rules">
            <a-row>
              <a-col :span="24">
                <a-form-item label="生产工艺" name="FunctionCode" title="生产工艺">
                  <a-select v-model:value="FunctionCode" allowClear>
                    <a-select-option v-for="item in QCfunOption" :key="item.FunctionCode" :value="item.FunctionCode">{{
                      item.FunctionName }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
            <a-row>
              <a-col :span="24">
                <a-form-item label="投料位置" name="RecipeStyleName" title="投料位置">
                  <a-input v-model:value="RecipeStyleName" :readonly="true"></a-input>
                </a-form-item>
              </a-col>
            </a-row>
            <a-row v-for="item in editFunParam" :key="item.OrderId"
              v-show="item.FieldName != 'EditFunName' && item.FieldName != 'ParantID'">
              <a-col :title="item.ParamName" :span="24">
                <a-form-item :label="item.ParamName" :name="item.FieldName" :title="item.ParamName"
                  style="white-space: nowrap; overflow: hidden" :rules="DnyRules(item.FieldName)">
                   <div v-if="item.FieldName === 'QcBusinessCode'&&CHQCUseLims&&!ifFromTemplate"
                    style="display: flex;align-items: center;width: 100%;height: 25px;position:relative"
                    @click=handleShowSelectBoxDlg(item,$event)>
                    <span
                      style="display:inline-block;text-align:left;width: calc(100% - 14px);border-radius: 5px 0 0 5px;border:0px solid #d1dbe5;overflow:hidden">{{form[item.FieldName] }}</span>
                    <em type="" style="margin:0;padding:0;position:absolute;right:0px;color:#4687ef"
                      className="icon icon-search">
                    </em>
                  </div>
                  <a-checkbox v-model:checked="form[item.FieldName]" v-else-if="item.ParamStyle == 1" />
                  <a-input v-model:value="form[item.FieldName]" v-else-if="item.ParamStyle == 0"
                    :readonly="item.FieldName == 'ParantID'" />
                  <div v-else-if="showSelect(item)">
                    <a-select v-model:value="form[item.FieldName]" @change="handleInputSelect(item)"
                      v-if="showCommonSelect(item)" style="width: 100%;" clearable>
                      <a-select-option v-for="list in item.SelectItems" :key="list.ItemId" :label="list.ItemName"
                        :value="list.ItemId.toString()">{{ list.ItemName  }}
                      </a-select-option>
                    </a-select>
                    <a-select v-model:value="form[item.FieldName]" v-else style="width: 100%;" clearable>
                      <a-select-option v-for="list in getSelectItems(item)" :key="list.ItemId" :label="list.ItemName"
                        :value="list.ItemId.toString()">{{ list.ItemName }}
                      </a-select-option>
                    </a-select>
                  </div>
                </a-form-item>
              </a-col>
            </a-row>
            <a-row v-for="list in Script" :key="list.label">
              <a-col :span="24">
                <a-form-item :label="list.label" style="white-space: nowrap; overflow: hidden">
                  <a-select v-model:value="list.ExecId" :rules="DnyRules(list.ExecId)">
                    <a-select-option v-for="item in selectOption" :key="item.id" :value="item.id">{{ item.label
                    }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
            <a-row v-if="ifHaveQCDetails&&!CHQCUseLims" style="width: 100%;">
              <a-row style="height: 312px;width: 100%;">
                <a-row style="width: 100%; height: 100%; margin: 0; padding: 2px;">
                  <qcList :data="QCItems" :QCItemCols="QCItemCols" :QCItemOptions="QCItemOptions"
                    :QCVariable="QCVariable" :showOperate="ifCanBeEdite" @editQCItem="handleRowEdit"
                    @delQCItem="handleRowDel" @addQCItem="handleNewQCItem"></qcList>
                </a-row>
              </a-row>
            </a-row>
            <a-row v-if="CHQCUseLims&&form.QcBusinessCode" style="width: 100%;">
              <a-row style="height: 312px;width: 100%;">
                <a-row style="width: 100%; height: 100%; margin: 0; padding: 2px;">
                  <head-title title="QC检测项"></head-title>
                  <lims-scheme-detail-list
                    :table-params="props.limsSchemeDetailTableParams"
                    :table-data="limsSchemeDetailData"
                  >
                  </lims-scheme-detail-list>
                </a-row>
              </a-row>
            </a-row>
            <a-row style="text-align: center;margin: auto;display: flex;">
              <div style="padding: 5px;margin: auto;">
                <a-button v-on:click="handleConfirm" type="primary" :disabled="editFunParam.length == 0">
                  <template #icon>
                    <check-outlined />
                  </template>
                  确认
                </a-button>
                <a-button :disabled="editFunParam.length == 0" v-on:click="handleReset" style="margin-left: 10px;">
                  <template #icon>
                    <close-outlined />
                  </template>
                  重置</a-button>
              </div>
            </a-row>
          </a-form>
        </lt-a-form>
      </a-card>
    </a-layout-content>
    <lt-a-model v-model:open="visible" :width="750" title="QC细项编辑" v-model:fullscreen="dlgfullscreen"
      :destroyOnClose="true" @ok="handleOnOK">
      <qcList :data="QCItems" :QCItemCols="QCItemCols" :QCItemOptions="QCItemOptions" :showEdit="false"
        :fullscreen="dlgfullscreen" :QCVariable="QCVariable" :showOperate="ifCanBeEdite" @editQCItem="handleRowEdit"
        @delQCItem="handleRowDel" @addQCItem="handleNewQCItem" />
    </lt-a-model>
     <component
        :is="state.dynComponent"
        :targetMatCode="state.targetMatCode"
        :tableParams="props.limsSchemeMainTableParams"
        :detailTableParams="props.limsSchemeDetailTableParams"
        @dlgConfirm="handleDlgConfirm"
        @dlgCancel="handleDlgCancel"
    ></component>
  </a-layout>
</template>

<script setup lang="ts">
import Api from "../../api/api";
import qcList from "./qc-list.vue"; //QC细项
import {ref, reactive, watch, getCurrentInstance, onBeforeMount, PropType, computed, nextTick} from "vue"; //vue引用
import { Modal } from "ant-design-vue";
import { events } from "../../../bus";
import { GuidID } from "../commonMethods/commonMethods"
import { cloneDeep } from "lodash-es";
import { ColStruct } from "../../../hooks/type";
import headTitle from "../common-component/head-title.vue";
import LIMSSchemeDataDlg from "../common-data-dlg/lims-scheme-data-dlg.vue";
import {useI18n} from "vue-i18n";
import {message} from "ant-design-vue";
import LIMSBaseInformationService from "../../api/LIMSBaseInformationService.ts";
import LIMSServices from "../../api/LIMSServices";
import LimsSchemeDetailList from "../../component/common-component/lt-common-table/lt-table-local.vue";

const {t, locale} = useI18n();
const CurrentInstance = getCurrentInstance()
const props = defineProps({
  limsSchemeDetailTableParams:{
    type:Object,
    default(){
      return null
    }
  },
  limsSchemeMainTableParams:{
    type:Object,
    default(){
      return null
    }
  },
  CHQCUseLims: {
    type: Boolean,
    default: false,
  },
  ifFromTemplate: {
    type: Boolean,
    default: false,
  },
  mainData: {
    type: Object,
    default() {
      return null
    }
  },
  ParamNameReplaceRelation: {
    type: Object,
    default() {
      return null
    }
  },
  ifCanBeEdite: {
    type: Boolean,
    default: true,
  },
  formData: {
    type: Object,
    default() {
      return null;
    },
  },
  porperEvent: Object,
  moduleType: {
    type: String,
    default: "Formular",
  },
  QCItemCols: {
    type: Array as PropType<ColStruct[]>,
    default() {
      return [];
    },
  },
  QCfunOption: {
    type: Array as PropType<QCfunOptionItemStruct[]>,
    default() {
      return [];
    },
  },
  QCItemOptions: {
    //QC检验细项
    type: Array,
    default() {
      return [];
    },
  },
  QCVariable: {
    //QC检验变量
    type: Array,
    default() {
      return [];
    },
  },
  ifHaveQCDetails: {
    type: Boolean,
    default: true,
  },
})

interface editFunParamStruct {
  [key:string]:any
  FieldName: string
  OrderId: string|number
  ParamName: string
  ParamStyle: number
  SelectItems: any[]
}

interface selectOptionStruct {
  label: string
  id: string
}

interface QCItemStruct {
  ItemId: string | number
  QCName: string
  QCStyle: any
  Standard: string | number
  UpperLimit: string | number
  QCSelected: string
  LowerLimit: string | number
  Unit: string
  Remark: string
  Field01: string
  Field02: string
  Field03: string
  Field04: string
  Field05: string
}

interface QCfunOptionItemStruct {
  FunctionCode: string
  FunctionName: string
}

interface formStruct {
  QCItems: QCItemStruct[]
  [key: string]: string | number | boolean | any;
}

interface DynamicDtruct {
  [key: string]: any;
}

const emits = defineEmits(['closeParamForm'])

const refFormComFunctQC = ref();

let form = ref({
  QCItems: [],
} as formStruct)
// let scriptStruct = reactive({})
let Script = ref({} as any)
let RecipeStyleName = ref("") //投料位置名称 
let FunctionCode = ref(null) //工艺CODE
let functID = ref(-1) //工艺ID
let funParam = ref([]) //工艺参数
let editFunParam = ref([] as editFunParamStruct[]) //编辑参数
let showFunParam = ref([] as editFunParamStruct[]) //图形中显示的参数
let dataStruct = reactive({} as formStruct) //原始数据结构
let selectOption = ref([] as selectOptionStruct[])  //执行分支选择
let cellsArray = ref([])  //执行分支目标对象
let QCItems = ref([] as QCItemStruct[])  //QC细项
let QCdataStruct = reactive({
  ItemId: "",
  QCName: "",
  QCStyle: null,
  Standard: "",
  UpperLimit: "",
  QCSelected: "",
  LowerLimit: "",
  Unit: "",
  Remark: "",
  Field01: "",
  Field02: "",
  Field03: "",
  Field04: "",
  Field05: "",
} as QCItemStruct)
let QCItemsForm = reactive({} as QCItemStruct)
let QCItemsIndex = ref(0)
let ParantID = ref("")
let DetailOrderKey = ref("")
const DisplayMaxRow = ref(6)
const visible = ref(false)
const dlgfullscreen = ref(false)
const state=reactive({
  dynComponent:null as any,
  targetMatCode:null,
  selectBoxDlgCol:null as any
})
const limsSchemeDetailData=ref([])

const rules = computed(() => {
  let rulesTemp = {}
  for (let i = 0; i < funParam.value.length; i++) {
    //@ts-ignore
    if (funParam.value[i].MustHaveValue == true) {
      //@ts-ignore
      rulesTemp[funParam.value[i].FieldName] = [
        { required: true, message: "非空值", trigger: 'change', type: 'string' },
      ]
    }

  }
  return rulesTemp
})


const initFormVal = function () {
  const newValue = props.formData
  debugger
  if (newValue && typeof newValue != 'undefined') {
    const cell = newValue.cell
    if (cell.cellType == "Qc") {
      RecipeStyleName.value = cell.RecipeStyleName;
      ParantID.value = cell.RecipeStyle;
      DetailOrderKey.value = cell.DetailOrderKey;
      getSelectOption(cell);
      if (cell.value == null || cell.value == "") {
        handleReset(); //如果面板值的空，则表示是新增
        // getSelectOption(newValue);
      } else {
        FunctionCode.value = cell.EditFunName;
        for (const i in props.QCfunOption) {
          //@ts-ignore
          if (props.QCfunOption[i].FunctionCode == FunctionCode.value) {
            //@ts-ignore
            functID.value = props.QCfunOption[i].FuncId;
            break;
          }
        }
        getFunParam(functID.value, cell);
        QCItems.value = []
        if (cell.QCItems != undefined) {
          QCItems.value = cloneDeep(cell.QCItems)
        } else {
          QCItems.value = [];
        }
      }
    }
  }
}

const DnyRules = function (fieldName: string) {
  let temp = [] as any[]
  const target = funParam.value.filter((item) => {
    //@ts-ignore
    return item.FieldName == fieldName
  })
  if (target.length > 0) {
    //@ts-ignore
    if (target[0].MustHaveValue == true) {
      temp = [
        { required: true, message: "非空值", trigger: 'change', type: 'string' },
      ]
    }
  }
  return temp
}

const showSelect = function (value: any) {
  //20241106新增下拉选项类型
  if (value.ParamStyle == 2) {
    return true
  } else {
    return false
  }
}

const showCommonSelect = function (value: any) {
  return typeof value.ItemFilterField == "undefined" || value.ItemFilterField == "" || value.ItemFilterField == null || value.ItemFilterField.length == 0
}
const getSelectItems = function (item: any) {

  if (typeof form.value[item.ItemFilterField] != "undefined" && form.value[item.ItemFilterField] != "") {
    //form.value[item.FieldName] = "" //值改变的时候，清空当前选项
    return item.SelectItems.filter((list: any) => {
      return list.FilterValue.indexOf(parseInt(form.value[item.ItemFilterField])) != -1 || list.FilterValue == null
    })
  } else {
    return item.SelectItems
  }
}

const handleInputSelect = function (item: any) {
  //此处强制更新视图
  if (item.ChildItemFields) {
    for (let i = 0; i < item.ChildItemFields.length; i++) {
      form.value[item.ChildItemFields[i]] = ""
    }
  }

}

const handleUpdateForm = function (formData: DynamicDtruct) {
  //通过update来获取数据更新
  // for (let i in formData) {
  //   if (i != "QCItems") {
  //     form.value[i] = formData[i]
  //   }

  // }
}

const handleOnOK = function () {
  visible.value = false
}

const handleNewQCItem = function () {
  QCItemsIndex.value++;
  QCItemsForm = QCdataStruct;
  QCItemsForm.ItemId = GuidID()
  let copy = cloneDeep(QCItemsForm)
  QCItems.value.push(copy)
}

const handleRowDel = function (row: any) {
  for (let i = 0; i < QCItems.value.length; i++) {
    if (row.ItemId == QCItems.value[i].ItemId) {
      QCItems.value.splice(i, 1);
      break;
    }
  }
}

const handleRowEdit = function () {
  visible.value = true
}

const getSelectOption = function (newValue: any) {
  //获取执行分支
  const edges = newValue.edges;
  if (!edges) {
    // scriptStruct = {};
    return;
  }
  selectOption.value = [];
  cellsArray.value = [];
  for (let i = 0; i < edges.length; i++) {
    if (edges[i].source.id == newValue.id) {
      //@ts-ignore
      cellsArray.value.push(edges[i]);
      const item = {
        label: edges[i].value,
        id: edges[i].target.id,
      };
      let copy = cloneDeep(item)

      selectOption.value.push(copy);
    }
  }
  formateScriptStruct();
}
const formateScriptStruct = function () {
  Script.value = {};
  let temp = {
    case1: {
      index: 1,
      ExecId: "",
      label: "合格",
      isOK: true,
    },
    case2: {
      index: 2,
      ExecId: "",
      label: "不合格",
      isOK: false,
    },
  };

  Script.value = cloneDeep(temp)


}
const handleConfirm = function () {
  //确认修改数据
  if (
    !FunctionCode.value ||
    FunctionCode.value == "" ||
    typeof FunctionCode.value == "undefined"
  ) {
    Modal.warning({
      title: "警告",
      content: "FunctionCode不能为空"
    })
    return;
  }
  if (!chechItemsData()) {
    return
  }


  for (let j = 0; j < QCItems.value.length; j++) {
    //在提交数据之前，将数据改成整型值
    QCItems.value[j].ItemId = j + 1
  }
  //@ts-ignore
  form.value.QCItems = cloneDeep(QCItems.value)
  //$.extend(true, form.value.QCItems, QCItems.value)
  //@ts-ignore
  form.value.ParantID = ParantID.value; //提交数据的时候，赋值parentID
  //@ts-ignore
  form.value.EditFunName = FunctionCode.value;
  //@ts-ignore
  // form.value.DetailOrderKey = DetailOrderKey.value;
  form.value.Script = Script.value

  const sendData = {
    showData: showFunParam.value,
    validData: form.value,
    Descript: getFunNameByFunID(functID.value),
    DisplayMaxRow: DisplayMaxRow.value
  };

  sendData.validData.DisplayMaxRow = DisplayMaxRow.value

  const propertyData = {
    data: sendData,
    type: "Qc", //标准工艺
  };
  
  events.emit("porpertyChange", propertyData);
  handleReset(); //数据推送之后清空面板
  emits("closeParamForm", 1)
}
const handleReset = function () {
  //初始化的时候，将页面初始化成初始结构
  form.value = cloneDeep(dataStruct)
  if (typeof Script.value != 'undefined') {
    Script.value = {
      case1: {
        index: 1,
        ExecId: "",
        label: "合格",
        isOK: true,
      },
      case2: {
        index: 2,
        ExecId: "",
        label: "不合格",
        isOK: false,
      },
    };
  }
  FunctionCode.value = null;
  QCItems.value = [];

}
const chechItemsData = function () {
  let bRet = true
  for (let i = 0; i < QCItems.value.length; i++) {
    if (QCItems.value[i].QCStyle == 2 || QCItems.value[i].QCStyle == 6 || QCItems.value[i].QCStyle == 7 || QCItems.value[i].QCStyle == 8 || QCItems.value[i].QCStyle == 9) {
      if (QCItems.value[i].UpperLimit == "" || !QCItems.value[i].UpperLimit || typeof QCItems.value[i].UpperLimit == 'undefined') {
        Modal.warning({
          content: "行" + (i + 1) + "上限值不能为空，请检查",
          title: "警告"
        })
        bRet = false
        break
      }
    }
    if (QCItems.value[i].QCStyle == 1 || QCItems.value[i].QCStyle == 3 || QCItems.value[i].QCStyle == 5 || QCItems.value[i].QCStyle == 6 || QCItems.value[i].QCStyle == 7 || QCItems.value[i].QCStyle == 8 || QCItems.value[i].QCStyle == 9) {
      if (QCItems.value[i].LowerLimit == "" || !QCItems.value[i].LowerLimit || typeof QCItems.value[i].LowerLimit == 'undefined') {
        Modal.warning({
          content: "行" + (i + 1) + "下限值不能为空，请检查",
          title: "警告"
        })
        bRet = false
        break
      }
    }
    if (
      QCItems.value[i].QCStyle == 6 ||
      QCItems.value[i].QCStyle == 7 ||
      QCItems.value[i].QCStyle == 8 ||
      QCItems.value[i].QCStyle == 9
    ) {
      if (
        //@ts-ignore
        parseFloat(QCItems.value[i].UpperLimit) -
        //@ts-ignore
        parseFloat(QCItems.value[i].LowerLimit) <=
        0
      ) {
        Modal.warning({
          title: "警告", content: "行" + (i + 1) + "上限必须大于下限"
        })
        bRet = false
        break
      }
    }
  }
  return bRet
}


const getFunNameByFunID = function (value: number) {
  let name = "";
  if (value == -1) {
    return "物料计量";
  }
  for (let i = 0; i < props.QCfunOption.length; i++) {
    //@ts-ignore
    if (props.QCfunOption[i].FuncId == value) {
      //@ts-ignore
      name = props.QCfunOption[i].FunctionName2;
      break;
    }
  }
  return name;
}


const getFunParam = function (functID: number | string, parentData?: any) {

  if (functID == null) {
    return;
  }
  funParam.value = [];
  Api.LoadFunctionParam(functID)
    .then((data: any) => {
      if (data.Params.length < 1) return;
      if (props.ParamNameReplaceRelation) {
        for (let key in props.ParamNameReplaceRelation) {
          let target = data.Params.find((item: any) => item.FieldName === key)
          if (target) {
            target.ParamName = props.ParamNameReplaceRelation[key]
          }
        }
      }
      funParam.value = data.Params;
      DisplayMaxRow.value = data.DisplayMaxRow ? data.DisplayMaxRow : 10

      let isInit = typeof parentData == 'undefined' ? false : true
      getEditAndShowData(isInit)

      formatDataStruct(funParam.value, parentData);
    })
    .catch((error: any) => {
      //提交失败的时候，直接抛出后台报错
    });
}

const getEditAndShowData = function (isInit: boolean) {
  const newValue = funParam.value
  if (newValue.length < 1) {
    return;
  }

  editFunParam.value = []; //编辑参数
  showFunParam.value = []; //页面显示参数
  for (let i = 0; i < newValue.length; i++) {
    if (props.moduleType == "Formular") {
      //配方的情况下
      //@ts-ignore
      if (newValue[i].EditProperty == 0 || newValue[i].EditProperty == 2) {
        editFunParam.value.push(newValue[i]); //面板属性
        if (isInit) {
          //@ts-ignore
          form.value[newValue[i].FieldName] = "" //初始化需要界面显示的字段
        }
      }
      if (
        //@ts-ignore
        newValue[i].DisplayProperty == 0 ||
        //@ts-ignore
        newValue[i].DisplayProperty == 2
      ) {
        showFunParam.value.push(newValue[i]); //图元显示属性
      }
    } else {
      //@ts-ignore
      if (newValue[i].EditProperty == 0 || newValue[i].EditProperty == 1) {
        editFunParam.value.push(newValue[i]); //面板属性
        if (isInit) {
          //@ts-ignore
          form.value[newValue[i].FieldName] = "" //初始化需要界面显示的字段
        }

      }
      if (
        //@ts-ignore
        newValue[i].DisplayProperty == 0 ||
        //@ts-ignore
        newValue[i].DisplayProperty == 1
      ) {
        showFunParam.value.push(newValue[i]); //图元显示属性
      }
    }
  }
  if(props.CHQCUseLims){
    editFunParam.value.push({
    ChildItemFields:[],
    ColumnWidth:null,
    DisplayProperty:0,
    EditProperty:0,
    FieldName:"QcBusinessCode",
    FuncId:newValue.length,
    ItemFilterField:null,
    MaxValue:0,
    MixValue:0,
    MustHaveValue:false,
    OrderId:newValue.length,
    ParamName:"检验码",
    ParamStyle:0,
    SelectItemTemplate:0,
    SelectItems:[],
    Unit:null,
  })
  showFunParam.value.push({ 
    ChildItemFields:[],
    ColumnWidth:null,
    DisplayProperty:0,
    EditProperty:0,
    FieldName:"QcBusinessCode",
    FuncId:newValue.length,
    ItemFilterField:null,
    MaxValue:0,
    MixValue:0,
    MustHaveValue:false,
    OrderId:newValue.length,
    ParamName:"检验码",
    ParamStyle:0,
    SelectItemTemplate:0,
    SelectItems:[],
    Unit:null, 
  })
  form.value.QcBusinessCode = ""
  }
}
const formatDataStruct = function (data: any, parentData: any) {
  //根据接口，初始化form结构
  //@ts-ignore
  form.value.ParantID = props.formData.cell.RecipeStyle;
  if (data.length < 1) {
    return;
  }

  for (let i = 0; i < data.length; i++) {
    //@ts-ignore
    dataStruct[data[i].FieldName] = "";
  }
  form.value = cloneDeep(dataStruct)

  if (typeof parentData != 'undefined') {
    for (const j in form.value) {
      //@ts-ignore
      form.value[j] = typeof parentData[j] == 'undefined' ? "" : parentData[j];
    }
    let temp = {};
    for (let j = 0; j < selectOption.value.length; j++) {
      for (const k in parentData.Script) {
        parentData.Script[k].ExecId += "";
        if (
          parentData.Script[k].ExecId == selectOption.value[j].id
        ) {
          //@ts-ignore
          temp[k] = parentData.Script[k]; //将图形上读取的id转换成字符串
        }
      }
    }
    formateScriptStruct();
    for (const n in temp) {
      if (typeof n == 'undefined') {
        return;
      }
      //@ts-ignore
      Script.value[n] = temp[n];
    }
  }
}
const handleShowSelectBoxDlg = (col: any, e: any) => {
  state.selectBoxDlgCol=col
  switch(col.FieldName){
    case "QcBusinessCode":
      if (!props.mainData.ProductCode) {
        return message.warning(locale.value==='en'?'SelectQcBusinessCodeWarning':'请先选择主表产品');
      }
      state.targetMatCode = props.mainData.ProductCode
      state.dynComponent = LIMSSchemeDataDlg
      break;
  }
}
const handleDlgCancel = () => {
  state.dynComponent = null
}
const handleDlgConfirm = (data:any[]) => {
  switch(state.selectBoxDlgCol.FieldName){
    case "QcBusinessCode": {
      debugger
      form.value.QcBusinessCode = data[0].TestCode
      break
    }
  }
  state.dynComponent = null
}
const getLIMSSchemeDetailData=async ()=>{
  if (props.CHQCUseLims) {
    if(props.formData.Status>=1){
      await LIMSServices.LoadTestTaskDetailInfo({
        "PageSize": 0,
        "Page": 0,
        "Conditions": [
          {
            "Key": "Relation",
            "Value": props.formData.Key,
            "MatchCase": "Equal",
          },
          {
            "Key": "MaterialCode",
            "Value": props.mainData.ProductCode,
            "MatchCase": "Equal",
          },
          {
            "Key": "MaterialBatch",
            "Value": props.mainData.PlanBatch,
            "MatchCase": "Equal",
          }
        ],
      }).then((res: any) => {
        limsSchemeDetailData.value = res.Data
      }).finally(() => {
      })
      state.dynComponent = null
    }else{
      let targetScheme = null
      await LIMSBaseInformationService.LoadProgramBindMaterialcodeInfo({
        Conditions: [
          {
            Key: 'TestCode',
            Value: props.formData.QcBusinessCode,
            MatchCase: 'Equal'
          }
        ],
        Page: 0,
        PageSize: 0
      }).then((res: any) => {
        targetScheme = res.Datas[0]
      })
      if (!targetScheme) {
        return message.warning(t('Formula.Common.ShowQCItemsWarning'))
      }
      await LIMSBaseInformationService.LoadTestProgramDetailInfo({
        Id: targetScheme.TestProgramHeadld
      }).then((res: any) => {
        limsSchemeDetailData.value = res.Datas
      }).finally(() => {
      })
    }
  }
}

watch(() => props.formData, () => {
  initFormVal()
})
watch(() => FunctionCode.value, (val) => {
  for (const i in props.QCfunOption) {
    //@ts-ignore
    if (props.QCfunOption[i].FunctionCode == val) {
      //@ts-ignore
      functID.value = props.QCfunOption[i].FuncId;
      break;
    }
  }
  getFunParam(functID.value, props.formData.cell);
})
watch(()=>form.value.QcBusinessCode,(nVal)=>{
  if(nVal){
    getLIMSSchemeDetailData()
  }
},{immediate:true})

onBeforeMount(() => {
  initFormVal()
})
</script>

<style scoped>
.header-bread {
  margin-top: 8px;
  margin-left: 10px;
  font-size: 16px;
  font-weight: bold;
}

.codeRow {
  height: 500px;
}

.ltui-form label.a-form-item__label {
  overflow: hidden !important;
}
</style>

<style>
#refFormComFunctQC .ant-form-item {
  border-left: 0px !important;
}
</style>
