<!-- 设备使用 -->
<template>
  <div class="mainContentBox">
    <avue-crud
      ref="crudRef2"
      v-model:search="search"
      v-model="form"
      :data="data"
      :option="option"
      v-model:page="page"
      @row-update="updateRow"
    >
      <template #menu="{ size, row, index }">
        <el-button
          v-if="row.equitCode != undefined && row.equitCode != null"
          type="primary"
          link
          size="small"
          @click="doEdit(row, index)"
          ><i-ep-edit />编辑
        </el-button>
      </template>
    </avue-crud>
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="850px"
      @close="dialog.visible = false"
      :destroy-on-close="true"
    >
      <device-page
        @deviceInfo="deviceInfo"
        :deviceType="deviceDetail.equitType"
      />
    </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();

// 传入一个url，后面不带/
const {
  form,
  data,
  option,
  search,
  page,
  toDeleteIds,
  Methords,
  Utils,
  commonConfig,
} = useCrud({
  src: "/api/v1/process/web/traceability/equitRecordInfo",
});
const dialog = reactive({
  title: "设备选择",
  visible: false,
});
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具

const crudRef2 = ref(null); //crudRef.value 获取avue-crud对象
const userStore = useUserStoreHook();
const refreshTra = (row) => {
  commonConfig.value.params = {
    seqNo: row.seqNo,
    workOrderCode: row.workOrderCode,
  };
  dataList();
};
const deviceDetail = ref({});
const doEdit = (row, index) => {
  crudRef2.value && crudRef2.value.rowEdit(row, index);
  deviceDetail.value = row;
};

const deviceInfo = (value) => {
  form.value.equitCode = value.deviceNo;
  form.value.equitName = value.deviceName;
  form.value.validDate = value.meteringDate;
  form.value.equitId = value.id;
  dialog.visible = false;
};
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,
  menu: false,
  editBtn: false,
  viewBtn: false,
  delBtn: false,
  header: false,
  rowKey: "opId",
  column: [
    {
      label: "工序Id",
      prop: "opId",
      display: false,
      hide: true,
    },
    {
      label: "工序名称",
      prop: "opName",
      display: false,
    },
    {
      label: "设备类型",
      prop: "equitType",
      search: false,
      disabled: true,
      type: "select",
      dicData: dicts.device_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
    },
    {
      label: "精度要求",
      prop: "accuracy",
      search: false,
      disabled: true,
    },
    {
      label: "设备编码",
      prop: "equitCode",
      search: false,
      rules: [
        {
          required: true,
          message: "请选择设备编码",
          trigger: "blur",
        },
      ],
      click: ({ value, column }) => {
        dialog.visible = true;
      },
      change: ({ value, column }) => {
        if (value != form.value.equitCode) {
          dialog.visible = true;
        }
      },
    },
    {
      label: "计量有效期",
      prop: "validDate",
      search: false,
      disabled: true,
    },
    {
      label: "设备名称",
      prop: "equitName",
      search: false,
      disabled: true,
    },
    {
      label: "设备id",
      prop: "equitId",
      search: false,
      display: false,
      hide: true,
    },
    {
      label: "录入时间",
      prop: "created",
      search: false,
      format: "YYYY-MM-DD HH:mm:ss",
      valueFormat: "YYYY-MM-DD HH:mm:ss",
      type: "datetime",
    },
  ],
});
</script>
