<template>
  <div class="mainContentBox">
    <avue-crud
      ref="crudRefJSSJ"
      v-model:search="search"
      v-model="form"
      :data="data"
      :option="option"
      @row-update="updateRow"
      v-model:page="page"
      @search-change="searchChange"
      @search-reset="resetChange"
      @size-change="dataList"
      @current-change="dataList"
    >
      <template #menu="{ size, row, index }">
        <el-button
          type="text"
          size="mini"
          @click="lookData(row)"
          v-if="!row.operationIds"
        >
          查看
        </el-button>
        <el-button
          v-if="row.operationId != undefined && row.operationId != null"
          type="primary"
          link
          size="small"
          @click="doEdit(row, index)"
          ><i-ep-edit />编辑
        </el-button>
      </template>
    </avue-crud>
  </div>
  <el-dialog title="详细信息" v-model="dialogVisible">
    <el-table :data="tableData" class="tableView-cssj">
      <el-table-column prop="parameterName" label="测试项目" align="center" />
      <el-table-column prop="deviceCode" label="测试设备" align="center" />
      <el-table-column prop="value" label="测试值" align="center" />
    </el-table>
    <el-pagination
      background
      :page-size="20"
      :pager-count="5"
      layout="->,prev, pager, next"
      :total="pageTotal"
      @current-change="handlePageChange"
    />
  </el-dialog>
</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";
import { testData } from "@/api/process/index";

// 数据字典相关
const { dicts } = useDictionaryStore();
const userStore = useUserStoreHook();

// 传入一个url，后面不带/
const {
  form,
  data,
  option,
  search,
  page,
  toDeleteIds,
  Methords,
  Utils,
  commonConfig,
} = useCrud({
  src: "/api/v1/testData/testData",
});
const crudRefJSSJ = ref(null);
const doEdit = (row, index) => {
  crudRefJSSJ.value && crudRefJSSJ.value.rowEdit(row, index);
};
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
const onUserSelectedFinish = (selectedValue) => {
  form.value.operator = selectedValue.userName;
};
const crudRef = ref(null); //crudRef.value 获取avue-crud对象
const userRef = ref(null);
const refreshTra = (row) => {
  commonConfig.value.params = {
    seqNo: row.seqNo,
    workOrderCode: row.workOrderCode,
  };
  dataList();
};
defineExpose({ refreshTra });
onMounted(() => {
  if (userStore.user.userId === 10000) {
    option.value.menu = true;
  }
});

option.value = Object.assign(option.value, {
  searchEnter: true,
  // selection: false,
  // border: true,
  // index: false,
  // expandLevel: 3,
  // headerAlign: "center",
  // align: "center",
  // labelWidth: 100,
  // addBtn: false,
  // delBtn: false,
  // menuWidth: 0,
  // editBtn: false,
  // viewBtn: false,
  // // menu: false,
  header: false,
  viewBtn: false,
  editBtn: false,
  delBtn: false,
  rowKey: "operationIds",
  column: [
    {
      label: "工序名称",
      prop: "operationName",
    },
    {
      label: "测试时间",
      prop: "created",
    },
    {
      label: "测试人",
      prop: "creator",
    },
    {
      label: "测试设备",
      prop: "deviceName",
    },
    {
      label: "物料名称",
      prop: "materialName",
    },
    // {
    //   label: "操作",
    //   prop: "op",
    // },
  ],
});

const pageTotal = ref(0);
const currentPage = ref(1);
const selectRow = ref(null);

const handlePageChange = (val) => {
  currentPage.value = val;
  lookData(selectRow.value);
};
// 查看相关代码
const dialogVisible = ref(false);
const tableData = ref([]);
const lookData = (row) => {
  selectRow.value = row;
  // if (row.testData && "[]" !== row.testData) {
  //   tableData.value = JSON.parse(row.testData);
  //   dialogVisible.value = true;
  //   return;
  // }
  testData(row.processId, row.deviceNo, currentPage.value.toString()).then(
    (res) => {
      if (res.code === "200") {
        dialogVisible.value = true;
        tableData.value = JSON.parse(res.data.testData);
        pageTotal.value = res.data.total;
      }
    }
  );
};

const handleDialogCancel = () => {
  dialogVisible.value = false;
};
</script>
