<template>
  <div class="mainContentBox">
    <avue-crud
      ref="crudRef"
      v-model:search="search"
      v-model="form"
      :data="data"
      :option="option"
      v-model:page="page"
      @row-save="createRow"
      @row-update="updateRow"
      @row-del="deleteRow"
      @search-change="searchChange"
      @search-reset="resetChange"
      @size-change="dataList"
      @current-change="dataList"
      @selection-change="selectionChange"
    >
      <template #menu-left="{ size }">
        <el-button
          :disabled="toDeleteIds.length < 1"
          type="danger"
          icon="el-icon-delete"
          :size="size"
          @click="multipleDelete"
          >删除</el-button
        >
      </template>
      <template #enable="scope">
        <el-switch
          :active-value="1"
          :inactive-value="0"
          inline-prompt
          active-text="启用"
          inactive-text="禁用"
          v-model="scope.row.enable"
          @click="changeItem(scope.row)"
          class="ml-2"
          style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
        />
      </template>
    </avue-crud>
    <CommonTable
      ref="ctableRef"
      tableTitle="物料"
      tableType="MARTERIAL"
      @selected-sure="onSelectedFinish"
    />
  </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 { updateRework } from "@/api/rework";
import {
  useCommonStoreHook,
  useDictionaryStore,
  useDictionaryStoreHook,
} from "@/store";
const changeItem = (row) => {
  updateRework(row).then(() => {
    ElMessage.success("操作成功");
    dataList();
  });
};
// 数据字典相关
const { dicts } = useDictionaryStore();

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

const crudRef = ref(null); //crudRef.value 获取avue-crud对象
const repeatObj = ref({});
// 公共弹窗相关
const ctableRef = ref(null);
const startCreat = () => {
  if (ctableRef.value) {
    ctableRef.value.startSelect();
  }
};
defineExpose({ startCreat });
/**
 * 上传excel相关
 */
const uploadRef = ref(null);
const uploadFinished = () => {
  // 上传完成后的刷新操作
  page.currentPage = 1;
  dataList();
};
const importExcelData = () => {
  if (uploadRef.value) {
    uploadRef.value.show("/api/v1/plan/order/import");
  }
};
const onSelectedFinish = (selectedValue) => {
  form.value.materialCode = selectedValue.materialCode;
  repeatObj.value.showCreate = false;
};

option.value = Object.assign(option.value, {
  searchEnter: true,
  selection: true,
  labelWidth: 110,
  searchLabelWidth: 110,
  column: [
    {
      label: "返工编号",
      prop: "code",
      clearable: false,
      search: true,
      rules: [
        {
          required: true,
          trigger: "change",
        },
      ],
    },
    {
      label: "返工原因",
      prop: "name",
      type: "textarea",
      search: true,
      rules: [
        {
          required: true,
          trigger: "change",
        },
      ],
    },
    {
      label: "返工内容",
      prop: "remark",
      type: "textarea",
      rules: [
        {
          required: true,
          trigger: "change",
        },
      ],
    },
    {
      label: "启用状态",
      slot: true,
      headerAlign: "center",
      prop: "enable",
      width: 100,
      addDisplay: false,
      editDisplay: false,
      value: 0,
    },
  ],
});
onMounted(() => {
  // console.log("crudRef", crudRef)
  dataList();
});
</script>
