<!-- 生产履历 -->
<template>
  <div class="mainContentBox">
    <avue-crud
      ref="crudRef2"
      v-model:search="search"
      v-model="form"
      :data="data"
      :option="option"
      @row-update="updateRow"
      v-model:page="page"
    />
    <CommonTable
      ref="userRef"
      tableTitle="操作人选择"
      tableType="USERS"
      @selected-sure="onUserSelectedFinish"
    />
  </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 userStore = useUserStoreHook();

// 传入一个url，后面不带/
const {
  form,
  data,
  option,
  search,
  page,
  toDeleteIds,
  Methords,
  Utils,
  commonConfig,
} = useCrud({
  src: "/api/v1/process/info",
});
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 };
  dataList();
};
defineExpose({ refreshTra });
onMounted(() => {
  if (userStore.user.userId === 10000) {
    option.value.menu = true;
    option.value.editBtn = true;
  }
});

option.value = Object.assign(option.value, {
  selection: false,
  border: true,
  index: false,
  expandLevel: 3,
  headerAlign: "center",
  align: "center",
  labelWidth: 100,
  addBtn: false,
  delBtn: false,
  menuWidth: 100,
  editBtn: false,
  viewBtn: false,
  menu: false,
  header: false,
  column: [
    {
      label: "生产过程id",
      prop: "id",
      display: false,
      hide: true,
      width: 120,
    },
    {
      label: "工序名称",
      prop: "operationName",
      editDisplay: false,
      width: 120,
    },
    {
      label: "工段",
      prop: "workSection",
      editDisplay: false,
      type: "select",
      dicData: dicts.workshop_section,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
    },
    {
      label: "状态",
      editDisplay: false,
      prop: "currentState",
      search: false,
      dicData: dicts.process_state,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
    },
    {
      label: "开始时间",
      prop: "realStartWhen",
      search: false,
      format: "YYYY-MM-DD HH:mm:ss",
      valueFormat: "YYYY-MM-DD HH:mm:ss",
      width: 160,
      type: "datetime",
      rules: [
        {
          validator: (rule, value, callback) => {
            if (value == null || form.value.realEndWhen == null) {
              callback();
            } else if (
              new Date(value).getTime() <=
              new Date(form.value.realEndWhen).getTime()
            ) {
              callback();
            } else {
              callback(new Error("开始时间应小于等于结束时间"));
            }
          },
          message: "开始时间应小于等于结束时间",
          required: true,
          trigger: "change",
        },
      ],
    },
    {
      label: "结束时间",
      prop: "realEndWhen",
      format: "YYYY-MM-DD HH:mm:ss",
      valueFormat: "YYYY-MM-DD HH:mm:ss",
      width: 160,
      type: "datetime",
      rules: [
        {
          validator: (rule, value, callback) => {
            if (value == null || form.value.realStartWhen == null) {
              callback();
            } else if (
              new Date(value).getTime() >=
              new Date(form.value.realStartWhen).getTime()
            ) {
              callback();
            } else {
              callback(new Error("\结束时间应大于等于结束时间"));
            }
          },
          required: true,
          message: "结束时间应大于等于开始时间",
          trigger: "change",
        },
      ],
    },
    {
      label: "操作人",
      prop: "operator",
      search: false,
      width: 100,
      overHidden: true,
      click: ({ value, column }) => {
        userRef.value.startSelect();
      },
    },
    {
      label: "工时(秒)",
      prop: "totalTime",
      editDisplay: false,
      search: false,
    },
    {
      label: "工步",
      prop: "operationSort",
      editDisplay: false,
      search: false,
    },
  ],
});

// const validateTimeEnd = (rule, value, callback) => {
//   if (value === "") {
//     callback(new Error("Please input the password again"));
//   } else if (value !== ruleForm.pass) {
//     callback(new Error("Two inputs don't match!"));
//   } else {
//     callback();
//   }
// };
</script>
