<!-- 表格数据 -->
<template>
  <div class="mainContentBox">
    <avue-crud
      v-model="form"
      @row-update="createRow"
      :data="data"
      :option="option"
      v-model:page="page"
    >
      <template #menu="{ row }">
        <el-button
          v-if="row.deviceNo"
          link
          size="small"
          type="primary"
          @click="opView(row)"
          style="margin: 0"
          ><i-ep-view />查看
        </el-button>
        <!--        <el-button
          v-if="row.excelData != '' && row.excelData != null"
          type="warning"
          link
          size="small"
          @click="opUpdate(row)"
          style="margin: 0"
          ><i-ep-edit />修改表格数据
        </el-button>-->
      </template>
    </avue-crud>
    <el-dialog
      v-model="updataShow"
      :title="updateTitle"
      @close="closeShow"
      width="1600"
    >
      <avue-crud
        v-model="useCrud2.form"
        :data="useCrud2.data"
        v-model:search="useCrud2.search"
        :option="useCrud2.option"
        v-model:page="useCrud2.page"
        @search-change="useCrud2.Methords.searchChange"
        @search-reset="useCrud2.Methords.resetChange"
        @size-change="useCrud2.Methords.dataList"
        @current-change="useCrud2.Methords.dataList"
      />
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import dictDataUtil from "@/common/configs/dictDataUtil";
import ButtonPermKeys from "@/common/configs/buttonPermission";
import {
  useCommonStoreHook,
  useDictionaryStore,
  useUserStoreHook,
} from "@/store";
// 数据字典相关
const { dicts } = useDictionaryStore();
const updataShow = ref(false);
const updateTitle = ref("采集详情");
const opUpdate = (row) => {
  ExData.value = row;
  ExData.value.lookStatus = false;
  if (row.writeData == "" || row.writeData == null) {
    ElMessage.error("数据有误请联系管理员");
    return;
  }
  updataShow.value = true;
};

const closeShow = () => {
  updataShow.value = false;
};
const ExData = ref("");
// 传入一个url，后面不带/
const {
  form,
  data,
  option,
  search,
  page,
  toDeleteIds,
  Methords,
  Utils,
  commonConfig,
} = useCrud({
  src: "/api/v1/process/web/traceability/collect",
});
const useCrud2 = ref(
  useCrud({
    src: "/api/v1/process/web/traceability/collectData",
  })
);
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
const userStore = useUserStoreHook();
const refreshTra = (row) => {
  commonConfig.value.params = {
    seqNo: row.seqNo,
    workOrderCode: row.workOrderCode,
  };
  dataList();
};
const refreshDatalist = () => {
  dataList();
};
defineExpose({ refreshTra });

option.value = Object.assign(option.value, {
  selection: false,
  border: true,
  index: false,
  expandLevel: 3,
  headerAlign: "center",
  align: "center",
  labelWidth: 100,
  addBtn: false,
  delBtn: false,
  viewBtn: false,
  menu: false,
  header: false,
  editBtn: false,
  rowKey: "operationId",
  column: [
    {
      label: "工序Id",
      prop: "operationId",
      display: false,
      hide: true,
    },
    {
      label: "工序名称",
      prop: "operationName",
      disabled: true,
      search: false,
      display: false,
    },
    {
      label: "设备名称",
      disabled: true,
      prop: "deviceName",
      search: false,
    },
    {
      label: "设备编号",
      disabled: true,
      prop: "deviceNo",
      search: false,
    },
  ],
});

useCrud2.value.option = Object.assign(useCrud2.value.option, {
  selection: false,
  border: true,
  index: false,
  expandLevel: 3,
  headerAlign: "center",
  align: "center",
  labelWidth: 100,
  addBtn: false,
  delBtn: false,
  viewBtn: false,
  menu: false,
  header: false,
  editBtn: false,
  rowKey: "operationId",
  column: [
    {
      label: "设备名称",
      disabled: true,
      prop: "deviceName",
      search: false,
    },
    {
      label: "设备编号",
      disabled: true,
      prop: "deviceNo",
      search: false,
    },
    {
      label: "采集数据",
      prop: "data",
      html: true,
      formatter: (val) => {
        return dataShow(val.data);
      },
    },
  ],
});
const showLable = (key) => {
  switch (key) {
    case "tipTemperature":
      return "温度";
    case "CH5Val":
      return "温度";
    case "CH6Val":
      return "湿度";
    case "DataTimes":
      return "采集时间";
    case "torquetarget":
      return "当前任务目标扭力";
    case "tightenTorqueMax":
      return "拧紧过程中最大扭力值";
    case "taskNo":
      return "当前任务号";
    case "taskName":
      return "任务名";
    case "warning":
      return "警报";
    case "tightenResult":
      return "最终拧紧结果";
    case "totalCycles":
      return "总圈数";
    case "length":
      return "长度";
    case "height":
      return "高度";
    case "DDLSD1":
      return "拧紧的模式";
    case "DDLSD2":
      return "拧紧旋转方向";
    case "DDLSD3":
      return "目标扭力";
    case "DDLSD4":
      return "扭力保持时间";
    case "DDLSD5":
      return "浮高界定圈数";
    case "DDLSD6":
      return "滑牙界定圈数";
    case "DDLSD7":
      return "触发速度切换的扭力比值";
    case "DDLSD8":
      return "扭力补偿值";
    case "DDLSD9":
      return "开启浮高滑牙检测";
    case "DDLSD10":
      return "触发速度切换的扭力";
    case "DDLSD11":
      return "触发速度切换的速度比值";
    case "DDLSD12":
      return "扭力偏差上限";
    case "DDLSD13":
      return "扭力偏差下限";
    case "DDLSD14":
      return "扭力免检圈数";
    case "DDLSD15":
      return "拧松有效触发的扭力阀值";
    case "DDLSD16":
      return "拧松有效触发的保持时间";
    case "DDLSD17":
      return "自由旋转方向";
    case "DDLSD18":
      return "STEP-00拧紧圈数";
    case "DDLSD19":
      return "STEP-00拧紧速度";
    case "DDLSD20":
      return "STEP-01拧紧圈数";
    case "DDLSD21":
      return "STEP-01拧紧速度";
    case "DDLSD22":
      return "STEP-02拧紧圈数";
    case "DDLSD23":
      return "STEP-02拧紧速度";
    case "DDLSD24":
      return "STEP-03拧紧圈数";
    case "DDLSD25":
      return "STEP-03拧紧速度";
    case "DDLSD26":
      return "STEP-04拧紧圈数";
    case "DDLSD27":
      return "STEP-04拧紧速度";
    case "DDLSD28":
      return "STEP-00拧松圈数";
    case "DDLSD29":
      return "STEP-00拧松速度";
    case "DDLSD30":
      return "STEP-01拧松圈数";
    case "DDLSD31":
      return "STEP-01拧松速度";
    case "DDLSD32":
      return "STEP-02拧松圈数";
    case "DDLSD33":
      return "STEP-02拧松速度";
    case "DDLSD34":
      return "STEP-00自由圈数";
    case "DDLSD35":
      return "STEP-00自由速度";
    default:
      return null;
  }
};
const dataShow = (dataJSON) => {
  const obj = JSON.parse(dataJSON);
  let str = "";
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      if (showLable(key) == null) continue;
      let str1 = showLable(key);
      let str2 = obj[key];
      let res = str1 + ":" + str2 + "<br/>";
      str += res;
    }
  }
  return str;
};
const opView = (row) => {
  updataShow.value = true;
  useCrud2.value.search = {
    seqNo: commonConfig.value.params.seqNo,
    workOrderCode: commonConfig.value.params.workOrderCode,
    deviceNo: row.deviceNo,
    operationId: row.operationId,
  };
  useCrud2.value.Methords.dataList();
};
onMounted(() => {
  if (userStore.user.userId === 10000) {
    option.value.menu = true;
  }
});
</script>
